diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 6980b29d8..315cba31f 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -33,6 +33,8 @@ struct fancy_serializer_style unsigned int depth_limit = std::numeric_limits::max(); unsigned int strings_maximum_length = 0; + + bool multiline = false; }; template @@ -242,7 +244,7 @@ class fancy_serializer { indent_string.resize(indent_string.size() * 2, active_style->indent_char); } - const int newline_len = (active_style->indent_step > 0); + const int newline_len = (active_style->multiline ? 1 : 0); o->write_characters("{\n", 1 + newline_len); @@ -287,7 +289,7 @@ class fancy_serializer { indent_string.resize(indent_string.size() * 2, active_style->indent_char); } - const int newline_len = (active_style->indent_step > 0); + const int newline_len = (active_style->multiline ? 1 : 0); o->write_characters("[\n", 1 + newline_len); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 5ca8601d3..6edec302f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10103,6 +10103,8 @@ struct fancy_serializer_style unsigned int depth_limit = std::numeric_limits::max(); unsigned int strings_maximum_length = 0; + + bool multiline = false; }; template @@ -10312,7 +10314,7 @@ class fancy_serializer { indent_string.resize(indent_string.size() * 2, active_style->indent_char); } - const int newline_len = (active_style->indent_step > 0); + const int newline_len = (active_style->multiline ? 1 : 0); o->write_characters("{\n", 1 + newline_len); @@ -10357,7 +10359,7 @@ class fancy_serializer { indent_string.resize(indent_string.size() * 2, active_style->indent_char); } - const int newline_len = (active_style->indent_step > 0); + const int newline_len = (active_style->multiline ? 1 : 0); o->write_characters("[\n", 1 + newline_len); diff --git a/test/src/unit-fancy-serialization.cpp b/test/src/unit-fancy-serialization.cpp index 26af09939..6f00a9b21 100644 --- a/test/src/unit-fancy-serialization.cpp +++ b/test/src/unit-fancy-serialization.cpp @@ -202,6 +202,7 @@ TEST_CASE("serialization") CHECK(str_flat == "[1,[...]]"); style.indent_step = 4; + style.multiline = true; auto str_lines = fancy_to_string({1, {1}}, style); CHECK(str_lines == dedent(R"( [ @@ -219,6 +220,7 @@ TEST_CASE("serialization") CHECK(str_flat == "[1,{...}]"); style.indent_step = 4; + style.multiline = true; auto str_lines = fancy_to_string({1, {{"one", 1}}}, style); CHECK(str_lines == dedent(R"( [ @@ -234,7 +236,9 @@ TEST_CASE("serialization") { fancy_serializer_stylizer stylizer; stylizer.get_default_style().indent_step = 4; + stylizer.get_default_style().multiline = true; stylizer.get_or_insert_style("one line").indent_step = 0; + stylizer.get_or_insert_style("one line").multiline = false; auto str = fancy_to_string( { @@ -261,6 +265,7 @@ TEST_CASE("serialization") { fancy_serializer_stylizer stylizer; stylizer.get_default_style().indent_step = 4; + stylizer.get_default_style().multiline = 4; stylizer.get_or_insert_style("one line").indent_step = 0; auto str = fancy_to_string( @@ -282,6 +287,7 @@ TEST_CASE("serialization") { fancy_serializer_style style; style.indent_step = 4; + style.multiline = 4; auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style); CHECK(str == dedent(R"( [ @@ -301,6 +307,7 @@ TEST_CASE("serialization") fancy_serializer_style style; style.indent_step = 1; style.indent_char = '\t'; + style.multiline = true; auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style); CHECK(str == @@ -322,6 +329,7 @@ TEST_CASE("serialization") fancy_serializer_style style; style.indent_step = 300; style.indent_char = 'X'; + style.multiline = true; auto str = fancy_to_string({1, {1}}, style); @@ -340,6 +348,7 @@ TEST_CASE("serialization") fancy_serializer_style style; style.indent_step = 300; style.indent_char = 'X'; + style.multiline = true; auto str = fancy_to_string({{"key", {{"key", 1}}}}, style);