diff --git a/src/space/space.h b/src/space/space.h index 41fd446..3458cd2 100644 --- a/src/space/space.h +++ b/src/space/space.h @@ -561,6 +561,13 @@ public: template void serialize_to_json_stream(StreamType & str, bool pretty_print = false) const + { + serialize_to_json_stream(str, pretty_print, 0); + } + + // make me private + template + 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 @@ -1240,53 +1248,69 @@ protected: } template - 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 - 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 void serialize_to_space_stream(StreamType & str, bool pretty_print, int level, bool is_main_object) const