Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: made sure that empty documents just serialize to [] or {} #460

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions include/fkYAML/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class basic_serializer {
void serialize_node(const BasicNodeType& node, const uint32_t cur_indent, std::string& str) {
switch (node.get_type()) {
case node_type::SEQUENCE:
if (node.size() == 0) {
str += "[]\n";
return;
}
for (const auto& seq_item : node) {
insert_indentation(cur_indent, str);
str += "-";
Expand Down Expand Up @@ -175,6 +179,10 @@ class basic_serializer {
}
break;
case node_type::MAPPING:
if (node.size() == 0) {
str += "{}\n";
return;
}
for (auto itr : node.map_items()) {
insert_indentation(cur_indent, str);

Expand Down Expand Up @@ -228,19 +236,13 @@ class basic_serializer {
}

const bool is_empty = itr->empty();
if (!is_empty) {
str += "\n";
serialize_node(value_node, cur_indent + 2, str);
continue;
}

// an empty sequence or mapping
if (value_node.is_sequence()) {
str += " []\n";
if (is_empty) {
str += " ";
}
else /*value_node.is_mapping()*/ {
str += " {}\n";
else {
str += "\n";
}
serialize_node(value_node, cur_indent + 2, str);
}
break;
case node_type::NULL_OBJECT:
Expand Down
Loading