Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
cinder::JsonTree Class Reference

#include <Json.h>

Classes

class  ExcChildNotFound
 Exception expressing the absence of an expected child node. More...
 
class  Exception
 Base class for JsonTree exceptions. More...
 
class  ExcJsonParserError
 Exception expressing the existence of errors when serializing or deserializing JSON. More...
 
class  ExcNonConvertible
 Exception expressing the inability to convert a node's value to a requested type. More...
 
class  ParseOptions
 Options for JSON parsing. Passed to the JsonTree constructor. More...
 
class  WriteOptions
 Options for JSON writing. Passed to the write method. More...
 

Public Types

enum  NodeType {
  NODE_UNKNOWN, NODE_NULL, NODE_ARRAY, NODE_OBJECT,
  NODE_VALUE
}
 Enum listing all types of JSON nodes understood by the parser. More...
 

Public Member Functions

 JsonTree ()
 Creates a null JsonTree. More...
 
 JsonTree (const JsonTree &jsonTree)
 Parses the JSON contained in the string xmlString. More...
 
 JsonTree (DataSourceRef dataSource, ParseOptions parseOptions=ParseOptions())
 Parses JSON contained in dataSource. Commonly used with the results of loadUrl(), loadFile() or loadResource().
JsonTree myDoc( loadUrl( "http://search.twitter.com/search.json?q=libcinder&rpp=10&result_type=recent" ) ); More...
 
 JsonTree (const std::string &jsonString, ParseOptions parseOptions=ParseOptions())
 Parses the JSON contained in the string jsonString . More...
 
 JsonTree (const std::string &key, bool value)
 Creates a JsonTree with key key and boolean value . More...
 
 JsonTree (const std::string &key, double value)
 Creates a JsonTree with key key and double value . More...
 
 JsonTree (const std::string &key, float value)
 Creates a JsonTree with key key and double value cast from float. More...
 
 JsonTree (const std::string &key, int value)
 Creates a JsonTree with key key and int value . More...
 
 JsonTree (const std::string &key, const std::string &value)
 Creates a JsonTree with key key and string value . More...
 
 JsonTree (const std::string &key, const char *value)
 Creates a JsonTree with key key and string value cast from const char*. More...
 
 JsonTree (const std::string &key, uint32_t value)
 Creates a JsonTree with key key and uint32_t value . More...
 
 JsonTree (const std::string &key, int64_t value)
 Creates a JsonTree with key key and int64_t value . More...
 
 JsonTree (const std::string &key, uint64_t value)
 Creates a JsonTree with key key and uint64_t value . More...
 
std::string serialize () const
 Returns the JsonTree as a string with standard formatting. More...
 
Iter begin ()
 Returns an Iter to the first child of this node. More...
 
ConstIter begin () const
 Returns a ConstIter to the first child of this node. More...
 
Iter end ()
 Returns an Iter which marks the end of the children of this node. More...
 
ConstIter end () const
 Returns a ConstIter which marks the end of the children of this node. More...
 
JsonTreeoperator= (const JsonTree &jsonTree)
 Assigns the JsonTree a new value, and creates it if it doesn't exist. More...
 
JsonTreeoperator[] (const std::string &relativePath)
 
const JsonTreeoperator[] (const std::string &relativePath) const
 
JsonTreeoperator[] (size_t index)
 Returns the child at index. Throws ExcChildNotFound if none matches. More...
 
const JsonTreeoperator[] (size_t index) const
 Returns the child at index. Throws ExcChildNotFound if none matches. More...
 
JsonTreegetChild (const std::string &relativePath, bool caseSensitive=false, char separator= '.')
 
