esl_json.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright (c) 2009 Dave Gamble
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "esl.h"
  20. #ifndef cJSON__h
  21. #define cJSON__h
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. /* cJSON Types: */
  27. #define cJSON_False 0
  28. #define cJSON_True 1
  29. #define cJSON_NULL 2
  30. #define cJSON_Number 3
  31. #define cJSON_String 4
  32. #define cJSON_Array 5
  33. #define cJSON_Object 6
  34. #define cJSON_IsReference 256
  35. /* The cJSON structure: */
  36. typedef struct cJSON {
  37. struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
  38. struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
  39. int type; /* The type of the item, as above. */
  40. char *valuestring; /* The item's string, if type==cJSON_String */
  41. int valueint; /* The item's number, if type==cJSON_Number */
  42. double valuedouble; /* The item's number, if type==cJSON_Number */
  43. char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
  44. } cJSON;
  45. typedef struct cJSON_Hooks {
  46. void *(*malloc_fn)(size_t sz);
  47. void (*free_fn)(void *ptr);
  48. } cJSON_Hooks;
  49. /* Supply malloc, realloc and free functions to cJSON */
  50. ESL_DECLARE(void) cJSON_InitHooks(cJSON_Hooks* hooks);
  51. /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
  52. ESL_DECLARE(cJSON *)cJSON_Parse(const char *value);
  53. /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
  54. ESL_DECLARE(char *)cJSON_Print(cJSON *item);
  55. /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
  56. ESL_DECLARE(char *)cJSON_PrintUnformatted(cJSON *item);
  57. /* Delete a cJSON entity and all subentities. */
  58. ESL_DECLARE(void) cJSON_Delete(cJSON *c);
  59. /* Returns the number of items in an array (or object). */
  60. ESL_DECLARE(int) cJSON_GetArraySize(cJSON *array);
  61. /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
  62. ESL_DECLARE(cJSON *)cJSON_GetArrayItem(cJSON *array,int item);
  63. /* Get item "string" from object. Case insensitive. */
  64. ESL_DECLARE(cJSON *)cJSON_GetObjectItem(cJSON *object,const char *string);
  65. /* 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 cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
  66. ESL_DECLARE(const char *)cJSON_GetErrorPtr(void);
  67. /* These calls create a cJSON item of the appropriate type. */
  68. ESL_DECLARE(cJSON *)cJSON_CreateNull(void);
  69. ESL_DECLARE(cJSON *)cJSON_CreateTrue(void);
  70. ESL_DECLARE(cJSON *)cJSON_CreateFalse(void);
  71. ESL_DECLARE(cJSON *)cJSON_CreateBool(int b);
  72. ESL_DECLARE(cJSON *)cJSON_CreateNumber(double num);
  73. ESL_DECLARE(cJSON *)cJSON_CreateString(const char *string);
  74. ESL_DECLARE(cJSON *)cJSON_CreateArray(void);
  75. ESL_DECLARE(cJSON *)cJSON_CreateObject(void);
  76. /* These utilities create an Array of count items. */
  77. ESL_DECLARE(cJSON *)cJSON_CreateIntArray(int *numbers,int count);
  78. ESL_DECLARE(cJSON *)cJSON_CreateFloatArray(float *numbers,int count);
  79. ESL_DECLARE(cJSON *)cJSON_CreateDoubleArray(double *numbers,int count);
  80. ESL_DECLARE(cJSON *)cJSON_CreateStringArray(const char **strings,int count);
  81. /* Append item to the specified array/object. */
  82. ESL_DECLARE(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
  83. ESL_DECLARE(void) cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
  84. /* 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. */
  85. ESL_DECLARE(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
  86. ESL_DECLARE(void) cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
  87. /* Remove/Detatch items from Arrays/Objects. */
  88. ESL_DECLARE(cJSON *)cJSON_DetachItemFromArray(cJSON *array,int which);
  89. ESL_DECLARE(void) cJSON_DeleteItemFromArray(cJSON *array,int which);
  90. ESL_DECLARE(cJSON *)cJSON_DetachItemFromObject(cJSON *object,const char *string);
  91. ESL_DECLARE(void) cJSON_DeleteItemFromObject(cJSON *object,const char *string);
  92. /* Update array items. */
  93. ESL_DECLARE(void) cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
  94. ESL_DECLARE(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
  95. #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
  96. #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
  97. #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
  98. #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
  99. #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif
  104. /* For Emacs:
  105. * Local Variables:
  106. * mode:c
  107. * indent-tabs-mode:t
  108. * tab-width:4
  109. * c-basic-offset:4
  110. * End:
  111. * For VIM:
  112. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  113. */