diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 62798d5e4..f40fb7532 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -29,7 +29,7 @@ namespace nlohmann { -struct fancy_serializer_style +struct print_style { unsigned int indent_step = 4; char indent_char = ' '; @@ -43,23 +43,23 @@ struct fancy_serializer_style bool multiline = false; - fancy_serializer_style() = default; + print_style() = default; - fancy_serializer_style(bool s_colon, bool s_comma, bool ml) + print_style(bool s_colon, bool s_comma, bool ml) : space_after_colon(s_colon), space_after_comma(s_comma), multiline(ml) {} - static const fancy_serializer_style preset_compact; - static const fancy_serializer_style preset_one_line; - static const fancy_serializer_style preset_multiline; + static const print_style preset_compact; + static const print_style preset_one_line; + static const print_style preset_multiline; }; -const fancy_serializer_style fancy_serializer_style::preset_compact(false, false, false); -const fancy_serializer_style fancy_serializer_style::preset_one_line(true, true, false); -const fancy_serializer_style fancy_serializer_style::preset_multiline(true, true, true); +const print_style print_style::preset_compact(false, false, false); +const print_style print_style::preset_one_line(true, true, false); +const print_style print_style::preset_multiline(true, true, true); template -class basic_fancy_serializer_stylizer +class basic_print_stylizer { public: using string_t = typename BasicJsonType::string_t; @@ -69,27 +69,27 @@ class basic_fancy_serializer_stylizer using context_matcher_predicate = std::function; using matcher_predicate = std::function; - basic_fancy_serializer_stylizer(fancy_serializer_style const& ds) + basic_print_stylizer(print_style const& ds) : default_style(ds) {} - basic_fancy_serializer_stylizer() = default; + basic_print_stylizer() = default; public: - const fancy_serializer_style& get_default_style() const + const print_style& get_default_style() const { return default_style; } - fancy_serializer_style& get_default_style() + print_style& get_default_style() { return default_style; } - const fancy_serializer_style* get_new_style_or_active( + const print_style* get_new_style_or_active( const json_pointer_t& pointer, const json& j, - const fancy_serializer_style* active_style) const + const print_style* active_style) const { for (auto const& pair : styles) { @@ -101,18 +101,18 @@ class basic_fancy_serializer_stylizer return active_style; } - fancy_serializer_style& register_style( + print_style& register_style( matcher_predicate p, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { styles.emplace_back(p, style); return styles.back().second; } template - fancy_serializer_style& register_style_object_pred( + print_style& register_style_object_pred( Predicate p, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { auto wrapper = [p](const json_pointer_t&, const BasicJsonType & j) { @@ -123,9 +123,9 @@ class basic_fancy_serializer_stylizer } template - fancy_serializer_style& register_style_context_pred( + print_style& register_style_context_pred( Predicate p, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { auto wrapper = [p](const json_pointer_t& c, const BasicJsonType&) { @@ -135,9 +135,9 @@ class basic_fancy_serializer_stylizer return styles.back().second; } - fancy_serializer_style& register_key_matcher_style( + print_style& register_key_matcher_style( string_t str, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { return register_style_context_pred([str](const json_pointer_t& pointer) { @@ -147,14 +147,14 @@ class basic_fancy_serializer_stylizer style); } - fancy_serializer_style& last_registered_style() + print_style& last_registered_style() { return styles.back().second; } private: - fancy_serializer_style default_style; - std::vector> styles; + print_style default_style; + std::vector> styles; }; namespace detail @@ -166,7 +166,7 @@ namespace detail template class fancy_serializer { - using stylizer_t = basic_fancy_serializer_stylizer; + using stylizer_t = basic_print_stylizer; using primitive_serializer_t = primitive_serializer; using string_t = typename BasicJsonType::string_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -216,7 +216,7 @@ class fancy_serializer void dump(const BasicJsonType& val, const bool ensure_ascii, const unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { active_style = stylizer.get_new_style_or_active(context, val, active_style); @@ -290,7 +290,7 @@ class fancy_serializer template void dump_object_key_value( Iterator i, bool ensure_ascii, unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline; @@ -306,7 +306,7 @@ class fancy_serializer void dump_object(const BasicJsonType& val, bool ensure_ascii, unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { if (val.m_value.object->empty()) @@ -352,7 +352,7 @@ class fancy_serializer void dump_array(const BasicJsonType& val, bool ensure_ascii, unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { if (val.m_value.array->empty()) @@ -405,7 +405,7 @@ class fancy_serializer } void dump_string(const string_t& str, bool ensure_ascii, - const fancy_serializer_style* active_style) + const print_style* active_style) { o->write_character('\"'); if (active_style->strings_maximum_length == 0) @@ -484,7 +484,7 @@ class fancy_serializer template std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, - basic_fancy_serializer_stylizer const& stylizer) + basic_print_stylizer const& stylizer) { // do the actual serialization detail::fancy_serializer s(detail::output_adapter(o), stylizer); @@ -493,10 +493,9 @@ std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, } template -std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, - fancy_serializer_style style) +std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, print_style style) { - basic_fancy_serializer_stylizer stylizer(style); + basic_print_stylizer stylizer(style); return fancy_dump(o, j, stylizer); } diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 16984d5f7..bfe032045 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -7625,7 +7625,7 @@ class basic_json /// @} }; -using fancy_serializer_stylizer = basic_fancy_serializer_stylizer; +using print_stylizer = basic_print_stylizer; } // namespace nlohmann /////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b888e1545..d72fbf1a2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10834,7 +10834,7 @@ class json_pointer namespace nlohmann { -struct fancy_serializer_style +struct print_style { unsigned int indent_step = 4; char indent_char = ' '; @@ -10848,23 +10848,23 @@ struct fancy_serializer_style bool multiline = false; - fancy_serializer_style() = default; + print_style() = default; - fancy_serializer_style(bool s_colon, bool s_comma, bool ml) + print_style(bool s_colon, bool s_comma, bool ml) : space_after_colon(s_colon), space_after_comma(s_comma), multiline(ml) {} - static const fancy_serializer_style preset_compact; - static const fancy_serializer_style preset_one_line; - static const fancy_serializer_style preset_multiline; + static const print_style preset_compact; + static const print_style preset_one_line; + static const print_style preset_multiline; }; -const fancy_serializer_style fancy_serializer_style::preset_compact(false, false, false); -const fancy_serializer_style fancy_serializer_style::preset_one_line(true, true, false); -const fancy_serializer_style fancy_serializer_style::preset_multiline(true, true, true); +const print_style print_style::preset_compact(false, false, false); +const print_style print_style::preset_one_line(true, true, false); +const print_style print_style::preset_multiline(true, true, true); template -class basic_fancy_serializer_stylizer +class basic_print_stylizer { public: using string_t = typename BasicJsonType::string_t; @@ -10874,27 +10874,27 @@ class basic_fancy_serializer_stylizer using context_matcher_predicate = std::function; using matcher_predicate = std::function; - basic_fancy_serializer_stylizer(fancy_serializer_style const& ds) + basic_print_stylizer(print_style const& ds) : default_style(ds) {} - basic_fancy_serializer_stylizer() = default; + basic_print_stylizer() = default; public: - const fancy_serializer_style& get_default_style() const + const print_style& get_default_style() const { return default_style; } - fancy_serializer_style& get_default_style() + print_style& get_default_style() { return default_style; } - const fancy_serializer_style* get_new_style_or_active( + const print_style* get_new_style_or_active( const json_pointer_t& pointer, const json& j, - const fancy_serializer_style* active_style) const + const print_style* active_style) const { for (auto const& pair : styles) { @@ -10906,18 +10906,18 @@ class basic_fancy_serializer_stylizer return active_style; } - fancy_serializer_style& register_style( + print_style& register_style( matcher_predicate p, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { styles.emplace_back(p, style); return styles.back().second; } template - fancy_serializer_style& register_style_object_pred( + print_style& register_style_object_pred( Predicate p, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { auto wrapper = [p](const json_pointer_t&, const BasicJsonType & j) { @@ -10928,9 +10928,9 @@ class basic_fancy_serializer_stylizer } template - fancy_serializer_style& register_style_context_pred( + print_style& register_style_context_pred( Predicate p, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { auto wrapper = [p](const json_pointer_t& c, const BasicJsonType&) { @@ -10940,9 +10940,9 @@ class basic_fancy_serializer_stylizer return styles.back().second; } - fancy_serializer_style& register_key_matcher_style( + print_style& register_key_matcher_style( string_t str, - fancy_serializer_style style = fancy_serializer_style()) + print_style style = print_style()) { return register_style_context_pred([str](const json_pointer_t& pointer) { @@ -10952,14 +10952,14 @@ class basic_fancy_serializer_stylizer style); } - fancy_serializer_style& last_registered_style() + print_style& last_registered_style() { return styles.back().second; } private: - fancy_serializer_style default_style; - std::vector> styles; + print_style default_style; + std::vector> styles; }; namespace detail @@ -10971,7 +10971,7 @@ namespace detail template class fancy_serializer { - using stylizer_t = basic_fancy_serializer_stylizer; + using stylizer_t = basic_print_stylizer; using primitive_serializer_t = primitive_serializer; using string_t = typename BasicJsonType::string_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -11021,7 +11021,7 @@ class fancy_serializer void dump(const BasicJsonType& val, const bool ensure_ascii, const unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { active_style = stylizer.get_new_style_or_active(context, val, active_style); @@ -11095,7 +11095,7 @@ class fancy_serializer template void dump_object_key_value( Iterator i, bool ensure_ascii, unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline; @@ -11111,7 +11111,7 @@ class fancy_serializer void dump_object(const BasicJsonType& val, bool ensure_ascii, unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { if (val.m_value.object->empty()) @@ -11157,7 +11157,7 @@ class fancy_serializer void dump_array(const BasicJsonType& val, bool ensure_ascii, unsigned int depth, - const fancy_serializer_style* active_style, + const print_style* active_style, const json_pointer_t& context) { if (val.m_value.array->empty()) @@ -11210,7 +11210,7 @@ class fancy_serializer } void dump_string(const string_t& str, bool ensure_ascii, - const fancy_serializer_style* active_style) + const print_style* active_style) { o->write_character('\"'); if (active_style->strings_maximum_length == 0) @@ -11289,7 +11289,7 @@ class fancy_serializer template std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, - basic_fancy_serializer_stylizer const& stylizer) + basic_print_stylizer const& stylizer) { // do the actual serialization detail::fancy_serializer s(detail::output_adapter(o), stylizer); @@ -11298,10 +11298,9 @@ std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, } template -std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, - fancy_serializer_style style) +std::ostream& fancy_dump(std::ostream& o, const BasicJsonType& j, print_style style) { - basic_fancy_serializer_stylizer stylizer(style); + basic_print_stylizer stylizer(style); return fancy_dump(o, j, stylizer); } @@ -18979,7 +18978,7 @@ class basic_json /// @} }; -using fancy_serializer_stylizer = basic_fancy_serializer_stylizer; +using print_stylizer = basic_print_stylizer; } // namespace nlohmann /////////////////////// diff --git a/test/src/unit-fancy-serialization.cpp b/test/src/unit-fancy-serialization.cpp index 1ac0ec720..6299bfef2 100644 --- a/test/src/unit-fancy-serialization.cpp +++ b/test/src/unit-fancy-serialization.cpp @@ -35,8 +35,8 @@ SOFTWARE. using nlohmann::json; using nlohmann::json_pointer; using nlohmann::fancy_dump; -using nlohmann::fancy_serializer_style; -using nlohmann::fancy_serializer_stylizer; +using nlohmann::print_style; +using nlohmann::print_stylizer; // Chops off the first line (if empty, but if it *isn't* empty you're // probably using this wrong), measures the leading indent on the @@ -73,14 +73,14 @@ std::string dedent(const char* str) return ans; } -std::string fancy_to_string(json j, fancy_serializer_style style = fancy_serializer_style()) +std::string fancy_to_string(json j, print_style style = print_style()) { std::stringstream ss; fancy_dump(ss, j, style); return ss.str(); } -std::string fancy_to_string(json j, fancy_serializer_stylizer stylizer) +std::string fancy_to_string(json j, print_stylizer stylizer) { std::stringstream ss; fancy_dump(ss, j, stylizer); @@ -134,7 +134,7 @@ TEST_CASE("serialization") SECTION("long strings can be shortened") { - fancy_serializer_style style; + print_style style; style.strings_maximum_length = 10; auto str = fancy_to_string( @@ -158,7 +158,7 @@ TEST_CASE("serialization") for (auto test : tests) { - fancy_serializer_style style; + print_style style; style.strings_maximum_length = test.first; auto str = fancy_to_string(quick, style); CHECK(str == test.second); @@ -167,7 +167,7 @@ TEST_CASE("serialization") SECTION("But you cannot ask for a length of zero; that means unlimited") { - fancy_serializer_style style; + print_style style; style.strings_maximum_length = 0; auto str = fancy_to_string( @@ -179,7 +179,7 @@ TEST_CASE("serialization") SECTION("\"Limiting\" to something long doesn't do anything") { - fancy_serializer_style style; + print_style style; style.strings_maximum_length = 100; auto str = fancy_to_string( @@ -197,13 +197,13 @@ TEST_CASE("serialization") { SECTION("recursing past the maximum depth with a list elides the subobjects") { - fancy_serializer_style style; + print_style style; style.depth_limit = 1; auto str_flat = fancy_to_string({1, {1}}, style); CHECK(str_flat == "[1,[...]]"); - style = fancy_serializer_style::preset_multiline; + style = print_style::preset_multiline; style.depth_limit = 1; auto str_lines = fancy_to_string({1, {1}}, style); CHECK(str_lines == dedent(R"( @@ -215,13 +215,13 @@ TEST_CASE("serialization") SECTION("recursing past the maximum depth with an object elides the subobjects") { - fancy_serializer_style style; + print_style style; style.depth_limit = 1; auto str_flat = fancy_to_string({1, {{"one", 1}}}, style); CHECK(str_flat == "[1,{...}]"); - style = fancy_serializer_style::preset_multiline; + style = print_style::preset_multiline; style.depth_limit = 1; auto str_lines = fancy_to_string({1, {{"one", 1}}}, style); CHECK(str_lines == dedent(R"( @@ -236,8 +236,8 @@ TEST_CASE("serialization") { SECTION("can style objects of a key differently") { - fancy_serializer_stylizer stylizer; - stylizer.get_default_style() = fancy_serializer_style::preset_multiline; + print_stylizer stylizer; + stylizer.get_default_style() = print_style::preset_multiline; stylizer.register_key_matcher_style("one line"); auto str = fancy_to_string( @@ -263,8 +263,8 @@ TEST_CASE("serialization") SECTION("changes propagate (unless overridden)") { - fancy_serializer_stylizer stylizer; - stylizer.get_default_style() = fancy_serializer_style::preset_multiline; + print_stylizer stylizer; + stylizer.get_default_style() = print_style::preset_multiline; stylizer.register_key_matcher_style("one line"); auto str = fancy_to_string( @@ -283,8 +283,8 @@ TEST_CASE("serialization") SECTION("example of more sophisticated context matcher") { - fancy_serializer_stylizer stylizer; - stylizer.get_default_style() = fancy_serializer_style::preset_multiline; + print_stylizer stylizer; + stylizer.get_default_style() = print_style::preset_multiline; stylizer.register_style_context_pred( [] (const json_pointer& context) @@ -329,15 +329,15 @@ TEST_CASE("serialization") SECTION("example of more sophisticated json matcher") { - fancy_serializer_stylizer stylizer; - stylizer.get_default_style() = fancy_serializer_style::preset_multiline; + print_stylizer stylizer; + stylizer.get_default_style() = print_style::preset_multiline; stylizer.register_style_object_pred( [] (const json & j) { return j.type() == json::value_t::array; } - ) = fancy_serializer_style::preset_one_line; + ) = print_style::preset_one_line; auto str = fancy_to_string( { @@ -364,7 +364,7 @@ TEST_CASE("serialization") { SECTION("commas") { - fancy_serializer_style style; + print_style style; style.space_after_comma = true; auto str = fancy_to_string({1, 2, 3}, style); CHECK(str == "[1, 2, 3]"); @@ -372,7 +372,7 @@ TEST_CASE("serialization") SECTION("colons") { - fancy_serializer_style style; + print_style style; style.space_after_colon = true; auto str = fancy_to_string({{"one", 1}}, style); CHECK(str == "{\"one\": 1}"); @@ -380,7 +380,7 @@ TEST_CASE("serialization") SECTION("multiline can have no space") { - fancy_serializer_style style = fancy_serializer_style::preset_multiline; + print_style style = print_style::preset_multiline; style.space_after_colon = false; auto str = fancy_to_string({{"one", 1}}, style); CHECK(str == dedent(R"( @@ -393,7 +393,7 @@ TEST_CASE("serialization") SECTION("given width") { - fancy_serializer_style style = fancy_serializer_style::preset_multiline; + print_style style = print_style::preset_multiline; auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style); CHECK(str == dedent(R"( [ @@ -410,7 +410,7 @@ TEST_CASE("serialization") SECTION("given fill") { - fancy_serializer_style style = fancy_serializer_style::preset_multiline; + print_style style = print_style::preset_multiline; style.indent_step = 1; style.indent_char = '\t'; @@ -431,7 +431,7 @@ TEST_CASE("serialization") SECTION("indent_char is honored for deep indents in lists") { - fancy_serializer_style style = fancy_serializer_style::preset_multiline; + print_style style = print_style::preset_multiline; style.indent_step = 300; style.indent_char = 'X'; @@ -449,7 +449,7 @@ TEST_CASE("serialization") SECTION("indent_char is honored for deep indents in objects") { - fancy_serializer_style style = fancy_serializer_style::preset_multiline; + print_style style = print_style::preset_multiline; style.indent_step = 300; style.indent_char = 'X';