dxjson  1.0
JSON library for C++
 All Classes Namespaces Files Functions Variables Enumerations Pages
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
dx::JSON Class Reference

#include <dxjson.h>

Public Types

typedef std::map< std::string,
JSON >::iterator 
object_iterator
 
typedef std::map< std::string,
JSON >::const_iterator 
const_object_iterator
 
typedef std::vector< JSON >
::iterator 
array_iterator
 
typedef std::vector< JSON >
::const_iterator 
const_array_iterator
 
typedef std::map< std::string,
JSON >::const_reverse_iterator 
object_reverse_iterator
 
typedef std::map< std::string,
JSON >::reverse_iterator 
const_object_reverse_iterator
 
typedef std::vector< JSON >
::reverse_iterator 
array_reverse_iterator
 
typedef std::vector< JSON >
::const_reverse_iterator 
const_array_reverse_iterator
 

Public Member Functions

 JSON ()
 
 JSON (const JSON &rhs)
 
 JSON (const JSONValue &rhs)
 
template<typename T >
 JSON (const T &x)
 
void clear ()
 
void write (std::ostream &out) const
 
void read (std::istream &in)
 
void readFromString (const std::string &jstr)
 
std::string toString (bool onlyTopLevel=false) const
 
bool operator== (const JSON &other) const
 
const JSONoperator[] (const size_t &indx) const
 
const JSONoperator[] (const std::string &s) const
 
const JSONoperator[] (const JSON &j) const
 
const JSONoperator[] (const char *str) const
 
template<typename T >
const JSONoperator[] (const T &x) const
 
JSONoperator[] (const size_t &indx)
 
JSONoperator[] (const std::string &s)
 
JSONoperator[] (const JSON &j)
 
JSONoperator[] (const char *str)
 
template<typename T >
JSONoperator[] (const T &x)
 
template<typename T >
JSONoperator= (const T &rhs)
 
JSONoperator= (const JSON &)
 
JSONoperator= (const JSONValue &)
 
JSONoperator= (const char &c)
 
JSONoperator= (const std::string &s)
 
JSONoperator= (const bool &x)
 
JSONoperator= (const char s[])
 
JSONoperator= (const Null &x)
 
template<typename T >
JSONoperator= (const std::vector< T > &vec)
 
template<typename T >
JSONoperator= (const std::map< std::string, T > &m)
 
template<typename T >
 operator T () const
 
template<typename T >
get () const
 
JSONValue type () const
 
void resize_array (size_t desired_size)
 
size_t size () const
 
size_t length () const
 
template<typename T >
bool has (const T &indx) const
 
bool has (const size_t &indx) const
 
bool has (const std::string &key) const
 
bool has (const JSON &j) const
 
bool has (const char *key) const
 
void push_back (const JSON &j)
 
void erase (const size_t &indx)
 
void erase (const std::string &key)
 
const_object_iterator object_begin () const
 
object_iterator object_begin ()
 
const_array_iterator array_begin () const
 
array_iterator array_begin ()
 
const_object_iterator object_end () const
 
object_iterator object_end ()
 
const_array_iterator array_end () const
 
array_iterator array_end ()
 
const_array_reverse_iterator array_rbegin () const
 
array_reverse_iterator array_rbegin ()
 
const_array_reverse_iterator array_rend () const
 
array_reverse_iterator array_rend ()
 
 ~JSON ()
 

Static Public Member Functions

static double getEpsilon ()
 
static JSON parse (const std::string &str)
 

Public Attributes

Value * val
 

Detailed Description

The JSON class. Object of this class are capable of storing/operating on arbitrary JSON values.

Definition at line 137 of file dxjson.h.

Constructor & Destructor Documentation

dx::JSON::JSON ( )
inline

Default constructor for JSON. Creates JSON of type JSON_UNDEFINED.

Definition at line 175 of file dxjson.h.

JSON::JSON ( const JSON rhs)

Copy constructor

Parameters
rhsThis is the JSON object which will be copied.

Definition at line 547 of file dxjson.cpp.

JSON::JSON ( const JSONValue rhs)

Construct a blank JSON object of a particular JSONValue type, i.e., (this->type() == rhs) after construction.

Definition at line 542 of file dxjson.cpp.

template<typename T >
dx::JSON::JSON ( const T &  x)

This constructor copies the parameter x's value using operator=().

Exceptions
JSONExceptionIf no suitable operator=() implementation found for copying from a particular type.
Parameters
xThe new JSON object will be constructed from this value.

