Simpleson
Classes | Public Member Functions | Static Public Member Functions | List of all members
json::jobject Class Reference

The class used for manipulating JSON objects and arrays. More...

#include <json.h>

Classes

class  const_proxy
 Represents an entry as a constant proxy to the value. More...
 
class  const_value
 Represents an entry as a constant value. More...
 
class  entry
 Representation of a value in the object. More...
 
class  proxy
 A proxy that allows modification of the value. More...
 

Public Member Functions

 jobject (bool array=false)
 Default constructor. More...
 
 jobject (const jobject &other)
 Copy constructor.
 
virtual ~jobject ()
 Destructor.
 
bool is_array () const
 Flag for differentiating objects and arrays. More...
 
size_t size () const
 Returns the number of entries in the JSON object or array.
 
void clear ()
 Clears the JSON object or array.
 
bool operator== (const json::jobject other) const
 Comparison operator. More...
 
bool operator!= (const json::jobject other) const
 Comparison operator.
 
jobjectoperator= (const jobject rhs)
 Assignment operator.
 
jobjectoperator+= (const kvp &other)
 Appends a key-value pair to a JSON object. More...
 
jobjectoperator+= (const jobject &other)
 Appends one JSON object to another.
 
jobject operator+ (jobject &other)
 Merges two JSON objects.
 
bool has_key (const std::string &key) const
 Determines if an object contains a key. More...
 
void set (const std::string &key, const std::string &value)
 Sets the value assocaited with the key. More...
 
std::string get (const size_t index) const
 Returns the serialized value at a given index. More...
 
std::string get (const std::string &key) const
 Returns the serialized value associated with a key. More...
 
void remove (const std::string &key)
 Removes the entry associated with the key. More...
 
void remove (const size_t index)
 Removes the entry at the specified index. More...
 
virtual jobject::proxy operator[] (const std::string key)
 Returns an element of the JSON object. More...
 
virtual const jobject::const_proxy operator[] (const std::string key) const
 Returns an element of the JSON object. More...
 
const jobject::const_value array (const size_t index) const
 Returns the value of an element in an array. More...
 
 operator std::string () const
 
std::string as_string () const
 Serialzes the object or array. More...
 
std::string pretty (unsigned int indent_level=0) const
 Returns a pretty (multi-line indented) serialzed representation of the object or array. More...
 

Static Public Member Functions

static jobject parse (const char *input)
 Parses a serialized JSON string. More...
 
static jobject parse (const std::string input)
 Parses a serialized JSON string. More...
 
static bool tryparse (const char *input, jobject &output)
 

Detailed Description

The class used for manipulating JSON objects and arrays.

Examples
jobject.cpp, objectarray.cpp, and rootarray.cpp.

Constructor & Destructor Documentation

◆ jobject()

json::jobject::jobject ( bool  array = false)
inline

Default constructor.

Parameters
arrayIf true, the instance is initialized as an array. If false, the instance is initalized as an object.

Member Function Documentation

◆ array()

const jobject::const_value json::jobject::array ( const size_t  index) const
inline

Returns the value of an element in an array.

Parameters
indexThe index of the element to be returned
Returns
A proxy for the value
Note
While this method is intended for JSON arrays, this method is also valid for JSON objects
Examples
rootarray.cpp.

◆ as_string()

std::string json::jobject::as_string ( ) const
inline

Serialzes the object or array.

Note
The serialized object or array will be in the most compact form and will not contain any extra white space, even if the serialized string used to generate the object or array contained extra white space.
Examples
jobject.cpp.

◆ get() [1/2]

std::string json::jobject::get ( const size_t  index) const
inline

Returns the serialized value at a given index.

Parameters
indexThe index of the desired element
Returns
A serialized representation of the value at the given index
Examples
jobject.cpp.

◆ get() [2/2]

std::string json::jobject::get ( const std::string &  key) const
inline

Returns the serialized value associated with a key.

