Skip to content

Commit

Permalink
✨ implemented an indentation character #520
Browse files Browse the repository at this point in the history
An optional parameter for dump() allows to set the character to use for
indentation (default: space). In case a JSON value is serialized to an
output stream, its fill character is used (and can be set with
std::setfill).
  • Loading branch information
nlohmann committed May 7, 2017
1 parent fba1bcd commit 962da00
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 16 deletions.
2 changes: 2 additions & 0 deletions doc/examples/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
2 changes: 1 addition & 1 deletion doc/examples/dump.link
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/jC8OLyTjE6EOcMZt"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/HjYnuCSOxokNvsRW"><b>online</b></a>
13 changes: 13 additions & 0 deletions doc/examples/dump.output
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"two": 2
}

{
"one": 1,
"two": 2
}

[1,2,4,8,16]

[1,2,4,8,16]
Expand All @@ -32,3 +37,11 @@
16
]

[
1,
2,
4,
8,
16
]

1 change: 1 addition & 0 deletions doc/examples/operator_serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
2 changes: 1 addition & 1 deletion doc/examples/operator_serialize.link
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/N3DbIOGGiGvGfSQ3"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/ibCUjqfsQiAglDVe"><b>online</b></a>
5 changes: 5 additions & 0 deletions doc/examples/operator_serialize.output
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@
16
]

{
"one": 1,
"two": 2
}

39 changes: 26 additions & 13 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<char>::create(result));
serializer s(output_adapter<char>::create(result), indent_char);

if (indent >= 0)
{
Expand Down Expand Up @@ -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<char> s)
serializer(output_adapter_t<char> 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)
{}

/*!
Expand Down Expand Up @@ -7293,20 +7297,29 @@ 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:
/*!
@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
Expand All @@ -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)
{
Expand All @@ -7330,7 +7343,7 @@ class basic_json
o.width(0);

// do the actual serialization
serializer s(output_adapter<char>::create(o));
serializer s(output_adapter<char>::create(o), o.fill());
s.dump(j, pretty_print, static_cast<unsigned int>(indentation));
return o;
}
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-convenience.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TEST_CASE("convenience functions")
const char* escaped)
{
std::stringstream ss;
json::serializer s(json::output_adapter<char>::create(ss));
json::serializer s(json::output_adapter<char>::create(ss), ' ');
s.dump_escaped(original);
CHECK(ss.str() == escaped);
};
Expand Down
6 changes: 6 additions & 0 deletions test/src/unit-inspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) ==
Expand Down
20 changes: 20 additions & 0 deletions test/src/unit-serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>>")
Expand All @@ -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]");
}
}
}

0 comments on commit 962da00

Please sign in to comment.