OVR_JSON.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /************************************************************************************
  2. PublicHeader: None
  3. Filename : OVR_JSON.h
  4. Content : JSON format reader and writer
  5. Created : April 9, 2013
  6. Author : Brant Lewis
  7. Notes :
  8. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  9. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  10. you may not use the Oculus VR Rift SDK except in compliance with the License,
  11. which is provided at the time of installation or download, or which
  12. otherwise accompanies this software in either electronic or hard copy form.
  13. You may obtain a copy of the License at
  14. http://www.oculusvr.com/licenses/LICENSE-3.2
  15. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. ************************************************************************************/
  21. #ifndef OVR_JSON_h
  22. #define OVR_JSON_h
  23. #include "OVR_RefCount.h"
  24. #include "OVR_String.h"
  25. #include "OVR_List.h"
  26. namespace OVR {
  27. // JSONItemType describes the type of JSON item, specifying the type of
  28. // data that can be obtained from it.
  29. enum JSONItemType
  30. {
  31. JSON_None = 0,
  32. JSON_Null = 1,
  33. JSON_Bool = 2,
  34. JSON_Number = 3,
  35. JSON_String = 4,
  36. JSON_Array = 5,
  37. JSON_Object = 6
  38. };
  39. //-----------------------------------------------------------------------------
  40. // ***** JSON
  41. // JSON object represents a JSON node that can be either a root of the JSON tree
  42. // or a child item. Every node has a type that describes what is is.
  43. // New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
  44. class JSON : public RefCountBase<JSON>, public ListNode<JSON>
  45. {
  46. protected:
  47. List<JSON> Children;
  48. public:
  49. JSONItemType Type; // Type of this JSON node.
  50. String Name; // Name part of the {Name, Value} pair in a parent object.
  51. String Value;
  52. double dValue;
  53. public:
  54. ~JSON();
  55. // *** Creation of NEW JSON objects
  56. static JSON* CreateObject() { return new JSON(JSON_Object);}
  57. static JSON* CreateNull() { return new JSON(JSON_Null); }
  58. static JSON* CreateArray() { return new JSON(JSON_Array); }
  59. static JSON* CreateBool(bool b);
  60. static JSON* CreateNumber(double num);
  61. static JSON* CreateInt(int num);
  62. static JSON* CreateString(const char *s);
  63. // Creates a new JSON object from parsing string.
  64. // Returns null pointer and fills in *perror in case of parse error.
  65. static JSON* Parse(const char* buff, const char** perror = 0);
  66. // This version works for buffers that are not null terminated strings.
  67. static JSON* ParseBuffer(const char *buff, int len, const char** perror = 0);
  68. // Loads and parses a JSON object from a file.
  69. // Returns 0 and assigns perror with error message on fail.
  70. static JSON* Load(const char* path, const char** perror = 0);
  71. // Saves a JSON object to a file.
  72. bool Save(const char* path);
  73. // Return the String representation of a JSON object.
  74. String Stringify(bool fmt);
  75. // *** Object Member Access
  76. // These provide access to child items of the list.
  77. bool HasItems() const { return Children.IsEmpty(); }
  78. // Returns first/last child item, or null if child list is empty
  79. JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
  80. JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
  81. // Counts the number of items in the object; these methods are inefficient.
  82. unsigned GetItemCount() const;
  83. JSON* GetItemByIndex(unsigned i);
  84. JSON* GetItemByName(const char* name);
  85. // Accessors by name
  86. double GetNumberByName(const char *name, double defValue = 0.0);
  87. int GetIntByName(const char *name, int defValue = 0);
  88. bool GetBoolByName(const char *name, bool defValue = false);
  89. String GetStringByName(const char *name, const String &defValue = "");
  90. int GetArrayByName(const char *name, double values[], int count);
  91. // Returns next item in a list of children; 0 if no more items exist.
  92. JSON* GetNextItem(JSON* item) { return Children.IsLast (item) ? nullptr : item->GetNext(); }
  93. JSON* GetPrevItem(JSON* item) { return Children.IsFirst(item) ? nullptr : item->GetPrev(); }
  94. // Child item access functions
  95. void AddItem(const char *string, JSON* item);
  96. void AddNullItem(const char* name) { AddItem(name, CreateNull()); }
  97. void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); }
  98. void AddIntItem(const char* name, int n) { AddItem(name, CreateInt(n)); }
  99. void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); }
  100. void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); }
  101. // void ReplaceItem(unsigned index, JSON* new_item);
  102. // void DeleteItem(unsigned index);
  103. void RemoveLast();
  104. // *** Array Element Access
  105. // Add new elements to the end of array.
  106. void AddArrayElement(JSON *item);
  107. void InsertArrayElement(int index, JSON* item);
  108. void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); }
  109. void AddArrayInt(int n) { AddArrayElement(CreateInt(n)); }
  110. void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); }
  111. // Accessed array elements; currently inefficient.
  112. int GetArraySize();
  113. double GetArrayNumber(int index);
  114. const char* GetArrayString(int index);
  115. JSON* Copy(); // Create a copy of this object
  116. // Return text value of JSON. Use OVR_FREE when done with return value
  117. char* PrintValue(bool fmt);
  118. protected:
  119. JSON(JSONItemType itemType = JSON_Object);
  120. // JSON Parsing helper functions.
  121. const char* parseValue(const char *buff, const char** perror);
  122. const char* parseNumber(const char *num);
  123. const char* parseArray(const char* value, const char** perror);
  124. const char* parseObject(const char* value, const char** perror);
  125. const char* parseString(const char* str, const char** perror);
  126. char* PrintValue(int depth, bool fmt);
  127. char* PrintObject(int depth, bool fmt);
  128. char* PrintArray(int depth, bool fmt);
  129. };
  130. }
  131. #endif // OVR_JSON_h