Refactor: dump_object no longer splits cases by indent

This commit is contained in:
Evan Driscoll 2018-06-02 21:08:53 -05:00
parent 9cdf54b886
commit a983e47619
2 changed files with 50 additions and 104 deletions

View File

@ -157,10 +157,6 @@ class fancy_serializer
return; return;
} }
if (style.indent_step > 0)
{
o->write_characters("{\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto old_indent = depth * style.indent_step; const auto old_indent = depth * style.indent_step;
const auto new_indent = (depth + 1) * style.indent_step; const auto new_indent = (depth + 1) * style.indent_step;
@ -168,6 +164,9 @@ class fancy_serializer
{ {
indent_string.resize(indent_string.size() * 2, ' '); indent_string.resize(indent_string.size() * 2, ' ');
} }
const int newline_len = (style.indent_step > 0);
o->write_characters("{\n", 1 + newline_len);
// first n-1 elements // first n-1 elements
auto i = val.m_value.object->cbegin(); auto i = val.m_value.object->cbegin();
@ -176,9 +175,9 @@ class fancy_serializer
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
o->write_character('\"'); o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii); prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\": ", 3); o->write_characters("\": ", 2 + newline_len);
dump(i->second, ensure_ascii, depth + 1); dump(i->second, ensure_ascii, depth + 1);
o->write_characters(",\n", 2); o->write_characters(",\n", 1 + newline_len);
} }
// last element // last element
@ -187,39 +186,13 @@ class fancy_serializer
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
o->write_character('\"'); o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii); prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\": ", 3); o->write_characters("\": ", 2 + newline_len);
dump(i->second, ensure_ascii, depth + 1); dump(i->second, ensure_ascii, depth + 1);
o->write_character('\n'); o->write_characters("\n", newline_len);
o->write_characters(indent_string.c_str(), old_indent); o->write_characters(indent_string.c_str(), old_indent);
o->write_character('}'); o->write_character('}');
} }
else
{
o->write_character('{');
// first n-1 elements
auto i = val.m_value.object->cbegin();
for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
{
o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, ensure_ascii, depth + 1);
o->write_character(',');
}
// last element
assert(i != val.m_value.object->cend());
assert(std::next(i) == val.m_value.object->cend());
o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, ensure_ascii, depth + 1);
o->write_character('}');
}
}
void dump_array(const BasicJsonType& val, bool ensure_ascii, unsigned int depth) void dump_array(const BasicJsonType& val, bool ensure_ascii, unsigned int depth)
{ {

View File

@ -10227,10 +10227,6 @@ class fancy_serializer
return; return;
} }
if (style.indent_step > 0)
{
o->write_characters("{\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto old_indent = depth * style.indent_step; const auto old_indent = depth * style.indent_step;
const auto new_indent = (depth + 1) * style.indent_step; const auto new_indent = (depth + 1) * style.indent_step;
@ -10238,6 +10234,9 @@ class fancy_serializer
{ {
indent_string.resize(indent_string.size() * 2, ' '); indent_string.resize(indent_string.size() * 2, ' ');
} }
const int newline_len = (style.indent_step > 0);
o->write_characters("{\n", 1 + newline_len);
// first n-1 elements // first n-1 elements
auto i = val.m_value.object->cbegin(); auto i = val.m_value.object->cbegin();
@ -10246,9 +10245,9 @@ class fancy_serializer
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
o->write_character('\"'); o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii); prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\": ", 3); o->write_characters("\": ", 2 + newline_len);
dump(i->second, ensure_ascii, depth + 1); dump(i->second, ensure_ascii, depth + 1);
o->write_characters(",\n", 2); o->write_characters(",\n", 1 + newline_len);
} }
// last element // last element
@ -10257,39 +10256,13 @@ class fancy_serializer
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
o->write_character('\"'); o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii); prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\": ", 3); o->write_characters("\": ", 2 + newline_len);
dump(i->second, ensure_ascii, depth + 1); dump(i->second, ensure_ascii, depth + 1);
o->write_character('\n'); o->write_characters("\n", newline_len);
o->write_characters(indent_string.c_str(), old_indent); o->write_characters(indent_string.c_str(), old_indent);
o->write_character('}'); o->write_character('}');
} }
else
{
o->write_character('{');
// first n-1 elements
auto i = val.m_value.object->cbegin();
for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
{
o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, ensure_ascii, depth + 1);
o->write_character(',');
}
// last element
assert(i != val.m_value.object->cend());
assert(std::next(i) == val.m_value.object->cend());
o->write_character('\"');
prim_serializer.dump_escaped(*o, i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, ensure_ascii, depth + 1);
o->write_character('}');
}
}
void dump_array(const BasicJsonType& val, bool ensure_ascii, unsigned int depth) void dump_array(const BasicJsonType& val, bool ensure_ascii, unsigned int depth)
{ {