jaffarCommon
Loading...
Searching...
No Matches
Typedefs | Functions
json.hpp File Reference

Contains common functions related to JSON manipulation. More...

#include "exceptions.hpp"
#include <json/single_include/nlohmann/json.hpp>
#include <stdlib.h>
Include dependency graph for json.hpp:

Go to the source code of this file.

Typedefs

typedef nlohmann::json jaffarCommon::json::object
 

Functions

__JAFFAR_COMMON_INLINE__ const void jaffarCommon::json::checkEntry (const object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const std::string jaffarCommon::json::getString (const object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const objectjaffarCommon::json::getObject (const object &json, const std::string &key)
 
template<typename T >
__JAFFAR_COMMON_INLINE__ const std::vector< T > jaffarCommon::json::getArray (const object &json, const std::string &key)
 
template<typename T >
__JAFFAR_COMMON_INLINE__ const T jaffarCommon::json::getNumber (const object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const bool jaffarCommon::json::getBoolean (const object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const std::string jaffarCommon::json::popString (object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const object jaffarCommon::json::popObject (object &json, const std::string &key)
 
template<typename T >
__JAFFAR_COMMON_INLINE__ const std::vector< TjaffarCommon::json::popArray (object &json, const std::string &key)
 
template<typename T >
__JAFFAR_COMMON_INLINE__ const T jaffarCommon::json::popNumber (object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const bool jaffarCommon::json::popBoolean (object &json, const std::string &key)
 
__JAFFAR_COMMON_INLINE__ const void jaffarCommon::json::checkEmpty (const object &json, const std::string &context)
 

Detailed Description

Contains common functions related to JSON manipulation.

Typedef Documentation

◆ object

typedef nlohmann::json jaffarCommon::json::object

Masks the use of nlohmann namespace in favor or an implementation-agnostic naming

Function Documentation

◆ checkEmpty()

__JAFFAR_COMMON_INLINE__ const void jaffarCommon::json::checkEmpty ( const object json,
const std::string &  context 
)

Throws if the given json object still contains any keys. Used after a parser has consumed (via the pop*() functions) every key it recognizes: anything left is an unrecognized key, which is reported by name together with the context (e.g. the configuration section the keys came from).

Parameters
[in]jsonThe (already-consumed) json object that should be empty
[in]contextA human-readable description of where the keys came from, used in the error

◆ checkEntry()

__JAFFAR_COMMON_INLINE__ const void jaffarCommon::json::checkEntry ( const object json,
const std::string &  key 
)

Verifies the given key exists in the given json object

Note
An exception will occur if the input is not a json object (e.g., is an array)
An exception will occur if the given key does not exist in the json object
Parameters
[in]jsonThe json object to look into
[in]keyThe key to look for inside the json object

◆ getArray()

template<typename T >
__JAFFAR_COMMON_INLINE__ const std::vector< T > jaffarCommon::json::getArray ( const object json,
const std::string &  key 
)

Returns the array of the specified type stored in the given key of the given json object

Note
An exception will occur if the input is not a json object (e.g., is an array)
An exception will occur if the given key does not exist in the json object
An exception will occur if the given key is not an array
Parameters
[in]jsonThe json object to look into
[in]keyThe key to look for inside the json object
Returns
A vector of the type specified containing all the elements of the array of key entry

◆ getBoolean()

__JAFFAR_COMMON_INLINE__ const bool jaffarCommon::json::getBoolean ( const object json,
const std::string &  key 
)

Returns the boolean stored the given key of the given json object

Note
An exception will occur if the input is not a json object (e.g., is an array)
An exception will occur if the given key does not exist in the json object
An exception will occur if the given key is not a boolean
Parameters
[in]jsonThe json object to look into
[in]keyThe key to look for inside the json object
Returns
The boolean stored in the key entry

◆ getNumber()

template<typename T >
__JAFFAR_COMMON_INLINE__ const T jaffarCommon::json::getNumber ( const object json,
const std::string &  key 
)

Returns the number of the specified type stored the given key of the given json object

Note
An exception will occur if the input is not a json object (e.g., is an array)
An exception will occur if the given key does not exist in the json object
An exception will occur if the given key is not a number
Parameters
[in]jsonThe json object to look into
[in]keyThe key to look for inside the json object
Returns
The number stored in the key entry

◆ getObject()

__JAFFAR_COMMON_INLINE__ const object & jaffarCommon::json::getObject ( const object json,
const std::string &  key 
)

Returns the object stored in the given key of the given json object

Note
An exception will occur if the input is not a json object (e.g., is an array)
An exception will occur if the given key does not exist in the json object
An exception will occur if the given key is not an object
Parameters
[in]jsonThe json object to look into
[in]keyThe key to look for inside the json object
Returns
A json object contained in the key

◆ getString()

__JAFFAR_COMMON_INLINE__ const std::string jaffarCommon::json::getString ( const object json,
const std::string &  key 
)

Returns the string stored in the given key of the given json object

Note
An exception will occur if the input is not a json object (e.g., is an array)
An exception will occur if the given key does not exist in the json object
An exception will occur if the given key is not a string
Parameters
[in]jsonThe json object to look into
[in]keyThe key to look for inside the json object
Returns
A string contained in the key

◆ popArray()

template<typename T >
__JAFFAR_COMMON_INLINE__ const std::vector< T > jaffarCommon::json::popArray ( object json,
const std::string &  key 
)

Like getArray(), but erases the key from the json object. See popString().

Parameters
[in,out]jsonThe json object to read from and erase the key from
[in]keyThe key to read and erase
Returns
The array stored in the key entry

◆ popBoolean()

__JAFFAR_COMMON_INLINE__ const bool jaffarCommon::json::popBoolean ( object json,
const std::string &  key 
)

Like getBoolean(), but erases the key from the json object. See popString().

Parameters
[in,out]jsonThe json object to read from and erase the key from
[in]keyThe key to read and erase
Returns
The boolean stored in the key entry

◆ popNumber()

template<typename T >
__JAFFAR_COMMON_INLINE__ const T jaffarCommon::json::popNumber ( object json,
const std::string &  key 
)

Like getNumber(), but erases the key from the json object. See popString().

Parameters
[in,out]jsonThe json object to read from and erase the key from
[in]keyThe key to read and erase
Returns
The number stored in the key entry

◆ popObject()

__JAFFAR_COMMON_INLINE__ const object jaffarCommon::json::popObject ( object json,
const std::string &  key 
)

Like getObject(), but returns a copy and erases the key from the parent. See popString().

Parameters
[in,out]jsonThe json object to read from and erase the key from
[in]keyThe key to read and erase
Returns
A copy of the object stored in the key entry

◆ popString()

__JAFFAR_COMMON_INLINE__ const std::string jaffarCommon::json::popString ( object json,
const std::string &  key 
)

Consuming variant of getString(): reads the string value, then erases the key from the (mutable) json object. After a parser has read every key it recognizes with the pop*() functions, any keys left in the object are unrecognized – pass the object to checkEmpty().

Parameters
[in,out]jsonThe json object to read from and erase the key from
[in]keyThe key to read and erase
Returns
The string stored in the key entry