Json.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. /*
  3. Copyright (c) 2009 Dave Gamble
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "../Common.h"
  21. NS_BF_BEGIN
  22. /* The cJSON structure: */
  23. class Json
  24. {
  25. public:
  26. /* cJSON Types: */
  27. enum Type
  28. {
  29. Type_False,
  30. Type_True,
  31. Type_NULL,
  32. Type_Number,
  33. Type_String,
  34. Type_Array,
  35. Type_Object,
  36. Type_IsReference = 256
  37. };
  38. Json* mNext;
  39. Json* mPrev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
  40. Json* mChild; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
  41. Type mType; /* The type of the item, as above. */
  42. char* mValueString; /* The item's string, if type==cJSON_String */
  43. int mValueInt; /* The item's number, if type==cJSON_Number */
  44. double mValueDouble; /* The item's number, if type==cJSON_Number */
  45. char* mName; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
  46. const char* mSrcStart;
  47. const char* mSrcEnd;
  48. struct Hooks
  49. {
  50. void *(*malloc_fn)(size_t sz);
  51. void(*free_fn)(void *ptr);
  52. };
  53. static void *(*malloc)(size_t sz);
  54. static void (*free)(void *ptr);
  55. public:
  56. ~Json();
  57. /* Supply malloc, realloc and free functions to Json */
  58. void InitHooks(Hooks* hooks);
  59. /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
  60. char* Print();
  61. /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call Delete when finished. */
  62. static Json* Parse(const char *value);
  63. /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
  64. char *PrintUnformatted();
  65. /* Returns the number of items in an array (or object). */
  66. int GetArraySize();
  67. /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
  68. Json* GetArrayItem(int item);
  69. /* Get item "string" from object. Case insensitive. */
  70. Json* GetObjectItem(const char *string);
  71. /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when Parse() returns 0. 0 when Parse() succeeds. */
  72. static const char *GetErrorPtr(void);
  73. /* These calls create a cJSON item of the appropriate type. */
  74. static Json* CreateNull(void);
  75. static Json* CreateTrue(void);
  76. static Json* CreateFalse(void);
  77. static Json* CreateBool(int b);
  78. static Json* CreateNumber(double num);
  79. static Json* CreateString(const char *string);
  80. static Json* CreateArray(void);
  81. static Json* CreateObject(void);
  82. /* These utilities create an Array of count items. */
  83. static Json* CreateIntArray(const int *numbers, int count);
  84. static Json* CreateFloatArray(const float *numbers, int count);
  85. static Json* CreateDoubleArray(const double *numbers, int count);
  86. static Json* CreateStringArray(const char **strings, int count);
  87. /* Append item to the specified array/object. */
  88. void AddItemToArray(Json* item);
  89. void AddItemToObject(const char *string, Json* item);
  90. /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
  91. void AddItemReferenceToArray(Json* item);
  92. void AddItemReferenceToObject(const char *string, Json* item);
  93. /* Remove/Detatch items from Arrays/Objects. */
  94. static Json* DetachItemFromArray(Json* array, int which);
  95. static void DeleteItemFromArray(Json* array, int which);
  96. static Json* DetachItemFromObject(Json* object, const char *string);
  97. static void DeleteItemFromObject(Json* object, const char *string);
  98. /* Update array items. */
  99. static void ReplaceItemInArray(Json* array, int which, Json* newitem);
  100. static void ReplaceItemInObject(Json* object, const char *string, Json* newitem);
  101. /* Duplicate a cJSON item */
  102. static Json* Duplicate(Json* item, int recurse);
  103. /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
  104. need to be released. With recurse!=0, it will duplicate any children connected to the item.
  105. The item->next and ->prev pointers are always zero on return from Duplicate. */
  106. /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
  107. static Json* ParseWithOpts(const char *value, const char **return_parse_end, int require_null_terminated);
  108. /* Macros for creating things quickly. */
  109. void AddNullToObject(const char* name) { AddItemToObject(name, CreateNull()); }
  110. void AddTrueToObject(const char* name) { AddItemToObject(name, CreateTrue()); }
  111. void AddFalseToObject(const char* name) { AddItemToObject(name, CreateFalse()); }
  112. void AddBoolToObject(const char* name, bool b) { AddItemToObject(name, CreateBool(b)); }
  113. void AddNumberToObject(const char* name, int n) { AddItemToObject(name, CreateNumber(n)); }
  114. void AddNumberToObject(const char* name, float n) { AddItemToObject(name, CreateNumber(n)); }
  115. void AddNumberToObject(const char* name, double n) { AddItemToObject(name, CreateNumber(n)); }
  116. void AddStringToObject(const char* name, const char* s) { AddItemToObject(name, CreateString(s)); }
  117. /* When assigning an integer value, it needs to be propagated to valuedouble too. */
  118. void SetIntValue(int val) { mValueInt=val; mValueDouble=val; }
  119. static void Minify(char *json);
  120. };
  121. NS_BF_END