Nebula
|
#include <jsonreader.h>
Reads json formatted data with random access from a stream using pjson as backend.
The json document is represented as a tree of nodes, which can be navigated and queried.
Inherits IO::StreamReader.
Public Member Functions | |
JsonReader () | |
constructor | |
virtual | ~JsonReader () |
destructor | |
virtual bool | Open () |
begin reading from the stream | |
virtual void | Close () |
end reading from the stream | |
bool | HasNode (const Util::String &path) |
return true if node exists | |
void | SetToRoot () |
sets the current node to root | |
bool | SetToNode (const Util::String &path) |
set current node as path, accepts / as separator and [number] to access array/object elements | |
bool | SetToFirstChild (const Util::String &name="") |
set current node to first child, false if none exists | |
bool | SetToNextChild () |
set to next child, false if none exists | |
bool | SetToParent () |
set current node to parent, return false if no parent exists | |
Util::String | GetChildNodeName (SizeT childIndex) |
gets the childname of the child at index, or empty string if no child exists or has no name. | |
bool | IsArray () const |
check if current node is an array | |
bool | IsObject () const |
check if current node is an object (can have keys) | |
bool | HasChildren () const |
check if current node has children (json object or array) | |
SizeT | CurrentSize () const |
children count | |
bool | HasAttr (const char *attr) const |
return true if matching attribute exists on current node | |
Util::Array< Util::String > | GetAttrs () const |
return names of all attrs on current node | |
Util::String | GetCurrentNodeName () const |
get name of current node | |
Util::String | GetString (const char *attr=0) const |
get string attribute value from current node | |
Util::StringAtom | GetStringAtom (const char *attr=0) const |
get stringatom attribute value from current node | |
bool | GetBool (const char *attr=0) const |
get bool attribute value from current node | |
int | GetInt (const char *attr=0) const |
get int attribute value from current node | |
uint | GetUInt (const char *attr=0) const |
get unsigned int attribute value from current node | |
float | GetFloat (const char *attr=0) const |
get float attribute value from current node | |
Math::vec2 | GetVec2 (const char *attr=0) const |
get vec2 attribute value from current node | |
Math::vec3 | GetVec3 (const char *attr=0) const |
get vec4 attribute value from current node | |
Math::vec4 | GetVec4 (const char *attr=0) const |
get vec4 attribute value from current node | |
Math::mat4 | GetMat4 (const char *attr=0) const |
get mat4 attribute value from current node | |
Math::transform44 | GetTransform44 (const char *attr=0) const |
get transform44 attribute value from current node | |
template<typename T > | |
void | Get (T &target, const char *attr=0) |
generic getter for extension types | |
template<unsigned int N> | |
void | Get (Util::BitField< N > &target, const char *attr=0) |
getter for bitfield of N size | |
Util::String | GetOptString (const char *attr, const Util::String &defaultValue) const |
get optional string attribute value from current node | |
bool | GetOptBool (const char *attr, bool defaultValue) const |
get optional bool attribute value from current node | |
int | GetOptInt (const char *attr, int defaultValue) const |
get optional int attribute value from current node | |
float | GetOptFloat (const char *attr, float defaultValue) const |
get optional float attribute value from current node | |
Math::vec2 | GetOptVec2 (const char *attr, const Math::vec2 &defaultValue) const |
get vec2 attribute value from current node | |
Math::vec4 | GetOptVec4 (const char *attr, const Math::vec4 &defaultValue) const |
get optional vec4 attribute value from current node | |
Math::mat4 | GetOptMat4 (const char *attr, const Math::mat4 &defaultValue) const |
get optional mat4 attribute value from current node | |
Math::transform44 | GetOptTransform44 (const char *attr, const Math::transform44 &defaultValue) const |
get transform44 attribute value from current node | |
template<typename T > | |
bool | GetOpt (T &target, const char *attr=0) |
generic getter for optional items | |
template<typename T > | |
bool | GetOpt (T &target, const char *attr, const T &_default) |
generic getter for optional items | |
template<> | |
void | Get (bool &ret, const char *attr) |
template<> | |
void | Get (int64_t &ret, const char *attr) |
template<> | |
void | Get (int32_t &ret, const char *attr) |
template<> | |
void | Get (int16_t &ret, const char *attr) |
template<> | |
void | Get (int8_t &ret, const char *attr) |
template<> | |
void | Get (char &ret, const char *attr) |
template<> | |
void | Get (uint64_t &ret, const char *attr) |
template<> | |
void | Get (uint32_t &ret, const char *attr) |
template<> | |
void | Get (uint16_t &ret, const char *attr) |
template<> | |
void | Get (uint8_t &ret, const char *attr) |
template<> | |
void | Get (float &ret, const char *attr) |
template<> | |
bool | GetOpt (bool &ret, const char *attr) |
template<> | |
bool | GetOpt (int &ret, const char *attr) |
template<> | |
bool | GetOpt (uint16_t &ret, const char *attr) |
template<> | |
bool | GetOpt (uint32_t &ret, const char *attr) |
template<> | |
bool | GetOpt (float &ret, const char *attr) |
template<> | |
void | Get (bool &ret, const char *attr) |
template<> | |
void | Get (int64_t &ret, const char *attr) |
template<> | |
void | Get (int32_t &ret, const char *attr) |
template<> | |
void | Get (int16_t &ret, const char *attr) |
template<> | |
void | Get (int8_t &ret, const char *attr) |
template<> | |
void | Get (char &ret, const char *attr) |
template<> | |
void | Get (uint32_t &ret, const char *attr) |
template<> | |
void | Get (uint16_t &ret, const char *attr) |
template<> | |
void | Get (uint8_t &ret, const char *attr) |
template<> | |
void | Get (float &ret, const char *attr) |
template<> | |
bool | GetOpt (bool &ret, const char *attr) |
template<> | |
bool | GetOpt (int &ret, const char *attr) |
template<> | |
bool | GetOpt (uint16_t &ret, const char *attr) |
template<> | |
bool | GetOpt (uint32_t &ret, const char *attr) |
template<> | |
bool | GetOpt (float &ret, const char *attr) |
Public Member Functions inherited from IO::StreamReader | |
StreamReader () | |
constructor | |
virtual | ~StreamReader () |
destructor | |
void | SetStream (const Ptr< Stream > &s) |
set stream to read from | |
const Ptr< Stream > & | GetStream () const |
get currently set stream | |
bool | HasStream () const |
return true if a stream is set | |
bool | Eof () const |
return true if the stream has reached EOF | |
bool | IsOpen () const |
return true if currently open | |
Public Member Functions inherited from Core::RefCounted | |
RefCounted () | |
constructor | |
int | GetRefCount () const |
get the current refcount | |
void | AddRef () |
increment refcount by one | |
void | Release () |
decrement refcount and destroy object if refcount is zero | |
bool | IsInstanceOf (const Rtti &rtti) const |
return true if this object is instance of given class | |
bool | IsInstanceOf (const Util::String &className) const |
return true if this object is instance of given class by string | |
bool | IsInstanceOf (const Util::FourCC &classFourCC) const |
return true if this object is instance of given class by fourcc | |
bool | IsA (const Rtti &rtti) const |
return true if this object is instance of given class, or a derived class | |
bool | IsA (const Util::String &rttiName) const |
return true if this object is instance of given class, or a derived class, by string | |
bool | IsA (const Util::FourCC &rttiFourCC) const |
return true if this object is instance of given class, or a derived class, by fourcc | |
const Util::String & | GetClassName () const |
get the class name | |
Util::FourCC | GetClassFourCC () const |
get the class FourCC code | |
Private Member Functions | |
__DeclareClass (JsonReader) | |
const pjson::value_variant * | GetChild (const char *key=0) const |
Private Attributes | |
pjson::document * | document |
const pjson::value_variant * | curNode |
IndexT | childIdx = -1 |
Util::Stack< const pjson::value_variant * > | parents |
Util::Stack< IndexT > | parentIdx |
char * | buffer = nullptr |
Additional Inherited Members | |
Static Public Member Functions inherited from Core::RefCounted | |
static void | DumpRefCountingLeaks () |
dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!) | |
Protected Member Functions inherited from Core::RefCounted | |
virtual | ~RefCounted () |
destructor (called when refcount reaches zero) | |
Protected Attributes inherited from IO::StreamReader | |
Ptr< Stream > | stream |
bool | isOpen |
bool | streamWasOpen |
IO::JsonReader::JsonReader | ( | ) |
constructor
|
virtual |
destructor
|
private |
|
virtual |
end reading from the stream
Reimplemented from IO::StreamReader.
SizeT IO::JsonReader::CurrentSize | ( | ) | const |
children count
void IO::JsonReader::Get | ( | bool & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | bool & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | char & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | char & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | float & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | float & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int16_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int16_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int32_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int32_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int64_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int64_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int8_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | int8_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | T & | target, |
const char * | attr = 0 ) |
generic getter for extension types
void IO::JsonReader::Get | ( | uint16_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | uint16_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | uint32_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | uint32_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | uint64_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | uint8_t & | ret, |
const char * | attr ) |
void IO::JsonReader::Get | ( | uint8_t & | ret, |
const char * | attr ) |
|
inline |
getter for bitfield of N size
return names of all attrs on current node
Return array with names of all attrs on current node.
bool IO::JsonReader::GetBool | ( | const char * | name = 0 | ) | const |
get bool attribute value from current node
Return the provided attribute as a bool.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
|
private |
Util::String IO::JsonReader::GetChildNodeName | ( | SizeT | childIndex | ) |
gets the childname of the child at index, or empty string if no child exists or has no name.
Util::String IO::JsonReader::GetCurrentNodeName | ( | ) | const |
get name of current node
float IO::JsonReader::GetFloat | ( | const char * | name = 0 | ) | const |
get float attribute value from current node
Return the provided attribute as float.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
int IO::JsonReader::GetInt | ( | const char * | name = 0 | ) | const |
get int attribute value from current node
Return the provided attribute as int.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
mat4 IO::JsonReader::GetMat4 | ( | const char * | name = 0 | ) | const |
get mat4 attribute value from current node
Return the provided attribute as mat4.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
bool IO::JsonReader::GetOpt | ( | bool & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | bool & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | float & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | float & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | int & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | int & | ret, |
const char * | attr ) |
|
inline |
generic getter for optional items
|
inline |
generic getter for optional items
bool IO::JsonReader::GetOpt | ( | uint16_t & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | uint16_t & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | uint32_t & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOpt | ( | uint32_t & | ret, |
const char * | attr ) |
bool IO::JsonReader::GetOptBool | ( | const char * | name, |
bool | defaultValue ) const |
get optional bool attribute value from current node
Return the provided optional attribute as bool.
If the attribute doesn't exist, the default value will be returned.
float IO::JsonReader::GetOptFloat | ( | const char * | name, |
float | defaultValue ) const |
get optional float attribute value from current node
Return the provided optional attribute as float.
If the attribute doesn't exist, the default value will be returned.
int IO::JsonReader::GetOptInt | ( | const char * | name, |
int | defaultValue ) const |
get optional int attribute value from current node
Return the provided optional attribute as int.
If the attribute doesn't exist, the default value will be returned.
mat4 IO::JsonReader::GetOptMat4 | ( | const char * | name, |
const Math::mat4 & | defaultValue ) const |
get optional mat4 attribute value from current node
Return the provided optional attribute as mat4.
If the attribute doesn't exist, the default value will be returned.
String IO::JsonReader::GetOptString | ( | const char * | name, |
const Util::String & | defaultValue ) const |
get optional string attribute value from current node
Return the provided optional attribute as string.
If the attribute doesn't exist, the default value will be returned.
transform44 IO::JsonReader::GetOptTransform44 | ( | const char * | name, |
const Math::transform44 & | defaultValue ) const |
get transform44 attribute value from current node
Return the provided optional attribute as transform44.
If the attribute doesn't exist, the default value will be returned.
vec2 IO::JsonReader::GetOptVec2 | ( | const char * | name, |
const Math::vec2 & | defaultValue ) const |
get vec2 attribute value from current node
Return the provided optional attribute as vec2.
If the attribute doesn't exist, the default value will be returned.
vec4 IO::JsonReader::GetOptVec4 | ( | const char * | name, |
const Math::vec4 & | defaultValue ) const |
get optional vec4 attribute value from current node
Return the provided optional attribute as vec4.
If the attribute doesn't exist, the default value will be returned.
String IO::JsonReader::GetString | ( | const char * | name = 0 | ) | const |
get string attribute value from current node
Return the provided attribute as string.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
StringAtom IO::JsonReader::GetStringAtom | ( | const char * | name = 0 | ) | const |
get stringatom attribute value from current node
Return the provided attribute as stringatom.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
transform44 IO::JsonReader::GetTransform44 | ( | const char * | name = 0 | ) | const |
get transform44 attribute value from current node
Return the provided attribute as transform44.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
uint IO::JsonReader::GetUInt | ( | const char * | attr = 0 | ) | const |
get unsigned int attribute value from current node
Return the provided attribute as vec2.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
vec2 IO::JsonReader::GetVec2 | ( | const char * | name = 0 | ) | const |
get vec2 attribute value from current node
Return the provided attribute as vec2.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
Math::vec3 IO::JsonReader::GetVec3 | ( | const char * | attr = 0 | ) | const |
get vec4 attribute value from current node
vec4 IO::JsonReader::GetVec4 | ( | const char * | name = 0 | ) | const |
get vec4 attribute value from current node
Return the provided attribute as vec4.
If the attribute does not exist the method will fail hard (use HasAttr() to check for its existance).
bool IO::JsonReader::HasAttr | ( | const char * | attr | ) | const |
return true if matching attribute exists on current node
Return true if an attribute of the given name exists on the current node.
bool IO::JsonReader::HasChildren | ( | ) | const |
check if current node has children (json object or array)
bool IO::JsonReader::HasNode | ( | const Util::String & | path | ) |
return true if node exists
This method returns true if the node identified by path exists.
Path follows the normal filesystem path conventions, "/" is the separator, starts with a "/", a relative path doesn't.
bool IO::JsonReader::IsArray | ( | ) | const |
check if current node is an array
bool IO::JsonReader::IsObject | ( | ) | const |
check if current node is an object (can have keys)
|
virtual |
begin reading from the stream
Opens the stream and reads the content of the stream into pjson.
Reimplemented from IO::StreamReader.
bool IO::JsonReader::SetToFirstChild | ( | const Util::String & | name = "" | ) |
set current node to first child, false if none exists
Sets the current node to the first child node.
If no child node exists, the current node will remain unchanged and the method will return false. If name is a valid string, only child element matching the name will be returned. If name is empty, all child nodes will be considered.
bool IO::JsonReader::SetToNextChild | ( | ) |
set to next child, false if none exists
Sets the current node to the next sibling.
If no more children exist, the current node will be reset to the parent node and the method will return false. If name is a valid string, only child element matching the name will be returned. If name is empty, all child nodes will be considered.
bool IO::JsonReader::SetToNode | ( | const Util::String & | path | ) |
set current node as path, accepts / as separator and [number] to access array/object elements
Set the node pointed to by the path string as current node.
The path may be absolute or relative, following the usual filesystem path conventions. Separator is a slash. Arrays or objects with children can be indexed with []
bool IO::JsonReader::SetToParent | ( | ) |
set current node to parent, return false if no parent exists
Sets the current node to its parent.
If no parent exists, the current node will remain unchanged and the method will return false.
void IO::JsonReader::SetToRoot | ( | ) |
sets the current node to root
Resets the json reader to the root node.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |