oculus1

annotate libovr/Src/OVR_JSON.h @ 23:0c76f70fb7e9

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Sep 2013 04:13:33 +0300
parents
children
rev   line source
nuclear@1 1 /************************************************************************************
nuclear@1 2
nuclear@1 3 PublicHeader: None
nuclear@1 4 Filename : OVR_JSON.h
nuclear@1 5 Content : JSON format reader and writer
nuclear@1 6 Created : April 9, 2013
nuclear@1 7 Author : Brant Lewis
nuclear@1 8 Notes :
nuclear@1 9
nuclear@1 10 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@1 11
nuclear@1 12 Use of this software is subject to the terms of the Oculus license
nuclear@1 13 agreement provided at the time of installation or download, or which
nuclear@1 14 otherwise accompanies this software in either electronic or hard copy form.
nuclear@1 15
nuclear@1 16 ************************************************************************************/
nuclear@1 17
nuclear@1 18 #ifndef OVR_JSON_H
nuclear@1 19 #define OVR_JSON_H
nuclear@1 20
nuclear@1 21 #include "Kernel/OVR_RefCount.h"
nuclear@1 22 #include "Kernel/OVR_String.h"
nuclear@1 23 #include "Kernel/OVR_List.h"
nuclear@1 24
nuclear@1 25 namespace OVR {
nuclear@1 26
nuclear@1 27 // JSONItemType describes the type of JSON item, specifying the type of
nuclear@1 28 // data that can be obtained from it.
nuclear@1 29 enum JSONItemType
nuclear@1 30 {
nuclear@1 31 JSON_None = 0,
nuclear@1 32 JSON_Null = 1,
nuclear@1 33 JSON_Bool = 2,
nuclear@1 34 JSON_Number = 3,
nuclear@1 35 JSON_String = 4,
nuclear@1 36 JSON_Array = 5,
nuclear@1 37 JSON_Object = 6
nuclear@1 38 };
nuclear@1 39
nuclear@1 40
nuclear@1 41 //-----------------------------------------------------------------------------
nuclear@1 42 // ***** JSON
nuclear@1 43
nuclear@1 44 // JSON object represents a JSON node that can be either a root of the JSON tree
nuclear@1 45 // or a child item. Every node has a type that describes what is is.
nuclear@1 46 // New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
nuclear@1 47
nuclear@1 48 class JSON : public RefCountBase<JSON>, public ListNode<JSON>
nuclear@1 49 {
nuclear@1 50 protected:
nuclear@1 51 List<JSON> Children;
nuclear@1 52
nuclear@1 53 public:
nuclear@1 54 JSONItemType Type; // Type of this JSON node.
nuclear@1 55 String Name; // Name part of the {Name, Value} pair in a parent object.
nuclear@1 56 String Value;
nuclear@1 57 double dValue;
nuclear@1 58
nuclear@1 59 public:
nuclear@1 60 ~JSON();
nuclear@1 61
nuclear@1 62 // *** Creation of NEW JSON objects
nuclear@1 63
nuclear@1 64 static JSON* CreateObject() { return new JSON(JSON_Object);}
nuclear@1 65 static JSON* CreateNull() { return new JSON(JSON_Null); }
nuclear@1 66 static JSON* CreateArray() { return new JSON(JSON_Array); }
nuclear@1 67 static JSON* CreateBool(bool b) { return createHelper(JSON_Bool, b ? 1.0 : 0.0); }
nuclear@1 68 static JSON* CreateNumber(double num) { return createHelper(JSON_Number, num); }
nuclear@1 69 static JSON* CreateString(const char *s) { return createHelper(JSON_String, 0.0, s); }
nuclear@1 70
nuclear@1 71 // Creates a new JSON object from parsing string.
nuclear@1 72 // Returns null pointer and fills in *perror in case of parse error.
nuclear@1 73 static JSON* Parse(const char* buff, const char** perror = 0);
nuclear@1 74
nuclear@1 75 // Loads and parses a JSON object from a file.
nuclear@1 76 // Returns 0 and assigns perror with error message on fail.
nuclear@1 77 static JSON* Load(const char* path, const char** perror = 0);
nuclear@1 78
nuclear@1 79 // Saves a JSON object to a file.
nuclear@1 80 bool Save(const char* path);
nuclear@1 81
nuclear@1 82
nuclear@1 83 // *** Object Member Access
nuclear@1 84
nuclear@1 85 // These provide access to child items of the list.
nuclear@1 86 bool HasItems() const { return Children.IsEmpty(); }
nuclear@1 87 // Returns first/last child item, or null if child list is empty
nuclear@1 88 JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
nuclear@1 89 JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
nuclear@1 90
nuclear@1 91 // Counts the number of items in the object; these methods are inefficient.
nuclear@1 92 unsigned GetItemCount() const;
nuclear@1 93 JSON* GetItemByIndex(unsigned i);
nuclear@1 94 JSON* GetItemByName(const char* name);
nuclear@1 95
nuclear@1 96 // Returns next item in a list of children; 0 if no more items exist.
nuclear@1 97 JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; }
nuclear@1 98 JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; }
nuclear@1 99
nuclear@1 100
nuclear@1 101 // Child item access functions
nuclear@1 102 void AddItem(const char *string, JSON* item);
nuclear@1 103 void AddNullItem(const char* name) { AddItem(name, CreateNull()); }
nuclear@1 104 void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); }
nuclear@1 105 void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); }
nuclear@1 106 void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); }
nuclear@1 107 // void ReplaceItem(unsigned index, JSON* new_item);
nuclear@1 108 // void DeleteItem(unsigned index);
nuclear@1 109
nuclear@1 110 // *** Array Element Access
nuclear@1 111
nuclear@1 112 // Add new elements to the end of array.
nuclear@1 113 void AddArrayElement(JSON *item);
nuclear@1 114 void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); }
nuclear@1 115 void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); }
nuclear@1 116
nuclear@1 117 // Accessed array elements; currently inefficient.
nuclear@1 118 int GetArraySize();
nuclear@1 119 double GetArrayNumber(int index);
nuclear@1 120 const char* GetArrayString(int index);
nuclear@1 121
nuclear@1 122
nuclear@1 123 protected:
nuclear@1 124 JSON(JSONItemType itemType = JSON_Object);
nuclear@1 125
nuclear@1 126 static JSON* createHelper(JSONItemType itemType, double dval, const char* strVal = 0);
nuclear@1 127
nuclear@1 128 // JSON Parsing helper functions.
nuclear@1 129 const char* parseValue(const char *buff, const char** perror);
nuclear@1 130 const char* parseNumber(const char *num);
nuclear@1 131 const char* parseArray(const char* value, const char** perror);
nuclear@1 132 const char* parseObject(const char* value, const char** perror);
nuclear@1 133 const char* parseString(const char* str, const char** perror);
nuclear@1 134
nuclear@1 135 char* PrintValue(int depth, bool fmt);
nuclear@1 136 char* PrintObject(int depth, bool fmt);
nuclear@1 137 char* PrintArray(int depth, bool fmt);
nuclear@1 138 };
nuclear@1 139
nuclear@1 140
nuclear@1 141 }
nuclear@1 142
nuclear@1 143 #endif