Argo  1.0
A C++ library for handling JSON.
Classes | Public Types | Public Member Functions | Public Attributes | List of all members
argo::json Class Reference

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)
 
jsonoperator= (const json &other)
 
jsonoperator= (json &&other) noexcept
 
jsonoperator= (int i)
 
jsonoperator= (double d)
 
jsonoperator= (bool b)
 
jsonoperator= (const char *s)
 
jsonoperator= (const std::string &s)
 
jsonoperator= (std::unique_ptr< std::string > s)
 
jsonoperator= (const std::map< std::string, std::unique_ptr< json >> &o)
 
jsonoperator= (const std::vector< std::unique_ptr< json >> &a)
 
jsonoperator= (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
 
jsonoperator[] (const std::string &name)
 
jsonoperator[] (const char *name)
 
const jsonoperator[] (const std::string &name) const
 
const jsonoperator[] (const char *name) const
 
jsonoperator[] (size_t index)
 
jsonoperator[] (int index)
 
const jsonoperator[] (size_t index) const
 
const jsonoperator[] (int index) const
 
const jsonappend (const json &j)
 
const jsonappend (std::unique_ptr< json > j)
 
const jsoninsert (const std::string &name, const json &j)
 
const jsoninsert (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
 

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
 
const jsonfind (const pointer &p) const
 

Public Attributes

decltype(nullptr) typedef null_t
 

Detailed Description

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.

if (static_cast<int>(j1) < static_cast<int>(j2))
{
// do something...
}

Definition at line 63 of file json.hpp.

Member Enumeration Documentation

JSON types as per RFC 4627 but with numbers split into int and double.

Definition at line 73 of file json.hpp.

Constructor & Destructor Documentation

json::json ( )
noexcept

New json null instance.

Definition at line 37 of file json.cpp.

json::~json ( )

Destructor.

Definition at line 41 of file json.cpp.

json::json ( const json other)

Copy constructor. Note that this is a full deep copy so use carefully to avoid performance problems.

Definition at line 107 of file json.cpp.

json::json ( json &&  other)
noexcept

Move constructor - shallow copy.

Definition at line 137 of file json.cpp.

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 : ""

Exceptions
json_exceptionif t isn't a valid type.

Definition at line 142 of file json.cpp.

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.

Parameters
tnumber_int_e, number_double_e or string_e
raw_valuee.g. 999989999999999999 (too big to be an int)
Exceptions
json_exceptionif t is not one of the allowed types.

Definition at line 172 of file json.cpp.

json::json ( int  i)

New json instance of number int type.

Definition at line 183 of file json.cpp.

json::json ( double  d)

New json instance of number double type.

Definition at line 189 of file json.cpp.

json::json ( bool  b)

New json instance of bool type.

Definition at line 195 of file json.cpp.

json::json ( const std::string &  s)

New json instance of string type. In this case the string is copied.

Definition at line 201 of file json.cpp.

json::json ( const char *  s)

New json instance of string type. In this case the string is copied.

Definition at line 207 of file json.cpp.

json::json ( null_t  )
noexcept

New json instance of a null type

Definition at line 213 of file json.cpp.

json::json ( std::unique_ptr< std::string >  s)

New json instance of string type. 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.

Definition at line 217 of file json.cpp.

Member Function Documentation

const json & json::append ( const json j)

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.

Exceptions
json_exceptionif the instance isn't an array.

Definition at line 514 of file json.cpp.

const json & json::append ( std::unique_ptr< json j)

Append a value to an array instance. Ownership of the passed instance is taken over. E.g.:

json array(json::array_e);
unique_ptr<json> i(new json(1000));
array.append(move(i));
Exceptions
json_exceptionif the instance isn't an array.

Definition at line 519 of file json.cpp.

const json & json::find ( const pointer p) const

Find the thing pointed at by a pointer.

Exceptions
json_exceptionIf the pointer didn't match, a json_exception is throw.

Definition at line 850 of file json.cpp.

vector< unique_ptr< json > > & json::get_array ( )

Get the underlying vector of JSON instances in an array JSON instance.

Exceptions
json_exceptionif the instance isn't an array.

Definition at line 305 of file json.cpp.

const vector< unique_ptr< json > > & json::get_array ( ) const

Get the underlying const vector of JSON instances in a const array of JSON instances.

Exceptions
json_exceptionif the instance isn't an array.

Definition at line 310 of file json.cpp.

json::type json::get_instance_type ( ) const

Get the type of a JSON instance.

Definition at line 300 of file json.cpp.

const char * json::get_instance_type_name ( ) const

Get the type name of a JSON instance - mainly for logging etc.

Definition at line 552 of file json.cpp.

map< string, unique_ptr< json > > & json::get_object ( )

Get the underlying map of JSON instances in an object JSON instance.

Exceptions
json_exceptionif the instance isn't an object.

Definition at line 315 of file json.cpp.

const map< string, unique_ptr< json > > & json::get_object ( ) const

Get the underlying const map of JSON instances in a const object JSON instance.

Exceptions
json_exceptionif the instance isn't an object.

Definition at line 320 of file json.cpp.

const string & json::get_raw_value ( ) const

Gets the uninterpretted raw text for a scalar value from the JSON file or string that was parsed. For objects and arrays, this will be empty.

Definition at line 325 of file json.cpp.

const json & json::insert ( const std::string &  name,
const json j 
)

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.

Exceptions
json_exceptionif the instance isn't an object.

Definition at line 533 of file json.cpp.

const json & json::insert ( const std::string &  name,
std::unique_ptr< json j 
)

Insert a value into an object instance. Ownership of the passed instance is taken over.

json object(json::object_e);
unique_ptr<json> i(new json(1000));
object.insert("bob", move(i));
Exceptions
json_exceptionif the instance isn't an object.

Definition at line 538 of file json.cpp.

json::operator bool ( ) const

Cast the object to a bool.

Exceptions
json_exceptionif the instance isn't a bool.

Definition at line 386 of file json.cpp.

json::operator const std::string & ( ) const

Cast the object to a string reference.

Exceptions
json_exceptionif the instance isn't a string.

Definition at line 370 of file json.cpp.

json::operator double ( ) const

Cast the object to a double.

Exceptions
json_exceptionif the instance isn't a double or an int.

Definition at line 350 of file json.cpp.

json::operator int ( ) const

Cast the object to an int.

Exceptions
json_exceptionif the instance isn't an int or a double.

Definition at line 330 of file json.cpp.

bool json::operator!= ( const json other) const

Inequality operator. This will not work if either side is a raw value.

Exceptions
json_exceptionif the instance is a raw value (constructed with json(type, raw_value).

Definition at line 665 of file json.cpp.

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.

Exceptions
json_exceptionif either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable.

Definition at line 790 of file json.cpp.

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.

Exceptions
json_exceptionif either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable.

Definition at line 815 of file json.cpp.

json & json::operator= ( const json other)

Assignment. Full deep copy.

Definition at line 112 of file json.cpp.

json & json::operator= ( json &&  other)
noexcept

Move assignment. Shallow copy therefore much more efficient than a deep copy.

Definition at line 131 of file json.cpp.

json & json::operator= ( int  i)

Assign the instance an int value. All previous values are erased and/or freed.

Definition at line 223 of file json.cpp.

json & json::operator= ( double  d)

Assign the instance a double value. All previous values are erased and/or freed.

Definition at line 231 of file json.cpp.

json & json::operator= ( bool  b)

Assign the instance a bool value. All previous values are erased and/or freed.

Definition at line 239 of file json.cpp.

json & json::operator= ( const char *  s)

Assign the instance a string value. All previous values are erased and/or freed. In this case, a copy of the string is made.

Definition at line 255 of file json.cpp.

json & json::operator= ( const std::string &  s)

Assign the instance a string value. All previous values are erased and/or freed. In this case, a copy of the string is made.

Definition at line 247 of file json.cpp.

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.

Definition at line 263 of file json.cpp.

json & json::operator= ( const std::map< std::string, std::unique_ptr< json >> &  o)

Assign the instance an object value. All previous values are erased and/or freed. All values are deep copied so this can be inefficient for large objects.

Definition at line 271 of file json.cpp.

json & json::operator= ( const std::vector< std::unique_ptr< json >> &  a)

Assign the instance an array value. All previous values are erased and/or freed. All values are deep copied so this can be inefficient for large arrays.

Definition at line 282 of file json.cpp.

json & json::operator= ( null_t  )

Assign the instance a null value. All previous values are erased and/or freed. All values are deep copied so this can be inefficient for large arrays.

Definition at line 293 of file json.cpp.

bool json::operator== ( const json other) const

Equality operator. This will not work if either side is a raw value.

Exceptions
json_exceptionif the instance is a raw value (constructed with json(type, raw_value).

Definition at line 629 of file json.cpp.

bool json::operator== ( int  i) const

== operator - throws for raw values

Full set of relevant comparison operators.

Definition at line 670 of file json.cpp.

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.

Exceptions
json_exceptionif either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable.

Definition at line 840 of file json.cpp.

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.

Exceptions
json_exceptionif either instance is a raw value (constructed with json(type, raw_value) or if the types aren't comparable.

Definition at line 845 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an object.

Definition at line 402 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an object.

Definition at line 422 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an object.
json_invalid_key_exceptionif the slot doesn't exist.

Definition at line 489 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an object.
json_invalid_key_exceptionif the slot doesn't exist.

Definition at line 509 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an array.
json_array_index_range_exceptionif the index is out of range.

Definition at line 427 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an array.
json_array_index_range_exceptionif the index is out of range.

Definition at line 446 of file json.cpp.

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.

Exceptions
json_exceptionif the instance isn't an array.
json_array_index_range_exceptionif the index is out of range.

Definition at line 458 of file json.cpp.

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

Exceptions
json_exceptionif the instance isn't an array.
json_array_index_range_exceptionif the index is out of range.

Definition at line 477 of file json.cpp.


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