diff --git a/space/space.h b/space/space.h index 8647527..5af887e 100644 --- a/space/space.h +++ b/space/space.h @@ -336,49 +336,16 @@ public: void serialize_to_space_to(std::wstring & str, bool pretty_print = false) const; template - 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_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; + serialize_to_space_stream(str, pretty_print, -1, true); } } + std::string serialize_to_json_str() const; std::wstring serialize_to_json_wstr() const; void serialize_to_json_to(std::string & str) const; @@ -669,7 +636,7 @@ protected: } template - 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 ) { @@ -686,6 +653,7 @@ protected: bool quote_field = should_field_be_quoted(map_item.first); + print_level(pretty_print, level, str); print_if(quote_field, str, '"'); serialize_string_buffer(map_item.first.c_str(), str, Escape::escape_space); print_if(quote_field, str, '"'); @@ -694,15 +662,16 @@ protected: 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; } 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 ) { + print_level(pretty_print, level - 2, str); str << '}'; print_if(pretty_print, str, '\n'); } @@ -710,7 +679,7 @@ protected: template - 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() ) { @@ -724,6 +693,7 @@ protected: { bool quote_field = should_field_be_quoted(*child_space->name); + print_level(pretty_print, level, str); print_if(quote_field, str, '"'); serialize_string_buffer(child_space->name->c_str(), str, Escape::escape_space); print_if(quote_field, str, '"'); @@ -731,7 +701,7 @@ protected: 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'); } } @@ -739,7 +709,7 @@ protected: template - 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 is_first = true; @@ -757,11 +727,13 @@ protected: if( !is_first ) 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; } print_if(pretty_print && multivalue_table, str, '\n'); + print_level(pretty_print && multivalue_table, level - 2, str); str << ')'; } @@ -924,6 +896,50 @@ public: protected: + template + 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 bool should_field_be_quoted(StringType & str) const { @@ -944,6 +960,17 @@ protected: } + template + void print_level(bool pretty_print, int level, StreamType & str) const + { + if( pretty_print ) + { + for(int i=0 ; i < level ; ++i) + { + str << ' '; + } + } + } template