Add a new mode to the variant->json generator to restore the normal
behavior of writing numbers out as numbers instead of strings
This commit is contained in:
parent
e5a5323642
commit
c2451f30f1
2 changed files with 51 additions and 40 deletions
|
|
@ -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<typename T>
|
||||
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<typename T>
|
||||
|
|
@ -53,19 +58,19 @@ namespace fc
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
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<typename T>
|
||||
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<typename T>
|
||||
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 );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ namespace fc
|
|||
template<typename T, json::parse_type parser_type> variant number_from_stream( T& in );
|
||||
template<typename T> variant token_from_stream( T& in );
|
||||
void escape_string( const string& str, ostream& os );
|
||||
template<typename T> void to_stream( T& os, const variants& a );
|
||||
template<typename T> void to_stream( T& os, const variant_object& o );
|
||||
template<typename T> void to_stream( T& os, const variant& v );
|
||||
template<typename T> void to_stream( T& os, const variants& a, json::output_formatting format );
|
||||
template<typename T> void to_stream( T& os, const variant_object& o, json::output_formatting format );
|
||||
template<typename T> 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<typename T>
|
||||
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<typename T>
|
||||
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<typename T>
|
||||
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 << '"'<<v.as_string()<<'"';
|
||||
else
|
||||
os << i;
|
||||
|
||||
return;
|
||||
}
|
||||
case variant::uint64_type:
|
||||
{
|
||||
uint64_t i = v.as_uint64();
|
||||
if( i > 0xffffffff )
|
||||
if( format == json::stringify_large_ints_and_doubles &&
|
||||
i > 0xffffffff )
|
||||
os << '"'<<v.as_string()<<'"';
|
||||
else
|
||||
os << i;
|
||||
|
||||
return;
|
||||
}
|
||||
case variant::double_type:
|
||||
//os << v.as_string();
|
||||
if (format == json::stringify_large_ints_and_doubles)
|
||||
os << '"'<<v.as_string()<<'"';
|
||||
else
|
||||
os << v.as_string();
|
||||
return;
|
||||
case variant::bool_type:
|
||||
os << v.as_string();
|
||||
|
|
@ -623,22 +629,22 @@ namespace fc
|
|||
case variant::array_type:
|
||||
{
|
||||
const variants& a = v.get_array();
|
||||
to_stream( os, a );
|
||||
to_stream( os, a, format );
|
||||
return;
|
||||
}
|
||||
case variant::object_type:
|
||||
{
|
||||
const variant_object& o = v.get_object();
|
||||
to_stream(os, o );
|
||||
to_stream(os, o, format );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fc::string json::to_string( const variant& v )
|
||||
fc::string json::to_string( const variant& v, output_formatting format /* = stringify_large_ints_and_doubles */ )
|
||||
{
|
||||
fc::stringstream ss;
|
||||
fc::to_stream( ss, v );
|
||||
fc::to_stream( ss, v, format );
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
|
@ -733,23 +739,23 @@ namespace fc
|
|||
|
||||
|
||||
|
||||
fc::string json::to_pretty_string( const variant& v )
|
||||
fc::string json::to_pretty_string( const variant& v, output_formatting format /* = stringify_large_ints_and_doubles */ )
|
||||
{
|
||||
return pretty_print(to_string(v), 2);
|
||||
return pretty_print(to_string(v, format), 2);
|
||||
}
|
||||
|
||||
void json::save_to_file( const variant& v, const fc::path& fi, bool pretty )
|
||||
void json::save_to_file( const variant& v, const fc::path& fi, bool pretty, output_formatting format /* = stringify_large_ints_and_doubles */ )
|
||||
{
|
||||
if( pretty )
|
||||
{
|
||||
auto str = json::to_pretty_string( v );
|
||||
auto str = json::to_pretty_string( v, format );
|
||||
fc::ofstream o(fi);
|
||||
o.write( str.c_str(), str.size() );
|
||||
}
|
||||
else
|
||||
{
|
||||
fc::ofstream o(fi);
|
||||
fc::to_stream( o, v );
|
||||
fc::to_stream( o, v, format );
|
||||
}
|
||||
}
|
||||
variant json::from_file( const fc::path& p, parse_type ptype )
|
||||
|
|
@ -789,19 +795,19 @@ namespace fc
|
|||
}
|
||||
}
|
||||
|
||||
ostream& json::to_stream( ostream& out, const variant& v )
|
||||
ostream& json::to_stream( ostream& out, const variant& v, output_formatting format /* = stringify_large_ints_and_doubles */ )
|
||||
{
|
||||
fc::to_stream( out, v );
|
||||
fc::to_stream( out, v, format );
|
||||
return out;
|
||||
}
|
||||
ostream& json::to_stream( ostream& out, const variants& v )
|
||||
ostream& json::to_stream( ostream& out, const variants& v, output_formatting format /* = stringify_large_ints_and_doubles */ )
|
||||
{
|
||||
fc::to_stream( out, v );
|
||||
fc::to_stream( out, v, format );
|
||||
return out;
|
||||
}
|
||||
ostream& json::to_stream( ostream& out, const variant_object& v )
|
||||
ostream& json::to_stream( ostream& out, const variant_object& v, output_formatting format /* = stringify_large_ints_and_doubles */ )
|
||||
{
|
||||
fc::to_stream( out, v );
|
||||
fc::to_stream( out, v, format );
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue