From ef679f89884b16e1401aa47a1a5e6add5c6b67f4 Mon Sep 17 00:00:00 2001 From: Evan Driscoll Date: Sat, 2 Jun 2018 23:17:42 -0500 Subject: [PATCH] Fancy serializer: styles continue to propagate if not overridden This is half a bug fix half just continuing work from befor --- .../detail/output/fancy_serializer.hpp | 10 ++++++---- single_include/nlohmann/json.hpp | 10 ++++++---- test/src/unit-fancy-serialization.cpp | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 2d783b77c..2e4bba8b2 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -58,10 +58,12 @@ class basic_fancy_serializer_stylizer return default_style; } - const fancy_serializer_style& get_style(const string_t& j) const + const fancy_serializer_style* get_new_style_or_active( + const string_t& j, + const fancy_serializer_style* active_style) const { auto iter = key_styles.find(j); - return iter == key_styles.end() ? default_style : iter->second; + return iter == key_styles.end() ? active_style : &iter->second; } fancy_serializer_style& get_or_insert_style(const string_t& j) @@ -236,7 +238,7 @@ class fancy_serializer o->write_character('\"'); prim_serializer.dump_escaped(*o, i->first, ensure_ascii); o->write_characters("\": ", 2 + newline_len); - auto new_style = &stylizer.get_style(i->first); + auto new_style = stylizer.get_new_style_or_active(i->first, active_style); dump(i->second, ensure_ascii, depth + 1, new_style); o->write_characters(",\n", 1 + newline_len); } @@ -248,7 +250,7 @@ class fancy_serializer o->write_character('\"'); prim_serializer.dump_escaped(*o, i->first, ensure_ascii); o->write_characters("\": ", 2 + newline_len); - auto new_style = &stylizer.get_style(i->first); + auto new_style = stylizer.get_new_style_or_active(i->first, active_style); dump(i->second, ensure_ascii, depth + 1, new_style); o->write_characters("\n", newline_len); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0009c3715..87fa8de2e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10128,10 +10128,12 @@ class basic_fancy_serializer_stylizer return default_style; } - const fancy_serializer_style& get_style(const string_t& j) const + const fancy_serializer_style* get_new_style_or_active( + const string_t& j, + const fancy_serializer_style* active_style) const { auto iter = key_styles.find(j); - return iter == key_styles.end() ? default_style : iter->second; + return iter == key_styles.end() ? active_style : &iter->second; } fancy_serializer_style& get_or_insert_style(const string_t& j) @@ -10306,7 +10308,7 @@ class fancy_serializer o->write_character('\"'); prim_serializer.dump_escaped(*o, i->first, ensure_ascii); o->write_characters("\": ", 2 + newline_len); - auto new_style = &stylizer.get_style(i->first); + auto new_style = stylizer.get_new_style_or_active(i->first, active_style); dump(i->second, ensure_ascii, depth + 1, new_style); o->write_characters(",\n", 1 + newline_len); } @@ -10318,7 +10320,7 @@ class fancy_serializer o->write_character('\"'); prim_serializer.dump_escaped(*o, i->first, ensure_ascii); o->write_characters("\": ", 2 + newline_len); - auto new_style = &stylizer.get_style(i->first); + auto new_style = stylizer.get_new_style_or_active(i->first, active_style); dump(i->second, ensure_ascii, depth + 1, new_style); o->write_characters("\n", newline_len); diff --git a/test/src/unit-fancy-serialization.cpp b/test/src/unit-fancy-serialization.cpp index 8c82a32c4..26af09939 100644 --- a/test/src/unit-fancy-serialization.cpp +++ b/test/src/unit-fancy-serialization.cpp @@ -256,6 +256,26 @@ TEST_CASE("serialization") ] })")); } + + SECTION("changes propagate (unless overridden)") + { + fancy_serializer_stylizer stylizer; + stylizer.get_default_style().indent_step = 4; + stylizer.get_or_insert_style("one line").indent_step = 0; + + auto str = fancy_to_string( + { + { + "one line", {{"still one line", {1, 2}}} + }, + }, + stylizer); + + CHECK(str == dedent(R"( + { + "one line": {"still one line":[1,2]} + })")); + } } SECTION("given width")