static void format_collection( JsonIn &jsin, JsonOut &jsout, int depth, std::function<void(JsonIn &, JsonOut &, int, bool )>write_func ) { if( depth > 1 ) { // We're backtracking by storing jsin and jsout state before formatting // and restoring it afterwards if necessary. int in_start_pos = jsin.tell(); bool ate_seperator = jsin.get_ate_separator(); int out_start_pos = jsout.tell(); bool need_separator = jsout.get_need_separator(); write_func( jsin, jsout, depth, false ); if( jsout.tell() - out_start_pos <= 120 ) { // Line is short enough, so we're done. return; } else { // Reset jsin and jsout to their initial state, // and we'll serialize while forcing wrapping. jsin.seek( in_start_pos ); jsin.set_ate_separator( ate_seperator ); jsout.seek( out_start_pos ); if( need_separator ) { jsout.set_need_separator(); } } } write_func( jsin, jsout, depth, true ); }
static void write_object( JsonIn &jsin, JsonOut &jsout, int depth, bool force_wrap ) { jsout.start_object( force_wrap ); jsin.start_object(); while( !jsin.end_object() ) { std::string name = jsin.get_member_name(); jsout.member( name ); bool override_wrap = false; if( name == "rows" || name == "blueprint" ) { // Introspect into the row, if it has more than one element, force it to wrap. int in_start_pos = jsin.tell(); bool ate_seperator = jsin.get_ate_separator(); { JsonArray arr = jsin.get_array(); if( arr.size() > 1 ) { override_wrap = true; } } jsin.seek( in_start_pos ); jsin.set_ate_separator( ate_seperator ); } format( jsin, jsout, depth, override_wrap ); } jsout.end_object(); }