]> granicus.if.org Git - json-c/commitdiff
Use json_visit() to clear the serializer data set when parsing so the output from...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 29 Oct 2016 19:13:16 +0000 (15:13 -0400)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 29 Oct 2016 19:13:16 +0000 (15:13 -0400)
tests/test_parse.c
tests/test_parse.expected

index 5222eef1e7eec458a1241ae8bc4ceb08e6168899..8f664419787b7988d290c38fd19ea20063e7be81 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "json.h"
 #include "json_tokener.h"
+#include "json_visit.h"
 
 static void test_basic_parse(void);
 static void test_verbose_parse(void);
@@ -24,6 +25,9 @@ int main(void)
        puts(separator);
 }
 
+static json_c_visit_userfunc clear_serializer;
+static void do_clear_serializer(json_object *jso);
+
 static void test_basic_parse()
 {
        json_object *new_obj;
@@ -157,6 +161,48 @@ static void test_basic_parse()
        new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
        printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
        json_object_put(new_obj);
+
+       new_obj = json_tokener_parse("{ \"abc\": \"blue\nred\\ngreen\" }");
+       printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+       json_object_put(new_obj);
+
+       new_obj = json_tokener_parse("[0e]");
+       do_clear_serializer(new_obj);
+       printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+       json_object_put(new_obj);
+
+       new_obj = json_tokener_parse("[0e+]");
+       do_clear_serializer(new_obj);
+       printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+       json_object_put(new_obj);
+
+       new_obj = json_tokener_parse("[0e+-1]");
+       do_clear_serializer(new_obj);
+       printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+       json_object_put(new_obj);
+
+       new_obj = json_tokener_parse("[18446744073709551616]");
+       do_clear_serializer(new_obj);
+       printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+       json_object_put(new_obj);
+}
+
+// Clear the re-serialization information that the tokener
+// saves to ensure that the output reflects the actual
+// values we parsed, rather than just the original input.
+static void do_clear_serializer(json_object *jso)
+{
+       json_c_visit(jso, 0, clear_serializer, NULL);
+}
+
+static int clear_serializer(json_object *jso, int flags,
+                     json_object *parent_jso,
+                     const char *jso_key,
+                     size_t *jso_index, void *userarg)
+{
+       if (jso)
+               json_object_set_serializer(jso, NULL, NULL, NULL);
+       return JSON_C_VISIT_RETURN_CONTINUE;
 }
 
 static void test_verbose_parse()
index 65fcc359872bf8ae44bfffa97dadc7d0145dbae3..215800118689efbbb74ea1ff03fe5ee6a94916b5 100644 (file)
@@ -30,6 +30,11 @@ new_obj.to_string()={ "foo": "bar" }
 new_obj.to_string()={ "foo": "bar", "baz": null, "bool0": true }
 new_obj.to_string()={ "foo": [ null, "foo" ] }
 new_obj.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] }
+new_obj.to_string()={ "abc": "blue\nred\ngreen" }
+new_obj.to_string()=[ 0 ]
+new_obj.to_string()=[ 0 ]
+new_obj.to_string()=null
+new_obj.to_string()=[ 9223372036854775807 ]
 ==================================
 json_tokener_parse_versbose() OK
 ==================================