Parameters
keyThe key for the desired element
Returns
A serialized representation of the value associated with the key
Exceptions
json::invalid_keyException thrown if the key does not exist in the object or the object actually represents a JSON array

◆ has_key()

bool json::jobject::has_key ( const std::string &  key) const
inline

Determines if an object contains a key.

Parameters
keyThe key to check for
Returns
True if the object contains the provided key and false if the object does not contain the key
Note
If the object represents a JSON array, then this function will always return false
Examples
jobject.cpp.

◆ is_array()

bool json::jobject::is_array ( ) const
inline

Flag for differentiating objects and arrays.

Returns
True if the instance represents an array, false if the instance represents an object
Examples
jobject.cpp.

◆ operator std::string()

json::jobject::operator std::string ( ) const

◆ operator+=()

jobject & json::jobject::operator+= ( const kvp other)
inline

Appends a key-value pair to a JSON object.

Exceptions
json::parsing_errorThrown if the key-value is incompatable with the existing object (object/array mismatch)

◆ operator==()

bool json::jobject::operator== ( const json::jobject  other) const
inline

Comparison operator.

Todo:
Currently, the comparison just seralizes both objects and compares the strings, which is probably not as efficent as it could be

◆ operator[]() [1/2]

virtual jobject::proxy json::jobject::operator[] ( const std::string  key)
inlinevirtual

Returns an element of the JSON object.

Parameters
keyThe key of the element to be returned
Returns
A proxy for the value paired with the key
Exceptions
json::invalid_keyException thrown if the object is actually a JSON array

◆ operator[]() [2/2]

virtual const jobject::const_proxy json::jobject::operator[] ( const std::string  key) const
inlinevirtual

Returns an element of the JSON object.

Parameters
keyThe key of the element to be returned
Returns
A proxy for the value paired with the key
Exceptions
json::invalid_keyException thrown if the object is actually a JSON array

◆ parse() [1/2]

json::jobject json::jobject::parse ( const char *  input)
static

Parses a serialized JSON string.

Parameters
inputSerialized JSON string
Returns
JSON object or array
Exceptions
json::parsing_errorThrown when the input string is not valid JSON
Examples
jobject.cpp, objectarray.cpp, and rootarray.cpp.

◆ parse() [2/2]

static jobject json::jobject::parse ( const std::string  input)
inlinestatic

Parses a serialized JSON string.

See also
json::jobject::parse(const char*)

◆ pretty()

std::string json::jobject::pretty ( unsigned int  indent_level = 0) const

Returns a pretty (multi-line indented) serialzed representation of the object or array.

Parameters
indent_levelThe number of indents (tabs) to start with
Returns
A "pretty" version of the serizlied object or array
Examples
objectarray.cpp.

◆ remove() [1/2]

void json::jobject::remove ( const size_t  index)
inline

Removes the entry at the specified index.

Parameters
indexThe index of the element to be removed

◆ remove() [2/2]

void json::jobject::remove ( const std::string &  key)

Removes the entry associated with the key.

Parameters
keyThe key of the key value pair to be removed
Note
If the key is not found in the object, no action is taken

◆ set()

void json::jobject::set ( const std::string &  key,
const std::string &  value 
)

Sets the value assocaited with the key.

If the key exists, then the value is updated. If the key does not exist, then the key value pair is added to the object.

Parameters
keyThe key for the entry
valueThe value for the entry
Exceptions
json::invalid_keyException thrown if the object actually represents a JSON array
Examples
jobject.cpp.

◆ tryparse()

static bool json::jobject::tryparse ( const char *  input,
jobject output 
)
inlinestatic

/brief Attempts to parse the input string

Parameters
inputA serialized JSON object or array
[out]outputShould the parsing attempt be successful, the resultant JSON object or array
Returns
True of the parsing attempt was successful and false if the parsing attempt was not successful

The documentation for this class was generated from the following files: