ovr_sdk
diff LibOVR/Src/OVR_JSON.h @ 0:1b39a1b46319
initial 0.4.4
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 14 Jan 2015 06:51:16 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/LibOVR/Src/OVR_JSON.h Wed Jan 14 06:51:16 2015 +0200 1.3 @@ -0,0 +1,165 @@ 1.4 +/************************************************************************************ 1.5 + 1.6 +PublicHeader: None 1.7 +Filename : OVR_JSON.h 1.8 +Content : JSON format reader and writer 1.9 +Created : April 9, 2013 1.10 +Author : Brant Lewis 1.11 +Notes : 1.12 + 1.13 +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. 1.14 + 1.15 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 1.16 +you may not use the Oculus VR Rift SDK except in compliance with the License, 1.17 +which is provided at the time of installation or download, or which 1.18 +otherwise accompanies this software in either electronic or hard copy form. 1.19 + 1.20 +You may obtain a copy of the License at 1.21 + 1.22 +http://www.oculusvr.com/licenses/LICENSE-3.2 1.23 + 1.24 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 1.25 +distributed under the License is distributed on an "AS IS" BASIS, 1.26 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.27 +See the License for the specific language governing permissions and 1.28 +limitations under the License. 1.29 + 1.30 +************************************************************************************/ 1.31 + 1.32 +#ifndef OVR_JSON_H 1.33 +#define OVR_JSON_H 1.34 + 1.35 +#include "Kernel/OVR_RefCount.h" 1.36 +#include "Kernel/OVR_String.h" 1.37 +#include "Kernel/OVR_List.h" 1.38 + 1.39 +namespace OVR { 1.40 + 1.41 +// JSONItemType describes the type of JSON item, specifying the type of 1.42 +// data that can be obtained from it. 1.43 +enum JSONItemType 1.44 +{ 1.45 + JSON_None = 0, 1.46 + JSON_Null = 1, 1.47 + JSON_Bool = 2, 1.48 + JSON_Number = 3, 1.49 + JSON_String = 4, 1.50 + JSON_Array = 5, 1.51 + JSON_Object = 6 1.52 +}; 1.53 + 1.54 +//----------------------------------------------------------------------------- 1.55 +// ***** JSON 1.56 + 1.57 +// JSON object represents a JSON node that can be either a root of the JSON tree 1.58 +// or a child item. Every node has a type that describes what is is. 1.59 +// New JSON trees are typically loaded JSON::Load or created with JSON::Parse. 1.60 + 1.61 +class JSON : public RefCountBase<JSON>, public ListNode<JSON> 1.62 +{ 1.63 +protected: 1.64 + List<JSON> Children; 1.65 + 1.66 +public: 1.67 + JSONItemType Type; // Type of this JSON node. 1.68 + String Name; // Name part of the {Name, Value} pair in a parent object. 1.69 + String Value; 1.70 + double dValue; 1.71 + 1.72 +public: 1.73 + ~JSON(); 1.74 + 1.75 + // *** Creation of NEW JSON objects 1.76 + 1.77 + static JSON* CreateObject() { return new JSON(JSON_Object);} 1.78 + static JSON* CreateNull() { return new JSON(JSON_Null); } 1.79 + static JSON* CreateArray() { return new JSON(JSON_Array); } 1.80 + static JSON* CreateBool(bool b); 1.81 + static JSON* CreateNumber(double num); 1.82 + static JSON* CreateInt(int num); 1.83 + static JSON* CreateString(const char *s); 1.84 + 1.85 + // Creates a new JSON object from parsing string. 1.86 + // Returns null pointer and fills in *perror in case of parse error. 1.87 + static JSON* Parse(const char* buff, const char** perror = 0); 1.88 + 1.89 + // This version works for buffers that are not null terminated strings. 1.90 + static JSON* ParseBuffer(const char *buff, int len, const char** perror = 0); 1.91 + 1.92 + // Loads and parses a JSON object from a file. 1.93 + // Returns 0 and assigns perror with error message on fail. 1.94 + static JSON* Load(const char* path, const char** perror = 0); 1.95 + 1.96 + // Saves a JSON object to a file. 1.97 + bool Save(const char* path); 1.98 + 1.99 + // *** Object Member Access 1.100 + 1.101 + // These provide access to child items of the list. 1.102 + bool HasItems() const { return Children.IsEmpty(); } 1.103 + // Returns first/last child item, or null if child list is empty 1.104 + JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; } 1.105 + JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; } 1.106 + 1.107 + // Counts the number of items in the object; these methods are inefficient. 1.108 + unsigned GetItemCount() const; 1.109 + JSON* GetItemByIndex(unsigned i); 1.110 + JSON* GetItemByName(const char* name); 1.111 + 1.112 + // Accessors by name 1.113 + double GetNumberByName(const char *name, double defValue = 0.0); 1.114 + int GetIntByName(const char *name, int defValue = 0); 1.115 + bool GetBoolByName(const char *name, bool defValue = false); 1.116 + String GetStringByName(const char *name, const String &defValue = ""); 1.117 + 1.118 + // Returns next item in a list of children; 0 if no more items exist. 1.119 + JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; } 1.120 + JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; } 1.121 + 1.122 + 1.123 + // Child item access functions 1.124 + void AddItem(const char *string, JSON* item); 1.125 + void AddNullItem(const char* name) { AddItem(name, CreateNull()); } 1.126 + void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); } 1.127 + void AddIntItem(const char* name, int n) { AddItem(name, CreateInt(n)); } 1.128 + void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); } 1.129 + void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); } 1.130 +// void ReplaceItem(unsigned index, JSON* new_item); 1.131 +// void DeleteItem(unsigned index); 1.132 + void RemoveLast(); 1.133 + 1.134 + // *** Array Element Access 1.135 + 1.136 + // Add new elements to the end of array. 1.137 + void AddArrayElement(JSON *item); 1.138 + void InsertArrayElement(int index, JSON* item); 1.139 + void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); } 1.140 + void AddArrayInt(int n) { AddArrayElement(CreateInt(n)); } 1.141 + void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); } 1.142 + 1.143 + // Accessed array elements; currently inefficient. 1.144 + int GetArraySize(); 1.145 + double GetArrayNumber(int index); 1.146 + const char* GetArrayString(int index); 1.147 + 1.148 + JSON* Copy(); // Create a copy of this object 1.149 + 1.150 +protected: 1.151 + JSON(JSONItemType itemType = JSON_Object); 1.152 + 1.153 + // JSON Parsing helper functions. 1.154 + const char* parseValue(const char *buff, const char** perror); 1.155 + const char* parseNumber(const char *num); 1.156 + const char* parseArray(const char* value, const char** perror); 1.157 + const char* parseObject(const char* value, const char** perror); 1.158 + const char* parseString(const char* str, const char** perror); 1.159 + 1.160 + char* PrintValue(int depth, bool fmt); 1.161 + char* PrintObject(int depth, bool fmt); 1.162 + char* PrintArray(int depth, bool fmt); 1.163 +}; 1.164 + 1.165 + 1.166 +} 1.167 + 1.168 +#endif