OVR_JSON.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 PublicHeader:   None
00004 Filename    :   OVR_JSON.h
00005 Content     :   JSON format reader and writer
00006 Created     :   April 9, 2013
00007 Author      :   Brant Lewis
00008 Notes       :
00009 
00010 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00011 
00012 Use of this software is subject to the terms of the Oculus license
00013 agreement provided at the time of installation or download, or which
00014 otherwise accompanies this software in either electronic or hard copy form.
00015 
00016 ************************************************************************************/
00017 
00018 #ifndef OVR_JSON_H
00019 #define OVR_JSON_H
00020 
00021 #include "Kernel/OVR_RefCount.h"
00022 #include "Kernel/OVR_String.h"
00023 #include "Kernel/OVR_List.h"
00024 
00025 namespace OVR {  
00026 
00027 // JSONItemType describes the type of JSON item, specifying the type of
00028 // data that can be obtained from it.
00029 enum JSONItemType
00030 {
00031     JSON_None      = 0,
00032     JSON_Null      = 1,
00033     JSON_Bool      = 2,
00034     JSON_Number    = 3,
00035     JSON_String    = 4,
00036     JSON_Array     = 5,
00037     JSON_Object    = 6
00038 };
00039 
00040 
00041 //-----------------------------------------------------------------------------
00042 // ***** JSON
00043 
00044 // JSON object represents a JSON node that can be either a root of the JSON tree
00045 // or a child item. Every node has a type that describes what is is.
00046 // New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
00047 
00048 class JSON : public RefCountBase<JSON>, public ListNode<JSON>
00049 {
00050 protected:
00051     List<JSON>      Children;
00052 
00053 public:
00054     JSONItemType    Type;       // Type of this JSON node.
00055     String          Name;       // Name part of the {Name, Value} pair in a parent object.
00056     String          Value;
00057     double          dValue;
00058 
00059 public:
00060     ~JSON();
00061 
00062     // *** Creation of NEW JSON objects
00063 
00064     static JSON*    CreateObject()               { return new JSON(JSON_Object);}
00065     static JSON*    CreateNull()                 { return new JSON(JSON_Null); }
00066     static JSON*    CreateArray()                { return new JSON(JSON_Array); }
00067     static JSON*    CreateBool(bool b)           { return createHelper(JSON_Bool, b ? 1.0 : 0.0); }
00068     static JSON*    CreateNumber(double num)     { return createHelper(JSON_Number, num); }
00069     static JSON*    CreateString(const char *s)  { return createHelper(JSON_String, 0.0, s); }
00070 
00071     // Creates a new JSON object from parsing string.
00072     // Returns null pointer and fills in *perror in case of parse error.
00073     static JSON*    Parse(const char* buff, const char** perror = 0);
00074 
00075     // Loads and parses a JSON object from a file.
00076     // Returns 0 and assigns perror with error message on fail.
00077     static JSON*    Load(const char* path, const char** perror = 0);
00078 
00079     // Saves a JSON object to a file.
00080     bool            Save(const char* path);
00081 
00082 
00083     // *** Object Member Access
00084 
00085     // These provide access to child items of the list.
00086     bool            HasItems() const         { return Children.IsEmpty(); }
00087     // Returns first/last child item, or null if child list is empty
00088     JSON*           GetFirstItem()           { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
00089     JSON*           GetLastItem()            { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
00090 
00091     // Counts the number of items in the object; these methods are inefficient.
00092     unsigned        GetItemCount() const;
00093     JSON*           GetItemByIndex(unsigned i);
00094     JSON*           GetItemByName(const char* name);
00095 
00096     // Returns next item in a list of children; 0 if no more items exist.
00097     JSON*           GetNextItem(JSON* item)  { return Children.IsNull(item->pNext) ? 0 : item->pNext; }
00098     JSON*           GetPrevItem(JSON* item)  { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; }
00099 
00100 
00101     // Child item access functions
00102     void            AddItem(const char *string, JSON* item);
00103     void            AddNullItem(const char* name)                    { AddItem(name, CreateNull()); }
00104     void            AddBoolItem(const char* name, bool b)            { AddItem(name, CreateBool(b)); }
00105     void            AddNumberItem(const char* name, double n)        { AddItem(name, CreateNumber(n)); }
00106     void            AddStringItem(const char* name, const char* s)   { AddItem(name, CreateString(s)); }
00107 //    void            ReplaceItem(unsigned index, JSON* new_item);
00108 //    void            DeleteItem(unsigned index);
00109 
00110     // *** Array Element Access
00111 
00112     // Add new elements to the end of array.
00113     void            AddArrayElement(JSON *item);
00114     void            AddArrayNumber(double n)        { AddArrayElement(CreateNumber(n)); }
00115     void            AddArrayString(const char* s)   { AddArrayElement(CreateString(s)); }
00116 
00117     // Accessed array elements; currently inefficient.
00118     int             GetArraySize();
00119     double          GetArrayNumber(int index);
00120     const char*     GetArrayString(int index);
00121 
00122 
00123 protected:
00124     JSON(JSONItemType itemType = JSON_Object);
00125 
00126     static JSON*    createHelper(JSONItemType itemType, double dval, const char* strVal = 0);
00127 
00128     // JSON Parsing helper functions.
00129     const char*     parseValue(const char *buff, const char** perror);
00130     const char*     parseNumber(const char *num);
00131     const char*     parseArray(const char* value, const char** perror);
00132     const char*     parseObject(const char* value, const char** perror);
00133     const char*     parseString(const char* str, const char** perror);
00134 
00135     char*           PrintValue(int depth, bool fmt);
00136     char*           PrintObject(int depth, bool fmt);
00137     char*           PrintArray(int depth, bool fmt);
00138 };
00139 
00140 
00141 }
00142 
00143 #endif


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:11