Browse Source

Bug fix + more complex test for json_next

PUBLISHED_FROM=a7ca7ec79bb138294449f9b24f4a219c1e57b673
Бобби 8 years ago
parent
commit
de2f5286e8
2 changed files with 19 additions and 4 deletions
  1. 3 3
      frozen.c
  2. 16 1
      unit_test.c

+ 3 - 3
frozen.c

@@ -1254,7 +1254,7 @@ static void prettify_cb(void *userdata, const char *name, size_t name_len,
 
 
 int json_prettify(const char *s, int len, struct json_out *out) WEAK;
 int json_prettify(const char *s, int len, struct json_out *out) WEAK;
 int json_prettify(const char *s, int len, struct json_out *out) {
 int json_prettify(const char *s, int len, struct json_out *out) {
-  struct prettify_data pd = { out, 0, JSON_TYPE_INVALID };
+  struct prettify_data pd = {out, 0, JSON_TYPE_INVALID};
   return json_walk(s, len, prettify_cb, &pd);
   return json_walk(s, len, prettify_cb, &pd);
 }
 }
 
 
@@ -1316,9 +1316,9 @@ static void next_cb(void *userdata, const char *name, size_t name_len,
   const char *p = path + d->path_len;
   const char *p = path + d->path_len;
   if (d->found) return;
   if (d->found) return;
   if (d->path_len >= (int) strlen(path)) return;
   if (d->path_len >= (int) strlen(path)) return;
-  if (strchr(p, '.') != NULL) return;     /* More nested objects - skip */
+  if (strncmp(d->path, path, d->path_len) != 0) return;
+  if (strchr(p + 1, '.') != NULL) return; /* More nested objects - skip */
   if (strchr(p + 1, '[') != NULL) return; /* Ditto for arrays */
   if (strchr(p + 1, '[') != NULL) return; /* Ditto for arrays */
-
   // {OBJECT,ARRAY}_END types do not pass name, _START does. Save key.
   // {OBJECT,ARRAY}_END types do not pass name, _START does. Save key.
   if (t->type == JSON_TYPE_OBJECT_START || t->type == JSON_TYPE_ARRAY_START) {
   if (t->type == JSON_TYPE_OBJECT_START || t->type == JSON_TYPE_ARRAY_START) {
     // printf("SAV %s %d %p\n", path, t->type, t->ptr);
     // printf("SAV %s %d %p\n", path, t->type, t->ptr);

+ 16 - 1
unit_test.c

@@ -861,7 +861,7 @@ static const char *test_json_next(void) {
     int i = 0;
     int i = 0;
     const char *results[] = {"[a] -> [[]]", "[b] -> [[ 1, {} ]]",
     const char *results[] = {"[a] -> [[]]", "[b] -> [[ 1, {} ]]",
                              "[c] -> [true]"};
                              "[c] -> [true]"};
-    while ((h = json_next_key(s, len, h, ".", &key, &val)) != NULL) {
+    while ((h = json_next_key(s, len, h, "", &key, &val)) != NULL) {
       snprintf(buf, sizeof(buf), "[%.*s] -> [%.*s]", key.len, key.ptr, val.len,
       snprintf(buf, sizeof(buf), "[%.*s] -> [%.*s]", key.len, key.ptr, val.len,
                val.ptr);
                val.ptr);
       ASSERT(strcmp(results[i], buf) == 0);
       ASSERT(strcmp(results[i], buf) == 0);
@@ -883,6 +883,21 @@ static const char *test_json_next(void) {
     ASSERT(i == 2);
     ASSERT(i == 2);
   }
   }
 
 
+  {
+    /* Traverse more complex object */
+    const char *s = "{ \"a\": [], \"b\": { \"c\": true, \"d\": 1234 } }";
+    void *h = NULL;
+    int i = 0;
+    const char *results[] = {"[c] -> [true]", "[d] -> [1234]"};
+    while ((h = json_next_key(s, len, h, ".b", &key, &val)) != NULL) {
+      snprintf(buf, sizeof(buf), "[%.*s] -> [%.*s]", key.len, key.ptr, val.len,
+               val.ptr);
+      ASSERT(strcmp(results[i], buf) == 0);
+      i++;
+    }
+    ASSERT(i == 2);
+  }
+
   return NULL;
   return NULL;
 }
 }