Nebula
Loading...
Searching...
No Matches
IO::JsonReader Class Reference

#include <jsonreader.h>

Detailed Description

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::StringGetAttrs () 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<>
void Get (double &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<>
bool GetOpt (double &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<>
void Get (double &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<>
bool GetOpt (double &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::StringGetClassName () 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< IndexTparentIdx
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< Streamstream
bool isOpen
bool streamWasOpen

Constructor & Destructor Documentation

◆ JsonReader()

IO::JsonReader::JsonReader ( )

constructor

◆ ~JsonReader()

IO::JsonReader::~JsonReader ( )
virtual

destructor

Member Function Documentation

◆ __DeclareClass()

IO::JsonReader::__DeclareClass ( JsonReader )
private

◆ Close()

void IO::JsonReader::Close ( )
virtual

end reading from the stream

Reimplemented from IO::StreamReader.

◆ CurrentSize()

SizeT IO::JsonReader::CurrentSize ( ) const

children count

◆ Get() [1/25]

template<>
void IO::JsonReader::Get ( bool & ret,
const char * attr )

◆ Get() [2/25]

template<>
void IO::JsonReader::Get ( bool & ret,
const char * attr )

◆ Get() [3/25]

template<>
void IO::JsonReader::Get ( char & ret,
const char * attr )

◆ Get() [4/25]

template<>
void IO::JsonReader::Get ( char & ret,
const char * attr )

◆ Get() [5/25]

template<>
void IO::JsonReader::Get ( double & ret,
const char * attr )

◆ Get() [6/25]

template<>
void IO::JsonReader::Get ( double & ret,
const char * attr )

◆ Get() [7/25]

template<>
void IO::JsonReader::Get ( float & ret,
const char * attr )

◆ Get() [8/25]

template<>
void IO::JsonReader::Get ( float & ret,
const char * attr )

◆ Get() [9/25]

template<>
void IO::JsonReader::Get ( int16_t & ret,
const char * attr )

◆ Get() [10/25]

template<>
void IO::JsonReader::Get ( int16_t & ret,
const char * attr )

◆ Get() [11/25]

template<>
void IO::JsonReader::Get ( int32_t & ret,
const char * attr )

◆ Get() [12/25]

template<>
void IO::JsonReader::Get ( int32_t & ret,
const char * attr )

◆ Get() [13/25]

template<>
void IO::JsonReader::Get ( int64_t & ret,
const char * attr )

◆ Get() [14/25]

template<>
void IO::JsonReader::Get ( int64_t & ret,
const char * attr )

◆ Get() [15/25]

template<>
void IO::JsonReader::Get ( int8_t & ret,
const char * attr )

◆ Get() [16/25]

template<>
void IO::JsonReader::Get ( int8_t & ret,
const char * attr )

◆ Get() [17/25]

template<typename T>
void IO::JsonReader::Get ( T & target,
const char * attr = 0 )

generic getter for extension types

◆ Get() [18/25]

template<>
void IO::JsonReader::Get ( uint16_t & ret,
const char * attr )

◆ Get() [19/25]

template<>
void IO::JsonReader::Get ( uint16_t & ret,
const char * attr )

◆ Get() [20/25]

template<>
void IO::JsonReader::Get ( uint32_t & ret,
const char * attr )

◆ Get() [21/25]

template<>
void IO::JsonReader::Get ( uint32_t & ret,
const char * attr )

◆ Get() [22/25]

template<>
void IO::JsonReader::Get ( uint64_t & ret,
const char * attr )

◆ Get() [23/25]

template<>
void IO::JsonReader::Get ( uint8_t & ret,
const char * attr )

◆ Get() [24/25]

template<>
void IO::JsonReader::Get ( uint8_t & ret,
const char * attr )

◆ Get() [25/25]

template<unsigned int N>
void IO::JsonReader::Get ( Util::BitField< N > & target,
const char * attr = 0 )
inline

getter for bitfield of N size

◆ GetAttrs()

Array< String > IO::JsonReader::GetAttrs ( ) const

return names of all attrs on current node

Return array with names of all attrs on current node.

◆ GetBool()

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).

◆ GetChild()

const pjson::value_variant * IO::JsonReader::GetChild ( const char * key = 0) const
private

◆ GetChildNodeName()

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.

◆ GetCurrentNodeName()

Util::String IO::JsonReader::GetCurrentNodeName ( ) const

get name of current node

◆ GetFloat()

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).

◆ GetInt()

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).

◆ GetMat4()

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).