const JsonTreegetChild (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 
JsonTreegetChild (size_t index)
 Returns the child at index. Throws ExcChildNotFound if none matches. More...
 
const JsonTreegetChild (size_t index) const
 Returns the child at index. Throws ExcChildNotFound if none matches. More...
 
const Container & getChildren () const
 Returns a reference to the node's list of children nodes. More...
 
size_t getNumChildren () const
 Returns the number of child nodes. More...
 
bool hasChild (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 
bool hasChildren () const
 Returns whether this node has a parent node. More...
 
JsonTreegetParent ()
 Returns a reference to the node which is the parent of this node. More...
 
const JsonTreegetParent () const
 Returns a reference to the node which is the parent of this node. More...
 
bool hasParent () const
 Returns whether this node has a parent node. More...
 
void clear ()
 Removes all child nodes. More...
 
JsonTreeaddChild (const JsonTree &newChild)
 
void pushBack (const JsonTree &newChild)
 
void removeChild (size_t index)
 Removes the child at index. Throws ExcChildNotFound if none matches. More...
 
Iter removeChild (Iter pos)
 Removes the child at pos. Throws ExcChildNotFound if none matches. More...
 
void replaceChild (size_t index, const JsonTree &newChild)
 Replaces the child at index with JsonTree newChild. Throws ExcChildNotFound if none matches. More...
 
void replaceChild (Iter pos, const JsonTree &newChild)
 Replaces the child at pos with JsonTree newChild. Throws ExcChildNotFound if none matches. More...
 
void write (const fs::path &path, WriteOptions writeOptions=WriteOptions())
 
void write (DataTargetRef target, WriteOptions writeOptions=WriteOptions())
 
const std::stringgetKey () const
 Returns the node's key as a string. Returns index if node does not have a key. More...
 
std::string getPath (char separator= '.') const
 
template<typename T >
getValue () const
 Returns the value of the node cast to T using ci::fromString().
float value = myNode.getValue<float>( "key" ); More...
 
template<typename T >
getValueForKey (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for: More...
 
template<typename T >
getValueAtIndex (size_t index) const
 Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for: More...
 
const std::stringgetValue () const
 Returns the value of the node as a string. More...
 
const std::stringgetValueForKey (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for: More...
 
const std::stringgetValueAtIndex (size_t index) const
 Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for: More...
 
NodeType getNodeType () const
 Returns the type of the node. More...
 

Static Public Member Functions

static JsonTree makeArray (const std::string &key="")
 
static JsonTree makeObject (const std::string &key="")
 

Friends

std::ostream & operator<< (std::ostream &out, const JsonTree &json)
 Streams the JsonTree json to std::ostream out with standard formatting. More...
 

Member Enumeration Documentation

Enum listing all types of JSON nodes understood by the parser.

Enumerator
NODE_UNKNOWN 
NODE_NULL 
NODE_ARRAY 
NODE_OBJECT 
NODE_VALUE 

Constructor & Destructor Documentation

cinder::JsonTree::JsonTree ( )
explicit

Creates a null JsonTree.

cinder::JsonTree::JsonTree ( const JsonTree jsonTree)

Parses the JSON contained in the string xmlString.

cinder::JsonTree::JsonTree ( DataSourceRef  dataSource,
ParseOptions  parseOptions = ParseOptions() 
)
explicit

Parses JSON contained in dataSource. Commonly used with the results of loadUrl(), loadFile() or loadResource().
JsonTree myDoc( loadUrl( "http://search.twitter.com/search.json?q=libcinder&rpp=10&result_type=recent" ) );

cinder::JsonTree::JsonTree ( const std::string jsonString,
ParseOptions  parseOptions = ParseOptions() 
)
explicit

Parses the JSON contained in the string jsonString .

cinder::JsonTree::JsonTree ( const std::string key,
bool  value 
)
explicit

Creates a JsonTree with key key and boolean value .

cinder::JsonTree::JsonTree ( const std::string key,
double  value 
)
explicit

Creates a JsonTree with key key and double value .

cinder::JsonTree::JsonTree ( const std::string key,
float  value 
)
explicit

Creates a JsonTree with key key and double value cast from float.

cinder::JsonTree::JsonTree ( const std::string key,
int  value 
)
explicit

Creates a JsonTree with key key and int value .

cinder::JsonTree::JsonTree ( const std::string key,
const std::string value 
)
explicit

Creates a JsonTree with key key and string value .

cinder::JsonTree::JsonTree ( const std::string key,
const char *  value 
)
explicit

Creates a JsonTree with key key and string value cast from const char*.

cinder::JsonTree::JsonTree ( const std::string key,
uint32_t  value 
)
explicit

Creates a JsonTree with key key and uint32_t value .

cinder::JsonTree::JsonTree ( const std::string key,
int64_t  value 
)
explicit

Creates a JsonTree with key key and int64_t value .

cinder::JsonTree::JsonTree ( const std::string key,
uint64_t  value 
)
explicit

Creates a JsonTree with key key and uint64_t value .

Member Function Documentation

JsonTree cinder::JsonTree::makeArray ( const std::string key = "")
static

! Creates a JsonTree with key key and an empty array.

JsonTree cinder::JsonTree::makeObject ( const std::string key = "")
static

! Creates a JsonTree with key key as an empty object.

string cinder::JsonTree::serialize ( ) const

Returns the JsonTree as a string with standard formatting.

JsonTree::Iter cinder::JsonTree::begin ( )

Returns an Iter to the first child of this node.

JsonTree::ConstIter cinder::JsonTree::begin ( ) const

Returns a ConstIter to the first child of this node.

JsonTree::Iter cinder::JsonTree::end ( )

Returns an Iter which marks the end of the children of this node.

JsonTree::ConstIter cinder::JsonTree::end ( ) const

Returns a ConstIter which marks the end of the children of this node.

JsonTree & cinder::JsonTree::operator= ( const JsonTree jsonTree)

Assigns the JsonTree a new value, and creates it if it doesn't exist.

JsonTree & cinder::JsonTree::operator[] ( const std::string relativePath)

! Returns the child at relativePath. Throws ExcChildNotFound if none matches.
JsonTree node = myNode[ "path.to.child" ];

const JsonTree & cinder::JsonTree::operator[] ( const std::string relativePath) const

! Returns the child at relativePath. Throws ExcChildNotFound if none matches.
JsonTree node = myNode[ "path.to.child" ];

JsonTree & cinder::JsonTree::operator[] ( size_t  index)

Returns the child at index. Throws ExcChildNotFound if none matches.

const JsonTree & cinder::JsonTree::operator[] ( size_t  index) const

Returns the child at index. Throws ExcChildNotFound if none matches.

JsonTree & cinder::JsonTree::getChild ( const std::string relativePath,
bool  caseSensitive = false,
char  separator = '.' 
)

! Returns the child at relativePath. Throws ExcChildNotFound if none matches.
JsonTree node = myNode.getChild( "path.to.child" );

const JsonTree & cinder::JsonTree::getChild ( const std::string relativePath,
bool  caseSensitive = false,
char  separator = '.' 
) const

! Returns the child at relativePath. Throws ExcChildNotFound if none matches.
JsonTree node = myNode.getChild( "path.to.child" );

JsonTree & cinder::JsonTree::getChild ( size_t  index)

Returns the child at index. Throws ExcChildNotFound if none matches.

const JsonTree & cinder::JsonTree::getChild ( size_t  index) const

Returns the child at index. Throws ExcChildNotFound if none matches.

const JsonTree::Container & cinder::JsonTree::getChildren ( ) const

Returns a reference to the node's list of children nodes.

size_t cinder::JsonTree::getNumChildren ( ) const

Returns the number of child nodes.

bool cinder::JsonTree::hasChild ( const std::string relativePath,
bool  caseSensitive = false,
char  separator = '.' 
) const

! Returns whether the child at relativePath exists.
bool nodeExists = myNode.hasChild( "path.to.child" );

bool cinder::JsonTree::hasChildren ( ) const

Returns whether this node has a parent node.

JsonTree & cinder::JsonTree::getParent ( )

Returns a reference to the node which is the parent of this node.

const JsonTree & cinder::JsonTree::getParent ( ) const

Returns a reference to the node which is the parent of this node.

bool cinder::JsonTree::hasParent ( ) const

Returns whether this node has a parent node.

void cinder::JsonTree::clear ( )

Removes all child nodes.

JsonTree & cinder::JsonTree::addChild ( const JsonTree newChild)

! Appends a copy of the node newChild to the children of this node. If this is a value node, it will change to an object or an array. If newChild has a key, this becomes an object node. If not, this becomes an array node. Returns reference to itself.

void cinder::JsonTree::pushBack ( const JsonTree newChild)

! Appends a copy of the node newChild to the children of this node. If this is a value node, it will change to an object or an array. If newChild has a key, this becomes an object node. If not, this becomes an array node.

void cinder::JsonTree::removeChild ( size_t  index)

Removes the child at index. Throws ExcChildNotFound if none matches.

Iter cinder::JsonTree::removeChild ( Iter  pos)

Removes the child at pos. Throws ExcChildNotFound if none matches.

void cinder::JsonTree::replaceChild ( size_t  index,
const JsonTree newChild 
)

Replaces the child at index with JsonTree newChild. Throws ExcChildNotFound if none matches.

void cinder::JsonTree::replaceChild ( Iter  pos,
const JsonTree newChild 
)

Replaces the child at pos with JsonTree newChild. Throws ExcChildNotFound if none matches.

void cinder::JsonTree::write ( const fs::path &  path,
JsonTree::WriteOptions  writeOptions = WriteOptions() 
)

! Writes this JsonTree to path with standard formatting. If writeOptions creates a document then an implicit parent object node is created when necessary and this is treated as the root element. If writeOptions indents then the JSON string will be indented.

void cinder::JsonTree::write ( DataTargetRef  target,
JsonTree::WriteOptions  writeOptions = WriteOptions() 
)

! Writes this JsonTree to target. If writeOptions creates a document then an implicit parent object node is created when necessary and this is treated as the root element. If writeOptions indents then the JSON string will be indented.

const string & cinder::JsonTree::getKey ( ) const

Returns the node's key as a string. Returns index if node does not have a key.

string cinder::JsonTree::getPath ( char  separator = '.') const

! Returns a path to this node, separated by the character separator.

template<typename T >
T cinder::JsonTree::getValue ( ) const

Returns the value of the node cast to T using ci::fromString().
float value = myNode.getValue<float>( "key" );

!

template<typename T >
T cinder::JsonTree::getValueForKey ( const std::string relativePath,
bool  caseSensitive = false,
char  separator = '.' 
) const

Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for:

getChild( relativePath ).getValue<T>()
template<typename T >
T cinder::JsonTree::getValueAtIndex ( size_t  index) const

Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for:

const std::string& cinder::JsonTree::getValue ( ) const

Returns the value of the node as a string.

const std::string& cinder::JsonTree::getValueForKey ( const std::string relativePath,
bool  caseSensitive = false,
char  separator = '.' 
) const

Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for:

getChild( relativePath ).getValue<T>()
const std::string& cinder::JsonTree::getValueAtIndex ( size_t  index) const

Returns the value of the child at relativePath. Default type T is std::string. Convenience shortcut for:

NodeType cinder::JsonTree::getNodeType ( ) const

Returns the type of the node.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const JsonTree json 
)
friend

Streams the JsonTree json to std::ostream out with standard formatting.


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