Cinder  0.9.1
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
 
class  Exception
 
class  ExcJsonParserError
 
class  ExcNonConvertible
 
class  ParseOptions
 
class  WriteOptions
 

Public Types

enum  NodeType {
  NODE_UNKNOWN, NODE_NULL, NODE_ARRAY, NODE_OBJECT,
  NODE_VALUE
}
 

Public Member Functions

 JsonTree ()
 
 JsonTree (const JsonTree &jsonTree)
 
 JsonTree (DataSourceRef dataSource, ParseOptions parseOptions=ParseOptions())
 
 JsonTree (const std::string &jsonString, ParseOptions parseOptions=ParseOptions())
 
 JsonTree (const std::string &key, bool value)
 
 JsonTree (const std::string &key, double value)
 
 JsonTree (const std::string &key, float value)
 
 JsonTree (const std::string &key, int value)
 
 JsonTree (const std::string &key, const std::string &value)
 
 JsonTree (const std::string &key, const char *value)
 
 JsonTree (const std::string &key, uint32_t value)
 
 JsonTree (const std::string &key, int64_t value)
 
 JsonTree (const std::string &key, uint64_t value)
 
std::string serialize () const
 
Iter begin ()
 
ConstIter begin () const
 
Iter end ()
 
ConstIter end () const
 
JsonTreeoperator= (const JsonTree &jsonTree)
 
JsonTreeoperator[] (const std::string &relativePath)
 
const JsonTreeoperator[] (const std::string &relativePath) const
 
JsonTreeoperator[] (size_t index)
 
const JsonTreeoperator[] (size_t index) const
 
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)
 
const JsonTreegetChild (size_t index) const
 
const Container & getChildren () const
 
size_t getNumChildren () const
 
bool hasChild (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 
bool hasChildren () const
 
JsonTreegetParent ()
 
const JsonTreegetParent () const
 
bool hasParent () const
 
void clear ()
 
JsonTreeaddChild (const JsonTree &newChild)
 
void pushBack (const JsonTree &newChild)
 
void removeChild (size_t index)
 
Iter removeChild (Iter pos)
 
void replaceChild (size_t index, const JsonTree &newChild)
 
void replaceChild (Iter pos, const JsonTree &newChild)
 
void write (const fs::path &path, WriteOptions writeOptions=WriteOptions())
 
void write (DataTargetRef target, WriteOptions writeOptions=WriteOptions())
 
const std::string & getKey () const
 
std::string getPath (char separator= '.') const
 
template<typename T >
getValue () const
 
template<typename T >
getValueForKey (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 
template<typename T >
getValueAtIndex (size_t index) const
 
const std::string & getValue () const
 
const std::string & getValueForKey (const std::string &relativePath, bool caseSensitive=false, char separator= '.') const
 
const std::string & getValueAtIndex (size_t index) const
 
NodeType getNodeType () const
 

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)
 

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>();

!

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:

getChild( index ).getValue<T>()
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:

getChild( index ).getValue<T>()
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: