Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
tsa::json Class Referencefinal

A class representing JSON objects. More...

#include <TSAJSON.h>

Public Types

typedef std::initializer_list< std::pair< std::string, Type > > shape
 

Public Member Functions

 json () noexcept
 Constructor.
 
 json (std::nullptr_t) noexcept
 Constructor.
 
 json (double value)
 Constructor.
 
 json (int value)
 Constructor.
 
 json (int64_t value)
 Constructor.
 
 json (uint64_t value)
 Constructor.
 
 json (bool value)
 Constructor.
 
 json (const date_time &value)
 Constructor.
 
 json (const date &value)
 Constructor.
 
 json (const std::string &value)
 Constructor.
 
 json (std::string &&value)
 Constructor.
 
 json (const char *value)
 Constructor.
 
 json (const array &values)
 Constructor.
 
 json (array &&values)
 Constructor.
 
 json (const object &values)
 Constructor.
 
 json (object &&values)
 Constructor.
 
template<class T , class = decltype(&T::to_json)>
 json (const T &t)
 Implicit constructor: anything with a to_json() function.
 
template<class M , typename std::enable_if< std::is_constructible< std::string, typename M::key_type >::value &&std::is_constructible< json, typename M::mapped_type >::value, int >::type = 0>
 json (const M &m)
 Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
 
template<class V , typename std::enable_if< std::is_constructible< json, typename V::value_type >::value, int >::type = 0>
 json (const V &v)
 Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
 
 json (void *)=delete
 
bool bool_value (void) const
 Return the enclosed value if this is a boolean, false otherwise. More...
 
void dump (std::string &out) const
 Serialize self. More...
 
int int_value (void) const
 Return the enclosed value if this is a int, false otherwise. More...
 
bool is_array () const
 
bool is_bool () const
 
bool is_null () const
 
bool is_number () const
 
bool is_object () const
 
bool is_string () const
 
 operator bool (void) const
 Returns valuea as a boolean.
 
 operator date (void) const
 Returns value as a std::string.
 
 operator date_time (void) const
 Returns value as a std::string.
 
 operator double (void) const
 Returns value as a double;.
 
 operator int (void) const
 Returns valueas an int.
 
 operator int64_t (void) const
 Returns value as an int64_t.
 
 operator std::string (void) const
 Returns value as a std::string.
 
bool operator!= (const json &rhs) const
 
bool operator< (const json &rhs) const
 
bool operator<= (const json &rhs) const
 
bool operator== (const json &rhs) const
 
bool operator> (const json &rhs) const
 
bool operator>= (const json &rhs) const
 
const jsonoperator[] (size_t i) const
 Return a reference to arr[i] if this is an array, Json() otherwise. More...
 
const jsonoperator[] (const std::string &key) const
 Return a reference to obj[key] if this is an object, Json() otherwise. More...
 
const std::stringstring_value (void) const
 Return the enclosed string if this is a string, "" otherwise. More...
 
variant to_variant (void) const
 
Type type () const
 Accessors.
 

Static Public Member Functions

static json parse (const std::string &in, std::string &err)
 Parse. If parse fails, return Json() and assign an error message to err. More...
 
static std::vector< jsonparse_multi (const std::string &in, std::string &err)
 Parse multiple objects, concatenated or separated by whitespace. More...
 

Detailed Description

A class representing JSON objects.

Instances of this type can parse text files containing single or multiple JSON objects. JSON stands for 'Java Script Object Notation' and is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON files are easier to maintain than equivalent XML files.

Member Typedef Documentation

typedef std::initializer_list<std::pair<std::string, Type> > tsa::json::shape

has_shape(types, err)

Return true if this is a JSON object and, for each item in types, has a field of the given type. If not, return false and set err to a descriptive message.

Constructor & Destructor Documentation

tsa::json::json ( void *  )
delete

This prevents Json(some_pointer) from accidentally producing a bool. Use Json(bool(some_pointer)) if that behavior is desired.

Member Function Documentation

bool tsa::json::bool_value ( void  ) const

Return the enclosed value if this is a boolean, false otherwise.

Returns
true if it succeeds, false if it fails.
void tsa::json::dump ( std::string out) const

Serialize self.

Parameters
[in,out]outThe string to which self will be dumped.
int tsa::json::int_value ( void  ) const

Return the enclosed value if this is a int, false otherwise.

Returns
An int.
const json & tsa::json::operator[] ( size_t  i) const

Return a reference to arr[i] if this is an array, Json() otherwise.

Parameters
iZero-based index.
Returns
The indexed value.
const json & tsa::json::operator[] ( const std::string key) const

Return a reference to obj[key] if this is an object, Json() otherwise.

Parameters
keyThe key.
Returns
The indexed value.
json tsa::json::parse ( const std::string in,
std::string err 
)
static

Parse. If parse fails, return Json() and assign an error message to err.

Parameters
inThe input string to parse.
[in,out]errThe string to which any error message is written.
Returns
A json object.
std::vector< json > tsa::json::parse_multi ( const std::string in,
std::string err 
)
static

Parse multiple objects, concatenated or separated by whitespace.

Parameters
inThe input string containing multiple JSON objects.
[in,out]errThe error.
Returns
A std::vector<json>
const std::string & tsa::json::string_value ( void  ) const

Return the enclosed string if this is a string, "" otherwise.

Returns
A std::string&