From 7aca9ceda5ea0815dbae4c7f448384b767621165 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Mon, 21 Jan 2019 12:27:55 +0000 Subject: [PATCH] serialization: Fix issue with empty arrays in JsonSerializer --- src/serialization/JsonSerializer.cpp | 19 +++++++++++++------ src/serialization/XmlSerializer.cpp | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/serialization/JsonSerializer.cpp b/src/serialization/JsonSerializer.cpp index 70974ca..111c1d8 100644 --- a/src/serialization/JsonSerializer.cpp +++ b/src/serialization/JsonSerializer.cpp @@ -7,14 +7,14 @@ namespace ki { namespace serialization { - JsonSerializer::JsonSerializer(const pclass::TypeSystem& type_system, + JsonSerializer::JsonSerializer(const pclass::TypeSystem &type_system, const bool is_file) { m_type_system = &type_system; m_is_file = is_file; } - std::string JsonSerializer::save(const pclass::PropertyClass* object) const + std::string JsonSerializer::save(const pclass::PropertyClass *object) const { return save_object(object).dump( m_is_file ? FILE_INDENT_VALUE : -1, @@ -23,7 +23,7 @@ namespace serialization ); } - bool JsonSerializer::presave_object(json& j, const pclass::PropertyClass* object) const + bool JsonSerializer::presave_object(json &j, const pclass::PropertyClass *object) const { // Add the object's meta information j["_pclass_meta"] = { @@ -32,7 +32,7 @@ namespace serialization return object != nullptr; } - json JsonSerializer::save_object(const pclass::PropertyClass* object) const + json JsonSerializer::save_object(const pclass::PropertyClass *object) const { json j; if (!presave_object(j, object)) @@ -50,8 +50,15 @@ namespace serialization } void JsonSerializer::save_property( - json& j, const pclass::IProperty &prop) const + json &j, const pclass::IProperty &prop) const { + // Ensure that, even if there is no value, the property is added + // to the JSON object + if (prop.is_array()) + j[prop.get_name()] = std::vector(); + else + j[prop.get_name()] = json(); + for (std::size_t i = 0; i < prop.get_element_count(); ++i) { if (prop.get_type().get_kind() == pclass::Type::Kind::CLASS) @@ -90,7 +97,7 @@ namespace serialization } bool JsonSerializer::preload_object( - std::unique_ptr& dest, json& j) const + std::unique_ptr &dest, json& j) const { // If meta information is not present, assume that the type hash is null. if (!j.count("_pclass_meta")) diff --git a/src/serialization/XmlSerializer.cpp b/src/serialization/XmlSerializer.cpp index cd67476..622ebbe 100644 --- a/src/serialization/XmlSerializer.cpp +++ b/src/serialization/XmlSerializer.cpp @@ -65,9 +65,9 @@ namespace serialization void XmlSerializer::save_property(rapidxml::xml_node<> *object, const pclass::IProperty& prop) { + auto *property_name = m_document.allocate_string(prop.get_name().data()); for (std::size_t i = 0; i < prop.get_element_count(); ++i) { - auto *property_name = m_document.allocate_string(prop.get_name().data()); auto *property_node = m_document.allocate_node( rapidxml::node_element, property_name );