From: Petri Lehtinen Date: Wed, 12 May 2010 12:41:09 +0000 (+0300) Subject: Zero the visited flag after encoding an empty array or object X-Git-Tag: v1.3~3^2~2 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=jansson.git;a=commitdiff_plain;h=2630980f49088a06c32cebdc866e4f518106af29 Zero the visited flag after encoding an empty array or object Encoding an empty array or object worked, but encoding it again (possibly after adding some items) failed, because the visited flag (used for detecting circular references) wasn't zeroed. --- diff --git a/src/dump.c b/src/dump.c index bc06dfd..a36da03 100644 --- a/src/dump.c +++ b/src/dump.c @@ -231,8 +231,10 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(dump("[", 1, data)) return -1; - if(n == 0) + if(n == 0) { + array->visited = 0; return dump("]", 1, data); + } if(dump_indent(flags, depth + 1, 0, dump, data)) return -1; @@ -284,8 +286,10 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(dump("{", 1, data)) return -1; - if(!iter) + if(!iter) { + object->visited = 0; return dump("}", 1, data); + } if(dump_indent(flags, depth + 1, 0, dump, data)) return -1; diff --git a/test/.gitignore b/test/.gitignore index a960c50..44ff748 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -3,6 +3,7 @@ bin/json_process suites/api/test_array suites/api/test_equal suites/api/test_copy +suites/api/test_dump suites/api/test_load suites/api/test_number suites/api/test_object diff --git a/test/suites/api/Makefile.am b/test/suites/api/Makefile.am index 64523c0..7125792 100644 --- a/test/suites/api/Makefile.am +++ b/test/suites/api/Makefile.am @@ -4,6 +4,7 @@ check_PROGRAMS = \ test_array \ test_equal \ test_copy \ + test_dump \ test_load \ test_simple \ test_number \ @@ -11,6 +12,7 @@ check_PROGRAMS = \ test_array_SOURCES = test_array.c util.h test_copy_SOURCES = test_copy.c util.h +test_dump_SOURCES = test_dump.c util.h test_load_SOURCES = test_load.c util.h test_simple_SOURCES = test_simple.c util.h test_number_SOURCES = test_number.c util.h diff --git a/test/suites/api/test_dump.c b/test/suites/api/test_dump.c new file mode 100644 index 0000000..c471159 --- /dev/null +++ b/test/suites/api/test_dump.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2009, 2010 Petri Lehtinen + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include +#include "util.h" + +int main() +{ + json_t *json; + char *result; + + /* Encode an empty object/array, add an item, encode again */ + + json = json_object(); + result = json_dumps(json, 0); + if(!result || strcmp(result, "{}")) + fail("json_dumps failed"); + + json_object_set_new(json, "foo", json_integer(5)); + result = json_dumps(json, 0); + if(!result || strcmp(result, "{\"foo\": 5}")) + fail("json_dumps failed"); + + json_decref(json); + + json = json_array(); + result = json_dumps(json, 0); + if(!result || strcmp(result, "[]")) + fail("json_dumps failed"); + free(result); + + json_array_append_new(json, json_integer(5)); + result = json_dumps(json, 0); + if(!result || strcmp(result, "[5]")) + fail("json_dumps failed"); + free(result); + + json_decref(json); + + return 0; +}