]> git.kaiwu.me - klib.git/commitdiff
improved comments a little
authorHeng Li <lh3@me.com>
Sat, 29 Nov 2014 17:21:42 +0000 (12:21 -0500)
committerHeng Li <lh3@me.com>
Sat, 29 Nov 2014 17:21:42 +0000 (12:21 -0500)
kson.c
kson.h

diff --git a/kson.c b/kson.c
index 7b17e771219d356b5e975d67e87e1b46f90f9dd8..72f838ad44b6a27b12a2060ae623027e5e1be9bf 100644 (file)
--- a/kson.c
+++ b/kson.c
@@ -48,8 +48,8 @@ kson_node_t *kson_parse_core(const char *json, int *_n, int *error, const char *
                        }
                        start = i;
                        u = &a[stack[start-1]];
-                       u->n = top - 1 - start;
                        u->key = u->v.str;
+                       u->n = top - 1 - start;
                        u->v.child = (int*)malloc(u->n * sizeof(int));
                        for (i = start + 1; i < top; ++i)
                                u->v.child[i - start - 1] = stack[i];
@@ -75,17 +75,18 @@ kson_node_t *kson_parse_core(const char *json, int *_n, int *error, const char *
                        r = malloc(q - p + 1); strncpy(r, p, q - p); r[q-p] = 0; // equivalent to r=strndup(p, q-p)
                        p = c == '\'' || c == '"'? q : q - 1;
 
-                       if (top >= 2 && stack[top-1] == -3) { // this string is a value
+                       if (top >= 2 && stack[top-1] == -3) { // we have a key:value pair here
                                --top;
                                u = &a[stack[top-1]];
-                               u->key = u->v.str, u->v.str = r, u->type = type;
-                       } else { // this string is a key
+                               u->key = u->v.str, u->v.str = r, u->type = type; // move old value to key
+                       } else { // don't know if this is a bare value or a key:value pair; keep it as a value for now
                                __push_back(n_a);
                                __new_node(&u);
                                u->v.str = r, u->type = type;
                        }
                }
        }
+       while (*p && isblank(*p)) ++p; // skip trailing blanks
        *end = p;
        if (top != 1) *error = KSON_ERR_EXTRA_LEFT;
 
@@ -123,7 +124,7 @@ int main(int argc, char *argv[])
        kson_node_t *nodes;
        int n_nodes, error;
        const char *end;
-       nodes = kson_parse_core("{'a':1, 'b':[1,'c',true],'d':[]}", &n_nodes, &error, &end);
+       nodes = kson_parse_core("{'a' : 1, 'b':[0,'isn\\'t',true],'d':[{}]}", &n_nodes, &error, &end);
        if (error == 0) {
                kson_print_recur(nodes, &nodes[0]);
                putchar('\n');
diff --git a/kson.h b/kson.h
index 24c20b9fbd590d1e554d345f4cee6120432cacc4..adefa4b6068a4737cb8120fa90f08ba2058fea66 100644 (file)
--- a/kson.h
+++ b/kson.h
@@ -27,6 +27,7 @@ extern "C" {
 #endif
 
        kson_node_t *kson_parse_core(const char *json, int *_n, int *error, const char **end);
+       void kson_print_recur(kson_node_t *nodes, kson_node_t *root);
 
 #ifdef __cplusplus
 }