ovr_sdk

view LibOVR/Src/OVR_JSON.h @ 3:f12a8f74fe1f

added the Xcode project
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 21 Jan 2015 11:37:50 +0200
parents
children
line source
1 /************************************************************************************
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 :
10 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
12 Licensed under the Oculus VR Rift SDK License Version 3.2 (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.
17 You may obtain a copy of the License at
19 http://www.oculusvr.com/licenses/LICENSE-3.2
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.
27 ************************************************************************************/
29 #ifndef OVR_JSON_H
30 #define OVR_JSON_H
32 #include "Kernel/OVR_RefCount.h"
33 #include "Kernel/OVR_String.h"
34 #include "Kernel/OVR_List.h"
36 namespace OVR {
38 // JSONItemType describes the type of JSON item, specifying the type of
39 // data that can be obtained from it.
40 enum JSONItemType
41 {
42 JSON_None = 0,
43 JSON_Null = 1,
44 JSON_Bool = 2,
45 JSON_Number = 3,
46 JSON_String = 4,
47 JSON_Array = 5,
48 JSON_Object = 6
49 };
51 //-----------------------------------------------------------------------------
52 // ***** JSON
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.
58 class JSON : public RefCountBase<JSON>, public ListNode<JSON>
59 {
60 protected:
61 List<JSON> Children;
63 public:
64 JSONItemType Type; // Type of this JSON node.
65 String Name; // Name part of the {Name, Value} pair in a parent object.
66 String Value;
67 double dValue;
69 public:
70 ~JSON();
72 // *** Creation of NEW JSON objects
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);
78 static JSON* CreateNumber(double num);
79 static JSON* CreateInt(int num);
80 static JSON* CreateString(const char *s);
82 // Creates a new JSON object from parsing string.
83 // Returns null pointer and fills in *perror in case of parse error.
84 static JSON* Parse(const char* buff, const char** perror = 0);
86 // This version works for buffers that are not null terminated strings.
87 static JSON* ParseBuffer(const char *buff, int len, const char** perror = 0);
89 // Loads and parses a JSON object from a file.
90 // Returns 0 and assigns perror with error message on fail.
91 static JSON* Load(const char* path, const char** perror = 0);
93 // Saves a JSON object to a file.
94 bool Save(const char* path);
96 // *** Object Member Access
98 // These provide access to child items of the list.
99 bool HasItems() const { return Children.IsEmpty(); }
100 // Returns first/last child item, or null if child list is empty
101 JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
102 JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
104 // Counts the number of items in the object; these methods are inefficient.
105 unsigned GetItemCount() const;
106 JSON* GetItemByIndex(unsigned i);
107 JSON* GetItemByName(const char* name);
109 // Accessors by name
110 double GetNumberByName(const char *name, double defValue = 0.0);
111 int GetIntByName(const char *name, int defValue = 0);
112 bool GetBoolByName(const char *name, bool defValue = false);
113 String GetStringByName(const char *name, const String &defValue = "");
115 // Returns next item in a list of children; 0 if no more items exist.
116 JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; }
117 JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; }
120 // Child item access functions
121 void AddItem(const char *string, JSON* item);
122 void AddNullItem(const char* name) { AddItem(name, CreateNull()); }
123 void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); }
124 void AddIntItem(const char* name, int n) { AddItem(name, CreateInt(n)); }
125 void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); }
126 void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); }
127 // void ReplaceItem(unsigned index, JSON* new_item);
128 // void DeleteItem(unsigned index);
129 void RemoveLast();
131 // *** Array Element Access
133 // Add new elements to the end of array.
134 void AddArrayElement(JSON *item);
135 void InsertArrayElement(int index, JSON* item);
136 void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); }
137 void AddArrayInt(int n) { AddArrayElement(CreateInt(n)); }
138 void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); }
140 // Accessed array elements; currently inefficient.
141 int GetArraySize();
142 double GetArrayNumber(int index);
143 const char* GetArrayString(int index);
145 JSON* Copy(); // Create a copy of this object
147 protected:
148 JSON(JSONItemType itemType = JSON_Object);
150 // JSON Parsing helper functions.
151 const char* parseValue(const char *buff, const char** perror);
152 const char* parseNumber(const char *num);
153 const char* parseArray(const char* value, const char** perror);
154 const char* parseObject(const char* value, const char** perror);
155 const char* parseString(const char* str, const char** perror);
157 char* PrintValue(int depth, bool fmt);
158 char* PrintObject(int depth, bool fmt);
159 char* PrintArray(int depth, bool fmt);
160 };
163 }
165 #endif