From 1c1c78908424c3ec0515ed957a45e5ecfc363632 Mon Sep 17 00:00:00 2001 From: Evan Driscoll Date: Sat, 2 Jun 2018 21:34:55 -0500 Subject: [PATCH] Serializer edge case bug fix: very deep indents ignored indent_char --- .../detail/output/fancy_serializer.hpp | 4 +-- include/nlohmann/detail/output/serializer.hpp | 4 +-- single_include/nlohmann/json.hpp | 8 ++--- test/src/unit-fancy-serialization.cpp | 35 +++++++++++++++++++ test/src/unit-serialization.cpp | 31 ++++++++++++++++ 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 1cddcca3c..62462b085 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -162,7 +162,7 @@ class fancy_serializer const auto new_indent = (depth + 1) * style.indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, style.indent_char); } const int newline_len = (style.indent_step > 0); @@ -207,7 +207,7 @@ class fancy_serializer const auto new_indent = (depth + 1) * style.indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, style.indent_char); } const int newline_len = (style.indent_step > 0); diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index b4997adac..956df5685 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -93,7 +93,7 @@ class serializer const auto new_indent = current_indent + indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, indent_char); } // first n-1 elements @@ -166,7 +166,7 @@ class serializer const auto new_indent = current_indent + indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, indent_char); } // first n-1 elements diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 532e8fa19..a1d285474 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9876,7 +9876,7 @@ class serializer const auto new_indent = current_indent + indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, indent_char); } // first n-1 elements @@ -9949,7 +9949,7 @@ class serializer const auto new_indent = current_indent + indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, indent_char); } // first n-1 elements @@ -10232,7 +10232,7 @@ class fancy_serializer const auto new_indent = (depth + 1) * style.indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, style.indent_char); } const int newline_len = (style.indent_step > 0); @@ -10277,7 +10277,7 @@ class fancy_serializer const auto new_indent = (depth + 1) * style.indent_step; if (JSON_UNLIKELY(indent_string.size() < new_indent)) { - indent_string.resize(indent_string.size() * 2, ' '); + indent_string.resize(indent_string.size() * 2, style.indent_char); } const int newline_len = (style.indent_step > 0); diff --git a/test/src/unit-fancy-serialization.cpp b/test/src/unit-fancy-serialization.cpp index 71eab34ec..03c628d77 100644 --- a/test/src/unit-fancy-serialization.cpp +++ b/test/src/unit-fancy-serialization.cpp @@ -187,4 +187,39 @@ TEST_CASE("serialization") "]" ); } + + SECTION("indent_char is honored for deep indents in lists") + { + fancy_serializer_style style; + style.indent_step = 300; + style.indent_char = 'X'; + + auto str = fancy_to_string({1, {1}}, style); + + std::string indent(300, 'X'); + CHECK(str == + "[\n" + + indent + "1,\n" + + indent + "[\n" + + indent + indent + "1\n" + + indent + "]\n" + + "]"); + } + + SECTION("indent_char is honored for deep indents in objects") + { + fancy_serializer_style style; + style.indent_step = 300; + style.indent_char = 'X'; + + auto str = fancy_to_string({{"key", {{"key", 1}}}}, style); + + std::string indent(300, 'X'); + CHECK(str == + "{\n" + + indent + "\"key\": {\n" + + indent + indent + "\"key\": 1\n" + + indent + "}\n" + + "}"); + } } diff --git a/test/src/unit-serialization.cpp b/test/src/unit-serialization.cpp index 3abc7fb19..7918c171b 100644 --- a/test/src/unit-serialization.cpp +++ b/test/src/unit-serialization.cpp @@ -63,6 +63,37 @@ TEST_CASE("serialization") } } + SECTION("indent_char is honored for deep indents in lists") + { + std::stringstream ss; + json j = {1, {1}}; + ss << std::setw(300) << std::setfill('X') << j; + + std::string indent(300, 'X'); + CHECK(ss.str() == + "[\n" + + indent + "1,\n" + + indent + "[\n" + + indent + indent + "1\n" + + indent + "]\n" + + "]"); + } + + SECTION("indent_char is honored for deep indents in objects") + { + std::stringstream ss; + json j = {{"key", {{"key", 1}}}}; + ss << std::setw(300) << std::setfill('X') << j; + + std::string indent(300, 'X'); + CHECK(ss.str() == + "{\n" + + indent + "\"key\": {\n" + + indent + indent + "\"key\": 1\n" + + indent + "}\n" + + "}"); + } + SECTION("operator>>") { SECTION("no given width")