BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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
#define DEFAULT_MAX_RECURSION_DEPTH
Definition: json.hpp:5
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:638
T as(uint32_t max_depth) const
Definition: variant.hpp:336
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
An order-perserving dictionary of variant&#39;s.
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
std::vector< variant > variants
Definition: variant.hpp:170
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:798
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
static variant from_file(const fc::path &p, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:755
static ostream & to_stream(ostream &out, const std::string &)
Definition: json.cpp:550
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
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
static variant from_stream(buffered_istream &in, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:761
parse_type
Definition: json.hpp:20
output_formatting
Definition: json.hpp:30
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
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:736
Definition: api.hpp:15
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
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
Reads data from an unbuffered stream and enables peek functionality.
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