2013-06-05 19:19:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
#include <fc/variant.hpp>
|
2014-04-22 14:22:17 +00:00
|
|
|
#include <fc/filesystem.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2018-03-13 20:36:15 +00:00
|
|
|
#define DEFAULT_MAX_RECURSION_DEPTH 200
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
namespace fc
|
|
|
|
|
{
|
|
|
|
|
class ostream;
|
|
|
|
|
class buffered_istream;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provides interface for json serialization.
|
|
|
|
|
*
|
|
|
|
|
* json strings are always UTF8
|
|
|
|
|
*/
|
|
|
|
|
class json
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-12-16 15:25:02 +00:00
|
|
|
enum parse_type
|
|
|
|
|
{
|
|
|
|
|
legacy_parser = 0,
|
2018-03-13 20:36:15 +00:00
|
|
|
#ifdef WITH_EXOTIC_JSON_PARSERS
|
2014-12-16 15:25:02 +00:00
|
|
|
strict_parser = 1,
|
2015-03-31 13:47:54 +00:00
|
|
|
relaxed_parser = 2,
|
2018-03-13 20:36:15 +00:00
|
|
|
legacy_parser_with_string_doubles = 3,
|
|
|
|
|
#endif
|
|
|
|
|
broken_nul_parser = 4
|
2014-12-16 15:25:02 +00:00
|
|
|
};
|
2015-06-11 23:22:22 +00:00
|
|
|
enum output_formatting
|
|
|
|
|
{
|
|
|
|
|
stringify_large_ints_and_doubles = 0,
|
2018-03-13 20:36:15 +00:00
|
|
|
#ifdef WITH_EXOTIC_JSON_PARSERS
|
2015-06-11 23:22:22 +00:00
|
|
|
legacy_generator = 1
|
2018-03-13 20:36:15 +00:00
|
|
|
#endif
|
2015-06-11 23:22:22 +00:00
|
|
|
};
|
2014-12-16 15:25:02 +00:00
|
|
|
|
2018-03-13 20:36:15 +00:00
|
|
|
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, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
|
|
|
|
static ostream& to_stream( ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
|
|
|
|
static ostream& to_stream( ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2018-03-13 20:36:15 +00:00
|
|
|
static variant from_stream( buffered_istream& in, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2018-03-13 20:36:15 +00:00
|
|
|
static variant from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
|
|
|
|
static variants variants_from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
|
|
|
|
static string to_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
|
|
|
|
static string to_pretty_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
2013-07-18 23:09:18 +00:00
|
|
|
|
2018-03-13 20:36:15 +00:00
|
|
|
static bool is_valid( const std::string& json_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
2014-05-08 01:27:37 +00:00
|
|
|
|
2013-07-18 23:09:18 +00:00
|
|
|
template<typename T>
|
2018-03-13 20:36:15 +00:00
|
|
|
static void save_to_file( const T& v, const fc::path& fi, bool pretty = true, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
|
2013-07-18 23:09:18 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
save_to_file( variant(v, max_depth), fi, pretty, format, max_depth );
|
2013-07-18 23:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-13 20:36:15 +00:00
|
|
|
static void save_to_file( const variant& v, const fc::path& fi, bool pretty = true, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
|
|
|
|
static variant from_file( const fc::path& p, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
template<typename T>
|
2018-03-13 20:36:15 +00:00
|
|
|
static T from_file( const fc::path& p, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
return json::from_file(p, ptype, max_depth).as<T>(max_depth);
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2018-03-13 20:36:15 +00:00
|
|
|
static string to_string( const T& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
return to_string( variant(v, max_depth), format, max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2018-03-13 20:36:15 +00:00
|
|
|
static string to_pretty_string( const T& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
return to_pretty_string( variant(v, max_depth), format, max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2018-03-13 20:36:15 +00:00
|
|
|
static void save_to_file( const T& v, const std::string& p, bool pretty = true, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
save_to_file( variant(v, max_depth), fc::path(p), pretty, format, max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // fc
|
2018-03-13 20:36:15 +00:00
|
|
|
|
|
|
|
|
#undef DEFAULT_MAX_RECURSION_DEPTH
|