implemented pretty printing in Space::serialize_to_json_stream(StreamType & str, bool pretty_print, int level)

This commit is contained in:
Tomasz Sowa 2021-06-23 21:54:34 +02:00
parent 3c0b59e115
commit 2b6789754f
1 changed files with 35 additions and 11 deletions

View File

@ -561,6 +561,13 @@ public:
template<typename StreamType>
void serialize_to_json_stream(StreamType & str, bool pretty_print = false) const
{
serialize_to_json_stream(str, pretty_print, 0);
}
// make me private
template<typename StreamType>
void serialize_to_json_stream(StreamType & str, bool pretty_print, int level) const
{
switch(type)
{
@ -597,11 +604,11 @@ public:
break;
case type_object:
serialize_json_object(str);
serialize_json_object(str, pretty_print, level + 1);
break;
case type_table:
serialize_json_table(str);
serialize_json_table(str, pretty_print, level + 1);
break;
}
}
@ -1149,6 +1156,7 @@ protected:
protected:
template<typename StreamType>
@ -1240,53 +1248,69 @@ protected:
}
template<typename StreamType>
void serialize_json_object(StreamType & str) const
void serialize_json_object(StreamType & str, bool pretty_print, int level) const
{
str << '{';
bool is_first = true;
for(auto & map_item : value.value_object)
{
if( !is_first )
if( is_first )
{
print_if(pretty_print, str, '\n');
print_level(pretty_print, level, str);
}
else
{
str << ',';
print_if(pretty_print, str, '\n');
print_level(pretty_print, level, str);
}
str << '"';
serialize_string_buffer(map_item.first.c_str(), str, Escape::escape_json);
str << '"';
str << ':';
map_item.second->serialize_to_json_stream(str);
map_item.second->serialize_to_json_stream(str, pretty_print, level);
is_first = false;
}
print_if(pretty_print && !is_first, str, '\n');
print_level(pretty_print && !is_first, level - 1, str);
str << '}';
}
template<typename StreamType>
void serialize_json_table(StreamType & str) const
void serialize_json_table(StreamType & str, bool pretty_print, int level) const
{
str << '[';
bool is_first = true;
for(Space * space : value.value_table)
{
if( !is_first )
if( is_first )
{
print_if(pretty_print, str, '\n');
print_level(pretty_print, level, str);
}
else
{
str << ',';
print_if(pretty_print, str, '\n');
print_level(pretty_print, level, str);
}
space->serialize_to_json_stream(str);
space->serialize_to_json_stream(str, pretty_print, level);
is_first = false;
}
print_if(pretty_print && !is_first, str, '\n');
print_level(pretty_print && !is_first, level - 1, str);
str << ']';
}
protected:
template<typename StreamType>
void serialize_to_space_stream(StreamType & str, bool pretty_print, int level, bool is_main_object) const