rev |
line source |
nuclear@0
|
1 /************************************************************************************
|
nuclear@0
|
2
|
nuclear@0
|
3 PublicHeader: None
|
nuclear@0
|
4 Filename : OVR_JSON.h
|
nuclear@0
|
5 Content : JSON format reader and writer
|
nuclear@0
|
6 Created : April 9, 2013
|
nuclear@0
|
7 Author : Brant Lewis
|
nuclear@0
|
8 Notes :
|
nuclear@0
|
9
|
nuclear@0
|
10 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
|
nuclear@0
|
11
|
nuclear@0
|
12 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
|
nuclear@0
|
13 you may not use the Oculus VR Rift SDK except in compliance with the License,
|
nuclear@0
|
14 which is provided at the time of installation or download, or which
|
nuclear@0
|
15 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@0
|
16
|
nuclear@0
|
17 You may obtain a copy of the License at
|
nuclear@0
|
18
|
nuclear@0
|
19 http://www.oculusvr.com/licenses/LICENSE-3.2
|
nuclear@0
|
20
|
nuclear@0
|
21 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
|
nuclear@0
|
22 distributed under the License is distributed on an "AS IS" BASIS,
|
nuclear@0
|
23 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
nuclear@0
|
24 See the License for the specific language governing permissions and
|
nuclear@0
|
25 limitations under the License.
|
nuclear@0
|
26
|
nuclear@0
|
27 ************************************************************************************/
|
nuclear@0
|
28
|
nuclear@0
|
29 #ifndef OVR_JSON_H
|
nuclear@0
|
30 #define OVR_JSON_H
|
nuclear@0
|
31
|
nuclear@0
|
32 #include "Kernel/OVR_RefCount.h"
|
nuclear@0
|
33 #include "Kernel/OVR_String.h"
|
nuclear@0
|
34 #include "Kernel/OVR_List.h"
|
nuclear@0
|
35
|
nuclear@0
|
36 namespace OVR {
|
nuclear@0
|
37
|
nuclear@0
|
38 // JSONItemType describes the type of JSON item, specifying the type of
|
nuclear@0
|
39 // data that can be obtained from it.
|
nuclear@0
|
40 enum JSONItemType
|
nuclear@0
|
41 {
|
nuclear@0
|
42 JSON_None = 0,
|
nuclear@0
|
43 JSON_Null = 1,
|
nuclear@0
|
44 JSON_Bool = 2,
|
nuclear@0
|
45 JSON_Number = 3,
|
nuclear@0
|
46 JSON_String = 4,
|
nuclear@0
|
47 JSON_Array = 5,
|
nuclear@0
|
48 JSON_Object = 6
|
nuclear@0
|
49 };
|
nuclear@0
|
50
|
nuclear@0
|
51 //-----------------------------------------------------------------------------
|
nuclear@0
|
52 // ***** JSON
|
nuclear@0
|
53
|
nuclear@0
|
54 // JSON object represents a JSON node that can be either a root of the JSON tree
|
nuclear@0
|
55 // or a child item. Every node has a type that describes what is is.
|
nuclear@0
|
56 // New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
|
nuclear@0
|
57
|
nuclear@0
|
58 class JSON : public RefCountBase<JSON>, public ListNode<JSON>
|
nuclear@0
|
59 {
|
nuclear@0
|
60 protected:
|
nuclear@0
|
61 List<JSON> Children;
|
nuclear@0
|
62
|
nuclear@0
|
63 public:
|
nuclear@0
|
64 JSONItemType Type; // Type of this JSON node.
|
nuclear@0
|
65 String Name; // Name part of the {Name, Value} pair in a parent object.
|
nuclear@0
|
66 String Value;
|
nuclear@0
|
67 double dValue;
|
nuclear@0
|
68
|
nuclear@0
|
69 public:
|
nuclear@0
|
70 ~JSON();
|
nuclear@0
|
71
|
nuclear@0
|
72 // *** Creation of NEW JSON objects
|
nuclear@0
|
73
|
nuclear@0
|
74 static JSON* CreateObject() { return new JSON(JSON_Object);}
|
nuclear@0
|
75 static JSON* CreateNull() { return new JSON(JSON_Null); }
|
nuclear@0
|
76 static JSON* CreateArray() { return new JSON(JSON_Array); }
|
nuclear@0
|
77 static JSON* CreateBool(bool b);
|
nuclear@0
|
78 static JSON* CreateNumber(double num);
|
nuclear@0
|
79 static JSON* CreateInt(int num);
|
nuclear@0
|
80 static JSON* CreateString(const char *s);
|
nuclear@0
|
81
|
nuclear@0
|
82 // Creates a new JSON object from parsing string.
|
nuclear@0
|
83 // Returns null pointer and fills in *perror in case of parse error.
|
nuclear@0
|
84 static JSON* Parse(const char* buff, const char** perror = 0);
|
nuclear@0
|
85
|
nuclear@0
|
86 // This version works for buffers that are not null terminated strings.
|
nuclear@0
|
87 static JSON* ParseBuffer(const char *buff, int len, const char** perror = 0);
|
nuclear@0
|
88
|
nuclear@0
|
89 // Loads and parses a JSON object from a file.
|
nuclear@0
|
90 // Returns 0 and assigns perror with error message on fail.
|
nuclear@0
|
91 static JSON* Load(const char* path, const char** perror = 0);
|
nuclear@0
|
92
|
nuclear@0
|
93 // Saves a JSON object to a file.
|
nuclear@0
|
94 bool Save(const char* path);
|
nuclear@0
|
95
|
nuclear@0
|
96 // *** Object Member Access
|
nuclear@0
|
97
|
nuclear@0
|
98 // These provide access to child items of the list.
|
nuclear@0
|
99 bool HasItems() const { return Children.IsEmpty(); }
|
nuclear@0
|
100 // Returns first/last child item, or null if child list is empty
|
nuclear@0
|
101 JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
|
nuclear@0
|
102 JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
|
nuclear@0
|
103
|
nuclear@0
|
104 // Counts the number of items in the object; these methods are inefficient.
|
nuclear@0
|
105 unsigned GetItemCount() const;
|
nuclear@0
|
106 JSON* GetItemByIndex(unsigned i);
|
nuclear@0
|
107 JSON* GetItemByName(const char* name);
|
nuclear@0
|
108
|
nuclear@0
|
109 // Accessors by name
|
nuclear@0
|
110 double GetNumberByName(const char *name, double defValue = 0.0);
|
nuclear@0
|
111 int GetIntByName(const char *name, int defValue = 0);
|
nuclear@0
|
112 bool GetBoolByName(const char *name, bool defValue = false);
|
nuclear@0
|
113 String GetStringByName(const char *name, const String &defValue = "");
|
nuclear@0
|
114
|
nuclear@0
|
115 // Returns next item in a list of children; 0 if no more items exist.
|
nuclear@0
|
116 JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; }
|
nuclear@0
|
117 JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; }
|
nuclear@0
|
118
|
nuclear@0
|
119
|
nuclear@0
|
120 // Child item access functions
|
nuclear@0
|
121 void AddItem(const char *string, JSON* item);
|
nuclear@0
|
122 void AddNullItem(const char* name) { AddItem(name, CreateNull()); }
|
nuclear@0
|
123 void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); }
|
nuclear@0
|
124 void AddIntItem(const char* name, int n) { AddItem(name, CreateInt(n)); }
|
nuclear@0
|
125 void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); }
|
nuclear@0
|
126 void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); }
|
nuclear@0
|
127 // void ReplaceItem(unsigned index, JSON* new_item);
|
nuclear@0
|
128 // void DeleteItem(unsigned index);
|
nuclear@0
|
129 void RemoveLast();
|
nuclear@0
|
130
|
nuclear@0
|
131 // *** Array Element Access
|
nuclear@0
|
132
|
nuclear@0
|
133 // Add new elements to the end of array.
|
nuclear@0
|
134 void AddArrayElement(JSON *item);
|
nuclear@0
|
135 void InsertArrayElement(int index, JSON* item);
|
nuclear@0
|
136 void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); }
|
nuclear@0
|
137 void AddArrayInt(int n) { AddArrayElement(CreateInt(n)); }
|
nuclear@0
|
138 void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); }
|
nuclear@0
|
139
|
nuclear@0
|
140 // Accessed array elements; currently inefficient.
|
nuclear@0
|
141 int GetArraySize();
|
nuclear@0
|
142 double GetArrayNumber(int index);
|
nuclear@0
|
143 const char* GetArrayString(int index);
|
nuclear@0
|
144
|
nuclear@0
|
145 JSON* Copy(); // Create a copy of this object
|
nuclear@0
|
146
|
nuclear@0
|
147 protected:
|
nuclear@0
|
148 JSON(JSONItemType itemType = JSON_Object);
|
nuclear@0
|
149
|
nuclear@0
|
150 // JSON Parsing helper functions.
|
nuclear@0
|
151 const char* parseValue(const char *buff, const char** perror);
|
nuclear@0
|
152 const char* parseNumber(const char *num);
|
nuclear@0
|
153 const char* parseArray(const char* value, const char** perror);
|
nuclear@0
|
154 const char* parseObject(const char* value, const char** perror);
|
nuclear@0
|
155 const char* parseString(const char* str, const char** perror);
|
nuclear@0
|
156
|
nuclear@0
|
157 char* PrintValue(int depth, bool fmt);
|
nuclear@0
|
158 char* PrintObject(int depth, bool fmt);
|
nuclear@0
|
159 char* PrintArray(int depth, bool fmt);
|
nuclear@0
|
160 };
|
nuclear@0
|
161
|
nuclear@0
|
162
|
nuclear@0
|
163 }
|
nuclear@0
|
164
|
nuclear@0
|
165 #endif
|