1 #ifndef TSA_JSON__INCLUDED 2 #define TSA_JSON__INCLUDED 60 #include <initializer_list> 62 #include "TSATypeDef.h" 88 NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
92 typedef std::vector<json> array;
93 typedef std::map<std::string, json> object;
101 json(std::nullptr_t) noexcept;
109 json(uint64_t value);
121 json(
const char * value);
123 json(
const array &values);
125 json(array &&values);
127 json(
const object &values);
129 json(
object &&values);
134 template <
class T,
class = decltype(&T::to_json)>
138 template <
class M,
typename std::enable_if<
139 std::is_constructible<std::string, typename M::key_type>::value
140 && std::is_constructible<json, typename M::mapped_type>::value,
int>
::type = 0>
141 json(
const M & m) :
json(object(m.begin(), m.end())) {}
144 template <
class V,
typename std::enable_if<
145 std::is_constructible<json, typename V::value_type>::value,
int>::type = 0>
146 json(
const V & v) :
json(array(v.begin(), v.end())) {}
150 json(
void *) =
delete;
155 bool is_defined()
const {
return type() != NUL; }
170 template<
typename T> T
get(void)
const;
178 double number_value()
const;
182 double float64()
const;
190 int int32(
void)
const;
193 int64_t int64(
void)
const{
194 double value = float64();
195 int64_t integer = (int64_t)value;
196 if ((
double)integer != value)
197 throw std::exception(
"json cannot return number as int64_t, conversion error");
206 int64_t uint64(
void)
const{
207 double value = float64();
208 uint64_t integer = (int64_t)value;
209 if ((
double)integer != value)
210 throw std::exception(
"json cannot return number as uint64_t, conversion error");
219 bool boolean(
void)
const;
237 const array& array_items(
void)
const;
239 const array& get_array(
void)
const{
return array_items(); }
244 const object& object_items(
void)
const;
246 const object& get_object(
void)
const{
return object_items(); }
261 explicit operator double(
void)
const {
return float64(); }
263 explicit operator int(
void)
const {
return int32(); }
265 explicit operator int64_t (
void)
const {
return int64(); }
267 explicit operator uint64_t (
void)
const {
return uint64(); }
269 explicit operator bool(
void)
const {
return boolean(); }
283 void print(std::ostream& stream)
const;
285 void print_formated(std::ostream& stream)
const;
329 typedef std::initializer_list<std::pair<std::string, Type>>
shape;
330 bool has_shape(
const shape & types,
std::string & err)
const;
333 std::shared_ptr<json_value> m_ptr;
340 void push_back(
const json& value);
343 template<>
double json::get<double>(void)
const;
344 template<>
int json::get<int>(void)
const;
345 template<>
unsigned json::get<unsigned>(void)
const;
346 template<> int64_t json::get<int64_t>(void)
const;
347 template<> uint64_t json::get<uint64_t>(void)
const;
348 template<>
std::string json::get<std::string>(void)
const;
349 template<>
tsa::date json::get<tsa::date>(void)
const;
351 template<>
bool json::get<bool>(void)
const;
357 friend class JsonInt;
358 friend class JsonDouble;
360 virtual bool equals(
const json_value * other)
const = 0;
361 virtual bool less(
const json_value * other)
const = 0;
363 virtual double number_value()
const;
367 virtual const json::array &array_items()
const;
369 virtual const json::object &object_items()
const;
371 virtual ~json_value() {}
static json parse(const std::string &in, std::string &err)
Parse. If parse fails, return Json() and assign an error message to err.
Definition: TSAJSON.cpp:785
bool operator==(const json &rhs) const
Definition: TSAJSON.cpp:410
json(const T &t)
Implicit constructor: anything with a to_json() function.
Definition: TSAJSON.h:135
void set_from_iso_string(const std::string &iso_date_string)
Sets the date from the given ISO date string with format: "yyyy-mm-dd". This member function is much ...
Definition: TSATime.cpp:897
bool operator<(const json &rhs) const
Definition: TSAJSON.cpp:417
Namespace for the 'Trading System API' library.
Definition: original1.TSA3Core.cpp:20
static std::vector< json > parse_multi(const std::string &in, std::string &err)
Parse multiple objects, concatenated or separated by whitespace.
Definition: TSAJSON.cpp:813
Type
JSON value types.
Definition: TSAJSON.h:87
json(const V &v)
Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc) ...
Definition: TSAJSON.h:146
bool is_object() const
Definition: TSAJSON.h:167
bool is_string() const
Definition: TSAJSON.h:163
bool operator>(const json &rhs) const
Definition: TSAJSON.h:320
variant objects can represent values of different types.
Definition: TSAVariant.h:140
const json & operator[](size_t i) const
Return a reference to arr[i] if this is an array, Json() otherwise.
Definition: TSAJSON.cpp:371
bool is_bool() const
Definition: TSAJSON.h:161
void set_from_iso_string(const std::string &date_time_string)
Sets the date-time from a string in ISO format (yyyy-mm-ddThh:mm:ss:f). This member is very fast...
Definition: TSATime.cpp:2476
bool operator>=(const json &rhs) const
Definition: TSAJSON.h:322
A date of the Gregorian calendar.
Definition: TSATime.h:119
bool operator!=(const json &rhs) const
Definition: TSAJSON.h:316
json(const M &m)
Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
Definition: TSAJSON.h:141
json() noexcept
Constructor.
Definition: TSAJSON.cpp:289
bool operator<=(const json &rhs) const
Definition: TSAJSON.h:318
bool is_null() const
Definition: TSAJSON.h:157
bool bool_value(void) const
Return the enclosed value if this is a boolean, false otherwise.
Definition: TSAJSON.cpp:343
A class representing JSON objects.
Definition: TSAJSON.h:81
const std::string & string_value(void) const
Return the enclosed string if this is a string, "" otherwise.
Definition: TSAJSON.cpp:345
void dump(std::string &out) const
Serialize self.
Definition: TSAJSON.cpp:146
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
int64_t identifier_t
type for ID's
Definition: TSATypeDef.h:117
bool is_array() const
Definition: TSAJSON.h:165
Type type() const
Accessors.
Definition: TSAJSON.cpp:334
std::initializer_list< std::pair< std::string, Type > > shape
Definition: TSAJSON.h:329
bool is_number() const
Definition: TSAJSON.h:159
int int_value(void) const
Return the enclosed value if this is a int, false otherwise.
Definition: TSAJSON.cpp:341
variant to_variant(void) const
Definition: TSAJSON.cpp:351