From e7b02c10dd008a525da5307b33a8aefdd24995c8 Mon Sep 17 00:00:00 2001 From: Evan Driscoll Date: Sun, 3 Jun 2018 00:00:12 -0500 Subject: [PATCH] Spaces after a colon (in {"k": v}) controllable separately from multiline --- .../detail/output/fancy_serializer.hpp | 9 ++++- single_include/nlohmann/json.hpp | 9 ++++- test/src/unit-fancy-serialization.cpp | 40 +++++++++++++++---- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 580ecb571..c210992b9 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -34,7 +34,14 @@ struct fancy_serializer_style unsigned int strings_maximum_length = 0; + bool space_after_colon = false; + bool multiline = false; + + void set_old_multiline() + { + space_after_colon = multiline = true; + } }; template @@ -211,7 +218,7 @@ class fancy_serializer const fancy_serializer_style* active_style) { const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline; - const int newline_len = (active_style->indent_step > 0); + const int newline_len = active_style->space_after_colon; o->write_characters(indent_string.c_str(), new_indent); o->write_character('\"'); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fa5b7f251..5b78a676e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10104,7 +10104,14 @@ struct fancy_serializer_style unsigned int strings_maximum_length = 0; + bool space_after_colon = false; + bool multiline = false; + + void set_old_multiline() + { + space_after_colon = multiline = true; + } }; template @@ -10281,7 +10288,7 @@ class fancy_serializer const fancy_serializer_style* active_style) { const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline; - const int newline_len = (active_style->indent_step > 0); + const int newline_len = active_style->space_after_colon; o->write_characters(indent_string.c_str(), new_indent); o->write_character('\"'); diff --git a/test/src/unit-fancy-serialization.cpp b/test/src/unit-fancy-serialization.cpp index edb371593..9c13177a3 100644 --- a/test/src/unit-fancy-serialization.cpp +++ b/test/src/unit-fancy-serialization.cpp @@ -201,7 +201,7 @@ TEST_CASE("serialization") auto str_flat = fancy_to_string({1, {1}}, style); CHECK(str_flat == "[1,[...]]"); - style.multiline = true; + style.set_old_multiline(); auto str_lines = fancy_to_string({1, {1}}, style); CHECK(str_lines == dedent(R"( [ @@ -218,7 +218,7 @@ TEST_CASE("serialization") auto str_flat = fancy_to_string({1, {{"one", 1}}}, style); CHECK(str_flat == "[1,{...}]"); - style.multiline = true; + style.set_old_multiline(); auto str_lines = fancy_to_string({1, {{"one", 1}}}, style); CHECK(str_lines == dedent(R"( [ @@ -233,7 +233,7 @@ TEST_CASE("serialization") SECTION("can style objects of a key differently") { fancy_serializer_stylizer stylizer; - stylizer.get_default_style().multiline = true; + stylizer.get_default_style().set_old_multiline(); stylizer.get_or_insert_style("one line").multiline = false; auto str = fancy_to_string( @@ -260,7 +260,7 @@ TEST_CASE("serialization") SECTION("changes propagate (unless overridden)") { fancy_serializer_stylizer stylizer; - stylizer.get_default_style().multiline = true; + stylizer.get_default_style().set_old_multiline(); stylizer.get_or_insert_style("one line").indent_step = 0; auto str = fancy_to_string( @@ -278,10 +278,34 @@ TEST_CASE("serialization") } } + SECTION("Spaces after commas are controllable separately from multiline") + { + SECTION("colons") + { + fancy_serializer_style style; + style.space_after_colon = true; + auto str = fancy_to_string({{"one", 1}}, style); + CHECK(str == "{\"one\": 1}"); + } + + SECTION("multiline can have no space") + { + fancy_serializer_style style; + style.set_old_multiline(); + style.space_after_colon = false; + auto str = fancy_to_string({{"one", 1}}, style); + CHECK(str == dedent(R"( + { + "one":1 + })")); + + } + } + SECTION("given width") { fancy_serializer_style style; - style.multiline = true; + style.set_old_multiline(); auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style); CHECK(str == dedent(R"( [ @@ -301,7 +325,7 @@ TEST_CASE("serialization") fancy_serializer_style style; style.indent_step = 1; style.indent_char = '\t'; - style.multiline = true; + style.set_old_multiline(); auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style); CHECK(str == @@ -323,7 +347,7 @@ TEST_CASE("serialization") fancy_serializer_style style; style.indent_step = 300; style.indent_char = 'X'; - style.multiline = true; + style.set_old_multiline(); auto str = fancy_to_string({1, {1}}, style); @@ -342,7 +366,7 @@ TEST_CASE("serialization") fancy_serializer_style style; style.indent_step = 300; style.indent_char = 'X'; - style.multiline = true; + style.set_old_multiline(); auto str = fancy_to_string({{"key", {{"key", 1}}}}, style);