SPT JSON API
1.0.0
Developed by Sans Pareil Technologies, Inc. (SPT)
|
Represents a JSON array
value/document.
More...
#include <Array.h>
Public Member Functions | |
Array () | |
Create an empty JSON array/document. More... | |
Array (Array &&rhs) | |
Move CTOR. More... | |
Array & | operator= (Array &&rhs) |
const Value & | at (std::size_t index) const |
Retrieves the value for the element at the specified index in this array. More... | |
Value & | at (const std::size_t index) |
Non-constant version for modifying the value of specified element. More... | |
const Value & | operator[] (std::size_t index) const |
Retrieves the value for the element at the specified index from this array. More... | |
Value & | operator[] (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... | |
Array & | operator+= (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... | |
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 | |
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< Value > | Ptr |
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... | |
Represents a JSON array
value/document.
|
inline |
Create an empty JSON array/document.
|
inline |
Move CTOR.
|
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.
index | The index of the element to retrieve from this array. |
|
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.
index | The index at which to insert the value. Usual performance penalties will apply. |
value | The value to insert at the specified index. |
|
inline |
Emplace the specified value to the end of this array.
value | The value to add to the end of the array. |
|
inline |
index | Erase the element at the specified index from this array. |
|
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.
DataType | The data type for the value as stored. |
index | The index at which the value is stored. |
std::bad_cast | If the datatype does not match the value type. |
|
inline |
Constant version of template get method.
|
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.
index | The index of the element to check in this array. |
|
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.
SimpleType | The primitive represented by the element value. |
index | The index of the element to retrieve from this array. |
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.
index | The index of the value in the array. |
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 integral. |
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.
index | The index of the value in the array. |
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::Array::getValue | ( | std::size_t | index | ) | const |
Specialised method for retrieving the boolean value stored in a spt::json::Boolean instance.
index | The index of the value in the array. |
std::bad_cast | If the value at specified index is not of type spt::json::Value::Type::Boolean. |
|
inline |
Similar to emplace.
|
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.
index | The index of the element to retrieve from this array. |
|
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::vector< 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::vector< Value::Ptr > >.