From 33339ae59518a09a69d6cfb40a574f20c7199441 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sat, 29 Oct 2016 15:13:16 -0400 Subject: [PATCH] Use json_visit() to clear the serializer data set when parsing so the output from test_parse reflects the actual values parsed. --- tests/test_parse.c | 46 +++++++++++++++++++++++++++++++++++++++ tests/test_parse.expected | 5 +++++ 2 files changed, 51 insertions(+) diff --git a/tests/test_parse.c b/tests/test_parse.c index 5222eef..8f66441 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -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() diff --git a/tests/test_parse.expected b/tests/test_parse.expected index 65fcc35..2158001 100644 --- a/tests/test_parse.expected +++ b/tests/test_parse.expected @@ -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 ================================== -- 2.49.0