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:
Niels Lohmann 2017-05-07 19:27:40 +02:00
parent fba1bcdd0b
commit 962da00171
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
10 changed files with 76 additions and 16 deletions

View file

@ -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);
};

View file

@ -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) ==

View file

@ -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]");
}
}
}