This class represents either a json value an object or an array. More...

#include <json.hpp>

Public Types

enum  json_type {
  e_null , e_integer , e_real , e_boolean ,
  e_string , e_object , e_array
}
 
typedef json_iterator< jsoniterator
 
typedef const_json_iterator< jsonconst_iterator
 
typedef std::map< std::string, jsonobject_type
 
typedef std::vector< jsonarray_type
 

Public Member Functions

 json ()
 
template<class T >
 json (T val, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value >::type *=0)
 
template<class T >
 json (T val, typename std::enable_if< std::is_floating_point< T >::value >::type *=0)
 
template<class T >
 json (T val, typename std::enable_if< std::is_convertible< T, std::string >::value >::type *=0)
 
 json (bool b)
 
 json (std::initializer_list< json > l)
 
 json (std::nullptr_t n)
 
 json (json_type t)
 
 ~json ()
 
template<class T >
jsonoperator= (T val)
 
 json (const json &x)
 
 json (json &&x) noexcept
 
jsonoperator= (json &&x) noexcept
 
jsonoperator= (const json &x)
 
std::string str () const
 
std::string str (const json_format &format) const
 
void dump (std::ostream &out) const
 
jsonoperator[] (const std::string &key)
 
const jsonoperator[] (const std::string &key) const
 
bool contains (const std::string &key) const
 
jsonoperator[] (std::size_t i)
 
const jsonoperator[] (std::size_t i) const
 
template<class T >
void push_back (const T &val)
 
jsonback ()
 
const jsonback () const
 
template<class T >
void reset (T val, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value >::type *=0)
 
template<class T >
void reset (T val, typename std::enable_if< std::is_floating_point< T >::value >::type *=0)
 
void reset (const std::string &val)
 
void reset (const char *val)
 
void reset (bool b)
 
std::size_t size () const
 
bool empty () const
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
template<class T >
std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value, T >::type as () const
 
template<class T >
std::enable_if< std::is_floating_point< T >::value, T >::type as () const
 
template<class T >
std::enable_if< std::is_same< bool, T >::value, T >::type as () const
 
template<class T >
std::enable_if< std::is_convertible< T, std::string >::value, T >::type as () const
 
template<class T >
bool fits_to_type (typename std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value >::type *=0) const
 
template<class T >
bool fits_to_type (typename std::enable_if< std::is_floating_point< T >::value >::type *=0) const
 
template<class T >
bool fits_to_type (typename std::enable_if< std::is_same< bool, T >::value >::type *=0) const
 
template<class T >
bool fits_to_type (typename std::enable_if< std::is_convertible< T, std::string >::value >::type *=0) const
 
json_type type () const
 
bool is_number () const
 
bool is_real () const
 
bool is_integer () const
 
bool is_boolean () const
 
bool is_string () const
 
bool is_array () const
 
bool is_object () const
 
bool is_null () const
 
template<class T >
at (std::size_t pos) const
 
jsonget (const std::string &key)
 
const jsonget (const std::string &key) const
 
jsonat_path (const std::string &path, char delimiter='.')
 
const jsonat_path (const std::string &path, char delimiter='.') const
 
void clear ()
 
void erase (const std::string &key)
 
void erase (std::size_t i)
 

Static Public Member Functions

static json object ()
 
static json array ()
 

Friends

class json_iterator< json >
 
class const_json_iterator< json >
 
OOS_JSON_API bool operator== (const json &a, const json &b)
 
OOS_JSON_API bool operator!= (const json &a, const json &b)
 
OOS_JSON_API bool operator< (const json &a, const json &b)
 
OOS_JSON_API bool operator<= (const json &a, const json &b)
 
OOS_JSON_API bool operator> (const json &a, const json &b)
 
