Nebula
Loading...
Searching...
No Matches
jsonreader.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13#include "io/streamreader.h"
14#include "math/vec4.h"
15#include "math/vec2.h"
16#include "math/mat4.h"
17#include "util/stack.h"
18#include "util/array.h"
19#include "util/stringatom.h"
20#include "util/bitfield.h"
21#include "util/variant.h"
22
23namespace pjson
24{
25 class value_variant;
26 class document;
27}
28//------------------------------------------------------------------------------
29namespace IO
30{
32{
34public:
36 JsonReader();
38 virtual ~JsonReader();
39
41 virtual bool Open();
43 virtual void Close();
44
46 bool HasNode(const Util::String& path);
47
49 void SetToRoot();
51 bool SetToNode(const Util::String& path);
52
54 bool SetToFirstChild(const Util::String& name = "");
56 bool SetToNextChild();
58 bool SetToParent();
59
62
64 bool IsArray() const;
66 bool IsObject() const;
68 bool HasChildren() const;
70 SizeT CurrentSize() const;
72 bool HasAttr(const char* attr) const;
75
78
80 Util::String GetString(const char* attr = 0) const;
82 Util::StringAtom GetStringAtom(const char* attr = 0) const;
84 bool GetBool(const char* attr = 0) const;
86 int GetInt(const char* attr = 0) const;
88 uint GetUInt(const char* attr = 0) const;
90 float GetFloat(const char* attr = 0) const;
92 Math::vec2 GetVec2(const char* attr = 0) const;
94 Math::vec3 GetVec3(const char* attr = 0) const;
96 Math::vec4 GetVec4(const char* attr = 0) const;
98 Math::mat4 GetMat4(const char* attr = 0) const;
100 Math::transform44 GetTransform44(const char* attr = 0) const;
102 template<typename T> void Get(T& target, const char* attr = 0);
104 template<unsigned int N> void Get(Util::BitField<N>& target, const char* attr = 0);
105
107 Util::String GetOptString(const char* attr, const Util::String& defaultValue) const;
109 bool GetOptBool(const char* attr, bool defaultValue) const;
111 int GetOptInt(const char* attr, int defaultValue) const;
113 float GetOptFloat(const char* attr, float defaultValue) const;
115 Math::vec2 GetOptVec2(const char* attr, const Math::vec2& defaultValue) const;
117 Math::vec4 GetOptVec4(const char* attr, const Math::vec4& defaultValue) const;
119 Math::mat4 GetOptMat4(const char* attr, const Math::mat4& defaultValue) const;
121 Math::transform44 GetOptTransform44(const char* attr, const Math::transform44& defaultValue) const;
122
124 template<typename T> bool GetOpt(T& target, const char* attr = 0);
126 template<typename T> bool GetOpt(T& target, const char* attr, const T& _default);
127
128private:
130 const pjson::value_variant* GetChild(const char * key = 0) const;
131
132 pjson::document* document;
133 const pjson::value_variant* curNode;
137 // 0 terminated buffer containing the raw json file
138 char * buffer = nullptr;
139};
140
141template<> void JsonReader::Get<bool>(bool& ret, const char* attr);
142template<> void JsonReader::Get<Util::Array<uint32_t>>(Util::Array<uint32_t>& ret, const char* attr);
143template<> void JsonReader::Get<Util::Array<int>>(Util::Array<int>& ret, const char* attr);
144template<> void JsonReader::Get<int64_t>(int64_t& ret, const char* attr);
145template<> void JsonReader::Get<int32_t>(int32_t& ret, const char* attr);
146template<> void JsonReader::Get<int16_t>(int16_t& ret, const char* attr);
147template<> void JsonReader::Get<int8_t>(int8_t& ret, const char* attr);
148template<> void JsonReader::Get<char>(char& ret, const char* attr);
149template<> void JsonReader::Get<Math::mat4>(Math::mat4& ret, const char* attr);
150template<> void JsonReader::Get<Math::int2>(Math::int2& ret, const char* attr);
151template<> void JsonReader::Get<Math::vector>(Math::vector& ret, const char* attr);
152template<> void JsonReader::Get<Math::vec4>(Math::vec4& ret, const char* attr);
153template<> void JsonReader::Get<Math::vec3>(Math::vec3& ret, const char* attr);
154template<> void JsonReader::Get<Math::vec2>(Math::vec2& ret, const char* attr);
155template<> void JsonReader::Get<uint32_t>(uint32_t& ret, const char* attr);
156template<> void JsonReader::Get<uint16_t>(uint16_t& ret, const char* attr);
157template<> void JsonReader::Get<uint8_t>(uint8_t& ret, const char* attr);
158template<> void JsonReader::Get<float>(float& ret, const char* attr);
159template<> void JsonReader::Get<Util::Variant>(Util::Variant& ret, const char* attr);
160template<> void JsonReader::Get<Util::String>(Util::String& ret, const char* attr);
161template<> void JsonReader::Get<Util::FourCC>(Util::FourCC& ret, const char* attr);
162template<> void JsonReader::Get<Util::StringAtom>(Util::StringAtom& ret, const char* attr);
163template<> void JsonReader::Get<Util::Array<float>>(Util::Array<float>& ret, const char* attr);
164template<> void JsonReader::Get<Util::Array<Util::String>>(Util::Array<Util::String>& ret, const char* attr);
165template<> bool JsonReader::GetOpt<bool>(bool& ret, const char* attr);
166template<> bool JsonReader::GetOpt<int>(int& ret, const char* attr);
167template<> bool JsonReader::GetOpt<uint16_t>(uint16_t& ret, const char* attr);
168template<> bool JsonReader::GetOpt<uint32_t>(uint32_t& ret, const char* attr);
169template<> bool JsonReader::GetOpt<float>(float& ret, const char* attr);
170template<> bool JsonReader::GetOpt<Math::vec4>(Math::vec4& ret, const char* attr);
171template<> bool JsonReader::GetOpt<Math::quat>(Math::quat& ret, const char* attr);
172template<> bool JsonReader::GetOpt<Math::mat4>(Math::mat4& ret, const char* attr);
173template<> bool JsonReader::GetOpt<Util::String>(Util::String& ret, const char* attr);
174template<> bool JsonReader::GetOpt<Util::Array<int>>(Util::Array<int>& target, const char* attr);
175template<> bool JsonReader::GetOpt<Util::Array<uint32_t>>(Util::Array<uint32_t>& target, const char* attr);
176template<> bool JsonReader::GetOpt<Util::Array<float>>(Util::Array<float>& target, const char* attr);
177template<> bool JsonReader::GetOpt<Util::Array<Util::String>>(Util::Array<Util::String>& target, const char* attr);
178
179//------------------------------------------------------------------------------
182template<typename T>
183inline bool
184JsonReader::GetOpt(T& target, const char* attr)
185{
186 if (this->HasAttr(attr))
187 {
188 this->Get(target, attr);
189 return true;
190 }
191 return false;
192}
193
194//------------------------------------------------------------------------------
197template<typename T>
198inline bool
199JsonReader::GetOpt(T& target, const char* attr, const T& defaultVal)
200{
201 if (!this->GetOpt<T>(target, attr))
202 {
203 target = defaultVal;
204 return false;
205 }
206 return true;
207}
208
209//------------------------------------------------------------------------------
212template<unsigned int N>
213inline void
214JsonReader::Get(Util::BitField<N>& ret, const char* attr)
215{
217 this->Get<Util::Array<int>>(arr);
218
219 unsigned int count = arr.Size();
220 n_assert(count <= N);
221 for (unsigned int i = 0; i < count; i++)
222 {
223 ret.SetBit(arr[i]);
224 }
225}
226
227
228
229} // namespace IO
230//------------------------------------------------------------------------------
Reads json formatted data with random access from a stream using pjson as backend.
Definition jsonreader.h:32
bool GetOptBool(const char *attr, bool defaultValue) const
get optional bool attribute value from current node
Definition jsonreader.cc:661
float GetFloat(const char *attr=0) const
get float attribute value from current node
Definition jsonreader.cc:525
bool SetToParent()
set current node to parent, return false if no parent exists
Definition jsonreader.cc:292
bool IsArray() const
check if current node is an array
Definition jsonreader.cc:331
Util::Stack< const pjson::value_variant * > parents
Definition jsonreader.h:135
virtual void Close()
end reading from the stream
Definition jsonreader.cc:92
Util::String GetCurrentNodeName() const
get name of current node
Definition jsonreader.cc:414
Math::vec4 GetVec4(const char *attr=0) const
get vec4 attribute value from current node
Definition jsonreader.cc:576
__DeclareClass(JsonReader)
bool GetOpt(T &target, const char *attr=0)
generic getter for optional items
Definition jsonreader.h:184
pjson::document * document
Definition jsonreader.h:132
bool SetToFirstChild(const Util::String &name="")
set current node to first child, false if none exists
Definition jsonreader.cc:226
bool SetToNode(const Util::String &path)
set current node as path, accepts / as separator and [number] to access array/object elements
Definition jsonreader.cc:169
JsonReader()
constructor
Definition jsonreader.cc:29
virtual bool Open()
begin reading from the stream
Definition jsonreader.cc:53
Math::vec4 GetOptVec4(const char *attr, const Math::vec4 &defaultValue) const
get optional vec4 attribute value from current node
Definition jsonreader.cc:733
float GetOptFloat(const char *attr, float defaultValue) const
get optional float attribute value from current node
Definition jsonreader.cc:697
void SetToRoot()
sets the current node to root
Definition jsonreader.cc:152
bool HasNode(const Util::String &path)
return true if node exists
Definition jsonreader.cc:110
int GetInt(const char *attr=0) const
get int attribute value from current node
Definition jsonreader.cc:495
Math::transform44 GetTransform44(const char *attr=0) const
get transform44 attribute value from current node
Definition jsonreader.cc:620
bool HasChildren() const
check if current node has children (json object or array)
Definition jsonreader.cc:355
virtual ~JsonReader()
destructor
Definition jsonreader.cc:39
Util::Array< Util::String > GetAttrs() const
return names of all attrs on current node
Definition jsonreader.cc:397
int GetOptInt(const char *attr, int defaultValue) const
get optional int attribute value from current node
Definition jsonreader.cc:679
Math::transform44 GetOptTransform44(const char *attr, const Math::transform44 &defaultValue) const
get transform44 attribute value from current node
Definition jsonreader.cc:769
Math::vec2 GetVec2(const char *attr=0) const
get vec2 attribute value from current node
Definition jsonreader.cc:541
Util::String GetOptString(const char *attr, const Util::String &defaultValue) const
get optional string attribute value from current node
Definition jsonreader.cc:643
Math::vec2 GetOptVec2(const char *attr, const Math::vec2 &defaultValue) const
get vec2 attribute value from current node
Definition jsonreader.cc:715
Util::Stack< IndexT > parentIdx
Definition jsonreader.h:136
void Get(T &target, const char *attr=0)
generic getter for extension types
Util::String GetString(const char *attr=0) const
get string attribute value from current node
Definition jsonreader.cc:450
uint GetUInt(const char *attr=0) const
get unsigned int attribute value from current node
Definition jsonreader.cc:510
const pjson::value_variant * GetChild(const char *key=0) const
Definition jsonreader.cc:428
bool SetToNextChild()
set to next child, false if none exists
Definition jsonreader.cc:265
Math::vec3 GetVec3(const char *attr=0) const
get vec4 attribute value from current node
Definition jsonreader.cc:554
Util::String GetChildNodeName(SizeT childIndex)
gets the childname of the child at index, or empty string if no child exists or has no name.
Definition jsonreader.cc:312
bool HasAttr(const char *attr) const
return true if matching attribute exists on current node
Definition jsonreader.cc:383
const pjson::value_variant * curNode
Definition jsonreader.h:133
SizeT CurrentSize() const
children count
Definition jsonreader.cc:367
Math::mat4 GetMat4(const char *attr=0) const
get mat4 attribute value from current node
Definition jsonreader.cc:598
bool IsObject() const
check if current node is an object (can have keys)
Definition jsonreader.cc:344
Util::StringAtom GetStringAtom(const char *attr=0) const
get stringatom attribute value from current node
Definition jsonreader.cc:465
Math::mat4 GetOptMat4(const char *attr, const Math::mat4 &defaultValue) const
get optional mat4 attribute value from current node
Definition jsonreader.cc:751
IndexT childIdx
Definition jsonreader.h:134
char * buffer
Definition jsonreader.h:138
bool GetBool(const char *attr=0) const
get bool attribute value from current node
Definition jsonreader.cc:480
Stream reader classes provide a specialized read-interface for a stream.
Definition streamreader.h:24
A 4x4 matrix which is described by translation, rotation and scale.
Definition transform44.h:20
Nebula's dynamic array class.
Definition array.h:60
const SizeT Size() const
get number of elements in array
Definition array.h:880
Implements large bit field.
Definition bitfield.h:24
constexpr void SetBit(const uint64_t bitIndex)
set a bit by index
Definition bitfield.h:215
A four-character-code is a quasi-human-readable 32-bit-id.
Definition fourcc.h:19
Nebula's stack class (a FILO container).
Definition stack.h:19
A StringAtom.
Definition stringatom.h:22
An "any type" variable.
Definition variant.h:31
#define n_assert(exp)
Definition debug.h:50
Instances of wrapped stream classes.
Definition orientation.cc:10
Definition jsonreader.h:24
Definition scalar.h:85
A 4x4 single point precision float matrix.
Definition mat4.h:49
A quaternion is usually used to represent an orientation in 3D space.
Definition quat.h:30
A 2-component float vector class.
Definition vec2.h:21
A 3D vector.
Definition vec3.h:40
A 4D vector.
Definition vec4.h:24
A vector is a 3D direction in space.
Definition vector.h:22
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
int IndexT
Definition types.h:48