From dae74309379490897be167dbc038245437aeceae Mon Sep 17 00:00:00 2001 From: Thomas Jandecka Date: Fri, 10 Aug 2018 13:42:50 +0200 Subject: [PATCH] (fix/json) drop ArrayElement items without value nor fixed --- src/refract/JsonValue.cc | 47 +++++++++++++++++-- .../fixed-type-array-primitive-nested.json | 2 +- test/fixtures/render/object-array-string.json | 2 +- .../schema/array-fixed-types-only.json | 2 +- test/fixtures/schema/array-of-types-only.json | 2 +- .../schema/array-with-nested-type.json | 2 +- .../schema/array-with-nested-types.json | 2 +- test/fixtures/schema/boolean-literal.json | 2 +- test/fixtures/schema/number-literal.json | 2 +- test/fixtures/schema/required-array.json | 2 +- test/fixtures/schema/string-literal.json | 2 +- 11 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/refract/JsonValue.cc b/src/refract/JsonValue.cc index 224051780..dcc951b8d 100644 --- a/src/refract/JsonValue.cc +++ b/src/refract/JsonValue.cc @@ -81,7 +81,10 @@ namespace void renderProperty(so::Object& obj, const ExtendElement& e, TypeAttributes options); void renderProperty(so::Object& obj, const IElement& e, TypeAttributes options); - void renderItem(so::Array& obj, const RefElement& e, TypeAttributes options); + void renderItemSpecific(so::Array& obj, const RefElement& e, TypeAttributes options); + void renderItemSpecific(so::Array& obj, const NumberElement& e, TypeAttributes options); + void renderItemSpecific(so::Array& obj, const StringElement& e, TypeAttributes options); + void renderItemSpecific(so::Array& obj, const BooleanElement& e, TypeAttributes options); void renderItem(so::Array& obj, const IElement& e, TypeAttributes options); so::Object renderValueSpecific(const ObjectElement& e, TypeAttributes options); @@ -102,7 +105,7 @@ namespace } template - void renderItem(so::Array& a, const T& e, TypeAttributes options) + void renderItemSpecific(so::Array& a, const T& e, TypeAttributes options) { LOG(debug) << "rendering item " << e.element() << "Element as JSON Value"; a.data.emplace_back(renderValue(e, inherit_or_pass_flags(options, e))); @@ -487,7 +490,7 @@ namespace }); } - void renderItem(so::Array& a, const RefElement& e, TypeAttributes options) + void renderItemSpecific(so::Array& a, const RefElement& e, TypeAttributes options) { LOG(debug) << "rendering item RefElement as JSON Value"; @@ -501,11 +504,47 @@ namespace } } + void renderItemSpecific(so::Array& a, const NumberElement& e, TypeAttributes options) + { + LOG(debug) << "rendering item " << e.element() << "Element as JSON Value"; + + options = updateTypeAttributes(e, options); + + if ((options.test(FIXED_FLAG) || definesValue(e))) + a.data.emplace_back(renderValue(e, inherit_or_pass_flags(options, e))); + else + LOG(debug) << "skipping empty non-fixed primitive element in ArrayElement"; + } + + void renderItemSpecific(so::Array& a, const StringElement& e, TypeAttributes options) + { + LOG(debug) << "rendering item " << e.element() << "Element as JSON Value"; + + options = updateTypeAttributes(e, options); + + if ((options.test(FIXED_FLAG) || definesValue(e))) + a.data.emplace_back(renderValue(e, inherit_or_pass_flags(options, e))); + else + LOG(debug) << "skipping empty non-fixed primitive element in ArrayElement"; + } + + void renderItemSpecific(so::Array& a, const BooleanElement& e, TypeAttributes options) + { + LOG(debug) << "rendering item " << e.element() << "Element as JSON Value"; + + options = updateTypeAttributes(e, options); + + if ((options.test(FIXED_FLAG) || definesValue(e))) + a.data.emplace_back(renderValue(e, inherit_or_pass_flags(options, e))); + else + LOG(debug) << "skipping empty non-fixed primitive element in ArrayElement"; + } + void renderItem(so::Array& a, const IElement& e, TypeAttributes options) { auto aPtr = &a; refract::visit(e, [aPtr, options](const auto& el) { // - renderItem(*aPtr, el, options); + renderItemSpecific(*aPtr, el, options); }); } } // namespace diff --git a/test/fixtures/mson/fixed-type-array-primitive-nested.json b/test/fixtures/mson/fixed-type-array-primitive-nested.json index 1c4fa6643..e731b1fe9 100644 --- a/test/fixtures/mson/fixed-type-array-primitive-nested.json +++ b/test/fixtures/mson/fixed-type-array-primitive-nested.json @@ -156,7 +156,7 @@ "content": "application/json" } }, - "content": "{\n \"s\": [\n \"\"\n ]\n}" + "content": "{\n \"s\": []\n}" }, { "element": "asset", diff --git a/test/fixtures/render/object-array-string.json b/test/fixtures/render/object-array-string.json index bd989c1b7..2cde1d6ff 100644 --- a/test/fixtures/render/object-array-string.json +++ b/test/fixtures/render/object-array-string.json @@ -184,7 +184,7 @@ "content": "application/json" } }, - "content": "{\n \"a\": [\n \"\",\n \"b\",\n \"c\"\n ]\n}" + "content": "{\n \"a\": [\n \"b\",\n \"c\"\n ]\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/array-fixed-types-only.json b/test/fixtures/schema/array-fixed-types-only.json index 082c9d94b..7bd9d4f96 100644 --- a/test/fixtures/schema/array-fixed-types-only.json +++ b/test/fixtures/schema/array-fixed-types-only.json @@ -159,7 +159,7 @@ "content": "application/json" } }, - "content": "{\n \"tags\": [\n \"\",\n 0\n ]\n}" + "content": "{\n \"tags\": []\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/array-of-types-only.json b/test/fixtures/schema/array-of-types-only.json index 246292ffe..db181ccc8 100644 --- a/test/fixtures/schema/array-of-types-only.json +++ b/test/fixtures/schema/array-of-types-only.json @@ -148,7 +148,7 @@ "content": "application/json" } }, - "content": "{\n \"tags\": [\n \"\",\n 0\n ]\n}" + "content": "{\n \"tags\": []\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/array-with-nested-type.json b/test/fixtures/schema/array-with-nested-type.json index 326d539ba..18b9fa13b 100644 --- a/test/fixtures/schema/array-with-nested-type.json +++ b/test/fixtures/schema/array-with-nested-type.json @@ -157,7 +157,7 @@ "content": "application/json" } }, - "content": "{\n \"address\": [\n 0,\n 4,\n 2,\n 42\n ]\n}" + "content": "{\n \"address\": [\n 4,\n 2,\n 42\n ]\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/array-with-nested-types.json b/test/fixtures/schema/array-with-nested-types.json index 90cab2904..ac6049b98 100644 --- a/test/fixtures/schema/array-with-nested-types.json +++ b/test/fixtures/schema/array-with-nested-types.json @@ -164,7 +164,7 @@ "content": "application/json" } }, - "content": "{\n \"address\": [\n 0,\n \"\",\n \"4\",\n \"2\",\n \"hello\",\n \"42\"\n ]\n}" + "content": "{\n \"address\": [\n \"4\",\n \"2\",\n \"hello\",\n \"42\"\n ]\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/boolean-literal.json b/test/fixtures/schema/boolean-literal.json index d44df491b..9296cdbc0 100644 --- a/test/fixtures/schema/boolean-literal.json +++ b/test/fixtures/schema/boolean-literal.json @@ -164,7 +164,7 @@ "content": "application/json" } }, - "content": "{\n \"street\": [\n false,\n true,\n false\n ]\n}" + "content": "{\n \"street\": [\n true,\n false\n ]\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/number-literal.json b/test/fixtures/schema/number-literal.json index 09801ec7a..b89da27f6 100644 --- a/test/fixtures/schema/number-literal.json +++ b/test/fixtures/schema/number-literal.json @@ -168,7 +168,7 @@ "content": "application/json" } }, - "content": "{\n \"street\": [\n 0,\n 0,\n 1.5,\n 340\n ]\n}" + "content": "{\n \"street\": [\n 0,\n 1.5,\n 340\n ]\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/required-array.json b/test/fixtures/schema/required-array.json index 1131fd118..40f679778 100644 --- a/test/fixtures/schema/required-array.json +++ b/test/fixtures/schema/required-array.json @@ -156,7 +156,7 @@ "content": "application/json" } }, - "content": "{\n \"digits\": [\n 0\n ]\n}" + "content": "{\n \"digits\": []\n}" }, { "element": "asset", diff --git a/test/fixtures/schema/string-literal.json b/test/fixtures/schema/string-literal.json index 3488d2528..ec868e60e 100644 --- a/test/fixtures/schema/string-literal.json +++ b/test/fixtures/schema/string-literal.json @@ -164,7 +164,7 @@ "content": "application/json" } }, - "content": "{\n \"street\": [\n \"\",\n \"bye bye\",\n \"san francisco\"\n ]\n}" + "content": "{\n \"street\": [\n \"bye bye\",\n \"san francisco\"\n ]\n}" }, { "element": "asset",