added some indentation when printing 'pretty Space format'

This commit is contained in:
Tomasz Sowa 2021-03-18 17:19:36 +01:00
parent 4119461f8e
commit 679b9e6173
1 changed files with 71 additions and 44 deletions

View File

@ -336,49 +336,16 @@ public:
void serialize_to_space_to(std::wstring & str, bool pretty_print = false) const; void serialize_to_space_to(std::wstring & str, bool pretty_print = false) const;
template<typename StreamType> template<typename StreamType>
void serialize_to_space_stream(StreamType & str, bool pretty_print = false, bool is_main_object = true) const void serialize_to_space_stream(StreamType & str, bool pretty_print = false) const
{ {
switch(type) if( is_object() )
{ {
case type_null: serialize_to_space_stream(str, pretty_print, -1, true);
serialize_space_null(str);
break;
case type_bool:
serialize_space_bool(str);
break;
case type_long:
serialize_space_long(str);
break;
case type_float:
serialize_space_float(str);
break;
case type_double:
serialize_space_double(str);
break;
case type_string:
serialize_space_string(str);
break;
case type_wstring:
serialize_space_wstring(str);
break;
case type_object:
serialize_space_object(str, pretty_print, is_main_object);
break;
case type_table:
serialize_space_table(str, pretty_print);
break;
} }
} }
std::string serialize_to_json_str() const; std::string serialize_to_json_str() const;
std::wstring serialize_to_json_wstr() const; std::wstring serialize_to_json_wstr() const;
void serialize_to_json_to(std::string & str) const; void serialize_to_json_to(std::string & str) const;
@ -669,7 +636,7 @@ protected:
} }
template<typename StreamType> template<typename StreamType>
void serialize_space_object(StreamType & str, bool pretty_print, bool is_main_object) const void serialize_space_object(StreamType & str, bool pretty_print, int level, bool is_main_object) const
{ {
if( !is_main_object ) if( !is_main_object )
{ {
@ -686,6 +653,7 @@ protected:
bool quote_field = should_field_be_quoted(map_item.first); bool quote_field = should_field_be_quoted(map_item.first);
print_level(pretty_print, level, str);
print_if(quote_field, str, '"'); print_if(quote_field, str, '"');
serialize_string_buffer(map_item.first.c_str(), str, Escape::escape_space); serialize_string_buffer(map_item.first.c_str(), str, Escape::escape_space);
print_if(quote_field, str, '"'); print_if(quote_field, str, '"');
@ -694,15 +662,16 @@ protected:
str << '='; str << '=';
print_if(pretty_print, str, ' '); print_if(pretty_print, str, ' ');
map_item.second->serialize_to_space_stream(str, pretty_print, false); map_item.second->serialize_to_space_stream(str, pretty_print, level + 1, false);
is_first = false; is_first = false;
} }
print_if(!is_first && pretty_print, str, '\n'); print_if(!is_first && pretty_print, str, '\n');
serialize_child_spaces(str, pretty_print); serialize_child_spaces(str, pretty_print, level);
if( !is_main_object ) if( !is_main_object )
{ {
print_level(pretty_print, level - 2, str);
str << '}'; str << '}';
print_if(pretty_print, str, '\n'); print_if(pretty_print, str, '\n');
} }
@ -710,7 +679,7 @@ protected:
template<typename StreamType> template<typename StreamType>
void serialize_child_spaces(StreamType & str, bool pretty_print) const void serialize_child_spaces(StreamType & str, bool pretty_print, int level) const
{ {
if( child_spaces && !child_spaces->empty() ) if( child_spaces && !child_spaces->empty() )
{ {
@ -724,6 +693,7 @@ protected:
{ {
bool quote_field = should_field_be_quoted(*child_space->name); bool quote_field = should_field_be_quoted(*child_space->name);
print_level(pretty_print, level, str);
print_if(quote_field, str, '"'); print_if(quote_field, str, '"');
serialize_string_buffer(child_space->name->c_str(), str, Escape::escape_space); serialize_string_buffer(child_space->name->c_str(), str, Escape::escape_space);
print_if(quote_field, str, '"'); print_if(quote_field, str, '"');
@ -731,7 +701,7 @@ protected:
str << ' '; str << ' ';
} }
child_space->serialize_to_space_stream(str, pretty_print, false); child_space->serialize_to_space_stream(str, pretty_print, level + 1, false);
print_if(pretty_print, str, '\n'); print_if(pretty_print, str, '\n');
} }
} }
@ -739,7 +709,7 @@ protected:
template<typename StreamType> template<typename StreamType>
void serialize_space_table(StreamType & str, bool pretty_print) const void serialize_space_table(StreamType & str, bool pretty_print, int level) const
{ {
bool multivalue_table = false; bool multivalue_table = false;
bool is_first = true; bool is_first = true;
@ -757,11 +727,13 @@ protected:
if( !is_first ) if( !is_first )
print_if(pretty_print, str, '\n', ','); print_if(pretty_print, str, '\n', ',');
space->serialize_to_space_stream(str, pretty_print, false); print_level(pretty_print && multivalue_table, level, str);
space->serialize_to_space_stream(str, pretty_print, level + 1, false);
is_first = false; is_first = false;
} }
print_if(pretty_print && multivalue_table, str, '\n'); print_if(pretty_print && multivalue_table, str, '\n');
print_level(pretty_print && multivalue_table, level - 2, str);
str << ')'; str << ')';
} }
@ -924,6 +896,50 @@ public:
protected: protected:
template<typename StreamType>
void serialize_to_space_stream(StreamType & str, bool pretty_print, int level, bool is_main_object) const
{
switch(type)
{
case type_null:
serialize_space_null(str);
break;
case type_bool:
serialize_space_bool(str);
break;
case type_long:
serialize_space_long(str);
break;
case type_float:
serialize_space_float(str);
break;
case type_double:
serialize_space_double(str);
break;
case type_string:
serialize_space_string(str);
break;
case type_wstring:
serialize_space_wstring(str);
break;
case type_object:
serialize_space_object(str, pretty_print, level + 1, is_main_object);
break;
case type_table:
serialize_space_table(str, pretty_print, level + 1);
break;
}
}
template<typename StringType> template<typename StringType>
bool should_field_be_quoted(StringType & str) const bool should_field_be_quoted(StringType & str) const
{ {
@ -944,6 +960,17 @@ protected:
} }
template<typename StreamType>
void print_level(bool pretty_print, int level, StreamType & str) const
{
if( pretty_print )
{
for(int i=0 ; i < level ; ++i)
{
str << ' ';
}
}
}
template<typename StreamType> template<typename StreamType>