From 9cdf54b8867f88feac4cc4035d0f9d64aadc6781 Mon Sep 17 00:00:00 2001 From: Evan Driscoll Date: Sat, 2 Jun 2018 21:01:22 -0500 Subject: [PATCH] Refactor: dump_array doesn't split cases for indent --- .../detail/output/fancy_serializer.hpp | 62 +++++++------------ single_include/nlohmann/json.hpp | 62 +++++++------------ 2 files changed, 42 insertions(+), 82 deletions(-) diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 190bcbc63..30b272e02 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -229,54 +229,34 @@ class fancy_serializer return; } - if (style.indent_step > 0) + // variable to hold indentation for recursive calls + const auto old_indent = depth * style.indent_step; + const auto new_indent = (depth + 1) * style.indent_step; + if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - o->write_characters("[\n", 2); + indent_string.resize(indent_string.size() * 2, ' '); + } + const int newline_len = (style.indent_step > 0); - // variable to hold indentation for recursive calls - const auto old_indent = depth * style.indent_step; - const auto new_indent = (depth + 1) * style.indent_step; - if (JSON_UNLIKELY(indent_string.size() < new_indent)) - { - indent_string.resize(indent_string.size() * 2, ' '); - } + o->write_characters("[\n", 1 + newline_len); - // first n-1 elements - for (auto i = val.m_value.array->cbegin(); - i != val.m_value.array->cend() - 1; ++i) - { - o->write_characters(indent_string.c_str(), new_indent); - dump(*i, ensure_ascii, depth + 1); - o->write_characters(",\n", 2); - } - - // last element - assert(not val.m_value.array->empty()); + // first n-1 elements + for (auto i = val.m_value.array->cbegin(); + i != val.m_value.array->cend() - 1; ++i) + { o->write_characters(indent_string.c_str(), new_indent); - dump(val.m_value.array->back(), ensure_ascii, depth + 1); - - o->write_character('\n'); - o->write_characters(indent_string.c_str(), old_indent); - o->write_character(']'); + dump(*i, ensure_ascii, depth + 1); + o->write_characters(",\n", 1 + newline_len); } - else - { - o->write_character('['); - // first n-1 elements - for (auto i = val.m_value.array->cbegin(); - i != val.m_value.array->cend() - 1; ++i) - { - dump(*i, ensure_ascii, depth + 1); - o->write_character(','); - } + // last element + assert(not val.m_value.array->empty()); + o->write_characters(indent_string.c_str(), new_indent); + dump(val.m_value.array->back(), ensure_ascii, depth + 1); - // last element - assert(not val.m_value.array->empty()); - dump(val.m_value.array->back(), ensure_ascii, depth + 1); - - o->write_character(']'); - } + o->write_characters("\n", newline_len); + o->write_characters(indent_string.c_str(), old_indent); + o->write_character(']'); } void dump_string(const string_t& str, bool ensure_ascii) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8782872ea..45601d51d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10299,54 +10299,34 @@ class fancy_serializer return; } - if (style.indent_step > 0) + // variable to hold indentation for recursive calls + const auto old_indent = depth * style.indent_step; + const auto new_indent = (depth + 1) * style.indent_step; + if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - o->write_characters("[\n", 2); + indent_string.resize(indent_string.size() * 2, ' '); + } + const int newline_len = (style.indent_step > 0); - // variable to hold indentation for recursive calls - const auto old_indent = depth * style.indent_step; - const auto new_indent = (depth + 1) * style.indent_step; - if (JSON_UNLIKELY(indent_string.size() < new_indent)) - { - indent_string.resize(indent_string.size() * 2, ' '); - } + o->write_characters("[\n", 1 + newline_len); - // first n-1 elements - for (auto i = val.m_value.array->cbegin(); - i != val.m_value.array->cend() - 1; ++i) - { - o->write_characters(indent_string.c_str(), new_indent); - dump(*i, ensure_ascii, depth + 1); - o->write_characters(",\n", 2); - } - - // last element - assert(not val.m_value.array->empty()); + // first n-1 elements + for (auto i = val.m_value.array->cbegin(); + i != val.m_value.array->cend() - 1; ++i) + { o->write_characters(indent_string.c_str(), new_indent); - dump(val.m_value.array->back(), ensure_ascii, depth + 1); - - o->write_character('\n'); - o->write_characters(indent_string.c_str(), old_indent); - o->write_character(']'); + dump(*i, ensure_ascii, depth + 1); + o->write_characters(",\n", 1 + newline_len); } - else - { - o->write_character('['); - // first n-1 elements - for (auto i = val.m_value.array->cbegin(); - i != val.m_value.array->cend() - 1; ++i) - { - dump(*i, ensure_ascii, depth + 1); - o->write_character(','); - } + // last element + assert(not val.m_value.array->empty()); + o->write_characters(indent_string.c_str(), new_indent); + dump(val.m_value.array->back(), ensure_ascii, depth + 1); - // last element - assert(not val.m_value.array->empty()); - dump(val.m_value.array->back(), ensure_ascii, depth + 1); - - o->write_character(']'); - } + o->write_characters("\n", newline_len); + o->write_characters(indent_string.c_str(), old_indent); + o->write_character(']'); } void dump_string(const string_t& str, bool ensure_ascii)