SPT JSON API  1.0.0
Developed by Sans Pareil Technologies, Inc. (SPT)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
spt::json::Object Struct Reference

Represents a JSON object value/document. More...

#include <Object.h>

+ Inheritance diagram for spt::json::Object:
+ Collaboration diagram for spt::json::Object:

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< ValuePtr
 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...
 
Objectoperator= (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 Valueat (const std::string &name) const
 Retrieves the value for the element with the specified name from this object. More...
 
Valueat (const std::string &name)
 Non-constant version for modifying the value of specified element. More...
 
const Valueoperator[] (const std::string &name) const
 Retrieves the value for the element with the specified name from this object. More...
 
Valueoperator[] (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...
 
Objectoperator+= (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...
 
Jsonoperator= (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...
 
Valueoperator= (Value &&rhs)
 Move assignment operator. More...
 
 Value (const Value &)=delete
 Copy CTOR. Deleted. More...
 
Valueoperator= (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...
 

Detailed Description

Represents a JSON object value/document.

Date
Created 2013/05/02 19:12
Author
Rakesh Vidyadharan
Version
Id
Object.h 5682 2014-10-27 17:03:21Z rakesh

Member Typedef Documentation

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.

Constructor & Destructor Documentation

spt::json::Object::Object ( )
inline

Create an empty JSON object/document.

spt::json::Object::Object ( Object &&  rhs)
inline

Move CTOR.

Member Function Documentation

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.

Parameters
nameThe name of the element to retrieve from this object.
Returns
The value for the element with specified name.
Value& spt::json::Object::at ( const std::string &  name)

Non-constant version for modifying the value of specified element.

bool spt::json::Object::contains ( const std::string &  name) const
inline

Check to see if an element with the specified name exists in this object.

Parameters
nameThe name of the element to check in this object.
Returns
Return 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.

Parameters
nameThe name for the new name/value mapping in this object.
valueThe value to associate with the name.
void spt::json::Object::erase ( const std::string &  name)
inline
Parameters
nameErase the element with specified name from this object.
template<typename ValueType >
ValueType& spt::json::Object::get ( const std::string &  name)
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.

Template Parameters
DataTypeThe data type for the value as stored.
Parameters
nameThe name to which the value is mapped.
Returns
The current value held in this object.
Exceptions
std::runtime_errorIf no mapping exists for the given name.
std::bad_castIf the datatype does not match the value type.
template<typename ValueType >
const ValueType& spt::json::Object::get ( const std::string &  name) const
inline

Constant version of template get method.

Names spt::json::Object::getNames ( ) const
Returns
Return the set of element names in this object.
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.

Parameters
nameThe name of the element to check in this object.
Returns
Returns the data type for the element.
template<typename SimpleType >
SimpleType spt::json::Object::getValue ( const std::string &  name) const
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.

Template Parameters
SimpleTypeThe primitive represented by the element value.
Parameters
nameThe name to which the value is mapped.
Returns
The simple value.
template<>
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.

Parameters
nameThe name to which the number value is mapped.
Returns
The integral value associated with the name.
Exceptions
std::bad_castIf 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.
template<>
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.

Parameters
nameThe name to which the number value is mapped.
Returns
The floating point value associated with the name.
Exceptions
std::bad_castIf 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.
template<>
bool spt::json::Object::getValue ( const std::string &  index) const

Specialised method for retrieving the boolean value stored in a spt::json::Boolean instance.

Parameters
nameThe name to which the number value is mapped.
Returns
The boolean value associated with the name.
Exceptions
std::bad_castIf the mapping for specified name is not of type spt::json::Value::Type::Boolean.
Object& spt::json::Object::operator+= ( Pair &&  pair)
inline

Similar to emplace.

Object& spt::json::Object::operator= ( Object &&  rhs)
inline

Move assignment operator.

const Value& spt::json::Object::operator[] ( const std::string &  name) const
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.

Parameters
nameThe name of the element to check in this object.
Returns
The value for the element with specified name.
Value& spt::json::Object::operator[] ( const std::string &  name)
inline

Non-constant version for modifying the value instance.

void spt::json::Object::toJson ( std::ostream &  stream,
bool  prettyPrint 
) const
overridevirtual

Write the data in this instance to the specified stream in JSON format.

Parameters
streamThe output stream to which the JSON data is to be written (file, buffer, ...)
prettyPrintFlag that indicates whether the JSON data should be formatted for ease of human reading.
Returns
Return true if the data was written without errors.
See also
spt::json::io::StreamWriter::write

Implements spt::json::Json< std::unordered_map< std::string, Value::Ptr > >.

std::string spt::json::Object::toString ( bool  prettyPrint) const
overridevirtual

Return the JSON string representation of the data in this instance.

Parameters
prettyPrintFlag that indicates whether the string representation should be formatted for ease of human reading.
Returns
The JSON string representing the data in this instance.
See also
spt::json::io::StringWriter::write

Implements spt::json::Json< std::unordered_map< std::string, Value::Ptr > >.


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