From c2451f30f1d8fc4804bb3191d1dbdf4324f9fb52 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Thu, 11 Jun 2015 19:22:22 -0400 Subject: [PATCH] Add a new mode to the variant->json generator to restore the normal behavior of writing numbers out as numbers instead of strings --- include/fc/io/json.hpp | 33 ++++++++++++++---------- src/io/json.cpp | 58 +++++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/include/fc/io/json.hpp b/include/fc/io/json.hpp index 4d97ac0..8a46d13 100644 --- a/include/fc/io/json.hpp +++ b/include/fc/io/json.hpp @@ -22,28 +22,33 @@ namespace fc relaxed_parser = 2, legacy_parser_with_string_doubles = 3 }; + enum output_formatting + { + stringify_large_ints_and_doubles = 0, + legacy_generator = 1 + }; - static ostream& to_stream( ostream& out, const fc::string& ); - static ostream& to_stream( ostream& out, const variant& v ); - static ostream& to_stream( ostream& out, const variants& v ); - static ostream& to_stream( ostream& out, const variant_object& v ); + static ostream& to_stream( ostream& out, const fc::string&); + static ostream& to_stream( ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles ); + static ostream& to_stream( ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles ); + static ostream& to_stream( ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles ); static variant from_stream( buffered_istream& in, parse_type ptype = legacy_parser ); static variant from_string( const string& utf8_str, parse_type ptype = legacy_parser ); static variants variants_from_string( const string& utf8_str, parse_type ptype = legacy_parser ); - static string to_string( const variant& v ); - static string to_pretty_string( const variant& v ); + static string to_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles ); + static string to_pretty_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles ); static bool is_valid( const std::string& json_str, parse_type ptype = legacy_parser ); template - static void save_to_file( const T& v, const fc::path& fi, bool pretty = true ) + static void save_to_file( const T& v, const fc::path& fi, bool pretty = true, output_formatting format = stringify_large_ints_and_doubles ) { - save_to_file( variant(v), fi, pretty ); + save_to_file( variant(v), fi, pretty, format ); } - static void save_to_file( const variant& v, const fc::path& fi, bool pretty = true ); + static void save_to_file( const variant& v, const fc::path& fi, bool pretty = true, output_formatting format = stringify_large_ints_and_doubles ); static variant from_file( const fc::path& p, parse_type ptype = legacy_parser ); template @@ -53,19 +58,19 @@ namespace fc } template - static string to_string( const T& v ) + static string to_string( const T& v, output_formatting format = stringify_large_ints_and_doubles ) { - return to_string( variant(v) ); + return to_string( variant(v), format ); } template - static string to_pretty_string( const T& v ) + static string to_pretty_string( const T& v, output_formatting format = stringify_large_ints_and_doubles ) { - return to_pretty_string( variant(v) ); + return to_pretty_string( variant(v), format ); } template - static void save_to_file( const T& v, const std::string& p, bool pretty = true ) + static void save_to_file( const T& v, const std::string& p, bool pretty = true, output_formatting format = stringify_large_ints_and_doubles ) { save_to_file( variant(v), fc::path(p), pretty ); } diff --git a/src/io/json.cpp b/src/io/json.cpp index 90fccc5..84446e1 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -25,9 +25,9 @@ namespace fc template variant number_from_stream( T& in ); template variant token_from_stream( T& in ); void escape_string( const string& str, ostream& os ); - template void to_stream( T& os, const variants& a ); - template void to_stream( T& os, const variant_object& o ); - template void to_stream( T& os, const variant& v ); + template void to_stream( T& os, const variants& a, json::output_formatting format ); + template void to_stream( T& os, const variant_object& o, json::output_formatting format ); + template void to_stream( T& os, const variant& v, json::output_formatting format ); fc::string pretty_print( const fc::string& v, uint8_t indent ); } @@ -549,14 +549,14 @@ namespace fc } template - void to_stream( T& os, const variants& a ) + void to_stream( T& os, const variants& a, json::output_formatting format ) { os << '['; auto itr = a.begin(); while( itr != a.end() ) { - to_stream( os, *itr ); + to_stream( os, *itr, format ); ++itr; if( itr != a.end() ) os << ','; @@ -564,7 +564,7 @@ namespace fc os << ']'; } template - void to_stream( T& os, const variant_object& o ) + void to_stream( T& os, const variant_object& o, json::output_formatting format ) { os << '{'; auto itr = o.begin(); @@ -573,7 +573,7 @@ namespace fc { escape_string( itr->key(), os ); os << ':'; - to_stream( os, itr->value() ); + to_stream( os, itr->value(), format ); ++itr; if( itr != o.end() ) os << ','; @@ -582,7 +582,7 @@ namespace fc } template - void to_stream( T& os, const variant& v ) + void to_stream( T& os, const variant& v, json::output_formatting format ) { switch( v.get_type() ) { @@ -592,24 +592,30 @@ namespace fc case variant::int64_type: { int64_t i = v.as_int64(); - if( i > 0xffffffff ) + if( format == json::stringify_large_ints_and_doubles && + i > 0xffffffff ) os << '"'< 0xffffffff ) + if( format == json::stringify_large_ints_and_doubles && + i > 0xffffffff ) os << '"'<