diff --git a/doc/examples/dump.cpp b/doc/examples/dump.cpp index 8190ae49..d1359684 100644 --- a/doc/examples/dump.cpp +++ b/doc/examples/dump.cpp @@ -13,8 +13,10 @@ int main() std::cout << j_object.dump(-1) << "\n\n"; std::cout << j_object.dump(0) << "\n\n"; std::cout << j_object.dump(4) << "\n\n"; + std::cout << j_object.dump(1, '\t') << "\n\n"; std::cout << j_array.dump() << "\n\n"; std::cout << j_array.dump(-1) << "\n\n"; std::cout << j_array.dump(0) << "\n\n"; std::cout << j_array.dump(4) << "\n\n"; + std::cout << j_array.dump(1, '\t') << "\n\n"; } diff --git a/doc/examples/dump.link b/doc/examples/dump.link index 3ced4448..ae5a4aba 100644 --- a/doc/examples/dump.link +++ b/doc/examples/dump.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/dump.output b/doc/examples/dump.output index 9b462eab..bc38c5ea 100644 --- a/doc/examples/dump.output +++ b/doc/examples/dump.output @@ -12,6 +12,11 @@ "two": 2 } +{ + "one": 1, + "two": 2 +} + [1,2,4,8,16] [1,2,4,8,16] @@ -32,3 +37,11 @@ 16 ] +[ + 1, + 2, + 4, + 8, + 16 +] + diff --git a/doc/examples/operator_serialize.cpp b/doc/examples/operator_serialize.cpp index 889bc108..1d817816 100644 --- a/doc/examples/operator_serialize.cpp +++ b/doc/examples/operator_serialize.cpp @@ -16,4 +16,5 @@ int main() // serialize with indentation std::cout << std::setw(4) << j_object << "\n\n"; std::cout << std::setw(2) << j_array << "\n\n"; + std::cout << std::setw(1) << std::setfill('\t') << j_object << "\n\n"; } diff --git a/doc/examples/operator_serialize.link b/doc/examples/operator_serialize.link index a77519c2..104ce0fd 100644 --- a/doc/examples/operator_serialize.link +++ b/doc/examples/operator_serialize.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/operator_serialize.output b/doc/examples/operator_serialize.output index 712a58da..7e86bfa2 100644 --- a/doc/examples/operator_serialize.output +++ b/doc/examples/operator_serialize.output @@ -15,3 +15,8 @@ 16 ] +{ + "one": 1, + "two": 2 +} + diff --git a/src/json.hpp b/src/json.hpp index e16f2e99..14e687d9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -2883,6 +2883,8 @@ class basic_json members will be pretty-printed with that indent level. An indent level of `0` will only insert newlines. `-1` (the default) selects the most compact representation. + @param[in] indent_char The character to use for indentation of @a indent is + greate than `0`. The default is ` ` (space). @return string containing the serialization of the JSON value @@ -2893,12 +2895,12 @@ class basic_json @see https://docs.python.org/2/library/json.html#json.dump - @since version 1.0.0 + @since version 1.0.0; indentaction character added in version 3.0.0 */ - string_t dump(const int indent = -1) const + string_t dump(const int indent = -1, const char indent_char = ' ') const { string_t result; - serializer s(output_adapter::create(result)); + serializer s(output_adapter::create(result), indent_char); if (indent >= 0) { @@ -6724,11 +6726,13 @@ class basic_json public: /*! @param[in] s output stream to serialize to + @param[in] ichar indentation character to use */ - serializer(output_adapter_t s) + serializer(output_adapter_t s, const char ichar) : o(s), loc(std::localeconv()), thousands_sep(!loc->thousands_sep ? '\0' : loc->thousands_sep[0]), - decimal_point(!loc->decimal_point ? '\0' : loc->decimal_point[0]) + decimal_point(!loc->decimal_point ? '\0' : loc->decimal_point[0]), + indent_char(ichar), indent_string(512, indent_char) {} /*! @@ -7293,8 +7297,11 @@ class basic_json /// the locale's decimal point character const char decimal_point = '\0'; + /// the indentation character + const char indent_char; + /// the indentation string - string_t indent_string = string_t(512, ' '); + string_t indent_string; }; public: @@ -7302,11 +7309,17 @@ class basic_json @brief serialize to stream Serialize the given JSON value @a j to the output stream @a o. The JSON - value will be serialized using the @ref dump member function. The - indentation of the output can be controlled with the member variable - `width` of the output stream @a o. For instance, using the manipulator - `std::setw(4)` on @a o sets the indentation level to `4` and the - serialization result is the same as calling `dump(4)`. + value will be serialized using the @ref dump member function. + + - The indentation of the output can be controlled with the member variable + `width` of the output stream @a o. For instance, using the manipulator + `std::setw(4)` on @a o sets the indentation level to `4` and the + serialization result is the same as calling `dump(4)`. + + - The indentation characrer can be controlled with the member variable + `fill` of the output stream @a o. For instance, the manipulator + `std::setfill('\\t')` sets indentation to use a tab character rather than + the default space character. @param[in,out] o stream to serialize to @param[in] j JSON value to serialize @@ -7318,7 +7331,7 @@ class basic_json @liveexample{The example below shows the serialization with different parameters to `width` to adjust the indentation level.,operator_serialize} - @since version 1.0.0 + @since version 1.0.0; indentaction character added in version 3.0.0 */ friend std::ostream& operator<<(std::ostream& o, const basic_json& j) { @@ -7330,7 +7343,7 @@ class basic_json o.width(0); // do the actual serialization - serializer s(output_adapter::create(o)); + serializer s(output_adapter::create(o), o.fill()); s.dump(j, pretty_print, static_cast(indentation)); return o; } diff --git a/test/src/unit-convenience.cpp b/test/src/unit-convenience.cpp index 5e16962d..f757541d 100644 --- a/test/src/unit-convenience.cpp +++ b/test/src/unit-convenience.cpp @@ -53,7 +53,7 @@ TEST_CASE("convenience functions") const char* escaped) { std::stringstream ss; - json::serializer s(json::output_adapter::create(ss)); + json::serializer s(json::output_adapter::create(ss), ' '); s.dump_escaped(original); CHECK(ss.str() == escaped); }; diff --git a/test/src/unit-inspection.cpp b/test/src/unit-inspection.cpp index 4900e425..8b294bb8 100644 --- a/test/src/unit-inspection.cpp +++ b/test/src/unit-inspection.cpp @@ -207,6 +207,12 @@ TEST_CASE("object inspection") "{\n\"array\": [\n1,\n2,\n3,\n4\n],\n\"boolean\": false,\n\"null\": null,\n\"number\": 42,\n\"object\": {},\n\"string\": \"Hello world\"\n}"); } + SECTION("indent=1, space='\t'") + { + CHECK(j.dump(1, '\t') == + "{\n\t\"array\": [\n\t\t1,\n\t\t2,\n\t\t3,\n\t\t4\n\t],\n\t\"boolean\": false,\n\t\"null\": null,\n\t\"number\": 42,\n\t\"object\": {},\n\t\"string\": \"Hello world\"\n}"); + } + SECTION("indent=4") { CHECK(j.dump(4) == diff --git a/test/src/unit-serialization.cpp b/test/src/unit-serialization.cpp index 3df56cdc..fb500ed9 100644 --- a/test/src/unit-serialization.cpp +++ b/test/src/unit-serialization.cpp @@ -51,6 +51,15 @@ TEST_CASE("serialization") CHECK(ss.str() == "[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]"); } + + SECTION("given fill") + { + std::stringstream ss; + json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + ss << std::setw(1) << std::setfill('\t') << j; + CHECK(ss.str() == + "[\n\t\"foo\",\n\t1,\n\t2,\n\t3,\n\tfalse,\n\t{\n\t\t\"one\": 1\n\t}\n]"); + } } SECTION("operator>>") @@ -72,5 +81,16 @@ TEST_CASE("serialization") CHECK(ss.str() == "[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]"); } + + SECTION("given fill") + { + std::stringstream ss; + json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + ss.width(1); + ss.fill('\t'); + j >> ss; + CHECK(ss.str() == + "[\n\t\"foo\",\n\t1,\n\t2,\n\t3,\n\tfalse,\n\t{\n\t\t\"one\": 1\n\t}\n]"); + } } }