Refactor: introduce explicit multiline control
Instead of depending on indent_step
This commit is contained in:
parent
5e10e97296
commit
af8dd92a0c
@ -33,6 +33,8 @@ struct fancy_serializer_style
|
|||||||
unsigned int depth_limit = std::numeric_limits<unsigned>::max();
|
unsigned int depth_limit = std::numeric_limits<unsigned>::max();
|
||||||
|
|
||||||
unsigned int strings_maximum_length = 0;
|
unsigned int strings_maximum_length = 0;
|
||||||
|
|
||||||
|
bool multiline = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
@ -242,7 +244,7 @@ class fancy_serializer
|
|||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
||||||
}
|
}
|
||||||
const int newline_len = (active_style->indent_step > 0);
|
const int newline_len = (active_style->multiline ? 1 : 0);
|
||||||
|
|
||||||
o->write_characters("{\n", 1 + newline_len);
|
o->write_characters("{\n", 1 + newline_len);
|
||||||
|
|
||||||
@ -287,7 +289,7 @@ class fancy_serializer
|
|||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
||||||
}
|
}
|
||||||
const int newline_len = (active_style->indent_step > 0);
|
const int newline_len = (active_style->multiline ? 1 : 0);
|
||||||
|
|
||||||
o->write_characters("[\n", 1 + newline_len);
|
o->write_characters("[\n", 1 + newline_len);
|
||||||
|
|
||||||
|
|||||||
@ -10103,6 +10103,8 @@ struct fancy_serializer_style
|
|||||||
unsigned int depth_limit = std::numeric_limits<unsigned>::max();
|
unsigned int depth_limit = std::numeric_limits<unsigned>::max();
|
||||||
|
|
||||||
unsigned int strings_maximum_length = 0;
|
unsigned int strings_maximum_length = 0;
|
||||||
|
|
||||||
|
bool multiline = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
@ -10312,7 +10314,7 @@ class fancy_serializer
|
|||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
||||||
}
|
}
|
||||||
const int newline_len = (active_style->indent_step > 0);
|
const int newline_len = (active_style->multiline ? 1 : 0);
|
||||||
|
|
||||||
o->write_characters("{\n", 1 + newline_len);
|
o->write_characters("{\n", 1 + newline_len);
|
||||||
|
|
||||||
@ -10357,7 +10359,7 @@ class fancy_serializer
|
|||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
|
||||||
}
|
}
|
||||||
const int newline_len = (active_style->indent_step > 0);
|
const int newline_len = (active_style->multiline ? 1 : 0);
|
||||||
|
|
||||||
o->write_characters("[\n", 1 + newline_len);
|
o->write_characters("[\n", 1 + newline_len);
|
||||||
|
|
||||||
|
|||||||
@ -202,6 +202,7 @@ TEST_CASE("serialization")
|
|||||||
CHECK(str_flat == "[1,[...]]");
|
CHECK(str_flat == "[1,[...]]");
|
||||||
|
|
||||||
style.indent_step = 4;
|
style.indent_step = 4;
|
||||||
|
style.multiline = true;
|
||||||
auto str_lines = fancy_to_string({1, {1}}, style);
|
auto str_lines = fancy_to_string({1, {1}}, style);
|
||||||
CHECK(str_lines == dedent(R"(
|
CHECK(str_lines == dedent(R"(
|
||||||
[
|
[
|
||||||
@ -219,6 +220,7 @@ TEST_CASE("serialization")
|
|||||||
CHECK(str_flat == "[1,{...}]");
|
CHECK(str_flat == "[1,{...}]");
|
||||||
|
|
||||||
style.indent_step = 4;
|
style.indent_step = 4;
|
||||||
|
style.multiline = true;
|
||||||
auto str_lines = fancy_to_string({1, {{"one", 1}}}, style);
|
auto str_lines = fancy_to_string({1, {{"one", 1}}}, style);
|
||||||
CHECK(str_lines == dedent(R"(
|
CHECK(str_lines == dedent(R"(
|
||||||
[
|
[
|
||||||
@ -234,7 +236,9 @@ TEST_CASE("serialization")
|
|||||||
{
|
{
|
||||||
fancy_serializer_stylizer stylizer;
|
fancy_serializer_stylizer stylizer;
|
||||||
stylizer.get_default_style().indent_step = 4;
|
stylizer.get_default_style().indent_step = 4;
|
||||||
|
stylizer.get_default_style().multiline = true;
|
||||||
stylizer.get_or_insert_style("one line").indent_step = 0;
|
stylizer.get_or_insert_style("one line").indent_step = 0;
|
||||||
|
stylizer.get_or_insert_style("one line").multiline = false;
|
||||||
|
|
||||||
auto str = fancy_to_string(
|
auto str = fancy_to_string(
|
||||||
{
|
{
|
||||||
@ -261,6 +265,7 @@ TEST_CASE("serialization")
|
|||||||
{
|
{
|
||||||
fancy_serializer_stylizer stylizer;
|
fancy_serializer_stylizer stylizer;
|
||||||
stylizer.get_default_style().indent_step = 4;
|
stylizer.get_default_style().indent_step = 4;
|
||||||
|
stylizer.get_default_style().multiline = 4;
|
||||||
stylizer.get_or_insert_style("one line").indent_step = 0;
|
stylizer.get_or_insert_style("one line").indent_step = 0;
|
||||||
|
|
||||||
auto str = fancy_to_string(
|
auto str = fancy_to_string(
|
||||||
@ -282,6 +287,7 @@ TEST_CASE("serialization")
|
|||||||
{
|
{
|
||||||
fancy_serializer_style style;
|
fancy_serializer_style style;
|
||||||
style.indent_step = 4;
|
style.indent_step = 4;
|
||||||
|
style.multiline = 4;
|
||||||
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
|
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
|
||||||
CHECK(str == dedent(R"(
|
CHECK(str == dedent(R"(
|
||||||
[
|
[
|
||||||
@ -301,6 +307,7 @@ TEST_CASE("serialization")
|
|||||||
fancy_serializer_style style;
|
fancy_serializer_style style;
|
||||||
style.indent_step = 1;
|
style.indent_step = 1;
|
||||||
style.indent_char = '\t';
|
style.indent_char = '\t';
|
||||||
|
style.multiline = true;
|
||||||
|
|
||||||
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
|
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
|
||||||
CHECK(str ==
|
CHECK(str ==
|
||||||
@ -322,6 +329,7 @@ TEST_CASE("serialization")
|
|||||||
fancy_serializer_style style;
|
fancy_serializer_style style;
|
||||||
style.indent_step = 300;
|
style.indent_step = 300;
|
||||||
style.indent_char = 'X';
|
style.indent_char = 'X';
|
||||||
|
style.multiline = true;
|
||||||
|
|
||||||
auto str = fancy_to_string({1, {1}}, style);
|
auto str = fancy_to_string({1, {1}}, style);
|
||||||
|
|
||||||
@ -340,6 +348,7 @@ TEST_CASE("serialization")
|
|||||||
fancy_serializer_style style;
|
fancy_serializer_style style;
|
||||||
style.indent_step = 300;
|
style.indent_step = 300;
|
||||||
style.indent_char = 'X';
|
style.indent_char = 'X';
|
||||||
|
style.multiline = true;
|
||||||
|
|
||||||
auto str = fancy_to_string({{"key", {{"key", 1}}}}, style);
|
auto str = fancy_to_string({{"key", {{"key", 1}}}}, style);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user