BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
json.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/variant.hpp>
3 #include <fc/filesystem.hpp>
4 
5 #define DEFAULT_MAX_RECURSION_DEPTH 200
6 
7 namespace fc
8 {
9  class ostream;
10  class buffered_istream;
11 
17  class json
18  {
19  public:
21  {
23 #ifdef WITH_EXOTIC_JSON_PARSERS
24  strict_parser = 1,
25  relaxed_parser = 2,
26  legacy_parser_with_string_doubles = 3,
27 #endif
29  };
31  {
34  };
35 
36  static ostream& to_stream( ostream& out, const std::string& );
37  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 );
40 
41  static variant from_stream( buffered_istream& in, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
42 
43  static variant from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
44  static variants variants_from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
45  static string to_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
46  static string to_pretty_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
47 
48  static bool is_valid( const std::string& json_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
49 
50  template<typename T>
51  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 )
52  {
53  save_to_file( variant(v, max_depth), fi, pretty, format, max_depth );
54  }
55 
56  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 );
57  static variant from_file( const fc::path& p, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
58 
59  template<typename T>
60  static T from_file( const fc::path& p, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
61  {
62  return json::from_file(p, ptype, max_depth).as<T>(max_depth);
63  }
64 
65  template<typename T>
66  static string to_string( const T& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
67  {
68  return to_string( variant(v, max_depth), format, max_depth );
69  }
70 
71  template<typename T>
72  static string to_pretty_string( const T& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
73  {
74  return to_pretty_string( variant(v, max_depth), format, max_depth );
75  }
76 
77  template<typename T>
78  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 )
79  {
80  save_to_file( variant(v, max_depth), fc::path(p), pretty, format, max_depth );
81  }
82  };
83 
84 } // fc
85 
86 #undef DEFAULT_MAX_RECURSION_DEPTH
fc::json::legacy_parser
@ legacy_parser
Definition: json.hpp:22
fc::variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:20
DEFAULT_MAX_RECURSION_DEPTH
#define DEFAULT_MAX_RECURSION_DEPTH
Definition: json.hpp:5
fc::json::to_string
static string to_string(const T &v, output_formatting format=stringify_large_ints_and_doubles, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.hpp:66
fc::json::output_formatting
output_formatting
Definition: json.hpp:30
fc::json
Definition: json.hpp:17
fc::json::broken_nul_parser
@ broken_nul_parser
Definition: json.hpp:28
fc
Definition: api.hpp:15
fc::json::from_string
static variant from_string(const string &utf8_str, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:458
fc::json::to_pretty_string
static string to_pretty_string(const variant &v, output_formatting format=stringify_large_ints_and_doubles, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:748
filesystem.hpp
fc::json::to_string
static string to_string(const variant &v, output_formatting format=stringify_large_ints_and_doubles, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:650
fc::json::stringify_large_ints_and_doubles
@ stringify_large_ints_and_doubles
Definition: json.hpp:32
fc::path
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::json::legacy_generator
@ legacy_generator
Definition: json.hpp:33
fc::ostream
Definition: iostream.hpp:41
fc::json::save_to_file
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)
Definition: json.hpp:51
fc::buffered_istream
Reads data from an unbuffered stream and enables peek functionality.
Definition: buffered_iostream.hpp:16
fc::json::to_pretty_string
static string to_pretty_string(const T &v, output_formatting format=stringify_large_ints_and_doubles, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.hpp:72
fc::json::is_valid
static bool is_valid(const std::string &json_str, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:810
fc::json::save_to_file
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)
Definition: json.hpp:78
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::json::from_file
static T from_file(const fc::path &p, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.hpp:60
variant.hpp
fc::json::from_file
static variant from_file(const fc::path &p, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:767
fc::json::to_stream
static ostream & to_stream(ostream &out, const std::string &)
Definition: json.cpp:562
fc::json::variants_from_string
static variants variants_from_string(const string &utf8_str, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:465
fc::json::parse_type
parse_type
Definition: json.hpp:20
fc::json::from_stream
static variant from_stream(buffered_istream &in, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:773