Cornell Cocos
Cornell Extensions to Cocos2d
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
JSONReader Class Reference

#include <CUJSONReader.h>

Inheritance diagram for JSONReader:

Public Member Functions

void setFile (std::string file)
 
std::string getFile () const
 
bool startJSON ()
 
bool startJSON (std::string source)
 
void endJSON ()
 
void reset ()
 
std::string getKey () const
 
bool isNull () const
 
bool isObject () const
 
bool isArray () const
 
bool isBool () const
 
bool isNumber () const
 
bool isString () const
 
bool isVec2 () const
 
bool isFloatArray () const
 
bool exists (std::string key) const
 
bool isNull (std::string key) const
 
bool isObject (std::string key) const
 
bool isArray (std::string key) const
 
bool isBool (std::string key) const
 
bool isNumber (std::string key) const
 
bool isString (std::string key) const
 
bool isVec2 (std::string key) const
 
bool isFloatArray (std::string key) const
 
bool getBool () const
 
float getNumber () const
 
std::string getString () const
 
Vec2 getVec2 () const
 
int getFloatArray (float *buffer) const
 
bool getBool (std::string key) const
 
float getNumber (std::string key) const
 
std::string getString (std::string key) const
 
Vec2 getVec2 (std::string key) const
 
int getFloatArray (std::string key, float *buffer) const
 
bool startObject (std::string key)
 
bool startObject ()
 
void endObject ()
 
int getSize () const
 
int getSize (std::string key) const
 
int startArray (std::string key)
 
int startArray ()
 
void endArray ()
 
bool advance ()
 
CC_CONSTRUCTOR_ACCESS _json (nullptr)
 
CC_CONSTRUCTOR_ACCESS _arraymode (false)
 
 ~JSONReader ()
 
bool init ()
 
bool init (std::string file)
 

Static Public Member Functions

static JSONReadercreate ()
 
static JSONReadercreate (std::string file)
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: JSONReader() : _root(nullptr)
 

Protected Attributes

Json * _root
 
Json * _json
 
std::string _file
 
std::vector< Json * > _stack
 
bool _arraymode
 
std::vector< bool > _states
 

Detailed Description

Class represents a simple DOM reader for parsing JSON

As most DOM readers, this uses a begin-end syntax to descend the document to tree. At any time, this reader has a cursor positioned at a particular node. You use methods to move this cursor. For example, to read the contents of an object, you must call startObject() to move the cursor to access its contents, and then must call endObject() when you are done. Similarly for arrays. Primitive types, such as bools, floats, and strings can all be access directly.

Objects and arrays are interchangeable. Any object can be treated as an array and traversed accordingly. Traversing an object as an array allows you to query both the keys and values of the object, instead of just the keys.

Elements of the JSON may be accessed without or without a name. If no name is given, it assumes that you are refering to the current node in the DOM tree. Otherwise, you are refering to a field of a given name. This is crucial for going back and forth between objects and arrays. For example, if you call startObject("foo"), it will descend the field "foo" from the current position. If you then call startArray() it will stay at the same position, but treat it as an array instead.

This reader tries to fail as little as possible. If a field or current position is treated as the wrong type, it will return a default value instead. This makes it easier to process missing data. If you really care about whether data is there or not, use the type-checking methods.

Constructor & Destructor Documentation

JSONReader::~JSONReader ( )
inline

Deletes the JSON reader, releasing all resources

Member Function Documentation

bool JSONReader::advance ( )

Advances the cursor to the next position in the array.

This method returns false if there are no more elements in the array. You must have called startArray() for this method to work.

Returns
true if there was an element to advance to
NS_CC_BEGIN JSONReader * JSONReader::create ( )
static

Creates a new JSONReader.

This constructor does not load any JSON, as there is no associate file. You can associate a file later with the setFile() method.

Returns
an autoreleased JSONReader object
JSONReader * JSONReader::create ( std::string  file)
static

Creates a new JSONReader.

This constructor does load the JSON file; it only stores the file name. You start parsing with the startJSON() method.

Parameters
filethe file to read from
Returns
an autoreleased JSONReader object
void JSONReader::endArray ( )

Ends processing of the current array, returning the cursor to its parent

void JSONReader::endJSON ( )

Ends the current JSON parsing session, erasing the DOM tree.

Once this method is called of the JSON methods will fail until startJSON() – either version – is called again.

void JSONReader::endObject ( )

Ends processing of the current object, returning the cursor to its parent

bool JSONReader::exists ( std::string  key) const

Returns true if there is an entry for the given key.

Parameters
keythe field name to query
Returns
true if there is an entry for the given key.
bool JSONReader::getBool ( ) const
inline

Returns the boolean value for the current cursor position.

If the current position is not a boolean, it returns False as a default.

