u = &a[stack[start-1]];
u->key = u->v.str;
u->n = top - 1 - start;
- u->v.child = (kson_ptr_t*)malloc(u->n * sizeof(kson_ptr_t));
+ u->v.tmp = (size_t*)malloc(u->n * sizeof(size_t));
for (i = start + 1; i < top; ++i)
- u->v.child[i - start - 1].i = stack[i];
+ u->v.tmp[i - start - 1] = stack[i];
u->type = *p == ']'? KSON_TYPE_BRACKET : KSON_TYPE_BRACE;
if ((top = start) == 1) break; // completed one object; remaining characters discarded
} else if (*p == ':') {
for (i = 0; i < n_a; ++i) {
u = &a[i];
for (j = 0; j < (long)u->n; ++j)
- u->v.child[j].p = &a[u->v.child[j].i];
+ u->v.child[j] = &a[u->v.tmp[j]];
}
free(stack);
putchar(',');
putchar('\n'); for (i = 0; i <= depth; ++i) fputs(" ", stdout);
}
- kson_format_recur(p->v.child[i].p, depth + 1);
+ kson_format_recur(p->v.child[i], depth + 1);
}
putchar('\n'); for (i = 0; i < depth; ++i) fputs(" ", stdout);
}
kson = kson_parse(json, &error);
free(json);
if (kson) {
- kson_format(kson);
+ kson_format(kson->nodes);
if (argc > 2) {
// path finding
const kson_node_t *p = kson->nodes;
const kson_node_t *p = kson_query(kson->nodes, 2, "b", 1);
if (p) printf("*** %s\n", p->v.str);
else printf("!!! not found\n");
- kson_format(kson);
+ kson_format(kson->nodes);
} else {
printf("Error code: %d\n", error);
}
#define KSON_ERR_EXTRA_RIGHT 2
#define KSON_ERR_NO_KEY 3
-struct kson_node_s;
-
-typedef union {
- long i; // this is a temporary variable; don't use it!
- struct kson_node_s *p;
-} kson_ptr_t;
-
typedef struct kson_node_s {
uint64_t type:3, n:61;
char *key;
union {
- kson_ptr_t *child;
+ size_t *tmp; // a temporary pointer used by the parser; don't use this!!!
+ const struct kson_node_s **child;
char *str;
} v;
} kson_node_t;
{
long i;
for (i = 0; i < (long)p->n; ++i) {
- const kson_node_t *q = p->v.child[i].p;
+ const kson_node_t *q = p->v.child[i];
if (q->key && strcmp(q->key, key) == 0)
return q;
}
static inline const kson_node_t *kson_by_index(const kson_node_t *p, long i)
{
- return 0 <= i && i < (long)p->n? p->v.child[i].p : 0;
+ return 0 <= i && i < (long)p->n? p->v.child[i] : 0;
}
#endif