diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 2e4bba8b2..6980b29d8 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -203,6 +203,22 @@ class fancy_serializer } private: + template + void dump_object_key_value( + Iterator i, bool ensure_ascii, unsigned int depth, + const fancy_serializer_style* active_style) + { + const auto new_indent = (depth + 1) * active_style->indent_step; + const int newline_len = (active_style->indent_step > 0); + + o->write_characters(indent_string.c_str(), new_indent); + o->write_character('\"'); + prim_serializer.dump_escaped(*o, i->first, ensure_ascii); + o->write_characters("\": ", 2 + newline_len); + auto new_style = stylizer.get_new_style_or_active(i->first, active_style); + dump(i->second, ensure_ascii, depth + 1, new_style); + } + void dump_object(const BasicJsonType& val, bool ensure_ascii, unsigned int depth, @@ -234,24 +250,14 @@ class fancy_serializer auto i = val.m_value.object->cbegin(); for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i) { - o->write_characters(indent_string.c_str(), new_indent); - o->write_character('\"'); - prim_serializer.dump_escaped(*o, i->first, ensure_ascii); - o->write_characters("\": ", 2 + newline_len); - auto new_style = stylizer.get_new_style_or_active(i->first, active_style); - dump(i->second, ensure_ascii, depth + 1, new_style); + dump_object_key_value(i, ensure_ascii, depth, active_style); o->write_characters(",\n", 1 + newline_len); } // last element assert(i != val.m_value.object->cend()); assert(std::next(i) == val.m_value.object->cend()); - o->write_characters(indent_string.c_str(), new_indent); - o->write_character('\"'); - prim_serializer.dump_escaped(*o, i->first, ensure_ascii); - o->write_characters("\": ", 2 + newline_len); - auto new_style = stylizer.get_new_style_or_active(i->first, active_style); - dump(i->second, ensure_ascii, depth + 1, new_style); + dump_object_key_value(i, ensure_ascii, depth, active_style); o->write_characters("\n", newline_len); o->write_characters(indent_string.c_str(), old_indent); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 87fa8de2e..5ca8601d3 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10273,6 +10273,22 @@ class fancy_serializer } private: + template + void dump_object_key_value( + Iterator i, bool ensure_ascii, unsigned int depth, + const fancy_serializer_style* active_style) + { + const auto new_indent = (depth + 1) * active_style->indent_step; + const int newline_len = (active_style->indent_step > 0); + + o->write_characters(indent_string.c_str(), new_indent); + o->write_character('\"'); + prim_serializer.dump_escaped(*o, i->first, ensure_ascii); + o->write_characters("\": ", 2 + newline_len); + auto new_style = stylizer.get_new_style_or_active(i->first, active_style); + dump(i->second, ensure_ascii, depth + 1, new_style); + } + void dump_object(const BasicJsonType& val, bool ensure_ascii, unsigned int depth, @@ -10304,24 +10320,14 @@ class fancy_serializer auto i = val.m_value.object->cbegin(); for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i) { - o->write_characters(indent_string.c_str(), new_indent); - o->write_character('\"'); - prim_serializer.dump_escaped(*o, i->first, ensure_ascii); - o->write_characters("\": ", 2 + newline_len); - auto new_style = stylizer.get_new_style_or_active(i->first, active_style); - dump(i->second, ensure_ascii, depth + 1, new_style); + dump_object_key_value(i, ensure_ascii, depth, active_style); o->write_characters(",\n", 1 + newline_len); } // last element assert(i != val.m_value.object->cend()); assert(std::next(i) == val.m_value.object->cend()); - o->write_characters(indent_string.c_str(), new_indent); - o->write_character('\"'); - prim_serializer.dump_escaped(*o, i->first, ensure_ascii); - o->write_characters("\": ", 2 + newline_len); - auto new_style = stylizer.get_new_style_or_active(i->first, active_style); - dump(i->second, ensure_ascii, depth + 1, new_style); + dump_object_key_value(i, ensure_ascii, depth, active_style); o->write_characters("\n", newline_len); o->write_characters(indent_string.c_str(), old_indent);