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