nuclear@1: /************************************************************************************ nuclear@1: nuclear@1: PublicHeader: None nuclear@1: Filename : OVR_JSON.h nuclear@1: Content : JSON format reader and writer nuclear@1: Created : April 9, 2013 nuclear@1: Author : Brant Lewis nuclear@1: Notes : nuclear@1: nuclear@1: Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. nuclear@1: nuclear@1: Use of this software is subject to the terms of the Oculus license nuclear@1: agreement provided at the time of installation or download, or which nuclear@1: otherwise accompanies this software in either electronic or hard copy form. nuclear@1: nuclear@1: ************************************************************************************/ nuclear@1: nuclear@1: #ifndef OVR_JSON_H nuclear@1: #define OVR_JSON_H nuclear@1: nuclear@1: #include "Kernel/OVR_RefCount.h" nuclear@1: #include "Kernel/OVR_String.h" nuclear@1: #include "Kernel/OVR_List.h" nuclear@1: nuclear@1: namespace OVR { nuclear@1: nuclear@1: // JSONItemType describes the type of JSON item, specifying the type of nuclear@1: // data that can be obtained from it. nuclear@1: enum JSONItemType nuclear@1: { nuclear@1: JSON_None = 0, nuclear@1: JSON_Null = 1, nuclear@1: JSON_Bool = 2, nuclear@1: JSON_Number = 3, nuclear@1: JSON_String = 4, nuclear@1: JSON_Array = 5, nuclear@1: JSON_Object = 6 nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: //----------------------------------------------------------------------------- nuclear@1: // ***** JSON nuclear@1: nuclear@1: // JSON object represents a JSON node that can be either a root of the JSON tree nuclear@1: // or a child item. Every node has a type that describes what is is. nuclear@1: // New JSON trees are typically loaded JSON::Load or created with JSON::Parse. nuclear@1: nuclear@1: class JSON : public RefCountBase, public ListNode nuclear@1: { nuclear@1: protected: nuclear@1: List Children; nuclear@1: nuclear@1: public: nuclear@1: JSONItemType Type; // Type of this JSON node. nuclear@1: String Name; // Name part of the {Name, Value} pair in a parent object. nuclear@1: String Value; nuclear@1: double dValue; nuclear@1: nuclear@1: public: nuclear@1: ~JSON(); nuclear@1: nuclear@1: // *** Creation of NEW JSON objects nuclear@1: nuclear@1: static JSON* CreateObject() { return new JSON(JSON_Object);} nuclear@1: static JSON* CreateNull() { return new JSON(JSON_Null); } nuclear@1: static JSON* CreateArray() { return new JSON(JSON_Array); } nuclear@1: static JSON* CreateBool(bool b) { return createHelper(JSON_Bool, b ? 1.0 : 0.0); } nuclear@1: static JSON* CreateNumber(double num) { return createHelper(JSON_Number, num); } nuclear@1: static JSON* CreateString(const char *s) { return createHelper(JSON_String, 0.0, s); } nuclear@1: nuclear@1: // Creates a new JSON object from parsing string. nuclear@1: // Returns null pointer and fills in *perror in case of parse error. nuclear@1: static JSON* Parse(const char* buff, const char** perror = 0); nuclear@1: nuclear@1: // Loads and parses a JSON object from a file. nuclear@1: // Returns 0 and assigns perror with error message on fail. nuclear@1: static JSON* Load(const char* path, const char** perror = 0); nuclear@1: nuclear@1: // Saves a JSON object to a file. nuclear@1: bool Save(const char* path); nuclear@1: nuclear@1: nuclear@1: // *** Object Member Access nuclear@1: nuclear@1: // These provide access to child items of the list. nuclear@1: bool HasItems() const { return Children.IsEmpty(); } nuclear@1: // Returns first/last child item, or null if child list is empty nuclear@1: JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; } nuclear@1: JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; } nuclear@1: nuclear@1: // Counts the number of items in the object; these methods are inefficient. nuclear@1: unsigned GetItemCount() const; nuclear@1: JSON* GetItemByIndex(unsigned i); nuclear@1: JSON* GetItemByName(const char* name); nuclear@1: nuclear@1: // Returns next item in a list of children; 0 if no more items exist. nuclear@1: JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; } nuclear@1: JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; } nuclear@1: nuclear@1: nuclear@1: // Child item access functions nuclear@1: void AddItem(const char *string, JSON* item); nuclear@1: void AddNullItem(const char* name) { AddItem(name, CreateNull()); } nuclear@1: void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); } nuclear@1: void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); } nuclear@1: void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); } nuclear@1: // void ReplaceItem(unsigned index, JSON* new_item); nuclear@1: // void DeleteItem(unsigned index); nuclear@1: nuclear@1: // *** Array Element Access nuclear@1: nuclear@1: // Add new elements to the end of array. nuclear@1: void AddArrayElement(JSON *item); nuclear@1: void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); } nuclear@1: void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); } nuclear@1: nuclear@1: // Accessed array elements; currently inefficient. nuclear@1: int GetArraySize(); nuclear@1: double GetArrayNumber(int index); nuclear@1: const char* GetArrayString(int index); nuclear@1: nuclear@1: nuclear@1: protected: nuclear@1: JSON(JSONItemType itemType = JSON_Object); nuclear@1: nuclear@1: static JSON* createHelper(JSONItemType itemType, double dval, const char* strVal = 0); nuclear@1: nuclear@1: // JSON Parsing helper functions. nuclear@1: const char* parseValue(const char *buff, const char** perror); nuclear@1: const char* parseNumber(const char *num); nuclear@1: const char* parseArray(const char* value, const char** perror); nuclear@1: const char* parseObject(const char* value, const char** perror); nuclear@1: const char* parseString(const char* str, const char** perror); nuclear@1: nuclear@1: char* PrintValue(int depth, bool fmt); nuclear@1: char* PrintObject(int depth, bool fmt); nuclear@1: char* PrintArray(int depth, bool fmt); nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: } nuclear@1: nuclear@1: #endif