Refactor: don't indent when multiline is false

This commit is contained in:
Evan Driscoll 2018-06-02 23:52:30 -05:00
parent af8dd92a0c
commit ec756e47ad
3 changed files with 14 additions and 20 deletions

View File

@ -27,7 +27,7 @@ namespace nlohmann
struct fancy_serializer_style
{
unsigned int indent_step = 0;
unsigned int indent_step = 4;
char indent_char = ' ';
unsigned int depth_limit = std::numeric_limits<unsigned>::max();
@ -210,7 +210,7 @@ class fancy_serializer
Iterator i, bool ensure_ascii, unsigned int depth,
const fancy_serializer_style* active_style)
{
const auto new_indent = (depth + 1) * active_style->indent_step;
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;
const int newline_len = (active_style->indent_step > 0);
o->write_characters(indent_string.c_str(), new_indent);
@ -238,8 +238,8 @@ class fancy_serializer
}
// variable to hold indentation for recursive calls
const auto old_indent = depth * active_style->indent_step;
const auto new_indent = (depth + 1) * active_style->indent_step;
const auto old_indent = depth * active_style->indent_step * active_style->multiline;
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;
if (JSON_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
@ -283,8 +283,8 @@ class fancy_serializer
}
// variable to hold indentation for recursive calls
const auto old_indent = depth * active_style->indent_step;
const auto new_indent = (depth + 1) * active_style->indent_step;
const auto old_indent = depth * active_style->indent_step * active_style->multiline;;
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;;
if (JSON_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, active_style->indent_char);

View File

@ -10097,7 +10097,7 @@ namespace nlohmann
struct fancy_serializer_style
{
unsigned int indent_step = 0;
unsigned int indent_step = 4;
char indent_char = ' ';
unsigned int depth_limit = std::numeric_limits<unsigned>::max();
@ -10280,7 +10280,7 @@ class fancy_serializer
Iterator i, bool ensure_ascii, unsigned int depth,
const fancy_serializer_style* active_style)
{
const auto new_indent = (depth + 1) * active_style->indent_step;
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;
const int newline_len = (active_style->indent_step > 0);
o->write_characters(indent_string.c_str(), new_indent);
@ -10308,8 +10308,8 @@ class fancy_serializer
}
// variable to hold indentation for recursive calls
const auto old_indent = depth * active_style->indent_step;
const auto new_indent = (depth + 1) * active_style->indent_step;
const auto old_indent = depth * active_style->indent_step * active_style->multiline;
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;
if (JSON_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, active_style->indent_char);
@ -10353,8 +10353,8 @@ class fancy_serializer
}
// variable to hold indentation for recursive calls
const auto old_indent = depth * active_style->indent_step;
const auto new_indent = (depth + 1) * active_style->indent_step;
const auto old_indent = depth * active_style->indent_step * active_style->multiline;;
const auto new_indent = (depth + 1) * active_style->indent_step * active_style->multiline;;
if (JSON_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, active_style->indent_char);

View File

@ -201,7 +201,6 @@ TEST_CASE("serialization")
auto str_flat = fancy_to_string({1, {1}}, style);
CHECK(str_flat == "[1,[...]]");
style.indent_step = 4;
style.multiline = true;
auto str_lines = fancy_to_string({1, {1}}, style);
CHECK(str_lines == dedent(R"(
@ -219,7 +218,6 @@ TEST_CASE("serialization")
auto str_flat = fancy_to_string({1, {{"one", 1}}}, style);
CHECK(str_flat == "[1,{...}]");
style.indent_step = 4;
style.multiline = true;
auto str_lines = fancy_to_string({1, {{"one", 1}}}, style);
CHECK(str_lines == dedent(R"(
@ -235,9 +233,7 @@ TEST_CASE("serialization")
SECTION("can style objects of a key differently")
{
fancy_serializer_stylizer stylizer;
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").multiline = false;
auto str = fancy_to_string(
@ -264,8 +260,7 @@ TEST_CASE("serialization")
SECTION("changes propagate (unless overridden)")
{
fancy_serializer_stylizer stylizer;
stylizer.get_default_style().indent_step = 4;
stylizer.get_default_style().multiline = 4;
stylizer.get_default_style().multiline = true;
stylizer.get_or_insert_style("one line").indent_step = 0;
auto str = fancy_to_string(
@ -286,8 +281,7 @@ TEST_CASE("serialization")
SECTION("given width")
{
fancy_serializer_style style;
style.indent_step = 4;
style.multiline = 4;
style.multiline = true;
auto str = fancy_to_string({"foo", 1, 2, 3, false, {{"one", 1}}}, style);
CHECK(str == dedent(R"(
[