From 783b8b7361cc3bdf05028b275acf7ad3d6400299 Mon Sep 17 00:00:00 2001 From: Glen Fraser Date: Thu, 11 Jan 2018 19:37:24 +0100 Subject: [PATCH 1/2] In to_json(), maintain the "type" of empty maps and vectors - fix issue #399. - make to_json() with an empty Map, Vector or Dynamic_Object return a similar/compatible type (JSON object or array), rather than "null". - include the fix for #381 (to_json() support for null values), so that can also be unit tested. --- include/chaiscript/utility/json_wrap.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/chaiscript/utility/json_wrap.hpp b/include/chaiscript/utility/json_wrap.hpp index 2aa81f34..05b9e404 100644 --- a/include/chaiscript/utility/json_wrap.hpp +++ b/include/chaiscript/utility/json_wrap.hpp @@ -80,7 +80,7 @@ namespace chaiscript try { const std::map m = chaiscript::boxed_cast &>(t_bv); - json::JSON obj; + json::JSON obj(json::JSON::Class::Object); for (const auto &o : m) { obj[o.first] = to_json_object(o.second); @@ -93,7 +93,7 @@ namespace chaiscript try { const std::vector v = chaiscript::boxed_cast &>(t_bv); - json::JSON obj; + json::JSON obj(json::JSON::Class::Array); for (size_t i = 0; i < v.size(); ++i) { obj[i] = to_json_object(v[i]); @@ -132,7 +132,7 @@ namespace chaiscript try { const chaiscript::dispatch::Dynamic_Object &o = boxed_cast(t_bv); - json::JSON obj; + json::JSON obj(json::JSON::Class::Object); for (const auto &attr : o.get_attrs()) { obj[attr.first] = to_json_object(attr.second); @@ -142,6 +142,8 @@ namespace chaiscript // not a dynamic object } + if (t_bv.is_null()) return json::JSON(); // a null value + throw std::runtime_error("Unknown object type to convert to JSON"); } From 3d97c93e494fec79c9e85e46716ab02128984056 Mon Sep 17 00:00:00 2001 From: Glen Fraser Date: Thu, 11 Jan 2018 19:43:32 +0100 Subject: [PATCH 2/2] Add unit test to validate to_json() --- unittests/json_15.chai | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 unittests/json_15.chai diff --git a/unittests/json_15.chai b/unittests/json_15.chai new file mode 100644 index 00000000..7e8ad652 --- /dev/null +++ b/unittests/json_15.chai @@ -0,0 +1,18 @@ +// Various to_json() tests +assert_equal(to_json(-13570), "-13570") +assert_equal(to_json(0.654321), "0.654321") +assert_equal(to_json("ChaiScript"), "\"ChaiScript\"") +assert_equal(to_json(true), "true") +assert_equal(to_json([1, 2, 3]), "[1, 2, 3]") +assert_equal(to_json(Vector()), "[]") // empty vector +assert_equal(to_json([]), "[]") // empty vector +assert_equal(to_json(Map()), "{\n\n}") // empty map +assert_equal(to_json(Dynamic_Object()), "{\n\n}") // empty object + +// Round-trip JSON tests +assert_equal(from_json(to_json([])), []) +assert_equal(from_json(to_json(Map())), Map()) +assert_equal(to_json(from_json("null")), "null") +assert_equal(from_json(to_json(["a": 5, "b": "stuff"])), ["a": 5, "b": "stuff"]) +auto x = [3.5, true, false, "test", [], Vector(), Map()] +assert_equal(from_json(to_json(x)), x)