◆ GetOpt() [1/14]

template<>
bool IO::JsonReader::GetOpt ( bool & ret,
const char * attr )

◆ GetOpt() [2/14]

template<>
bool IO::JsonReader::GetOpt ( bool & ret,
const char * attr )

◆ GetOpt() [3/14]

template<>
bool IO::JsonReader::GetOpt ( double & ret,
const char * attr )

◆ GetOpt() [4/14]

template<>
bool IO::JsonReader::GetOpt ( double & ret,
const char * attr )

◆ GetOpt() [5/14]

template<>
bool IO::JsonReader::GetOpt ( float & ret,
const char * attr )

◆ GetOpt() [6/14]

template<>
bool IO::JsonReader::GetOpt ( float & ret,
const char * attr )

◆ GetOpt() [7/14]

template<>
bool IO::JsonReader::GetOpt ( int & ret,
const char * attr )

◆ GetOpt() [8/14]

template<>
bool IO::JsonReader::GetOpt ( int & ret,
const char * attr )

◆ GetOpt() [9/14]

template<typename T>
bool IO::JsonReader::GetOpt ( T & target,
const char * attr,
const T & _default )
inline

generic getter for optional items

◆ GetOpt() [10/14]

template<typename T>
bool IO::JsonReader::GetOpt ( T & target,
const char * attr = 0 )
inline

generic getter for optional items

◆ GetOpt() [11/14]

template<>
bool IO::JsonReader::GetOpt ( uint16_t & ret,
const char * attr )

◆ GetOpt() [12/14]

template<>
bool IO::JsonReader::GetOpt ( uint16_t & ret,
const char * attr )

◆ GetOpt() [13/14]

template<>
bool IO::JsonReader::GetOpt ( uint32_t & ret,
const char * attr )

◆ GetOpt() [14/14]

template<>
bool IO::JsonReader::GetOpt ( uint32_t & ret,
const char * attr )

◆ GetOptBool()

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.

◆ GetOptFloat()

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.

◆ GetOptInt()

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.

◆ GetOptMat4()

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.

◆ GetOptString()

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.

◆ GetOptTransform44()

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.

◆ GetOptVec2()

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.

◆ GetOptVec4()

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.

◆ GetString()

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).

◆ GetStringAtom()

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).

◆ GetTransform44()

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).

◆ GetUInt()

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).

◆ GetVec2()

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).

◆ GetVec3()

Math::vec3 IO::JsonReader::GetVec3 ( const char * attr = 0) const

get vec4 attribute value from current node

◆ GetVec4()

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).

◆ HasAttr()

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.

◆ HasChildren()

bool IO::JsonReader::HasChildren ( ) const

check if current node has children (json object or array)

◆ HasNode()

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.

◆ IsArray()

bool IO::JsonReader::IsArray ( ) const

check if current node is an array

◆ IsObject()

bool IO::JsonReader::IsObject ( ) const

check if current node is an object (can have keys)

◆ Open()

bool IO::JsonReader::Open ( )
virtual

begin reading from the stream

Opens the stream and reads the content of the stream into pjson.

Reimplemented from IO::StreamReader.

◆ SetToFirstChild()

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.

◆ SetToNextChild()

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.

◆ SetToNode()

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 []

◆ SetToParent()

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.

◆ SetToRoot()

void IO::JsonReader::SetToRoot ( )

sets the current node to root

Resets the json reader to the root node.

Member Data Documentation

◆ buffer

char* IO::JsonReader::buffer = nullptr
private

◆ childIdx

IndexT IO::JsonReader::childIdx = -1
private

◆ curNode

const pjson::value_variant* IO::JsonReader::curNode
private

◆ document

pjson::document* IO::JsonReader::document
private

◆ parentIdx

Util::Stack<IndexT> IO::JsonReader::parentIdx
private

◆ parents

Util::Stack<const pjson::value_variant*> IO::JsonReader::parents
private

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