Definition at line 759 of file dxjson.h.

dx::JSON::~JSON ( )
inline

Erases and deallocate any memory for the current JSON object

Definition at line 611 of file dxjson.h.

Member Function Documentation

JSON::const_array_iterator JSON::array_begin ( ) const

Returns iterator to beginning.

Exceptions
JSONExceptionIf called on an object which is not a JSON_ARRAY
Returns
A const iterator to the first element in the container.

Definition at line 900 of file dxjson.cpp.

JSON::array_iterator JSON::array_begin ( )

Returns iterator to beginning.

Exceptions
JSONExceptionIf called on an object which is not a JSON_ARRAY
Returns
An iterator to the first element in the container.

Definition at line 912 of file dxjson.cpp.

JSON::const_array_iterator JSON::array_end ( ) const

Returns iterator to end.

Exceptions
JSONExceptionIf called on an object which is not a JSON_ARRAY
Returns
A const iterator to the element past-the-end of the container.

Definition at line 924 of file dxjson.cpp.

JSON::array_iterator JSON::array_end ( )

Returns iterator to end.

Exceptions
JSONExceptionIf called on an object which is not a JSON_ARRAY
Returns
An iterator to the element past-the-end of the container.

Definition at line 936 of file dxjson.cpp.

JSON::const_array_reverse_iterator JSON::array_rbegin ( ) const

Returns reverse iterator to reverse beginning.

Exceptions
JSONExceptionIf called on an object which not a JSON_ARRAY A reverse iterator referring to the last element in the container.

Definition at line 949 of file dxjson.cpp.

JSON::array_reverse_iterator JSON::array_rbegin ( )

Returns reverse iterator to reverse beginning.

Exceptions
JSONExceptionIf called on an object which not a JSON_ARRAY A const reverse iterator referring to the last element in the container.

Definition at line 961 of file dxjson.cpp.

JSON::const_array_reverse_iterator JSON::array_rend ( ) const

Returns reverse iterator to reverse end.

Exceptions
JSONExceptionIf called on an object which not a JSON_ARRAY A const reverse iterator referring to the element right before the first element (which is considered to be the reverse end)

Definition at line 973 of file dxjson.cpp.

JSON::array_reverse_iterator JSON::array_rend ( )

Returns reverse iterator to reverse end.

Exceptions
JSONExceptionIf called on an object which not a JSON_ARRAY A reverse iterator referring to the element right before the first element (which is considered to be the reverse end)

Definition at line 985 of file dxjson.cpp.

void dx::JSON::clear ( )
inline

Clears the content of JSON object. this->type() == JSON_UNDEFINED after the call

Definition at line 197 of file dxjson.h.

Referenced by operator=(), and ~JSON().

void JSON::erase ( const size_t &  indx)

Removes a particular index inside a JSON_ARRAY

Exceptions
JSONExceptionIf called for non JSON_ARRAY object.
Parameters
indxThe index to be removed from array

Definition at line 682 of file dxjson.cpp.

void JSON::erase ( const std::string &  key)

Removes a particular key and it's associated value inside a JSON_HASH

Exceptions
JSONExceptionIf called for non JSON_HASH object.
Note
The parameter "key" will be treated as serialized JSON string
Parameters
keyThe key value to be removed from object.

Definition at line 688 of file dxjson.cpp.

template<typename T >
T dx::JSON::get ( ) const
inline

Provide conversion functionality from JSON object to some fundamental data types. This generic version works only for numeric types (and has same effect as overloaded conversion operator). A specialization for std::string exist too.

Returns
Value of JSON object in desired fundamental type
Exceptions
JSONExceptionIf no conversion from JSON object to required type is possible.

Definition at line 442 of file dxjson.h.

double JSON::getEpsilon ( )
static

Returns the current "epsilon" paramerer value. This value determines the "slack" while checking equality of two JSON_REAL values.

Returns
The currrent value of "epsilon" parameter
See Also
json_epsilon

Definition at line 27 of file dxjson.cpp.

template<typename T >
bool dx::JSON::has ( const T &  indx) const
inline

Returns true if the given numeric value represent a valid index in current JSON_ARRAY.

Exceptions
JSONExceptionIf called for non JSON_ARRAY object.
Parameters
indxthe value representing an index inside array.
Returns
true if "indx" is a valid location in array, else false.

Definition at line 488 of file dxjson.h.

Referenced by has().

bool JSON::has ( const size_t &  indx) const

Returns true if the given size_t value represent a valid index in current JSON_ARRAY.

Exceptions
JSONExceptionIf called for non JSON_ARRAY object.
Parameters
indxthe value representing an index inside array.
Returns
true if "indx" is a valid location in array, else false.

Definition at line 648 of file dxjson.cpp.

bool JSON::has ( const std::string &  key) const

Returns true if the given std::string key represent a valid key in current JSON_OBJECT.

Exceptions
JSONExceptionIf called for non JSON_HASH object.
Note
The parameter "key" will be treated as serialized JSON string
Parameters
keythe value to be looked for inside current JSON_OBJECT.
Returns
true if is a valid key, else false.

Definition at line 655 of file dxjson.cpp.

bool JSON::has ( const JSON j) const

Allow using a JSON object for has() - converted to a suitable type before executing.

Exceptions
JSONExceptionIf suitable conversion of input parameter is not possible.

Definition at line 665 of file dxjson.cpp.

bool JSON::has ( const char *  key) const

Exactly same behavior as has(const std::string &key) function. The given C-string is converted to std::string before executing the function.

Exceptions
JSONExceptionIf called for non JSON_HASH object.
Note
The parameter "key" will be treated as serialized JSON string

Definition at line 661 of file dxjson.cpp.

size_t dx::JSON::length ( ) const
inline

Exactly same as size()

See Also
size()

Definition at line 480 of file dxjson.h.

JSON::const_object_iterator JSON::object_begin ( ) const

Returns iterator to beginning.

Exceptions
JSONExceptionIf called on an object which is not a JSON_HASH
Returns
A const iterator to the first element in the container.

Definition at line 894 of file dxjson.cpp.

JSON::object_iterator JSON::object_begin ( )

Returns iterator to beginning.

Exceptions
JSONExceptionIf called on an object which is not a JSON_HASH
Returns
An iterator to the first element in the container.

Definition at line 906 of file dxjson.cpp.

JSON::const_object_iterator JSON::object_end ( ) const

Returns iterator to end.

Exceptions
JSONExceptionIf called on an object which is not a JSON_HASH
Returns
A const iterator to the element past-the-end of the container.

Definition at line 918 of file dxjson.cpp.

JSON::object_iterator JSON::object_end ( )

Returns iterator to end.

Exceptions
JSONExceptionIf called on an object which is not a JSON_HASH
Returns
An iterator to the element past-the-end of the container.

Definition at line 930 of file dxjson.cpp.

template<typename T >
dx::JSON::operator T ( ) const

Conversion operator. Currently only typecasting to a numeric type (real/integer/bool) is supported.

Returns
The typecasted value of JSON object in requested type.

Definition at line 796 of file dxjson.h.

template<typename T >
JSON & dx::JSON::operator= ( const T &  rhs)

Sets the current JSON object's value to the provided numeric value. This is a templatized version, specialized only for numeric types.

Note
Current value in object will be erased.
The type of JSON object (JSON_INTEGER or JSON_REAL) will be determined by the type of value provided.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 766 of file dxjson.h.

Referenced by JSON(), and operator=().

JSON & JSON::operator= ( const JSON rhs)

Copies the provided JSON object's value to current JSON object.

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 569 of file dxjson.cpp.

JSON & JSON::operator= ( const JSONValue rhs)

Creates a blank JSON object of a particular JSONValue type, i.e., this->type() == rhs; to the function, after the call.

Note
Current value of object will be erased.
Parameters
rhsThe new type for the JSON
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 554 of file dxjson.cpp.

JSON & JSON::operator= ( const char &  c)

Copies the provided character value to current JSON object (as a JSON_STRING)

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 589 of file dxjson.cpp.

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

Copies the provided std::string value to current JSON object (as a JSON_STRING)

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 583 of file dxjson.cpp.

JSON & JSON::operator= ( const bool &  x)

Copies the provided boolean value to current JSON object (as a JSON_BOOLEAN)

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 593 of file dxjson.cpp.

JSON & JSON::operator= ( const char  s[])

Copies the provided char* value to current JSON object (as a JSON_STRING)

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object (a C-style string)
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 606 of file dxjson.cpp.

JSON& dx::JSON::operator= ( const Null &  x)

Copies the provided Null object's value to current JSON object (as a JSON_NULL).

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).
template<typename T >
JSON & dx::JSON::operator= ( const std::vector< T > &  vec)

Copies the provided std::vector value to current JSON object (as a JSON_ARRAY)

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 782 of file dxjson.h.

template<typename T >
JSON & dx::JSON::operator= ( const std::map< std::string, T > &  m)

Copies the provided std::map value to current JSON object (as a JSON_OBJECT)

Note
Current value of object will be erased.
Parameters
rhsThe value which will be copied to current object.
Returns
Reference to current object (to allow chaining of = operations).

Definition at line 789 of file dxjson.h.

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

Equality comparison operator. Returns true if two JSON objects are equal. A deep matching of JSON object is performed.

Note
JSON_UNDEFINED != JSON_UNDEFINED
Parameters
otherThe JSON object to which current object will be compared.
Returns
true if both objects are same, else false.
See Also
operator!=()

Definition at line 888 of file dxjson.cpp.

const JSON & JSON::operator[] ( const size_t &  indx) const

Access value stored inside a JSON array by numeric index

Parameters
indxIndex location to be accessed inside current JSON array.
Returns
A constant reference to JSON value stored at given location
Exceptions
JSONExceptionif indx is out of bounds or this->type() != JSON_ARRAY

Definition at line 501 of file dxjson.cpp.

Referenced by operator[]().

const JSON & JSON::operator[] ( const std::string &  s) const

Access value stored inside JSON object by it's key

Parameters
sA pre-existing Key inside current object
Returns
A constant reference to JSON value stored under given Key
Exceptions
JSONExceptionif key does not exist or this->type() != JSON_HASH

Definition at line 471 of file dxjson.cpp.

const JSON & JSON::operator[] ( const JSON j) const

Access a value inside a JSON object/array, and indexed by the provided JSON. The given JSON object (parameter j) must be numeric (if this->type() == JSON_ARRAY) or string (if this->type() == JSON_OBJECT).

Parameters
jThis JSON value will be used to index the current JSON object.
Returns
A constant reference to JSON value stored at given index.
Note
If j.type() == JSON_STRING, a non-serialized version of the string will be used.
Exceptions
JSONExceptionif conditions specified in descriptions are not met, or if the referenced property/index does not exist.

Definition at line 508 of file dxjson.cpp.

const JSON & JSON::operator[] ( const char *  str) const

Same as const JSON& operator[](const std::string &s), just that c style string "str" is converted to std::string, before calling it.

Parameters
strC style string, representing a key inside the object.
Returns
A constant reference to JSON value stored under given key.

Definition at line 492 of file dxjson.cpp.

template<typename T >
const JSON & dx::JSON::operator[] ( const T &  x) const

This function take care of all possible numeric types used for referencing JSON array.

Parameters
xA numeric value (specialized under std::numeric_limits). It is typecasted to size_t before using it as array index.
Exceptions
JSONExceptionIf parameter x cannot be converted to a numeric type or this->type() != JSON_ARRAY.
Returns
A constant reference to JSON value stored under given index.

Definition at line 816 of file dxjson.h.

JSON & JSON::operator[] ( const size_t &  indx)

A non-constant version of const JSON& operator[](const size_t &indx)

Note
Returns a non-constant JSON reference (can be modified).
See Also
const JSON& operator[](const size_t &indx)

Definition at line 537 of file dxjson.cpp.

JSON & JSON::operator[] ( const std::string &  s)

A non-constant version of const JSON& operator[](const std::string &indx)

Note
Returns a non-constant JSON reference (can be modified).
If the specified key (parameter s) is not present, then it will be created in the object and it's initial value will be set to JSON_UNDEFINED
See Also
const JSON& operator[](const std::string &s)

Definition at line 482 of file dxjson.cpp.

JSON & JSON::operator[] ( const JSON j)

A non-constant version of const JSON& operator[](const JSON &j)

Note
Returns a non-constant JSON reference (value can be modifed).
If the argument "j" is a JSON_STRING object, then effect will be similar to JSON& operator [](const std::string &s) where a non-serialized version of JSON_STRING "j" will be used as argument.
See Also
const JSON& operator[](const JSON &j)
JSON& operator [](const std::string &s)

Definition at line 522 of file dxjson.cpp.

JSON & JSON::operator[] ( const char *  str)

Same as JSON& operator[](const std::string &s), just that c style string "str" is converted to std::string, before calling it.

See Also
JSON& operator [](const std::string &s);

Definition at line 497 of file dxjson.cpp.

template<typename T >
JSON& dx::JSON::operator[] ( const T &  x)
inline

A non-constant version of const JSON& operator[](const JSON &indx)

Note
Returns a non-constant reference (value can modifed).
See Also
const JSON& operator[](const JSON &indx)

Definition at line 350 of file dxjson.h.

