Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::json Class Reference

#include <json.hpp>

Collaboration diagram for fc::json:

Public Types

enum class  parse_type : unsigned char { legacy_parser = 0 , strict_parser = 1 , relaxed_parser = 2 , legacy_parser_with_string_doubles = 3 }
 
enum class  output_formatting : unsigned char { stringify_large_ints_and_doubles = 0 , legacy_generator = 1 }
 
using yield_function_t = fc::optional_delegate<void(size_t)>
 

Static Public Member Functions

static variant from_string (const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
 
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)
 
static string to_string (const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
 
static string to_pretty_string (const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
 
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)
 
template<typename T >
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)
 
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)
 
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)
 
template<typename T >
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)
 
template<typename T >
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)
 
template<typename T >
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)
 
template<typename T >
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)
 

Static Public Attributes

static constexpr uint64_t max_length_limit = std::numeric_limits<uint64_t>::max()
 
static constexpr size_t escape_string_yield_check_count = 128
 

Detailed Description

Provides interface for json serialization.

json strings are always UTF8

Definition at line 19 of file json.hpp.

Member Typedef Documentation

◆ yield_function_t

Definition at line 34 of file json.hpp.

Member Enumeration Documentation

◆ output_formatting

enum class fc::json::output_formatting : unsigned char
strong
Enumerator
stringify_large_ints_and_doubles 
legacy_generator 

Definition at line 29 of file json.hpp.

◆ parse_type

enum class fc::json::parse_type : unsigned char
strong
Enumerator
legacy_parser 
strict_parser 
relaxed_parser 
legacy_parser_with_string_doubles 

Definition at line 22 of file json.hpp.

Member Function Documentation

◆ from_file() [1/2]

variant fc::json::from_file ( const fc::path & p,
const parse_type ptype = parse_type::legacy_parser,
const uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
static

Definition at line 797 of file json.cpp.

798 {
799 //auto tmp = std::make_shared<fc::ifstream>( p, ifstream::binary );
800 //auto tmp = std::make_shared<std::ifstream>( p.generic_string().c_str(), std::ios::binary );
801 //buffered_istream bi( tmp );
802 boost::filesystem::ifstream bi( p, std::ios::binary );
803 switch( ptype )
804 {
813 default:
814 FC_ASSERT( false, "Unknown JSON parser type {ptype}", ("ptype", static_cast<int>(ptype)) );
815 }
816 }
const mie::Vuint & p
Definition bn.cpp:27
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
variant variant_from_stream(T &in, uint32_t max_depth)
variant variant_from_stream(T &in, uint32_t max_depth)
Definition json.cpp:389
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_file() [2/2]

template<typename T >
static T fc::json::from_file ( const fc::path & p,
const parse_type ptype = parse_type::legacy_parser,
const uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
inlinestatic

Definition at line 54 of file json.hpp.

55 {
56 return json::from_file(p, ptype, max_depth).as<T>();
57 }
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
T as() const
Definition variant.hpp:327
#define T(meth, val, expected)
Here is the call graph for this function:

◆ from_string()

variant fc::json::from_string ( const string & utf8_str,
const parse_type ptype = parse_type::legacy_parser,
uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
static

Definition at line 442 of file json.cpp.

443 { try {
444 std::stringstream in( utf8_str );
445 //in.exceptions( std::ifstream::eofbit );
446 switch( ptype )
447 {
456 default:
457 FC_ASSERT( false, "Unknown JSON parser type {ptype}", ("ptype", static_cast<int>(ptype)) );
458 }
459 } FC_RETHROW_EXCEPTIONS( warn, "", ("str",utf8_str) ) }
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_valid()

bool fc::json::is_valid ( const std::string & json_str,
const parse_type ptype = parse_type::legacy_parser,
const uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
static

Definition at line 836 of file json.cpp.

837 {
838 if( utf8_str.size() == 0 ) return false;
839 std::stringstream in( utf8_str );
840 switch( ptype )
841 {
844 break;
847 break;
850 break;
853 break;
854 default:
855 FC_ASSERT( false, "Unknown JSON parser type {ptype}", ("ptype", static_cast<int>(ptype)) );
856 }
857 try { in.peek(); } catch ( const eof_exception& e ) { return true; }
858 return false;
859 }
Here is the call graph for this function:

◆ save_to_file() [1/3]

template<typename T >
static bool fc::json::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 )
inlinestatic

Definition at line 45 of file json.hpp.

46 {
47 return save_to_file( variant(v), fi, pretty, format );
48 }
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
cmd_format format
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_to_file() [2/3]

template<typename T >
static bool fc::json::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 )
inlinestatic

Definition at line 80 of file json.hpp.

81 {
82 return save_to_file( variant(v), fc::path(p), pretty, format );
83 }
wraps boost::filesystem::path to provide platform independent path manipulation.
Here is the call graph for this function:

◆ save_to_file() [3/3]

bool fc::json::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 )
static

Definition at line 781 of file json.cpp.

782 {
783 if( pretty ) {
785 std::ofstream o(fi.generic_string().c_str());
786 o.write( str.c_str(), str.size() );
787 return o.good();
788 } else {
789 std::ofstream o(fi.generic_string().c_str());
790 const auto yield = [&](size_t s) {
791 // no limitation
792 };
793 fc::to_stream( o, v, yield, format );
794 return o.good();
795 }
796 }
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
std::string generic_string() const
static constexpr time_point maximum()
Definition time.hpp:46
return str
Definition CLI11.hpp:1359
thread_local yield_t yield
Definition yield.hpp:52
void to_stream(T &os, const variants &a, const json::yield_function_t &yield, json::output_formatting format)
Definition json.cpp:576
char * s
Here is the call graph for this function:

◆ to_pretty_string() [1/2]

template<typename T >
static string fc::json::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 )
inlinestatic

