Spaces after a colon (in {"k": v}) controllable separately from multiline

This commit is contained in:
Evan Driscoll 2018-06-03 00:00:12 -05:00
parent ec756e47ad
commit e7b02c10dd
3 changed files with 48 additions and 10 deletions

View File

@ -34,7 +34,14 @@ struct fancy_serializer_style
unsigned int strings_maximum_length = 0;
bool space_after_colon = false;
bool multiline = false;
void set_old_multiline()
{
space_after_colon = multiline = true;
}
};
template<typename BasicJsonType>
@ -211,7 +218,7 @@ class fancy_serializer
const fancy_serializer_style* active_style)
{
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;
const int newline_len = (active_style->indent_step > 0);
const int newline_len = active_style->space_after_colon;
o->write_characters(indent_string.c_str(), new_indent);
o->write_character('\"');

View File

@ -10104,7 +10104,14 @@ struct fancy_serializer_style
unsigned int strings_maximum_length = 0;
bool space_after_colon = false;
bool multiline = false;
void set_old_multiline()
{
space_after_colon = multiline = true;
}
};
template<typename BasicJsonType>
@ -10281,7 +10288,7 @@ class fancy_serializer
const fancy_serializer_style* active_style)
{
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;
const int newline_len = (active_style->indent_step > 0);
const int newline_len = active_style->space_after_colon;
o->write_characters(indent_string.c_str(), new_indent);
o->write_character('\"');

View File

@ -201,7 +201,7 @@ TEST_CASE("serialization")
auto str_flat = fancy_to_string({1, {1}}, style);
CHECK(str_flat == "[1,[...]]");
style.multiline = true;
style.set_old_multiline();
auto str_lines = fancy_to_string({1, {1}}, style);
CHECK(str_lines == dedent(R"(
[
@ -218,7 +218,7 @@ TEST_CASE("serialization")
auto str_flat = fancy_to_string({1, {{"one", 1}}}, style);
CHECK(str_flat == "[1,{...}]");
style.multiline = true;
style.set_old_multiline();
auto str_lines = fancy_to_string({1, {{"one", 1}}}, style);
CHECK(str_lines == dedent(R"(
[
@ -233,7 +233,7 @@ TEST_CASE("serialization")
SECTION("can style objects of a key differently")
{
fancy_serializer_stylizer stylizer;
stylizer.get_default_style().multiline = true;
stylizer.get_default_style().set_old_multiline();
stylizer.get_or_insert_style("one line").multiline = false;
auto str = fancy_to_string(
@ -260,7 +260,7 @@ TEST_CASE("serialization")
SECTION("changes propagate (unless overridden)")
{
fancy_serializer_stylizer stylizer;
stylizer.get_default_style().multiline = true;
stylizer.get_default_style().set_old_multiline();
stylizer.get_or_insert_style("one line").indent_step = 0;
auto str = fancy_to_string(
@ -278,10 +278,34 @@ TEST_CASE("serialization")
}
}
SECTION("Spaces after commas are controllable separately from multiline")
{
SECTION("colons")
{
fancy_serializer_style style;
style.space_after_colon = true;
auto str = fancy_to_string({{"one", 1}}, style);
CHECK(str == "{\"one\": 1}");
}
SECTION("multiline can have no space")
{
fancy_serializer_style style;
style.set_old_multiline();
style.space_after_colon = false;
auto str = fancy_to_string({{"one", 1}}, style);
CHECK(str == dedent(R"(
{
"one":1
})"));
}
}
SECTION("given width")
{
fancy_serializer_style style;
style.multiline = true;
style.set_old_multiline();
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
CHECK(str == dedent(R"(
[
@ -301,7 +325,7 @@ TEST_CASE("serialization")
fancy_serializer_style style;
style.indent_step = 1;
style.indent_char = '\t';
style.multiline = true;
style.set_old_multiline();
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
CHECK(str ==
@ -323,7 +347,7 @@ TEST_CASE("serialization")
fancy_serializer_style style;
style.indent_step = 300;
style.indent_char = 'X';
style.multiline = true;
style.set_old_multiline();
auto str = fancy_to_string({1, {1}}, style);
@ -342,7 +366,7 @@ TEST_CASE("serialization")
fancy_serializer_style style;
style.indent_step = 300;
style.indent_char = 'X';
style.multiline = true;
style.set_old_multiline();
auto str = fancy_to_string({{"key", {{"key", 1}}}}, style);