Corelink CPP Client
Corelink C++ client library
 
Loading...
Searching...
No Matches
corelink::utils::json Class Reference

provides a wrapper over JSON libraries. Corelink supports a wrapper over RapidJSON, however, if the interface is maintained, another JSON library can be wrapped and used More...

#include <json.hpp>

Public Member Functions

 json (bool root_type_array=false)
 
 json (in< std::string > json_str)
 
 json (rvref< json > rhs) noexcept
 Move constructor.
 
jsonoperator= (rvref< json > rhs) noexcept
 Move assignment operator.
 
jsonoperator= (clvref< json > rhs)
 Copy assignment operator.
 
 json (clvref< json > rhs)
 Copy constructor.
 
 ~json ()=default
 
rapidjson::Document & operator() ()
 () operator overload.
 
clvref< rapidjson::Document > operator() () const
 () operator overload for const objects
 
int32_t get_int (in< std::string > name, int32_t def_val=0) const
 get the value associated with the key as an integer. Note that this function can throw if the underlying value is non-convertible to an integral type
 
int64_t get_int64 (in< std::string > name, int64_t def_val=0) const
 get the value associated with the key as an integer. Note that this function can throw if the underlying value is non-convertible to an integral type
 
std::string get_str (in< std::string > name, in< std::string > def_val="") const
 get the value associated with the key as std::string. Note that this function can throw if the underlying value is non-convertible to string type
 
double get_double (in< std::string > name, double def_val=0.0) const
 get the value associated with the key as double. Note that this function can throw if the underlying value is non-convertible to floating point type
 
bool get_bool (in< std::string > name, bool def_val=false) const
 get the value associated with the key as bool. Note that this function can throw if the underlying value is non-convertible to boolean type
 
uint32_t get_uint (in< std::string > name, uint32_t def_val=false) const
 get the value associated with the key as an unsigned integer. Note that this function can throw if the underlying value is non-convertible to an integral type
 
uint64_t get_uint64 (in< std::string > name, uint64_t def_val=false) const
 get the value associated with the key as an unsigned integer. Note that this function can throw if the underlying value is non-convertible to an integral type
 
bool try_parse (const std::string &json_str, std::string &error) noexcept
 
std::string to_string (bool pretty_print=false) const
 
template<class T >
jsonappend (const std::vector< std::map< std::string, T > > &arr_items)
 Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is scalar native type.
 
jsonappend (const std::vector< std::map< std::string, std::string > > &arr_items)
 Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is string type.
 
template<class T >
jsonappend (const std::vector< std::map< std::string, std::vector< T > > > &arr_items)
 Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is an array of scalar native type values.
 
jsonappend (const std::vector< std::map< std::string, std::vector< std::string > > > &arr_items)
 Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is an array of string type values.
 
template<class T >
jsonappend (const std::string &key, const T &val)
 Append a key value pair to the root JSON object where the value type is a native type.
 
jsonappend (const std::string &key, const std::string &val)
 Append a key value pair to the root JSON object where the value type is a string.
 
template<class T >
jsonappend (const std::string &key, const std::vector< T > &val)
 Append a key value pair to the root JSON object where the value type is an array of native type values.
 
jsonappend (const std::string &key, const std::vector< std::string > &val)
 Append a key value pair to the root JSON object where the value type is an array of strings.
 
template<class T >
jsonappend (const std::map< std::string, T > &kv_items)
 Append multiple key value pair to the root JSON object where the value type is a native type.
 
jsonappend (const std::map< std::string, std::string > &kv_items)
 Append multiple key value pair to the root JSON object where the value type is value of string type.
 
template<class T >
jsonappend (const std::map< std::string, std::vector< T > > &kv_items)
 Append multiple key value pair to the root JSON object where the value type is an array of native type values.
 
jsonappend (const std::map< std::string, std::vector< std::string > > &kv_items)
 Append multiple key value pair to the root JSON object where the value type is an array of string type values.
 
CORELINK_CPP_ATTR_MAYBE_UNUSED jsonremove (const std::string &key)
 
CORELINK_CPP_ATTR_MAYBE_UNUSED jsonclear ()
 

Detailed Description

provides a wrapper over JSON libraries. Corelink supports a wrapper over RapidJSON, however, if the interface is maintained, another JSON library can be wrapped and used

Constructor & Destructor Documentation

◆ json() [1/4]

corelink::utils::json::json ( bool root_type_array = false)
inlineexplicit

Constructor

Parameters
root_type_arrayif true, the default object created at the root level will be a JSON array. if false, the default object created at the root level will be a JSON object. The default value is false

◆ json() [2/4]

corelink::utils::json::json ( in< std::string > json_str)
inlineexplicit

Constructor

Parameters
json_strinitialize the JSON object with a JSON string

This constructor variant will try to parse the input string as JSON. However, if the string passed is not valid JSON, it will throw

◆ json() [3/4]

corelink::utils::json::json ( rvref< json > rhs)
inlinenoexcept

Move constructor.

Parameters
rhsjson object to move from

◆ json() [4/4]

corelink::utils::json::json ( clvref< json > rhs)
inline

Copy constructor.

Parameters
rhsobject to copy document from

◆ ~json()

corelink::utils::json::~json ( )
default

Default destructor

Member Function Documentation

◆ append() [1/12]

json & corelink::utils::json::append ( const std::map< std::string, std::string > & kv_items)
inline

Append multiple key value pair to the root JSON object where the value type is value of string type.

Note
Note that if the root JSON object is an Array, this function will add the key value pairs as an object to the root array
Parameters
kv_itemskey value pairs that you wish to add to the root JSON object.
Returns
reference to itself

◆ append() [2/12]

json & corelink::utils::json::append ( const std::map< std::string, std::vector< std::string > > & kv_items)
inline

Append multiple key value pair to the root JSON object where the value type is an array of string type values.

Note
Note that if the root JSON object is an Array, this function will add the key value pairs as an object to the root array
Parameters
kv_itemskey value pairs that you wish to add to the root JSON object.
Returns
reference to itself.

◆ append() [3/12]

template<class T >
json & corelink::utils::json::append ( const std::map< std::string, std::vector< T > > & kv_items)
inline

Append multiple key value pair to the root JSON object where the value type is an array of native type values.

Note
Note that if the root JSON object is an Array, this function will add the key value pairs as an object to the root array
Template Parameters
Tnative type
Parameters
kv_itemskey value pairs that you wish to add to the root JSON object.
Returns
reference to itself.

◆ append() [4/12]

template<class T >
json & corelink::utils::json::append ( const std::map< std::string, T > & kv_items)
inline

Append multiple key value pair to the root JSON object where the value type is a native type.

Note
Note that if the root JSON object is an Array, this function will add the key value pairs as an object to the root array
Template Parameters
Tnative value type
Parameters
kv_itemskey value pairs of items to be added to the root object
Returns
reference to itself

◆ append() [5/12]

json & corelink::utils::json::append ( const std::string & key,
const std::string & val )
inline

Append a key value pair to the root JSON object where the value type is a string.

Note
Note that if the root JSON object is an Array, this function will add the key value pair as an object to the root array
Parameters
keyjson object key
valstring type value
Returns
reference to itself

◆ append() [6/12]

json & corelink::utils::json::append ( const std::string & key,
const std::vector< std::string > & val )
inline

Append a key value pair to the root JSON object where the value type is an array of strings.

Note
Note that if the root JSON object is an Array, this function will add the key value pair as an object to the root array
Parameters
keystring key
valarray of strings
Returns
reference to itself

◆ append() [7/12]

template<class T >
json & corelink::utils::json::append ( const std::string & key,
const std::vector< T > & val )
inline

Append a key value pair to the root JSON object where the value type is an array of native type values.

Note
Note that if the root JSON object is an Array, this function will add the key value pair as an object to the root array
Template Parameters
nativevalue type
Parameters
keystring key
valarray of native type values
Returns
reference to itself

◆ append() [8/12]

template<class T >
json & corelink::utils::json::append ( const std::string & key,
const T & val )
inline

Append a key value pair to the root JSON object where the value type is a native type.

Note
If the root JSON object is an Array, this function will add the key value pair as an object to the root array
Template Parameters
Tnative type
Parameters
keyjson object key
valjson object native type value
Returns
reference to itself

◆ append() [9/12]

json & corelink::utils::json::append ( const std::vector< std::map< std::string, std::string > > & arr_items)
inline

Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is string type.

Parameters
arr_itemsvector of key value pairs which will be appended to the root JSON
Returns
reference to itself.

◆ append() [10/12]

json & corelink::utils::json::append ( const std::vector< std::map< std::string, std::vector< std::string > > > & arr_items)
inline

Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is an array of string type values.

Parameters
arr_itemsvector of key value pairs which will be appended to the root JSON
Returns
reference to itself.

◆ append() [11/12]

template<class T >
json & corelink::utils::json::append ( const std::vector< std::map< std::string, std::vector< T > > > & arr_items)
inline

Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is an array of scalar native type values.

Template Parameters
Tnative type. please do not specify custom types here as JSON values don't support it.
Parameters
arr_itemsvector of key value pairs which will be appended to the root JSON
Returns
reference to itself.

◆ append() [12/12]

template<class T >
json & corelink::utils::json::append ( const std::vector< std::map< std::string, T > > & arr_items)
inline

Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair is scalar native type.

Template Parameters
Tnative type. please do not specify custom types here as JSON values don't support it.
Parameters
arr_itemsvector of key value pairs which will be appended to the root JSON
Returns
reference to itself.

◆ clear()

CORELINK_CPP_ATTR_MAYBE_UNUSED json & corelink::utils::json::clear ( )
inline

Remove all elements from the root JSON document

Returns
reference to itself

◆ get_bool()

bool corelink::utils::json::get_bool ( in< std::string > name,
bool def_val = false ) const
inline

get the value associated with the key as bool. Note that this function can throw if the underlying value is non-convertible to boolean type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
boolean value associated with the key

◆ get_double()

double corelink::utils::json::get_double ( in< std::string > name,
double def_val = 0.0 ) const
inline

get the value associated with the key as double. Note that this function can throw if the underlying value is non-convertible to floating point type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
signed double value

◆ get_int()

int32_t corelink::utils::json::get_int ( in< std::string > name,
int32_t def_val = 0 ) const
inline

get the value associated with the key as an integer. Note that this function can throw if the underlying value is non-convertible to an integral type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
signed 32 bit integer value

◆ get_int64()

int64_t corelink::utils::json::get_int64 ( in< std::string > name,
int64_t def_val = 0 ) const
inline

get the value associated with the key as an integer. Note that this function can throw if the underlying value is non-convertible to an integral type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
signed 64 bit integer value

◆ get_str()

std::string corelink::utils::json::get_str ( in< std::string > name,
in< std::string > def_val = "" ) const
inline

get the value associated with the key as std::string. Note that this function can throw if the underlying value is non-convertible to string type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
std::string object with string representation of the value

◆ get_uint()

uint32_t corelink::utils::json::get_uint ( in< std::string > name,
uint32_t def_val = false ) const
inline

get the value associated with the key as an unsigned integer. Note that this function can throw if the underlying value is non-convertible to an integral type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
unsigned 32 bit integer value

◆ get_uint64()

uint64_t corelink::utils::json::get_uint64 ( in< std::string > name,
uint64_t def_val = false ) const
inline

get the value associated with the key as an unsigned integer. Note that this function can throw if the underlying value is non-convertible to an integral type

Parameters
namekey to look up in the object
def_valin case key is not found, specify a default value
Returns
unsigned 64 bit integer value

◆ operator()() [1/2]

rapidjson::Document & corelink::utils::json::operator() ( )
inline

() operator overload.

Returns
a reference to the underlying JSON object. It is advisable to use this only where you want to query the JSON object directly. Use the member functions of json to mutate the elements.

◆ operator()() [2/2]

clvref< rapidjson::Document > corelink::utils::json::operator() ( ) const
inline

() operator overload for const objects

Returns
a const reference to the underlying JSON document object. It is advisable to use this only where you want to query the JSON object directly. Use the member functions of json to mutate the elements. Note that this function returns a const reference, so mutability and certain member accesses will be limited

◆ operator=() [1/2]

json & corelink::utils::json::operator= ( clvref< json > rhs)
inline

Copy assignment operator.

Parameters
rhsobject to copy document from
Returns
reference to the current object to which document was copied to

◆ operator=() [2/2]

json & corelink::utils::json::operator= ( rvref< json > rhs)
inlinenoexcept

Move assignment operator.

Parameters
rhsjson object to move from
Returns
reference to the current object to which document was moved to

◆ remove()

CORELINK_CPP_ATTR_MAYBE_UNUSED json & corelink::utils::json::remove ( const std::string & key)
inline

Remove an element from the root JSON object based on a key, if found

Note
This function only works when the root JSON type is an object
Parameters
keykey of the entry to drop
Returns
reference to itself

◆ to_string()

std::string corelink::utils::json::to_string ( bool pretty_print = false) const
inline

Serialise the JSON object to string for transport or redirect to streams

Note
It is important to note that serializing the JSON to a string will be in memory, so be careful when you do this for larger objects.
Parameters
pretty_printSet to true if you wish to pretty print JSON with indentation. Defaulted to false
Returns
underlying JSON object as a string

◆ try_parse()

bool corelink::utils::json::try_parse ( const std::string & json_str,
std::string & error )
inlinenoexcept

Parse a JSON string as JSON object

Parameters
json_strJSON string
errorerror string if parse results in an error. empty if the conversion is successful
Returns
true if JSON parse was ok. false if it failed. please inspect error string if ret val is false.

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