From 0a7b4aaf5e70e388f32238cdb866c4d5e30bbe89 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sat, 29 Aug 2026 15:16:28 +0100 Subject: [PATCH] With JSON export, output properties before objects --- src/Json/Printer.cpp | 35 +++++++++++++------------- test/modules/Streaming.cpp | 12 +++------ test/modules/Union.cpp | 2 +- test/resource/async-update-result.json | 2 +- test/resource/database1.json | 2 +- test/resource/export-root.json | 1 + test/resource/root1.json | 2 +- 7 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 test/resource/export-root.json diff --git a/src/Json/Printer.cpp b/src/Json/Printer.cpp index f77a2085..e5ed2fdc 100644 --- a/src/Json/Printer.cpp +++ b/src/Json/Printer.cpp @@ -90,41 +90,42 @@ size_t Printer::operator()() n += p->print(isArray ? '[' : '{'); } - // Print child object - auto objectCount = object.getObjectCount(); - if(index < objectCount) { - auto obj = object.getObject(index); + // Print property + auto propertyCount = object.getPropertyCount(); + if(index < propertyCount) { + auto prop = static_cast(object).getProperty(index); if(object.streamPos++) { n += p->print(','); } n += newline(); - if(pretty && object.typeIs(ObjectType::ObjectArray)) { + if(pretty) { n += p->print(indent); n += p->print(" "); } - ++nesting; - objects[nesting] = obj; + if(prop.info().name.length()) { + n += p->print(quote(prop.info().name)); + n += p->print(colon); + } + n += p->print(prop.getJsonValue()); return n; } - index -= objectCount; + index -= propertyCount; - // Print property - if(index < object.getPropertyCount()) { - auto prop = static_cast(object).getProperty(index); + // Print child object + auto objectCount = object.getObjectCount(); + if(index < objectCount) { + auto obj = object.getObject(index); if(object.streamPos++) { n += p->print(','); } n += newline(); - if(pretty) { + if(pretty && object.typeIs(ObjectType::ObjectArray)) { n += p->print(indent); n += p->print(" "); } - if(prop.info().name.length()) { - n += p->print(quote(prop.info().name)); - n += p->print(colon); - } - n += p->print(prop.getJsonValue()); + ++nesting; + objects[nesting] = obj; return n; } diff --git a/test/modules/Streaming.cpp b/test/modules/Streaming.cpp index 74804436..505d1404 100644 --- a/test/modules/Streaming.cpp +++ b/test/modules/Streaming.cpp @@ -38,6 +38,7 @@ namespace json IMPORT_FSTR_LOCAL(update1, PROJECT_DIR "/resource/update1.json") IMPORT_FSTR_LOCAL(root1, PROJECT_DIR "/resource/root1.json") IMPORT_FSTR_LOCAL(database1, PROJECT_DIR "/resource/database1.json") +IMPORT_FSTR_LOCAL(export_root, PROJECT_DIR "/resource/export-root.json") IMPORT_FSTR_LOCAL(array_test_default, PROJECT_DIR "/resource/array_test_default.json") IMPORT_FSTR_LOCAL(async_update, PROJECT_DIR "/resource/async-update.json") IMPORT_FSTR_LOCAL(async_update_result, PROJECT_DIR "/resource/async-update-result.json") @@ -143,18 +144,13 @@ class StreamingTest : public TestGroup TEST_CASE("Streaming export options (root)") { // An un-named root object should produce same output (i.e. options are ignored) - String expectedContent = - F("{\"simple-union\":{\"obj2\":{\"value\":12}},\"int_array\":[13,28,39,40],\"string_array\":[\"a\"," - "\"b\",\"c\"],\"object_array\":[],\"color\":" - "\"red\",\"simple-bool\":true,\"simple-string\":\"donkey\",\"simple-int\":100,\"simple-float\":3." - "1415927}"); TestConfig::Root root(database); auto stream = root.createExportStream(ConfigDB::Json::format); - REQUIRE_EQ(getContent(*stream), expectedContent); + REQUIRE_EQ(getContent(*stream), json::export_root); stream = root.createExportStream(ConfigDB::Json::format, {.useName = true}); - REQUIRE_EQ(getContent(*stream), expectedContent); + REQUIRE_EQ(getContent(*stream), json::export_root); stream = root.createExportStream(ConfigDB::Json::format, {.asObject = true}); - REQUIRE_EQ(getContent(*stream), expectedContent); + REQUIRE_EQ(getContent(*stream), json::export_root); } TEST_CASE("Streaming export options (database)") diff --git a/test/modules/Union.cpp b/test/modules/Union.cpp index ff39ce11..7f62be13 100644 --- a/test/modules/Union.cpp +++ b/test/modules/Union.cpp @@ -228,7 +228,7 @@ class UnionTest : public TestGroup TEST_CASE("Root Union") { - DEFINE_FSTR_LOCAL(expectedRequest, "{\"request\":{\"args\":[\"all\"],\"method\":\"query\"}}") + DEFINE_FSTR_LOCAL(expectedRequest, "{\"request\":{\"method\":\"query\",\"args\":[\"all\"]}}") DEFINE_FSTR_LOCAL(expectedResponse, "{\"response\":{\"code\":-1,\"message\":\"undefined\"}}") TestRootUnion db("dummy"); TestRootUnion::Root root(db); diff --git a/test/resource/async-update-result.json b/test/resource/async-update-result.json index d7975670..9b301141 100644 --- a/test/resource/async-update-result.json +++ b/test/resource/async-update-result.json @@ -1 +1 @@ -{"simple-union":{"obj1":{"value":0}},"int_array":[100,1],"string_array":["go","away"],"object_array":[{"intval":1,"stringval":"biscuit"},{"intval":2,"stringval":"cake"}],"color":"green","simple-bool":true,"simple-string":"gorilla","simple-int":100,"simple-float":1.2e8} \ No newline at end of file +{"color":"green","simple-bool":true,"simple-string":"gorilla","simple-int":100,"simple-float":1.2e8,"simple-union":{"obj1":{"value":0}},"int_array":[100,1],"string_array":["go","away"],"object_array":[{"intval":1,"stringval":"biscuit"},{"intval":2,"stringval":"cake"}]} \ No newline at end of file diff --git a/test/resource/database1.json b/test/resource/database1.json index d1be0f86..dc1c1188 100644 --- a/test/resource/database1.json +++ b/test/resource/database1.json @@ -1 +1 @@ -{"simple-union":{"obj2":{"value":12}},"int_array":[13,28,39,40],"string_array":["a","b","c"],"object_array":[],"color":"red","simple-bool":true,"simple-string":"donkey","simple-int":100,"simple-float":3.1415927,"array-store":[],"object-array-store":[]} \ No newline at end of file +{"color":"red","simple-bool":true,"simple-string":"donkey","simple-int":100,"simple-float":3.1415927,"simple-union":{"obj2":{"value":12}},"int_array":[13,28,39,40],"string_array":["a","b","c"],"object_array":[],"array-store":[],"object-array-store":[]} \ No newline at end of file diff --git a/test/resource/export-root.json b/test/resource/export-root.json new file mode 100644 index 00000000..2d7a4971 --- /dev/null +++ b/test/resource/export-root.json @@ -0,0 +1 @@ +{"color":"red","simple-bool":true,"simple-string":"donkey","simple-int":100,"simple-float":3.1415927,"simple-union":{"obj2":{"value":12}},"int_array":[13,28,39,40],"string_array":["a","b","c"],"object_array":[]} \ No newline at end of file diff --git a/test/resource/root1.json b/test/resource/root1.json index 2f694ee9..2d7a4971 100644 --- a/test/resource/root1.json +++ b/test/resource/root1.json @@ -1 +1 @@ -{"simple-union":{"obj2":{"value":12}},"int_array":[13,28,39,40],"string_array":["a","b","c"],"object_array":[],"color":"red","simple-bool":true,"simple-string":"donkey","simple-int":100,"simple-float":3.1415927} \ No newline at end of file +{"color":"red","simple-bool":true,"simple-string":"donkey","simple-int":100,"simple-float":3.1415927,"simple-union":{"obj2":{"value":12}},"int_array":[13,28,39,40],"string_array":["a","b","c"],"object_array":[]} \ No newline at end of file