Definition at line 70 of file json.hpp.

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 }
#define FC_CHECK_DEADLINE(DEADLINE,...)
Here is the call graph for this function:

◆ to_pretty_string() [2/2]

std::string fc::json::to_pretty_string ( const variant & v,
const yield_function_t & yield,
const output_formatting format = output_formatting::stringify_large_ints_and_doubles )
static

Definition at line 775 of file json.cpp.

775 {
776
777 auto s = to_string(v, yield, format);
778 return pretty_print( std::move( s ), 2);
779 }
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
std::string pretty_print(const std::string &v, uint8_t indent)
Definition json.cpp:682
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_string() [1/2]

template<typename T >
static string fc::json::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 )
inlinestatic

Definition at line 60 of file json.hpp.

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 }
Here is the call graph for this function:

◆ to_string() [2/2]

std::string fc::json::to_string ( const variant & v,
const yield_function_t & yield,
const output_formatting format = output_formatting::stringify_large_ints_and_doubles )
static

Definition at line 674 of file json.cpp.

675 {
676 std::stringstream ss;
677 fc::to_stream( ss, v, yield, format );
678 yield(ss.tellp());
679 return ss.str();
680 }
static const Segment ss(Segment::ss)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ variants_from_string()

variants fc::json::variants_from_string ( const string & utf8_str,
const parse_type ptype = parse_type::legacy_parser,
uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH )
static

Definition at line 461 of file json.cpp.

462 { try {
463 variants result;
464 std::stringstream in( utf8_str );
465 //in.exceptions( std::ifstream::eofbit );
466 try {
467 while( true )
468 {
469 // result.push_back( variant_from_stream( in ));
470 result.push_back(json_relaxed::variant_from_stream<std::stringstream, false>( in, max_depth ));
471 }
472 } catch ( const fc::eof_exception& ){}
473 return result;
474 } FC_RETHROW_EXCEPTIONS( warn, "", ("str",utf8_str) ) }
std::vector< fc::variant > variants
Definition variant.hpp:173
Here is the call graph for this function:

Member Data Documentation

◆ escape_string_yield_check_count

size_t fc::json::escape_string_yield_check_count = 128
staticconstexpr

Definition at line 36 of file json.hpp.

◆ max_length_limit

uint64_t fc::json::max_length_limit = std::numeric_limits<uint64_t>::max()
staticconstexpr

Definition at line 35 of file json.hpp.


The documentation for this class was generated from the following files: