SPT JSON API
1.0.0
Developed by Sans Pareil Technologies, Inc. (SPT)
|
Represents a JSON object
value/document.
More...
#include <Object.h>
Public Types | |
typedef std::unordered_set < std::string > | Names |
Type that represents the element names in an object. More... | |
typedef std::pair< std::string, Value::Ptr > | Pair |
Typedef for a pair representing the name/value pair. More... | |
Public Types inherited from spt::json::Json< std::unordered_map< std::string, Value::Ptr > > | |
typedef std::unordered_map < std::string, Value::Ptr > ::const_iterator | Iterator |
The iterator used to iterate over the elements of the object. More... | |
Public Types inherited from spt::json::Value | |
enum | Type : int8_t { Type::String, Type::Number, Type::Object, Type::Array, Type::Boolean, Type::Null } |
Enumeration of JSON data types. More... | |
typedef std::unique_ptr< Value > | Ptr |
Unique pointer that holds a value instance. More... | |
Public Member Functions | |
Object () | |
Create an empty JSON object/document. More... | |
Object (Object &&rhs) | |
Move CTOR. More... | |
Object & | operator= (Object &&rhs) |
Move assignment operator. More... | |
Names | getNames () const |
bool | contains (const std::string &name) const |
Check to see if an element with the specified name exists in this object. More... | |
const Value & | at (const std::string &name) const |
Retrieves the value for the element with the specified name from this object. More... | |
Value & | at (const std::string &name) |
Non-constant version for modifying the value of specified element. More... | |
const Value & | operator[] (const std::string &name) const |
Retrieves the value for the element with the specified name from this object. More... | |
Value & | operator[] (const std::string &name) |
Non-constant version for modifying the value instance. More... | |
template<typename ValueType > | |
ValueType & | get (const std::string &name) |
Return a reference to the value for the mapping with the name in this object. More... | |
template<typename ValueType > | |
const ValueType & | get (const std::string &name) const |
Constant version of template get method. More... | |
template<typename SimpleType > | |
SimpleType | getValue (const std::string &name) const |
A convenience method to retrieve the value of a simple type. This cannot be implemented as a template specialisation of the get( const std::string& ) method since those methods return references to the value. More... | |
Type | getType (const std::string &name) const |
Return the type of the value associated with the associated name in the object. More... | |
void | emplace (std::string name, Value::Ptr &&value) |
Emplace the specified name/value pairs into this object. More... | |
Object & | operator+= (Pair &&pair) |
Similar to emplace. More... | |
void | erase (const std::string &name) |
void | toJson (std::ostream &stream, bool prettyPrint) const override |
Write the data in this instance to the specified stream in JSON format. More... | |
std::string | toString (bool prettyPrint) const override |
Return the JSON string representation of the data in this instance. More... | |
template<> | |
int64_t | getValue (const std::string &name) const |
Specialised method for retrieving the integral value stored in a spt::json::Number instance. More... | |
template<> | |
double | getValue (const std::string &name) const |
Specialised method for retrieving the floating point value stored in a spt::json::Number instance. More... | |
template<> | |
bool | getValue (const std::string &index) const |
Specialised method for retrieving the boolean value stored in a spt::json::Boolean instance. More... | |
Public Member Functions inherited from spt::json::Json< std::unordered_map< std::string, Value::Ptr > > | |
Json (Type type) | |
CTOR for initialising base class with specified type. More... | |
Json (Json &&rhs) | |
Move CTOR. More... | |
Json & | operator= (Json &&rhs) |
Move assignment operator. More... | |
Iterator | begin () const |
Iterator | end () const |
bool | empty () const |
std::size_t | size () const |
void | clear () |
Clear all the data stored in this instance. More... | |
Public Member Functions inherited from spt::json::Value | |
virtual | ~Value () |
Virtual DTOR for sub-classes. More... | |
Value (Value &&rhs) | |
Move CTOR. More... | |
Value & | operator= (Value &&rhs) |
Move assignment operator. More... | |
Value (const Value &)=delete | |
Copy CTOR. Deleted. More... | |
Value & | operator= (Value &)=delete |
Copy assignment operator deleted. More... | |
Type | getType () const |
Return the type of data that is represented by this instance. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from spt::json::Json< std::unordered_map< std::string, Value::Ptr > > | |
std::unordered_map < std::string, Value::Ptr > & | getElements () |
const std::unordered_map < std::string, Value::Ptr > & | getElements () const |
Constant version that returns the elements stored in this instance. More... | |
Protected Member Functions inherited from spt::json::Value | |
Value (Type t) | |
CTOR with specified type. More... | |
Represents a JSON object
value/document.
typedef std::unordered_set<std::string> spt::json::Object::Names |
Type that represents the element names in an object.
typedef std::pair<std::string, Value::Ptr> spt::json::Object::Pair |
Typedef for a pair representing the name/value pair.
|
inline |
Create an empty JSON object/document.
|
inline |
Move CTOR.
const Value& spt::json::Object::at | ( | const std::string & | name | ) | const |
Retrieves the value for the element with the specified name from this object.
Will throw std::out_of_range
exception if no such element exists in the document.
name | The name of the element to retrieve from this object. |
Value& spt::json::Object::at | ( | const std::string & | name | ) |
Non-constant version for modifying the value of specified element.
|
inline |
Check to see if an element with the specified name exists in this object.
name | The name of the element to check in this object. |
true
if an element exists. void spt::json::Object::emplace | ( | std::string | name, |
Value::Ptr && | value | ||
) |
Emplace the specified name/value pairs into this object.
Unlike std::map
emplace will replace an existing name/value pair if a mapping with the specified name already exists.
name | The name for the new name/value mapping in this object. |
value | The value to associate with the name. |
|
inline |
name | Erase the element with specified name from this object. |
|
inline |
Return a reference to the value for the mapping with the name in this object.
If the template type specified does not match the value data type, or if no mapping exists an exception is thrown.
DataType | The data type for the value as stored. |
name | The name to which the value is mapped. |
std::runtime_error | If no mapping exists for the given name. |
std::bad_cast | If the datatype does not match the value type. |
|
inline |
Constant version of template get method.
Names spt::json::Object::getNames | ( | ) | const |
Type spt::json::Object::getType | ( | const std::string & | name | ) | const |
Return the type of the value associated with the associated name in the object.
Will throw std::out_of_range
exception if no such element exists in the document.
name | The name of the element to check in this object. |
|
inline |
A convenience method to retrieve the value of a simple type. This cannot be implemented as a template specialisation of the get( const std::string& ) method since those methods return references to the value.
Template specialisations are provided for the standard simple types (int64_t
, double
and bool
) which will work as expected. This method makes it easier to retrieve simple values than the full syntax. For example:
const spt::json::Number& number = object.get<spt::json::Number>( "numVal" ); if ( number.isFloat() ) const double dbl = number.getFloat();
becomes
const double dbl = array.getValue<double>( "numVal" );
Note: A specialisation for std::string
is provided for the get( const std::string& ) method.
SimpleType | The primitive represented by the element value. |
name | The name to which the value is mapped. |
int64_t spt::json::Object::getValue | ( | const std::string & | name | ) | const |
Specialised method for retrieving the integral value stored in a spt::json::Number instance.
name | The name to which the number value is mapped. |
std::bad_cast | If the mapping for specified name is not of type spt::json::Value::Type::Number or if the value stored in the number instance is not integral. |
double spt::json::Object::getValue | ( | const std::string & | name | ) | const |
Specialised method for retrieving the floating point value stored in a spt::json::Number instance.
name | The name to which the number value is mapped. |
std::bad_cast | If the value at specified index is not of type spt::json::Value::Type::Number or if the value stored in the number instance is not floating point. |
bool spt::json::Object::getValue | ( | const std::string & | index | ) | const |
Specialised method for retrieving the boolean value stored in a spt::json::Boolean instance.
name | The name to which the number value is mapped. |
std::bad_cast | If the mapping for specified name is not of type spt::json::Value::Type::Boolean. |
|
inline |
Retrieves the value for the element with the specified name from this object.
Note: Will throw std::out_of_range
exception if no such element exists in the document. This is different from how std::unordered_map
behaves.
name | The name of the element to check in this object. |
|
inline |
Non-constant version for modifying the value instance.
|
overridevirtual |
Write the data in this instance to the specified stream in JSON format.
stream | The output stream to which the JSON data is to be written (file, buffer, ...) |
prettyPrint | Flag that indicates whether the JSON data should be formatted for ease of human reading. |
true
if the data was written without errors. Implements spt::json::Json< std::unordered_map< std::string, Value::Ptr > >.
|
overridevirtual |
Return the JSON string representation of the data in this instance.
prettyPrint | Flag that indicates whether the string representation should be formatted for ease of human reading. |
Implements spt::json::Json< std::unordered_map< std::string, Value::Ptr > >.