static JSON dx::JSON::parse ( const std::string &  str)
inlinestatic

Creates a new JSON object from a serialized representation. See notes for read() (applies here as well)

Parameters
strThe serialized json object.
Returns

Definition at line 167 of file dxjson.h.

void JSON::push_back ( const JSON j)

Appends a JSON value at end of current JSON_ARRAY object.

Exceptions
JSONExceptionIf called for non JSON_ARRAY object.
Parameters
jThe value to be appended to array.

Definition at line 632 of file dxjson.cpp.

void JSON::read ( std::istream &  in)

Reads and populates current JSON object from specified input stream containing a valid serialized represntation of JSON value.

Note
  • Previous contents will be cleared.
  • It's not necessary that the JSON value being read is serialization of an array or hash only, i.e., serialization of string, number, boolean, and null can be read as well.
  • JSON value started by first character of stream is read into object and once the value finishes, rest of the stream's content is not looked into. For example:
    • If stream contains: '{"hello": 12}blah', the function will read a JSON_OBJECT({"hello": 12}) and return (without looking ahead at 'blah')
    • If stream contains: '12Hello', then function will read a JSON_INTEGER (12) and return (without looking ahead at 'Hello')
    • If stream contains: 'null12', then function will read a JSON_NULL and return (without looking ahead at '12')
    • If stream contains: 'truenull' then function will read a JSON_BOOLEAN(true) (without looking ahead at 'null').
    • If stream contains: '"hello"World' then function will read a JSON_STRING("hello") (without looking at 'World')
    • If stream contains: 'truHtrue' then function will throw an error, since characters 'truHtrue' do not represent any legal JSON value starting from first location.
Parameters
inInput stream object (for reading the serialized JSON)
Exceptions
JSONExceptionIf string being read is illegal JSON, or error occured while reading the stream.
See Also
write()
readFromString()

Definition at line 678 of file dxjson.cpp.

Referenced by readFromString().

void JSON::readFromString ( const std::string &  jstr)

Populates current JSON object from the given stringified json value. See notes for read() (applies here as well).

Parameters
jstrString reprenting a valid JSON object
Exceptions
JSONExceptionIf parameter jstr contain illegaly formatted JSON
See Also
read()
toString()

Definition at line 466 of file dxjson.cpp.

Referenced by parse().

void JSON::resize_array ( size_t  desired_size)

Resizes an JSON_ARRAY. If current size of array = curr_size, and desired size provided by user = desired_size, then

Case curr_size < desired_size: (desired_size - curr_size) number of JSON_UNDEFINED elements are pushed at the end of JSON_ARRAY object.

Case curr_size > desired_size: Last (curr_size - desired_size) number of JSON elements are deleted from array

Case curr_size == desired_size: No effect

After executing this function, size() == desired_size

Parameters
desired_sizeThe desired size of new array

Definition at line 991 of file dxjson.cpp.

size_t JSON::size ( ) const

Returns total number of element in a JSON_ARRAY or JSON_HASH.

Exceptions
JSONExceptionIf called on a object which is not of type JSON_ARRAY or JSON_HASH
Returns
Total number of keys (if a JSON_OBJECT), and total number of values (if JSON_ARRAY).
See Also
length()

Definition at line 611 of file dxjson.cpp.

Referenced by has(), and length().

std::string JSON::toString ( bool  onlyTopLevel = false) const

Returns the stringified representation of JSON object.

Parameters
onlyTopLevelIf set to true, then only JSON objects of type JSON_OBJECT or JSON_ARRAY can call this function.
Note
If called on a JSON_STRING object directly, the string would be enclosed in quotes.
Returns
A string reprentation of current JSON object.
See Also
write()
readFromString()

Definition at line 640 of file dxjson.cpp.

JSONValue dx::JSON::type ( ) const
inline

Returns the type of current JSON object.

Returns
Type (a variable of type enum JSONValue) of current JSON object.

Definition at line 450 of file dxjson.h.

Referenced by array_begin(), array_end(), array_rbegin(), array_rend(), erase(), has(), JSON(), object_begin(), object_end(), operator=(), operator==(), operator[](), push_back(), resize_array(), size(), toString(), and write().

void JSON::write ( std::ostream &  out) const

Writes the serialized JSON object to the output stream

Parameters
outOutput stream object, to which the serialized object will be written to
Exceptions
JSONException
See Also
read()
toString()

Definition at line 457 of file dxjson.cpp.

Referenced by toString().

Member Data Documentation

Value* dx::JSON::val

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