serialization: Fix issue with empty arrays in JsonSerializer

This commit is contained in:
Joshua Scott 2019-01-21 12:27:55 +00:00
parent 680d378002
commit 7aca9ceda5
2 changed files with 14 additions and 7 deletions

View File

@ -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<json>();
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<pclass::PropertyClass>& dest, json& j) const
std::unique_ptr<pclass::PropertyClass> &dest, json& j) const
{
// If meta information is not present, assume that the type hash is null.
if (!j.count("_pclass_meta"))

View File

@ -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
);