![]() |
Argo
1.0
A C++ library for handling JSON.
|
All json things are represented by instances of this class. More...
#include <json.hpp>
Public Types | |
enum | type { object_e, array_e, boolean_e, null_e, number_int_e, number_double_e, string_e } |
Public Member Functions | |
json () noexcept | |
~json () | |
json (const json &other) | |
json (json &&other) noexcept | |
json (type t) | |
json (type t, const std::string &raw_value) | |
json (int i) | |
json (double d) | |
json (bool b) | |
json (const std::string &s) | |
json (const char *s) | |
json (null_t) noexcept | |
json (std::unique_ptr< std::string > s) | |
json & | operator= (const json &other) |
json & | operator= (json &&other) noexcept |
json & | operator= (int i) |
json & | operator= (double d) |
json & | operator= (bool b) |
json & | operator= (const char *s) |
json & | operator= (const std::string &s) |
json & | operator= (std::unique_ptr< std::string > s) |
json & | operator= (const std::map< std::string, std::unique_ptr< json >> &o) |
json & | operator= (const std::vector< std::unique_ptr< json >> &a) |
json & | operator= (null_t) |
type | get_instance_type () const |
const char * | get_instance_type_name () const |
std::vector< std::unique_ptr< json > > & | get_array () |
const std::vector< std::unique_ptr< json > > & | get_array () const |
std::map< std::string, std::unique_ptr< json > > & | get_object () |
const std::map< std::string, std::unique_ptr< json > > & | get_object () const |
operator int () const | |
operator double () const | |
operator const std::string & () const | |
operator bool () const | |
json & | operator[] (const std::string &name) |
json & | operator[] (const char *name) |
const json & | operator[] (const std::string &name) const |
const json & | operator[] (const char *name) const |
json & | operator[] (size_t index) |
json & | operator[] (int index) |
const json & | operator[] (size_t index) const |
const json & | operator[] (int index) const |
const json & | append (const json &j) |
const json & | append (std::unique_ptr< json > j) |
const json & | insert (const std::string &name, const json &j) |
const json & | insert (const std::string &name, std::unique_ptr< json > j) |
const std::string & | get_raw_value () const |
bool | operator== (const json &other) const |
bool | operator!= (const json &other) const |
bool | operator< (const json &other) const |
bool | operator<= (const json &other) const |
bool | operator> (const json &other) const |
bool | operator>= (const json &other) const |
bool | operator== (int i) const |
== operator - throws for raw values More... | |
bool | operator== (double d) const |
== operator - throws for raw values | |
bool | operator== (const std::string &s) const |
== operator - throws for raw values | |
bool | operator== (const char *s) const |
== operator - throws for raw values | |
bool | operator!= (int i) const |
!= operator - throws for raw values | |
bool | operator!= (double d) const |
!= operator - throws for raw values | |
bool | operator!= (const std::string &s) const |
!= operator - throws for raw values | |
bool | operator!= (const char *s) const |
!= operator - throws for raw values | |
bool | operator< (int i) const |
< operator - throws for raw values | |
bool | operator< (double d) const |
< operator - throws for raw values | |
bool | operator< (const std::string &s) const |
< operator - throws for raw values | |
bool | operator< (const char *s) const |
< operator - throws for raw values | |
bool | operator<= (int i) const |
<= operator - throws for raw values | |
bool | operator<= (double d) const |
<= operator - throws for raw values | |
bool | operator<= (const std::string &s) const |
<= operator - throws for raw values | |
bool | operator<= (const char *s) const |
<= operator - throws for raw values | |
bool | operator> (int i) const |
| |
bool | operator> (double d) const |
| |
bool | operator> (const std::string &s) const |
| |
bool | operator> (const char *s) const |
| |
bool | operator>= (int i) const |
>= operator - throws for raw values | |
bool | operator>= (double d) const |
>= operator - throws for raw values | |
bool | operator>= (const std::string &s) const |
>= operator - throws for raw values | |
bool | operator>= (const char *s) const |
>= operator - throws for raw values | |
const json & | find (const pointer &p) const |
Public Attributes | |
decltype(nullptr) typedef | null_t |
All json things are represented by instances of this class.
All JSON things are represented by instaces of this class. Due to the nature of JSON, which is fundamentally a construct of dynamically typed languages, there are inevitably a few compromises and edge cases to be handled.
Refer to RFC7159 for more details.
Notes on comparison operators: == and != are provided as the implementation is complex and the semantics in the case of comparing one type with another are simple - they're always not equal. <, <=, > & >= are not provided as a deliberate policy as it's complex and not obvious what would be wanted when the types of the two sides didn't match or were arrays/objects. The calling code is always free to cast scalar types to numbers or strings where appropriate and compare those.
E.g.
enum argo::json::type |
json::json | ( | const json & | other | ) |
|
noexcept |
json::json | ( | type | t | ) |
New json instance of the given type. The value is initialised according to the type as follows:
object_e : empty
array_e : empty
boolean_e : false
null_e : null
number_e : 0
string_e : ""
json_exception | if t isn't a valid type. |
json::json | ( | type | t, |
const std::string & | raw_value | ||
) |
New json instance of int, double or string type in the specific situation where the underlying text representation couldn't be converted to an stl string, an int or a double. This might arise when the number is out of range for an int or double variable or contains invalid unicode or escape characters. Storing it in an unconverted form allows code using this library to process it anyway if desired.
t | number_int_e, number_double_e or string_e |
raw_value | e.g. 999989999999999999 (too big to be an int) |
json_exception | if t is not one of the allowed types. |
json::json | ( | double | d | ) |
json::json | ( | const std::string & | s | ) |
json::json | ( | const char * | s | ) |
|
noexcept |
json::json | ( | std::unique_ptr< std::string > | s | ) |
Append a value to an array instance. A copy is made of the value so this can be inefficient for large structures. In that case use the append method that takes a unique_ptr.
json_exception | if the instance isn't an array. |
Append a value to an array instance. Ownership of the passed instance is taken over. E.g.:
json_exception | if the instance isn't an array. |
Find the thing pointed at by a pointer.
json_exception | If the pointer didn't match, a json_exception is throw. |
vector< unique_ptr< json > > & json::get_array | ( | ) |
Get the underlying vector of JSON instances in an array JSON instance.
json_exception | if the instance isn't an array. |
const vector< unique_ptr< json > > & json::get_array | ( | ) | const |
Get the underlying const vector of JSON instances in a const array of JSON instances.
json_exception | if the instance isn't an array. |
json::type json::get_instance_type | ( | ) | const |
const char * json::get_instance_type_name | ( | ) | const |
map< string, unique_ptr< json > > & json::get_object | ( | ) |
Get the underlying map of JSON instances in an object JSON instance.
json_exception | if the instance isn't an object. |
const map< string, unique_ptr< json > > & json::get_object | ( | ) | const |
Get the underlying const map of JSON instances in a const object JSON instance.
json_exception | if the instance isn't an object. |
const string & json::get_raw_value | ( | ) | const |
Insert a value into an object instance. A copy is made of the value so this can be inefficient for large structures. In that case use the insert method that takes a unique_ptr. Any existing slot with the same name will be overwitten.
json_exception | if the instance isn't an object. |
Insert a value into an object instance. Ownership of the passed instance is taken over.
json_exception | if the instance isn't an object. |
json::operator bool | ( | ) | const |
Cast the object to a bool.
json_exception | if the instance isn't a bool. |
json::operator const std::string & | ( | ) | const |
Cast the object to a string reference.
json_exception | if the instance isn't a string. |
json::operator double | ( | ) | const |
Cast the object to a double.
json_exception | if the instance isn't a double or an int. |
json::operator int | ( | ) | const |
Cast the object to an int.
json_exception | if the instance isn't an int or a double. |
bool json::operator!= | ( | const json & | other | ) | const |
Inequality operator. This will not work if either side is a raw value.
json_exception | if the instance is a raw value (constructed with json(type, raw_value). |
bool json::operator< | ( | const json & | other | ) | const |
< operator. This will not work if either side is a raw value and only works for numbers and strings. Strings can only be compared with strings but ints and double may be compared with each other and will be cast to doubles first.
json_exception | if either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable. |
bool json::operator<= | ( | const json & | other | ) | const |
< operator. This will not work if either side is a raw value and only works for numbers and strings. Strings can only be compared with strings but ints and double may be compared with each other and will be cast to doubles first.
json_exception | if either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable. |
json & json::operator= | ( | int | i | ) |
json & json::operator= | ( | double | d | ) |
json & json::operator= | ( | bool | b | ) |
json & json::operator= | ( | const char * | s | ) |
json & json::operator= | ( | const std::string & | s | ) |
json & json::operator= | ( | std::unique_ptr< std::string > | s | ) |
Assign the instance a string value. All previous values are erased and/or freed. In this case ownership of the string is taken over by the json object. The caller will need to move() the unique_ptr to call this - which makes the transfer of ownership explicit.
json & json::operator= | ( | null_t | ) |
bool json::operator== | ( | const json & | other | ) | const |
Equality operator. This will not work if either side is a raw value.
json_exception | if the instance is a raw value (constructed with json(type, raw_value). |
bool json::operator== | ( | int | i | ) | const |
bool json::operator> | ( | const json & | other | ) | const |
operator. This will not work if either side is a raw value and only works for numbers
and strings. Strings can only be compared with strings but ints and double may be compared with each other and will be cast to doubles first.
json_exception | if either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable. |
bool json::operator>= | ( | const json & | other | ) | const |
operator. This will not work if either side is a raw value and only works for numbers
and strings. Strings can only be compared with strings but ints and double may be compared with each other and will be cast to doubles first.
json_exception | if either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable. |
json & json::operator[] | ( | const std::string & | name | ) |
Find an entry in an object instance by name. If the entry doesn't exist, a new one is created with a json(null_e) value. Note, this is the same semantics as the STL map [] operator.
json_exception | if the instance isn't an object. |
json & json::operator[] | ( | const char * | name | ) |
Find an entry in an object instance by name. If the entry doesn't exist, a new one is created with a json(null_e) value. Note, this is the same semantics as the STL map [] operator.
json_exception | if the instance isn't an object. |
const json & json::operator[] | ( | const std::string & | name | ) | const |
Find an entry in an object instance by name. If the entry doesn't exist, an exception is thrown.
json_exception | if the instance isn't an object. |
json_invalid_key_exception | if the slot doesn't exist. |
const json & json::operator[] | ( | const char * | name | ) | const |
Find an entry in an object instance by name. If the entry doesn't exist, an exception is thrown.
json_exception | if the instance isn't an object. |
json_invalid_key_exception | if the slot doesn't exist. |
json & json::operator[] | ( | size_t | index | ) |
Find an entry in an array instance by index. If the index is out of range, an exception is thrown.
json_exception | if the instance isn't an array. |
json_array_index_range_exception | if the index is out of range. |
json & json::operator[] | ( | int | index | ) |
Find an entry in an array instance by index. If the index is out of range, an exception is thrown. This method is here to provide disambiguation from char * when a 0 literal is used.
json_exception | if the instance isn't an array. |
json_array_index_range_exception | if the index is out of range. |
const json & json::operator[] | ( | size_t | index | ) | const |
Find an entry in an array instance by index. If the index is out of range, an exception is thrown.
json_exception | if the instance isn't an array. |
json_array_index_range_exception | if the index is out of range. |
const json & json::operator[] | ( | int | index | ) | const |
Find an entry in an array instance by index. If the index is out of range, an exception is thrown. This method is here to provide disambiguation from char * when a 0 literal is
json_exception | if the instance isn't an array. |
json_array_index_range_exception | if the index is out of range. |