+static json_content_t content_result;
+static int content_level;
+
+static int json_determine_content_deeper(void *ctx) {
+ content_level++;
+ return 1;
+}
+
+static int json_determine_content_shallower(void *ctx) {
+ content_level--;
+ return 1;
+}
+
+static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
+ if (strcasecmp(last_key, "type") != 0 || content_level > 1)
+ return 1;
+
+ DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
+ if (strncasecmp((const char *)val, "workspace", len) == 0)
+ content_result = JSON_CONTENT_WORKSPACE;
+ return 0;
+}
+
+/*
+ * Returns true if the provided JSON could be parsed by yajl.
+ *
+ */
+bool json_validate(const char *buf, const size_t len) {
+ bool valid = true;
+ yajl_handle hand = yajl_alloc(NULL, NULL, NULL);
+ /* Allowing comments allows for more user-friendly layout files. */
+ yajl_config(hand, yajl_allow_comments, true);
+ /* Allow multiple values, i.e. multiple nodes to attach */
+ yajl_config(hand, yajl_allow_multiple_values, true);
+
+ setlocale(LC_NUMERIC, "C");
+ if (yajl_parse(hand, (const unsigned char *)buf, len) != yajl_status_ok) {
+ unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
+ ELOG("JSON parsing error: %s\n", str);
+ yajl_free_error(hand, str);
+ valid = false;
+ }
+ setlocale(LC_NUMERIC, "");
+
+ yajl_complete_parse(hand);
+ yajl_free(hand);
+
+ return valid;
+}
+
+/* Parses the given JSON file until it encounters the first “type” property to
+ * determine whether the file contains workspaces or regular containers, which
+ * is important to know when deciding where (and how) to append the contents.
+ * */
+json_content_t json_determine_content(const char *buf, const size_t len) {
+ // We default to JSON_CONTENT_CON because it is legal to not include
+ // “"type": "con"” in the JSON files for better readability.
+ content_result = JSON_CONTENT_CON;
+ content_level = 0;
+ static yajl_callbacks callbacks = {
+ .yajl_string = json_determine_content_string,
+ .yajl_map_key = json_key,
+ .yajl_start_array = json_determine_content_deeper,
+ .yajl_start_map = json_determine_content_deeper,
+ .yajl_end_map = json_determine_content_shallower,
+ .yajl_end_array = json_determine_content_shallower,
+ };
+ yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
+ /* Allowing comments allows for more user-friendly layout files. */
+ yajl_config(hand, yajl_allow_comments, true);
+ /* Allow multiple values, i.e. multiple nodes to attach */
+ yajl_config(hand, yajl_allow_multiple_values, true);
+ setlocale(LC_NUMERIC, "C");
+ const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
+ if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
+ unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
+ ELOG("JSON parsing error: %s\n", str);
+ yajl_free_error(hand, str);