1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/************************************************************************************
PublicHeader: None
Filename : OVR_JSON.h
Content : JSON format reader and writer
Created : April 9, 2013
Author : Brant Lewis
Notes :
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
https://developer.oculus.com/licenses/oculusmastersdk-1.0
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#ifndef OVR_JSON_h
#define OVR_JSON_h
#include "OVR_List.h"
#include "OVR_RefCount.h"
#include "OVR_String.h"
namespace OVR {
// JSONItemType describes the type of JSON item, specifying the type of
// data that can be obtained from it.
enum JSONItemType {
JSON_None = 0,
JSON_Null = 1,
JSON_Bool = 2,
JSON_Number = 3,
JSON_String = 4,
JSON_Array = 5,
JSON_Object = 6
};
//-----------------------------------------------------------------------------
// ***** JSON
// JSON object represents a JSON node that can be either a root of the JSON tree
// or a child item. Every node has a type that describes what is is.
// New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
class JSON : public RefCountBase<JSON>, public ListNode<JSON> {
protected:
List<JSON> Children;
public:
JSONItemType Type; // Type of this JSON node.
String Name; // Name part of the {Name, Value} pair in a parent object.
String Value;
double dValue;
public:
~JSON();
// *** Creation of NEW JSON objects
static JSON* CreateObject() {
return new JSON(JSON_Object);
}
static JSON* CreateNull() {
return new JSON(JSON_Null);
}
static JSON* CreateArray() {
return new JSON(JSON_Array);
}
static JSON* CreateBool(bool b);
static JSON* CreateNumber(double num);
static JSON* CreateInt(int num);
static JSON* CreateString(const char* s);
// Creates a new JSON object from parsing string.
// Returns null pointer and fills in *perror in case of parse error.
static JSON* Parse(const char* buff, const char** perror = 0);
// This version works for buffers that are not null terminated strings.
static JSON* ParseBuffer(const char* buff, int len, const char** perror = 0);
// Loads and parses a JSON object from a file.
// Returns 0 and assigns perror with error message on fail.
static JSON* Load(const char* path, const char** perror = 0);
// Saves a JSON object to a file.
bool Save(const char* path);
// Return the String representation of a JSON object.
String Stringify(bool fmt);
// *** Object Member Access
// These provide access to child items of the list.
bool HasItems() const {
return Children.IsEmpty();
}
// Returns first/last child item, or null if child list is empty
JSON* GetFirstItem() {
return (!Children.IsEmpty()) ? Children.GetFirst() : 0;
}
JSON* GetLastItem() {
return (!Children.IsEmpty()) ? Children.GetLast() : 0;
}
// Counts the number of items in the object; these methods are inefficient.
unsigned GetItemCount() const;
JSON* GetItemByIndex(unsigned i);
JSON* GetItemByName(const char* name);
// Accessors by name
double GetNumberByName(const char* name, double defValue = 0.0);
int GetIntByName(const char* name, int defValue = 0);
bool GetBoolByName(const char* name, bool defValue = false);
String GetStringByName(const char* name, const String& defValue = "");
template <typename T>
int GetArrayByName(const char* name, T values[], int count, T defaultValue = T(0)) {
// Zero values in case one or more elements not present in JSON
for (int i = 0; i < count; i++)
values[i] = defaultValue;
JSON* array = GetItemByName(name);
if (!array || array->Type != JSON_Array)
return 0;
int i = 0;
for (JSON* child = array->Children.GetFirst(); !array->Children.IsNull(child);
child = array->Children.GetNext(child)) {
if (i >= count)
break;
values[i++] = (T)child->dValue;
}
OVR_ASSERT(i <= count);
return i;
}
// Returns next item in a list of children; 0 if no more items exist.
JSON* GetNextItem(JSON* item) {
return Children.IsLast(item) ? nullptr : item->GetNext();
}
JSON* GetPrevItem(JSON* item) {
return Children.IsFirst(item) ? nullptr : item->GetPrev();
}
// Child item access functions
void AddItem(const char* string, JSON* item);
void AddNullItem(const char* name) {
AddItem(name, CreateNull());
}
void AddBoolItem(const char* name, bool b) {
AddItem(name, CreateBool(b));
}
void AddIntItem(const char* name, int n) {
AddItem(name, CreateInt(n));
}
void AddNumberItem(const char* name, double n) {
AddItem(name, CreateNumber(n));
}
void AddStringItem(const char* name, const char* s) {
AddItem(name, CreateString(s));
}
// void ReplaceItem(unsigned index, JSON* new_item);
// void DeleteItem(unsigned index);
void RemoveLast();
// *** Array Element Access
// Add new elements to the end of array.
void AddArrayElement(JSON* item);
void InsertArrayElement(int index, JSON* item);
void AddArrayNumber(double n) {
AddArrayElement(CreateNumber(n));
}
void AddArrayInt(int n) {
AddArrayElement(CreateInt(n));
}
void AddArrayString(const char* s) {
AddArrayElement(CreateString(s));
}
template <typename T>
void AddNumberArray(const char* name, const T array[], int arraySize) {
JSON* node = JSON::CreateArray();
AddItem(name, node);
for (int i = 0; i < arraySize; i++)
node->AddArrayNumber((double)array[i]);
}
// Accessed array elements; currently inefficient.
int GetArraySize();
double GetArrayNumber(int index);
const char* GetArrayString(int index);
JSON* Copy(); // Create a copy of this object
// Return text value of JSON. Use OVR_FREE when done with return value
char* PrintValue(bool fmt);
protected:
JSON(JSONItemType itemType = JSON_Object);
// JSON Parsing helper functions.
const char* parseValue(const char* buff, const char** perror);
const char* parseNumber(const char* num);
const char* parseArray(const char* value, const char** perror);
const char* parseObject(const char* value, const char** perror);
const char* parseString(const char* str, const char** perror);
char* PrintValue(int depth, bool fmt);
char* PrintObject(int depth, bool fmt);
char* PrintArray(int depth, bool fmt);
};
} // namespace OVR
#endif // OVR_JSON_h
|