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::Array Struct Reference

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

#include <Array.h>

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

Public Member Functions

 Array ()
 Create an empty JSON array/document. More...
 
 Array (Array &&rhs)
 Move CTOR. More...
 
Arrayoperator= (Array &&rhs)
 
const Valueat (std::size_t index) const
 Retrieves the value for the element at the specified index in this array. More...
 
Valueat (const std::size_t index)
 Non-constant version for modifying the value of specified element. More...
 
const Valueoperator[] (std::size_t index) const
 Retrieves the value for the element at the specified index from this array. More...
 
Valueoperator[] (std::size_t index)
 Non-constant version for modifying the value instance. More...
 
template<typename ValueType >
ValueType & get (std::size_t index)
 Return a reference to the value at the specified index in this array. More...
 
template<typename ValueType >
const ValueType & get (std::size_t index) const
 Constant version of template get method. More...
 
template<typename SimpleType >
SimpleType getValue (std::size_t index) const
 A convenience method to retrieve the value of a simple type. This cannot be implemented as a template specialisation of the get( std::size_t ) method since those methods return references to the value. More...
 
Type getType (std::size_t index) const
 Return the type of the value associated with the element at specified index in the array. More...
 
void emplace (std::size_t index, Value::Ptr &&value)
 
void emplace (Value::Ptr &&value)
 Emplace the specified value to the end of this array. More...
 
Arrayoperator+= (Value::Ptr &&value)
 Similar to emplace. More...
 
void erase (std::size_t index)
 
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 (std::size_t index) const
 Specialised method for retrieving the integral value stored in a spt::json::Number instance. More...
 
template<>
double getValue (std::size_t index) const
 Specialised method for retrieving the floating point value stored in a spt::json::Number instance. More...
 
template<>
bool getValue (std::size_t 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::vector< 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

- Public Types inherited from spt::json::Json< std::vector< Value::Ptr > >
typedef std::vector
< 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...
 
- Protected Member Functions inherited from spt::json::Json< std::vector< Value::Ptr > >
std::vector< Value::Ptr > & getElements ()
 
const std::vector< 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 array value/document.

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

Constructor & Destructor Documentation

spt::json::Array::Array ( )
inline

Create an empty JSON array/document.

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

Move CTOR.

Member Function Documentation

const Value& spt::json::Array::at ( std::size_t  index) const
inline

Retrieves the value for the element at the specified index in this array.

Will throw std::out_of_range exception if the index specified falls outside the range of indices for this array.

Parameters
indexThe index of the element to retrieve from this array.
Returns
The value for the element with specified name.
Value& spt::json::Array::at ( const std::size_t  index)
inline

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

void spt::json::Array::emplace ( std::size_t  index,
Value::Ptr &&  value 
)

Add the specified value at the specified index in the array. If the specified index is invalid, the value will be added to the end of the array.

Parameters
indexThe index at which to insert the value. Usual performance penalties will apply.
valueThe value to insert at the specified index.
void spt::json::Array::emplace ( Value::Ptr &&  value)
inline

Emplace the specified value to the end of this array.

Parameters
valueThe value to add to the end of the array.
void spt::json::Array::erase ( std::size_t  index)
inline
Parameters
indexErase the element at the specified index from this array.
template<typename ValueType >
ValueType& spt::json::Array::get ( std::size_t  index)
inline

Return a reference to the value at the specified index in this array.

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
indexThe index at which the value is stored.
Returns
The current value held in this object.
Exceptions
std::bad_castIf the datatype does not match the value type.
template<typename ValueType >
const ValueType& spt::json::Array::get ( std::size_t  index) const
inline

Constant version of template get method.

Type spt::json::Array::getType ( std::size_t  index) const
inline

Return the type of the value associated with the element at specified index in the array.

Will throw std::out_of_range exception if the index specified falls outside the range of indices for this array.

Parameters
indexThe index of the element to check in this array.
Returns
Returns the data type for the element.
template<typename SimpleType >
SimpleType spt::json::Array::getValue ( std::size_t  index) const
inline

A convenience method to retrieve the value of a simple type. This cannot be implemented as a template specialisation of the get( std::size_t ) 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 = array.get<spt::json::Number>( 1 );
if ( number.isFloat() ) const double dbl = number.getFloat();

becomes

const double dbl = array.getValue<double>( 1 );

Note: A specialisation for std::string is provided for the get( std::size_t ) method.

Template Parameters
SimpleTypeThe primitive represented by the element value.
Parameters
indexThe index of the element to retrieve from this array.
Returns
The simple value.
template<>
int64_t spt::json::Array::getValue ( std::size_t  index) const

Specialised method for retrieving the integral value stored in a spt::json::Number instance.

Parameters
indexThe index of the value in the array.
Returns
The integral value associated with the index.
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 integral.
template<>
double spt::json::Array::getValue ( std::size_t  index) const

Specialised method for retrieving the floating point value stored in a spt::json::Number instance.

Parameters
indexThe index of the value in the array.
Returns
The floating point value associated with the index.
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::Array::getValue ( std::size_t  index) const

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

Parameters
indexThe index of the value in the array.
Returns
The boolean value associated with the index.
Exceptions
std::bad_castIf the value at specified index is not of type spt::json::Value::Type::Boolean.
Array& spt::json::Array::operator+= ( Value::Ptr &&  value)
inline

Similar to emplace.

Array& spt::json::Array::operator= ( Array &&  rhs)
inline
const Value& spt::json::Array::operator[] ( std::size_t  index) const
inline

Retrieves the value for the element at the specified index from this array.

Note: Will throw std::out_of_range exception if no such element exists in the array. This is different from how std::vector behaves.

Parameters
indexThe index of the element to retrieve from this array.
Returns
The value for the element with specified name.
Value& spt::json::Array::operator[] ( std::size_t  index)
inline

Non-constant version for modifying the value instance.

void spt::json::Array::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::vector< Value::Ptr > >.

std::string spt::json::Array::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::vector< Value::Ptr > >.


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