OOS_JSON_API bool operator>= (const json &a, const json &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator< (const json &a, const T &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator< (const T &a, const json &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator<= (const json &a, const T &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator<= (const T &a, const json &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator> (const json &a, const T &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator> (const T &a, const json &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator>= (const json &a, const T &b)
 
template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator>= (const T &a, const json &b)
 
OOS_JSON_API std::ostream & operator<< (std::ostream &out, const json &val)
 

Detailed Description

This class represents either a json value an object or an array.

This class represents any json value

  • number (double or long long)
  • string
  • boolean
  • object
  • array

Member Typedef Documentation

◆ array_type

typedef std::vector<json> matador::json::array_type

Shortcut to json array vector type

◆ const_iterator

Shortcut to json const iterator type

◆ iterator

Shortcut to json iterator type

◆ object_type

typedef std::map<std::string, json> matador::json::object_type

Shortcut to json object map type

Member Enumeration Documentation

◆ json_type

Enum representing the different json value types

Enumerator
e_null 

Json null type

e_integer 

Json integer type (number)

e_real 

Json real type (number)

e_boolean 

Json boolean type

e_string 

Json string type

e_object 

Json object type

e_array 

Json array type

Constructor & Destructor Documentation

◆ json() [1/10]

matador::json::json ( )

Creates an json null value

◆ json() [2/10]

template<class T >
matador::json::json ( val,
typename std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value >::type = 0 
)
inline

Creates a number json value where the value is treated like an integer of type long long

Template Parameters
TType of the value
Parameters
valValue of the json object

◆ json() [3/10]

template<class T >
matador::json::json ( val,
typename std::enable_if< std::is_floating_point< T >::value >::type = 0 
)
inline

Creates a number json value where the value is treated like a floating point of type double

Template Parameters
TType of the value
Parameters
valValue of the json object

◆ json() [4/10]

template<class T >
matador::json::json ( val,
typename std::enable_if< std::is_convertible< T, std::string >::value >::type = 0 
)
inline

Creates a string json value where the value is treated like std::string

Template Parameters
TType of the value
Parameters
valValue of the json object

◆ json() [5/10]

matador::json::json ( bool  b)

Creates a boolean json value

Parameters
bThe boolean value of the json object

◆ json() [6/10]

matador::json::json ( std::initializer_list< json l)

Creates a json array from the initializer list.

Note: If the list contains just two elements and the first element is of type string an json object is created.

Parameters
l

◆ json() [7/10]

matador::json::json ( std::nullptr_t  n)

Creates a null value json object

Parameters
nNull value

◆ json() [8/10]

matador::json::json ( json_type  t)
explicit

Creates explicitly a json object of the give json type

Parameters
tType of the json object
See also
json_type

◆ ~json()

matador::json::~json ( )

Destroys the json object

◆ json() [9/10]

matador::json::json ( const json x)

Copy construct a josn object from the given json object

Parameters
xThe json object to copy from

◆ json() [10/10]

matador::json::json ( json &&  x)
noexcept

Copy move the given json object

Parameters
xThe json object to copy move

Member Function Documentation

◆ array()

static json matador::json::array ( )
static

Creates an empty json array.

Returns
An empty json array

◆ as() [1/4]

template<class T >
std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value, T >::type matador::json::as ( ) const
inline

Return the json value as an integral type

If json type isn't integer a std::logic_error is thrown

Exceptions
std::logic_errorIf the type isn't integral
Returns
The json value as requested integral type

◆ as() [2/4]

template<class T >
std::enable_if< std::is_floating_point< T >::value, T >::type matador::json::as ( ) const
inline

Return the json value as a floating point type

If json type isn't a floating point a std::logic_error is thrown

Exceptions
std::logic_errorIf the type isn't floating point
Returns
The json value as requested floating point type

◆ as() [3/4]

template<class T >
std::enable_if< std::is_same< bool, T >::value, T >::type matador::json::as ( ) const
inline

Return the json value as a boolean

If json type isn't boolean a std::logic_error is thrown

Exceptions
std::logic_errorIf the type isn't boolean
Returns
The json value as boolean

◆ as() [4/4]

template<class T >
std::enable_if< std::is_convertible< T, std::string >::value, T >::type matador::json::as ( ) const
inline

Return the json value as a string

If json type isn't string a std::logic_error is thrown

Exceptions
std::logic_errorIf the type isn't of type string
Returns
The json value as string

◆ at()

template<class T >
T matador::json::at ( std::size_t  pos) const
inline

Returns the json element at given position if the current json is of type array.

if current json isn't of type array an std::logic_error is thrown.

Parameters
posThe position of the requested element
Exceptions
std::logic_errorIf json isn't of type array
Returns
json object at given position

◆ at_path() [1/2]

json & matador::json::at_path ( const std::string &  path,
char  delimiter = '.' 
)

Returns the json value at the given path. The path is a list of keys delimited by a delimiter char. The default delimiter is a "." (dot).

Parameters
pathPath to find
delimiterDelimiter in the path
Returns
The json value at the path

◆ at_path() [2/2]

const json & matador::json::at_path ( const std::string &  path,
char  delimiter = '.' 
) const

Returns the json value at the given path. The path is a list of keys delimited by a delimiter char. The default delimiter is a "." (dot).

Parameters
pathPath to find
delimiterDelimiter in the path
Returns
The json value at the path

◆ back() [1/2]

json & matador::json::back ( )

Returns a reference to the last json object of the array.

If the current type isn't array an exception is thrown.

Exceptions
std::logic_errorThrows a std::logic_error if the current type isn't of type array
Returns
The reference of the last json object in the array

◆ back() [2/2]

const json & matador::json::back ( ) const

Returns a const reference to the last json object of the array.

If the current type isn't array an exception is thrown.

Exceptions
std::logic_errorThrows a std::logic_error if the current type isn't of type array
Returns
The const reference of the last json object in the array

◆ begin() [1/2]

iterator matador::json::begin ( )

Returns the begin iterator of the json array or the json object. If the json isn't of type array or object this json object is return in the iterator

See also
json_iterator
Returns
The begin iterator

◆ begin() [2/2]

const_iterator matador::json::begin ( ) const

Returns the begin iterator of the json array or the json object. If the json isn't of type array or object this json object is return in the iterator

See also
json_iterator
Returns
The begin iterator

◆ clear()

void matador::json::clear ( )
inline

Clears the current json object.

All json values are deleted and the type is set to null

◆ contains()

bool matador::json::contains ( const std::string &  key) const

Returns true if json is of type object and contains the given key

Parameters
keyKey to check
Returns
True if key is available

◆ dump()

void matador::json::dump ( std::ostream &  out) const

Print the json object to the given ostream

Parameters
outThe stream to write on

◆ empty()

bool matador::json::empty ( ) const

Returns the corresponding result of empty() if json is of type array or of type object. If json is of type null then false is returned. For all other types true is returned.

Returns
True if array or object is empty

◆ end() [1/2]

iterator matador::json::end ( )

Returns the end iterator of the json array or the json object. If the json isn't of type array or object this json object is return in the iterator

See also
json_iterator
Returns
The end iterator

◆ end() [2/2]

const_iterator matador::json::end ( ) const

Returns the end iterator of the json array or the json object. If the json isn't of type array or object this json object is return in the iterator

See also
json_iterator
Returns
The end iterator

◆ erase() [1/2]

void matador::json::erase ( const std::string &  key)

Erases the json element with given key. If json isn't of type object nothing is done.

Parameters
keyThe key of the element to erase

◆ erase() [2/2]

void matador::json::erase ( std::size_t  i)

Erases the json element at the given position. If json isn't of type array nothing is done. If the given position is out of bounce an appropriate std::logic_error is thrown

Parameters
iThe position of the element to erase

◆ fits_to_type() [1/4]

template<class T >
bool matador::json::fits_to_type ( typename std::enable_if< std::is_convertible< T, std::string >::value >::type = 0) const
inline

Returns true if the given type is a string

Returns
True if given type is a string

◆ fits_to_type() [2/4]

template<class T >
bool matador::json::fits_to_type ( typename std::enable_if< std::is_floating_point< T >::value >::type = 0) const
inline

Returns true if the given type is a floating point type

Returns
True if given type is a floating point type

◆ fits_to_type() [3/4]

template<class T >
bool matador::json::fits_to_type ( typename std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value >::type = 0) const
inline

Returns true if the given type is an integral type

Returns
True if given type is an integral type

◆ fits_to_type() [4/4]

template<class T >
bool matador::json::fits_to_type ( typename std::enable_if< std::is_same< bool, T >::value >::type = 0) const
inline

Returns true if the given type is a boolean

Returns
True if given type is a boolean

◆ get() [1/2]

json & matador::json::get ( const std::string &  key)

Gets the json value at the given key. If the key couldn't be found an exception is thrown

Parameters
keyKey to find
Returns
The json value with the given key

◆ get() [2/2]

const json & matador::json::get ( const std::string &  key) const

Gets the json value at the given key. If the key couldn't be found an exception is thrown

Parameters
keyKey to find
Returns
The json value with the given key

◆ is_array()

bool matador::json::is_array ( ) const

Returns true if json type is an array

Returns
True if json type is an array

◆ is_boolean()

bool matador::json::is_boolean ( ) const

Returns true if json type is a boolean

Returns
True if json type is a boolean

◆ is_integer()

bool matador::json::is_integer ( ) const

Returns true if json type is an integral type

Returns
True if json type is a integral type

◆ is_null()

bool matador::json::is_null ( ) const

Returns true if json type is null

Returns
True if json type is null

◆ is_number()

bool matador::json::is_number ( ) const

Returns true if json type is a number

Returns
True if json type is a number

◆ is_object()

bool matador::json::is_object ( ) const

Returns true if json type is an object

Returns
True if json type is an object

◆ is_real()

bool matador::json::is_real ( ) const

Returns true if json type is a floating point type

Returns
True if json type is a floating point type

◆ is_string()

bool matador::json::is_string ( ) const

Returns true if json type is a string

Returns
True if json type is a string

◆ object()

static json matador::json::object ( )
static

Creates an empty json object.

Returns
An empty json object

◆ operator=() [1/3]

json & matador::json::operator= ( const json x)

Copy assign the given json object

Parameters
xThe json object to copy assign
Returns
The copied json object

◆ operator=() [2/3]

json & matador::json::operator= ( json &&  x)
noexcept

Move assign the given json object

Parameters
xThe json object to move assign
Returns
The move assigned json object

◆ operator=() [3/3]

template<class T >
json & matador::json::operator= ( val)
inline

Assigns a value to the json object On json type change the changes are adjusted

Template Parameters
TType of the value
Parameters
valValue to assign
Returns
The assigned json object

◆ operator[]() [1/4]

json & matador::json::operator[] ( const std::string &  key)

Get the json object of the given key. If the json type isn't object the type is changed to object and a json null value is inserted at key

Parameters
keyThe key of the requested value
Returns
The requested value for the given key

◆ operator[]() [2/4]

const json & matador::json::operator[] ( const std::string &  key) const

Get the json object of the given key. If the json type isn't object the type the object itself is returned (this)

Parameters
keyThe key of the requested value
Returns
The requested value for the given key

◆ operator[]() [3/4]

json & matador::json::operator[] ( std::size_t  i)

Get the json object at given index i if the json if of type array.

If the json isn't of type array a reference the this json is returned. If the index is out of bound a logic_error is thrown

Parameters
iIndex of the requested value
Returns
The requested value for the given index
Exceptions
std::logic_errorIf the index is out of bounce

◆ operator[]() [4/4]

const json & matador::json::operator[] ( std::size_t  i) const

Get the json object at given index i if the json if of type array.

If the json isn't of type array a reference the this json is returned. If the index is out of bound a logic_error is thrown

Parameters
iIndex of the requested value
Returns
The requested value for the given index
Exceptions
std::logic_errorIf the index is out of bounce

◆ push_back()

template<class T >
void matador::json::push_back ( const T &  val)
inline

Push back the given value to the json array

If the current type of the json object isn't array the type is changed to array.

Template Parameters
TThe type of the value to push back
Parameters
valThe value to push back

◆ reset() [1/5]

void matador::json::reset ( bool  b)

Resets the json value to a new boolean type value.

Parameters
bValue to set

◆ reset() [2/5]

void matador::json::reset ( const char *  val)
inline

Resets the json value to a new string type value.

Template Parameters
TType of the new value
Parameters
valValue to set

◆ reset() [3/5]

void matador::json::reset ( const std::string &  val)
inline

Resets the json value to a new string type value.

Template Parameters
TType of the new value
Parameters
valValue to set

◆ reset() [4/5]

template<class T >
void matador::json::reset ( val,
typename std::enable_if< std::is_floating_point< T >::value >::type = 0 
)
inline

Resets the json value to a new floating point (number) type value.

Template Parameters
TType of the new value
Parameters
valValue to set

◆ reset() [5/5]

template<class T >
void matador::json::reset ( val,
typename std::enable_if< std::is_integral< T >::value &&!std::is_same< bool, T >::value >::type = 0 
)
inline

Resets the json value to a new integer (number) type value.

Template Parameters
TType of the new value
Parameters
valValue to set

◆ size()

std::size_t matador::json::size ( ) const

Returns either the size of the json array or the size of a json object. If the type is null then 0 (zero) is returned. For all other types 1 (one) is returned

Returns
The size (count of elements)

◆ str() [1/2]

std::string matador::json::str ( ) const

Creates a string of the json object

Returns
Json as string

◆ str() [2/2]

std::string matador::json::str ( const json_format format) const

Creates a string of the json object with the given json format

Parameters
formatFormat object defining the json output
Returns
Json as string

◆ type()

json_type matador::json::type ( ) const

Returns the json type enum

Returns
Json type enum

Friends And Related Symbol Documentation

◆ operator!=

OOS_JSON_API bool operator!= ( const json a,
const json b 
)
friend

Compares equality of two json objects

Parameters
aFirst json object to compare
bSecond json object to compare
Returns
True if both json objects are not equal

◆ operator< [1/3]

OOS_JSON_API bool operator< ( const json a,
const json b 
)
friend

Compares two json objects if first object is less than second.

Parameters
aFirst json object to compare
bSecond json object to compare
Returns
True if first json object is less than second

◆ operator< [2/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator< ( const json a,
const T &  b 
)
friend

Less operator with a json value with a scalar value

Template Parameters
TType of the scalar value
Parameters
aThe json value to compare
bThe scalar value to compare
Returns
True if the json value is less than the scalar value

◆ operator< [3/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator< ( const T &  a,
const json b 
)
friend

Less operator with a scalar value with a json value

Template Parameters
TType of the scalar value
Parameters
aThe scalar value to compare
bThe json value to compare
Returns
True if the scalar value is less than the json value

◆ operator<<

OOS_JSON_API std::ostream & operator<< ( std::ostream &  out,
const json val 
)
friend

Print the json object to the given ostream

Parameters
outThe stream to write on
valThe json object to write
Returns
The written on stream

◆ operator<= [1/3]

OOS_JSON_API bool operator<= ( const json a,
const json b 
)
friend

Compares two json objects if first object is less than or equal second.

Parameters
aFirst json object to compare
bSecond json object to compare
Returns
True if first json object is less than or equal second

◆ operator<= [2/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator<= ( const json a,
const T &  b 
)
friend

Less or equal to operator with a json value with a scalar value

Template Parameters
TType of the scalar value
Parameters
aThe json value to compare
bThe scalar value to compare
Returns
True if the json value is less or equal to than the scalar value

◆ operator<= [3/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator<= ( const T &  a,
const json b 
)
friend

Less or equal to operator with a scalar value with a json value

Template Parameters
TType of the scalar value
Parameters
aThe scalar value to compare
bThe json value to compare
Returns
True if the scalar value is less or equal to than the json value

◆ operator==

OOS_JSON_API bool operator== ( const json a,
const json b 
)
friend

Compares equality of two json objects

Parameters
aFirst json object to compare
bSecond json object to compare
Returns
True if both json objects are equal

◆ operator> [1/3]

OOS_JSON_API bool operator> ( const json a,
const json b 
)
friend

Compares two json objects if first object is greater than second.

Parameters
aFirst json object to compare
bSecond json object to compare
Returns
True if first json object is greater than second

◆ operator> [2/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator> ( const json a,
const T &  b 
)
friend

Greater operator with a json value with a scalar value

Template Parameters
TType of the scalar value
Parameters
aThe json value to compare
bThe scalar value to compare
Returns
True if the json value is greater than the scalar value

◆ operator> [3/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator> ( const T &  a,
const json b 
)
friend

Greater operator with a scalar value with a json value

Template Parameters
TType of the scalar value
Parameters
aThe scalar value to compare
bThe json value to compare
Returns
True if the scalar value is greater than the json value

◆ operator>= [1/3]

OOS_JSON_API bool operator>= ( const json a,
const json b 
)
friend

Compares two json objects if first object is greater than or equal second.

Parameters
aFirst json object to compare
bSecond json object to compare
Returns
True if first json object is greater than or equal second

◆ operator>= [2/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator>= ( const json a,
const T &  b 
)
friend

Greater or equal to operator with a json value with a scalar value

Template Parameters
TType of the scalar value
Parameters
aThe json value to compare
bThe scalar value to compare
Returns
True if the json value is greater or equal to than the scalar value

◆ operator>= [3/3]

template<class T , typename std::enable_if< std::is_scalar< T >::value >::type >
bool operator>= ( const T &  a,
const json b 
)
friend

Greater or equal to operator with a scalar value with a json value

Template Parameters
TType of the scalar value
Parameters
aThe scalar value to compare
bThe json value to compare
Returns
True if the scalar value is greater or equal to than the json value

The documentation for this class was generated from the following file:
  • matador/json/json.hpp