Returns
the boolean value for the current cursor position.
bool JSONReader::getBool ( std::string  key) const

Returns the boolean value for the given key.

If the key does not exist, or does not refer to a boolean, this method returns False as a default.

Parameters
keythe field name to query
Returns
the boolean value for the given key.
std::string JSONReader::getFile ( ) const
inline

Returns the file for this JSON reader.

If this value is "", then there is no file. Instead, the JSONReader is intended to be used directly on a string via the startJSON(string) method.

Parameters
filefor this JSON reader
int JSONReader::getFloatArray ( float *  buffer) const

Fills the buffer with the contents of the cursor position

This method assumes that the current cursor position is an array of floats (e.g. the children of the cursor are all float nodes), and writes these floats to the given buffer. The method returns the number of elements written. If the cursor is not a valid float array, nothing will be written.

The buffer must be sized large enough to recieve the float array. Use the method getSize() to determine the size of the array.

Parameters
bufferthe buffer to store the float values
Returns
number of elements written to the buffer
int JSONReader::getFloatArray ( std::string  key,
float *  buffer 
) const

Fills the buffer with the contents of the given key.

This method assumes that the key refers to an array of floats (e.g. the children of the key are all float nodes), and writes these floats to the given buffer. The method returns the number of elements written. If the key does not exist, or does not refer to a valid float array, nothing will be written.

The buffer must be sized large enough to recieve the float array. Use the method getSize() to determine the size of the array.

Parameters
bufferthe buffer to store the float values
keythe field name to query
Returns
number of elements written to the buffer
std::string JSONReader::getKey ( ) const
inline

Returns the key for the current cursor position in the DOM.

Returns
the key for the current cursor position in the DOM.
float JSONReader::getNumber ( ) const
inline

Returns the number for the current cursor position.

All numbers in JSON files are treated as floats. If the current position is not a number, it returns 0.0f as a default.

Returns
the number for the current cursor position.
float JSONReader::getNumber ( std::string  key) const

Returns the number for the given key.

If the key does not exist, or does not refer to a number, this method returns 0.0f as a default. All numbers in JSON files are treated as floats.

Parameters
keythe field name to query
Returns
the number for the given key.
int JSONReader::getSize ( ) const
inline

Returns the number of children for the cursor node

If the current cursor is an array, it returns the number of elements of the array. If it is an object, it returns the number of fields in the object.

Returns
the number of children for the cursor node
int JSONReader::getSize ( std::string  key) const

Returns the number of children for the given key

If the key refers to an array, it returns the number of elements of the array. If it refers to an object, it returns the number of fields in the object. If the key does not exist, it returns 0.

Parameters
keythe field name to query
Returns
the number of children for the given key
std::string JSONReader::getString ( ) const
inline

Returns the string for the current cursor position.

If the current position is not a string, it returns "" as a default.

Returns
the string for the current cursor position.
std::string JSONReader::getString ( std::string  key) const

Returns the string for the given key.

If the key does not exist, or does not refer to a string, this method returns "" as a default.

Parameters
keythe field name to query
Returns
the string for the given key.
Vec2 JSONReader::getVec2 ( ) const

Returns the Vec2 value for the current cursor position.

If the current position is not a Vec2, it returns Vec2::ZERO as a default.

Returns
the Vec2 value for the current cursor position.
Vec2 JSONReader::getVec2 ( std::string  key) const

Returns the Vec2 value for the given key.

If the key does not exist, or does not refer to a Vec2 value, this method returns Vec2::ZERO as a default.

Parameters
keythe field name to query
Returns
the Vec2 value for the given key.
bool JSONReader::init ( )

Initializes a new JSONReader.

This initializer does not load any JSON, as there is no associate file. You can associate a file later with the setFile() method.

Returns
true if the reader is initialized properly, false otherwise.
bool JSONReader::init ( std::string  file)

Initializes a new JSONReader.

This initializer does load the JSON file; it only stores the file name. You start parsing with the startJSON() method.

Parameters
filethe file to read from
Returns
true if the reader is initialized properly, false otherwise.
bool JSONReader::isArray ( ) const
inline

Returns true if the current cursor position represents an array.

Returns
true if the current cursor position represents an array.
bool JSONReader::isArray ( std::string  key) const

Returns true if the entry for key exists and represents an array.

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents an array.
bool JSONReader::isBool ( ) const
inline

Returns true if the current cursor position represents a boolean value

Returns
true if the current cursor position represents a boolean value
bool JSONReader::isBool ( std::string  key) const

Returns true if the entry for key exists and represents a boolean value

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents a boolean value
bool JSONReader::isFloatArray ( ) const

Returns true if the current cursor position represents an array of floats

This method is just an additional type-check on top of isArray.

Returns
true if the current cursor position represents an array of floats
bool JSONReader::isFloatArray ( std::string  key) const

