Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
json.hpp
Go to the documentation of this file.
1#pragma once
2#include <fc/variant.hpp>
3#include <fc/filesystem.hpp>
4#include <fc/time.hpp>
5#include <fc/utility.hpp>
7
8#define DEFAULT_MAX_RECURSION_DEPTH 200
9
10namespace fc
11{
12 using std::ostream;
13
19 class json
20 {
21 public:
22 enum class parse_type : unsigned char
23 {
24 legacy_parser = 0,
25 strict_parser = 1,
28 };
29 enum class output_formatting : unsigned char
30 {
33 };
35 static constexpr uint64_t max_length_limit = std::numeric_limits<uint64_t>::max();
36 static constexpr size_t escape_string_yield_check_count = 128;
37 static variant from_string( const string& utf8_str, const parse_type ptype = parse_type::legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
38 static variants variants_from_string( const string& utf8_str, const parse_type ptype = parse_type::legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
41
42 static bool is_valid( const std::string& json_str, const parse_type ptype = parse_type::legacy_parser, const uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
43
44 template<typename T>
45 static bool save_to_file( const T& v, const fc::path& fi, const bool pretty = true, const output_formatting format = output_formatting::stringify_large_ints_and_doubles )
46 {
47 return save_to_file( variant(v), fi, pretty, format );
48 }
49
50 static bool save_to_file( const variant& v, const fc::path& fi, const bool pretty = true, const output_formatting format = output_formatting::stringify_large_ints_and_doubles );
52
53 template<typename T>
55 {
56 return json::from_file(p, ptype, max_depth).as<T>();
57 }
58
59 template<typename T>
61 {
62 const auto yield = [&](size_t s) {
63 FC_CHECK_DEADLINE(deadline);
64 FC_ASSERT(s <= max_len);
65 };
66 return to_string( variant(v), yield, format );
67 }
68
69 template<typename T>
71 {
72 const auto yield = [&](size_t s) {
73 FC_CHECK_DEADLINE(deadline);
74 FC_ASSERT( s <= max_len );
75 };
76 return to_pretty_string( variant(v), yield, format );
77 }
78
79 template<typename T>
80 static bool save_to_file( const T& v, const std::string& p, const bool pretty = true, const output_formatting format = output_formatting::stringify_large_ints_and_doubles )
81 {
82 return save_to_file( variant(v), fc::path(p), pretty, format );
83 }
84 };
85
86 std::string escape_string( const std::string_view& str, const json::yield_function_t& yield, bool escape_control_chars = true );
87
88} // fc
89
90#undef DEFAULT_MAX_RECURSION_DEPTH
const mie::Vuint & p
Definition bn.cpp:27
static variant from_file(const fc::path &p, const parse_type ptype=parse_type::legacy_parser, const uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:797
static bool is_valid(const std::string &json_str, const parse_type ptype=parse_type::legacy_parser, const uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:836
static string to_string(const T &v, const fc::time_point &deadline, const output_formatting format=output_formatting::stringify_large_ints_and_doubles, const uint64_t max_len=max_length_limit)
Definition json.hpp:60
parse_type
Definition json.hpp:23
static bool save_to_file(const T &v, const fc::path &fi, const bool pretty=true, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.hpp:45
static string to_pretty_string(const T &v, const fc::time_point &deadline=fc::time_point::maximum(), const output_formatting format=output_formatting::stringify_large_ints_and_doubles, const uint64_t max_len=max_length_limit)
Definition json.hpp:70
static constexpr size_t escape_string_yield_check_count
Definition json.hpp:36
static string to_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:674
static T from_file(const fc::path &p, const parse_type ptype=parse_type::legacy_parser, const uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.hpp:54
static variants variants_from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:461
fc::optional_delegate< void(size_t)> yield_function_t
Definition json.hpp:34
static bool save_to_file(const T &v, const std::string &p, const bool pretty=true, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.hpp:80
static constexpr uint64_t max_length_limit
Definition json.hpp:35
static string to_pretty_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:775
static variant from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:442
output_formatting
Definition json.hpp:30
wraps boost::filesystem::path to provide platform independent path manipulation.
static constexpr time_point maximum()
Definition time.hpp:46
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
T as() const
Definition variant.hpp:327
Defines exception's used by fc.
#define FC_CHECK_DEADLINE(DEADLINE,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define DEFAULT_MAX_RECURSION_DEPTH
Definition json.hpp:8
namespace sysio::chain
Definition authority.cpp:3
std::vector< fc::variant > variants
Definition variant.hpp:173
std::string escape_string(const std::string_view &str, const json::yield_function_t &yield, bool escape_control_chars=true)
Definition json.cpp:495
#define T(meth, val, expected)
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
cmd_format format
char * s