nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: PublicHeader: None nuclear@0: Filename : OVR_JSON.h nuclear@0: Content : JSON format reader and writer nuclear@0: Created : April 9, 2013 nuclear@0: Author : Brant Lewis nuclear@0: Notes : nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #ifndef OVR_JSON_H nuclear@0: #define OVR_JSON_H nuclear@0: nuclear@0: #include "Kernel/OVR_RefCount.h" nuclear@0: #include "Kernel/OVR_String.h" nuclear@0: #include "Kernel/OVR_List.h" nuclear@0: nuclear@0: namespace OVR { nuclear@0: nuclear@0: // JSONItemType describes the type of JSON item, specifying the type of nuclear@0: // data that can be obtained from it. nuclear@0: enum JSONItemType nuclear@0: { nuclear@0: JSON_None = 0, nuclear@0: JSON_Null = 1, nuclear@0: JSON_Bool = 2, nuclear@0: JSON_Number = 3, nuclear@0: JSON_String = 4, nuclear@0: JSON_Array = 5, nuclear@0: JSON_Object = 6 nuclear@0: }; nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // ***** JSON nuclear@0: nuclear@0: // JSON object represents a JSON node that can be either a root of the JSON tree nuclear@0: // or a child item. Every node has a type that describes what is is. nuclear@0: // New JSON trees are typically loaded JSON::Load or created with JSON::Parse. nuclear@0: nuclear@0: class JSON : public RefCountBase, public ListNode nuclear@0: { nuclear@0: protected: nuclear@0: List Children; nuclear@0: nuclear@0: public: nuclear@0: JSONItemType Type; // Type of this JSON node. nuclear@0: String Name; // Name part of the {Name, Value} pair in a parent object. nuclear@0: String Value; nuclear@0: double dValue; nuclear@0: nuclear@0: public: nuclear@0: ~JSON(); nuclear@0: nuclear@0: // *** Creation of NEW JSON objects nuclear@0: nuclear@0: static JSON* CreateObject() { return new JSON(JSON_Object);} nuclear@0: static JSON* CreateNull() { return new JSON(JSON_Null); } nuclear@0: static JSON* CreateArray() { return new JSON(JSON_Array); } nuclear@0: static JSON* CreateBool(bool b); nuclear@0: static JSON* CreateNumber(double num); nuclear@0: static JSON* CreateInt(int num); nuclear@0: static JSON* CreateString(const char *s); nuclear@0: nuclear@0: // Creates a new JSON object from parsing string. nuclear@0: // Returns null pointer and fills in *perror in case of parse error. nuclear@0: static JSON* Parse(const char* buff, const char** perror = 0); nuclear@0: nuclear@0: // This version works for buffers that are not null terminated strings. nuclear@0: static JSON* ParseBuffer(const char *buff, int len, const char** perror = 0); nuclear@0: nuclear@0: // Loads and parses a JSON object from a file. nuclear@0: // Returns 0 and assigns perror with error message on fail. nuclear@0: static JSON* Load(const char* path, const char** perror = 0); nuclear@0: nuclear@0: // Saves a JSON object to a file. nuclear@0: bool Save(const char* path); nuclear@0: nuclear@0: // *** Object Member Access nuclear@0: nuclear@0: // These provide access to child items of the list. nuclear@0: bool HasItems() const { return Children.IsEmpty(); } nuclear@0: // Returns first/last child item, or null if child list is empty nuclear@0: JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; } nuclear@0: JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; } nuclear@0: nuclear@0: // Counts the number of items in the object; these methods are inefficient. nuclear@0: unsigned GetItemCount() const; nuclear@0: JSON* GetItemByIndex(unsigned i); nuclear@0: JSON* GetItemByName(const char* name); nuclear@0: nuclear@0: // Accessors by name nuclear@0: double GetNumberByName(const char *name, double defValue = 0.0); nuclear@0: int GetIntByName(const char *name, int defValue = 0); nuclear@0: bool GetBoolByName(const char *name, bool defValue = false); nuclear@0: String GetStringByName(const char *name, const String &defValue = ""); nuclear@0: nuclear@0: // Returns next item in a list of children; 0 if no more items exist. nuclear@0: JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; } nuclear@0: JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; } nuclear@0: nuclear@0: nuclear@0: // Child item access functions nuclear@0: void AddItem(const char *string, JSON* item); nuclear@0: void AddNullItem(const char* name) { AddItem(name, CreateNull()); } nuclear@0: void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); } nuclear@0: void AddIntItem(const char* name, int n) { AddItem(name, CreateInt(n)); } nuclear@0: void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); } nuclear@0: void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); } nuclear@0: // void ReplaceItem(unsigned index, JSON* new_item); nuclear@0: // void DeleteItem(unsigned index); nuclear@0: void RemoveLast(); nuclear@0: nuclear@0: // *** Array Element Access nuclear@0: nuclear@0: // Add new elements to the end of array. nuclear@0: void AddArrayElement(JSON *item); nuclear@0: void InsertArrayElement(int index, JSON* item); nuclear@0: void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); } nuclear@0: void AddArrayInt(int n) { AddArrayElement(CreateInt(n)); } nuclear@0: void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); } nuclear@0: nuclear@0: // Accessed array elements; currently inefficient. nuclear@0: int GetArraySize(); nuclear@0: double GetArrayNumber(int index); nuclear@0: const char* GetArrayString(int index); nuclear@0: nuclear@0: JSON* Copy(); // Create a copy of this object nuclear@0: nuclear@0: protected: nuclear@0: JSON(JSONItemType itemType = JSON_Object); nuclear@0: nuclear@0: // JSON Parsing helper functions. nuclear@0: const char* parseValue(const char *buff, const char** perror); nuclear@0: const char* parseNumber(const char *num); nuclear@0: const char* parseArray(const char* value, const char** perror); nuclear@0: const char* parseObject(const char* value, const char** perror); nuclear@0: const char* parseString(const char* str, const char** perror); nuclear@0: nuclear@0: char* PrintValue(int depth, bool fmt); nuclear@0: char* PrintObject(int depth, bool fmt); nuclear@0: char* PrintArray(int depth, bool fmt); nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: } nuclear@0: nuclear@0: #endif