Returns true if the entry for key exists and represents an array of floats

This method is just an additional type-check on top of isArray.

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents an array of floats
bool JSONReader::isNull ( ) const
inline

Returns true if the current cursor position has a nullptr value.

Returns
true if the current cursor position has a nullptr value.
bool JSONReader::isNull ( std::string  key) const

Returns true if the entry for key exists and has a nullptr value.

Parameters
keythe field name to query
Returns
true if the entry for key exists and has a nullptr value.
bool JSONReader::isNumber ( ) const
inline

Returns true if the current cursor position represents a number

All numbers in JSON files are treated as floats.

Returns
true if the current cursor position represents a number
bool JSONReader::isNumber ( std::string  key) const

Returns true if the entry for key exists and represents a number

All numbers in JSON files are treated as floats.

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents a number
bool JSONReader::isObject ( ) const
inline

Returns true if the current cursor position represents an object.

Returns
true if the current cursor position represents an object.
bool JSONReader::isObject ( std::string  key) const

Returns true if the entry for key exists and represents an object.

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents an object.
bool JSONReader::isString ( ) const
inline

Returns true if the current cursor position represents a string

Returns
true if the current cursor position represents a string
bool JSONReader::isString ( std::string  key) const

Returns true if the entry for key exists and represents a string

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents a string
bool JSONReader::isVec2 ( ) const

Returns true if the current cursor position represents a Vec2 value

In a JSON file, a Vec2 is just a two-element array, whose elements are both numbers.

Returns
true if the current cursor position represents a Vec2 value
bool JSONReader::isVec2 ( std::string  key) const

Returns true if the entry for key exists and represents a Vec2 value

In a JSON file, a Vec2 is just a two-element array, whose elements are both numbers.

Parameters
keythe field name to query
Returns
true if the entry for key exists and represents a Vec2 value
void JSONReader::reset ( )
inline

Resets the JSON parser to the top of the DOM tree.

No information about the JSON is lost. This method simply resets the cursor position.

void JSONReader::setFile ( std::string  file)
inline

Sets the file for this JSON reader.

This constructor does load the JSON file; it only stores the file name. You start parsing with the startJSON() method.

Parameters
filefor this JSON reader
int JSONReader::startArray ( std::string  key)

Moves the cursor to begin processing the given key as an array

The cursor will move to the first position of the array. This method will return the number of elements in the array. This method returns 0 if the cursor does not refer to an array. However, the cursor always moves, so you must call endArray() to restore the cursor even if it fails.

Parameters
keythe field name to query
Returns
the number of elements in the array
int JSONReader::startArray ( )

Moves the cursor to begin processing the current cursor position as an array

The cursor will move to the first position of the array. This method will return the number of elements in the array. This method returns 0 if key does not exist, or its value is not an array. However, the cursor always moves, so you must call endArray() to restore the cursor even if it fails.

Returns
the number of elements in the array
bool JSONReader::startJSON ( )

Starts a JSON parser for the current file.

The parser will fail if the JSON is not well-formed. In that case, the method will return false. There are (unfortunately) no error messages for why parsing failed.

Returns
true if the file is a well-formed JSON file.
bool JSONReader::startJSON ( std::string  source)

Starts a JSON parser for the given JSON string.

This method will ignore the associated file, and parse the provided string instead. The string should be an JSON string, not a file containing a JSON string.

The parser will fail if the JSON is not well-formed. In that case, the method will return false. There are (unfortunately) no error messages for why parsing failed.

Parameters
sourcethe JSON source string
Returns
true if the file is a well-formed JSON file.
bool JSONReader::startObject ( std::string  key)

Moves the cursor to the value for the given key, and treats it as an object.

This method returns false if the key does not exist, or its value is not an object. However, the cursor always moves, so you must call endObject() to restore the cursor even if it fails.

Parameters
keythe field name to query
Returns
true if the associated value is an object
bool JSONReader::startObject ( )

Treats the current cursor position as an object.

This method should be used when extracting an object while you are traversing an array (e.g. the object is an element inside the array). It changes the parsing state, but does not move the cursor.

This method returns false if the current cursor position is not an object. However, the state always changes, so you must call endObject() to restore the state even if it fails.

Returns
true if the cursor position is an object

Member Data Documentation

bool JSONReader::_arraymode
protected

Whether we are in object mode or array mode

std::string JSONReader::_file
protected

The file with the JSON source

Json* JSONReader::_json
protected

The current position in the DOM tree

Json* JSONReader::_root
protected

The root of the JSON DOM tree

std::vector<Json*> JSONReader::_stack
protected

A stack to allow us to traverse the tree

std::vector<bool> JSONReader::_states
protected

A stack to allow us to tracks states as we traverse the tree


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