diff --git a/include/fkYAML/detail/output/serializer.hpp b/include/fkYAML/detail/output/serializer.hpp index 6cca9597..6eb721be 100644 --- a/include/fkYAML/detail/output/serializer.hpp +++ b/include/fkYAML/detail/output/serializer.hpp @@ -155,17 +155,23 @@ class basic_serializer { str += " "; serialize_node(seq_item, cur_indent, str); str += "\n"; + continue; + } + + const bool is_empty = seq_item.empty(); + if (!is_empty) { + str += "\n"; + serialize_node(seq_item, cur_indent + 2, str); + continue; } - else if (seq_item.is_sequence() && seq_item.size() == 0) { + + // an empty sequence or mapping + if (seq_item.is_sequence()) { str += " []\n"; } - else if (seq_item.is_mapping() && seq_item.size() == 0) { + else /*seq_item.is_mapping()*/ { str += " {}\n"; } - else { - str += "\n"; - serialize_node(seq_item, cur_indent + 2, str); - } } break; case node_type::MAPPING: @@ -212,17 +218,23 @@ class basic_serializer { str += " "; serialize_node(*itr, cur_indent, str); str += "\n"; + continue; + } + + const bool is_empty = itr->empty(); + if (!is_empty) { + str += "\n"; + serialize_node(*itr, cur_indent + 2, str); + continue; } - else if (itr->is_sequence() && itr->size() == 0) { + + // an empty sequence or mapping + if (itr->is_sequence()) { str += " []\n"; } - else if (itr->is_mapping() && itr->size() == 0) { + else /*itr->is_mapping()*/ { str += " {}\n"; } - else { - str += "\n"; - serialize_node(*itr, cur_indent + 2, str); - } } break; case node_type::NULL_OBJECT: diff --git a/single_include/fkYAML/node.hpp b/single_include/fkYAML/node.hpp index 7afa3471..36d2290c 100644 --- a/single_include/fkYAML/node.hpp +++ b/single_include/fkYAML/node.hpp @@ -10529,17 +10529,23 @@ class basic_serializer { str += " "; serialize_node(seq_item, cur_indent, str); str += "\n"; + continue; + } + + const bool is_empty = seq_item.empty(); + if (!is_empty) { + str += "\n"; + serialize_node(seq_item, cur_indent + 2, str); + continue; } - else if (seq_item.is_sequence() && seq_item.size() == 0) { + + // an empty sequence or mapping + if (seq_item.is_sequence()) { str += " []\n"; } - else if (seq_item.is_mapping() && seq_item.size() == 0) { + else /*seq_item.is_mapping()*/ { str += " {}\n"; } - else { - str += "\n"; - serialize_node(seq_item, cur_indent + 2, str); - } } break; case node_type::MAPPING: @@ -10586,17 +10592,23 @@ class basic_serializer { str += " "; serialize_node(*itr, cur_indent, str); str += "\n"; + continue; + } + + const bool is_empty = itr->empty(); + if (!is_empty) { + str += "\n"; + serialize_node(*itr, cur_indent + 2, str); + continue; } - else if (itr->is_sequence() && itr->size() == 0) { + + // an empty sequence or mapping + if (itr->is_sequence()) { str += " []\n"; } - else if (itr->is_mapping() && itr->size() == 0) { + else /*itr->is_mapping()*/ { str += " {}\n"; } - else { - str += "\n"; - serialize_node(*itr, cur_indent + 2, str); - } } break; case node_type::NULL_OBJECT: diff --git a/tests/unit_test/test_serializer_class.cpp b/tests/unit_test/test_serializer_class.cpp index 7af79193..4e25634c 100644 --- a/tests/unit_test/test_serializer_class.cpp +++ b/tests/unit_test/test_serializer_class.cpp @@ -30,6 +30,36 @@ TEST_CASE("Serializer_MappingNode") { REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } +TEST_CASE("Serializer_EmptyCollectionNode") { + auto seq = fkyaml::node::sequence(); + auto map = fkyaml::node::mapping(); + fkyaml::detail::basic_serializer serializer; + + SECTION("child sequence item is an empty sequence node") { + seq.get_value_ref().emplace_back(fkyaml::node::sequence()); + std::string expected = "- []\n"; + REQUIRE(serializer.serialize(seq) == expected); + } + + SECTION("child sequence item is an empty mapping node") { + seq.get_value_ref().emplace_back(fkyaml::node::mapping()); + std::string expected = "- {}\n"; + REQUIRE(serializer.serialize(seq) == expected); + } + + SECTION("mapping value is an empty sequence node") { + map["foo"] = seq; + std::string expected = "foo: []\n"; + REQUIRE(serializer.serialize(map) == expected); + } + + SECTION("mapping value is an empty mapping node") { + map["foo"] = fkyaml::node::mapping(); + std::string expected = "foo: {}\n"; + REQUIRE(serializer.serialize(map) == expected); + } +} + TEST_CASE("Serializer_NullNode") { fkyaml::detail::basic_serializer serializer; fkyaml::node node;