✨ implemented an indentation character #520
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).
This commit is contained in:
parent
fba1bcdd0b
commit
962da00171
10 changed files with 76 additions and 16 deletions
|
@ -13,8 +13,10 @@ int main()
|
||||||
std::cout << j_object.dump(-1) << "\n\n";
|
std::cout << j_object.dump(-1) << "\n\n";
|
||||||
std::cout << j_object.dump(0) << "\n\n";
|
std::cout << j_object.dump(0) << "\n\n";
|
||||||
std::cout << j_object.dump(4) << "\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() << "\n\n";
|
||||||
std::cout << j_array.dump(-1) << "\n\n";
|
std::cout << j_array.dump(-1) << "\n\n";
|
||||||
std::cout << j_array.dump(0) << "\n\n";
|
std::cout << j_array.dump(0) << "\n\n";
|
||||||
std::cout << j_array.dump(4) << "\n\n";
|
std::cout << j_array.dump(4) << "\n\n";
|
||||||
|
std::cout << j_array.dump(1, '\t') << "\n\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
|
@ -12,6 +12,11 @@
|
||||||
"two": 2
|
"two": 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
"one": 1,
|
||||||
|
"two": 2
|
||||||
|
}
|
||||||
|
|
||||||
[1,2,4,8,16]
|
[1,2,4,8,16]
|
||||||
|
|
||||||
[1,2,4,8,16]
|
[1,2,4,8,16]
|
||||||
|
@ -32,3 +37,11 @@
|
||||||
16
|
16
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
4,
|
||||||
|
8,
|
||||||
|
16
|
||||||
|
]
|
||||||
|
|
||||||
|
|
|
@ -16,4 +16,5 @@ int main()
|
||||||
// serialize with indentation
|
// serialize with indentation
|
||||||
std::cout << std::setw(4) << j_object << "\n\n";
|
std::cout << std::setw(4) << j_object << "\n\n";
|
||||||
std::cout << std::setw(2) << j_array << "\n\n";
|
std::cout << std::setw(2) << j_array << "\n\n";
|
||||||
|
std::cout << std::setw(1) << std::setfill('\t') << j_object << "\n\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
|
@ -15,3 +15,8 @@
|
||||||
16
|
16
|
||||||
]
|
]
|
||||||
|
|
||||||
|
{
|
||||||
|
"one": 1,
|
||||||
|
"two": 2
|
||||||
|
}
|
||||||
|
|
||||||
|
|
39
src/json.hpp
39
src/json.hpp
|
@ -2883,6 +2883,8 @@ class basic_json
|
||||||
members will be pretty-printed with that indent level. An indent level of
|
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
|
`0` will only insert newlines. `-1` (the default) selects the most compact
|
||||||
representation.
|
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
|
@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
|
@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;
|
string_t result;
|
||||||
serializer s(output_adapter<char>::create(result));
|
serializer s(output_adapter<char>::create(result), indent_char);
|
||||||
|
|
||||||
if (indent >= 0)
|
if (indent >= 0)
|
||||||
{
|
{
|
||||||
|
@ -6724,11 +6726,13 @@ class basic_json
|
||||||
public:
|
public:
|
||||||
/*!
|
/*!
|
||||||
@param[in] s output stream to serialize to
|
@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()),
|
: o(s), loc(std::localeconv()),
|
||||||
thousands_sep(!loc->thousands_sep ? '\0' : loc->thousands_sep[0]),
|
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
|
/// the locale's decimal point character
|
||||||
const char decimal_point = '\0';
|
const char decimal_point = '\0';
|
||||||
|
|
||||||
|
/// the indentation character
|
||||||
|
const char indent_char;
|
||||||
|
|
||||||
/// the indentation string
|
/// the indentation string
|
||||||
string_t indent_string = string_t(512, ' ');
|
string_t indent_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -7302,11 +7309,17 @@ class basic_json
|
||||||
@brief serialize to stream
|
@brief serialize to stream
|
||||||
|
|
||||||
Serialize the given JSON value @a j to the output stream @a o. The JSON
|
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
|
value will be serialized using the @ref dump member function.
|
||||||
indentation of the output can be controlled with the member variable
|
|
||||||
`width` of the output stream @a o. For instance, using the manipulator
|
- The indentation of the output can be controlled with the member variable
|
||||||
`std::setw(4)` on @a o sets the indentation level to `4` and the
|
`width` of the output stream @a o. For instance, using the manipulator
|
||||||
serialization result is the same as calling `dump(4)`.
|
`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,out] o stream to serialize to
|
||||||
@param[in] j JSON value to serialize
|
@param[in] j JSON value to serialize
|
||||||
|
@ -7318,7 +7331,7 @@ class basic_json
|
||||||
@liveexample{The example below shows the serialization with different
|
@liveexample{The example below shows the serialization with different
|
||||||
parameters to `width` to adjust the indentation level.,operator_serialize}
|
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)
|
friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
|
||||||
{
|
{
|
||||||
|
@ -7330,7 +7343,7 @@ class basic_json
|
||||||
o.width(0);
|
o.width(0);
|
||||||
|
|
||||||
// do the actual serialization
|
// 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));
|
s.dump(j, pretty_print, static_cast<unsigned int>(indentation));
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ TEST_CASE("convenience functions")
|
||||||
const char* escaped)
|
const char* escaped)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
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);
|
s.dump_escaped(original);
|
||||||
CHECK(ss.str() == escaped);
|
CHECK(ss.str() == escaped);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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}");
|
"{\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")
|
SECTION("indent=4")
|
||||||
{
|
{
|
||||||
CHECK(j.dump(4) ==
|
CHECK(j.dump(4) ==
|
||||||
|
|
|
@ -51,6 +51,15 @@ TEST_CASE("serialization")
|
||||||
CHECK(ss.str() ==
|
CHECK(ss.str() ==
|
||||||
"[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
|
"[\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>>")
|
SECTION("operator>>")
|
||||||
|
@ -72,5 +81,16 @@ TEST_CASE("serialization")
|
||||||
CHECK(ss.str() ==
|
CHECK(ss.str() ==
|
||||||
"[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
|
"[\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]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue