+static struct json_node **json_get_objectp(struct json_node *n,
+ const char *name)
+{
+ if (n && n->type == json_node_object)
+ {
+ for (n = n->u.link[0]; n; n = n->u.link[1])
+ {
+ struct json_node *c = n->u.link[0];
+ if (c && c->type == json_node_pair &&
+ c->u.link[0] && c->u.link[0]->type == json_node_string)
+ if (!strcmp(name, c->u.link[0]->u.string))
+ return &c->u.link[1];
+ }
+ }
+ return 0;
+}
+
+struct json_node *json_get_object(struct json_node *n, const char *name)
+{
+ struct json_node **np = json_get_objectp(n, name);
+
+ if (np)
+ return *np;
+ return 0;
+}
+
+struct json_node *json_detach_object(struct json_node *n, const char *name)
+{
+ struct json_node **np = json_get_objectp(n, name);
+
+ if (np)
+ {
+ struct json_node *n = *np;
+ *np = 0;
+ return n;
+ }
+ return 0;
+}
+
+struct json_node *json_get_elem(struct json_node *n, int idx)
+{
+ if (n && n->type == json_node_array)
+ {
+ for (n = n->u.link[0]; n; n = n->u.link[1])
+ {
+ if (--idx < 0)
+ return n->u.link[0];
+ }
+ }
+ return 0;
+}
+
+int json_count_children(struct json_node *n)
+{
+ int i = 0;
+
+ if (n && (n->type == json_node_array || n->type == json_node_object))
+ {
+ for (n = n->u.link[0]; n; n = n->u.link[1])
+ i++;
+ }
+ return i;
+}
+
+int json_append_array(struct json_node *dst, struct json_node *src)
+{
+ if (dst && src &&
+ dst->type == json_node_array && src->type == json_node_array)
+ {
+ struct json_node **np = &dst->u.link[0];
+ while (*np)
+ np = &(*np)->u.link[1];
+ *np = src->u.link[0];
+ src->u.link[0] = 0;
+ json_remove_node(src);
+ return 0;
+ }
+ return -1;
+}
+