Cornell Cocos
Cornell Extensions to Cocos2d
|
#include <CUJSONReader.h>
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 JSONReader * | create () |
static JSONReader * | create (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 |
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.
|
inline |
Deletes the JSON reader, releasing all resources
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.
|
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.
|
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.
file | the file to read from |
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.
key | the field name to query |
|
inline |
Returns the boolean value for the current cursor position.
If the current position is not a boolean, it returns False as a default.
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.
key | the field name to query |
|
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.
file | for 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.
buffer | the buffer to store the float values |
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.
buffer | the buffer to store the float values |
key | the field name to query |
|
inline |
Returns the key for the current cursor position in the DOM.
|
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.
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.
key | the field name to query |
|
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.
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.
key | the field name to query |
|
inline |
Returns the string for the current cursor position.
If the current position is not a string, it returns "" as a default.
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.
key | the field name to query |
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.
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.
key | the field name to query |
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.
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.
file | the file to read from |
|
inline |
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.
key | the field name to query |
|
inline |
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
key | the field name to query |
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.
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.
key | the field name to query |
|
inline |
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.
key | the field name to query |
|
inline |
Returns true if the current cursor position represents a number
All numbers in JSON files are treated as floats.
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.
key | the field name to query |
|
inline |
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.
key | the field name to query |
|
inline |
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
key | the field name to query |
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.
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.
key | the field name to query |
|
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.
|
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.
file | for 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.
key | the field name to query |
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.
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.
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.
source | the JSON source string |
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.
key | the field name to query |
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.
|
protected |
Whether we are in object mode or array mode
|
protected |
The file with the JSON source
|
protected |
The current position in the DOM tree
|
protected |
The root of the JSON DOM tree
|
protected |
A stack to allow us to traverse the tree
|
protected |
A stack to allow us to tracks states as we traverse the tree