parson.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. SPDX-License-Identifier: MIT
  3. Parson 1.4.0 (https://github.com/kgabis/parson)
  4. Copyright (c) 2012 - 2022 Krzysztof Gabis
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. #ifndef parson_parson_h
  22. #define parson_parson_h
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #if 0
  28. } /* unconfuse xcode */
  29. #endif
  30. #define PARSON_VERSION_MAJOR 1
  31. #define PARSON_VERSION_MINOR 4
  32. #define PARSON_VERSION_PATCH 0
  33. #define PARSON_VERSION_STRING "1.4.0"
  34. #include <stddef.h> /* size_t */
  35. /* Types and enums */
  36. typedef struct json_object_t JSON_Object;
  37. typedef struct json_array_t JSON_Array;
  38. typedef struct json_value_t JSON_Value;
  39. enum json_value_type {
  40. JSONError = -1,
  41. JSONNull = 1,
  42. JSONString = 2,
  43. JSONNumber = 3,
  44. JSONObject = 4,
  45. JSONArray = 5,
  46. JSONBoolean = 6
  47. };
  48. typedef int JSON_Value_Type;
  49. enum json_result_t {
  50. JSONSuccess = 0,
  51. JSONFailure = -1
  52. };
  53. typedef int JSON_Status;
  54. typedef void * (*JSON_Malloc_Function)(size_t);
  55. typedef void (*JSON_Free_Function)(void *);
  56. /* Call only once, before calling any other function from parson API. If not called, malloc and free
  57. from stdlib will be used for all allocations */
  58. void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun);
  59. /* Sets if slashes should be escaped or not when serializing JSON. By default slashes are escaped.
  60. This function sets a global setting and is not thread safe. */
  61. void json_set_escape_slashes(int escape_slashes);
  62. /* Sets float format used for serialization of numbers.
  63. Make sure it can't serialize to a string longer than PARSON_NUM_BUF_SIZE.
  64. If format is null then the default format is used. */
  65. void json_set_float_serialization_format(const char *format);
  66. /* Parses first JSON value in a file, returns NULL in case of error */
  67. JSON_Value * json_parse_file(const char *filename);
  68. /* Parses first JSON value in a file and ignores comments (/ * * / and //),
  69. returns NULL in case of error */
  70. JSON_Value * json_parse_file_with_comments(const char *filename);
  71. /* Parses first JSON value in a string, returns NULL in case of error */
  72. JSON_Value * json_parse_string(const char *string);
  73. /* Parses first JSON value in a string and ignores comments (/ * * / and //),
  74. returns NULL in case of error */
  75. JSON_Value * json_parse_string_with_comments(const char *string);
  76. /* Serialization */
  77. size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */
  78. JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
  79. JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);
  80. char * json_serialize_to_string(const JSON_Value *value);
  81. /* Pretty serialization */
  82. size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */
  83. JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
  84. JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename);
  85. char * json_serialize_to_string_pretty(const JSON_Value *value);
  86. void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
  87. /* Comparing */
  88. int json_value_equals(const JSON_Value *a, const JSON_Value *b);
  89. /* Validation
  90. This is *NOT* JSON Schema. It validates json by checking if object have identically
  91. named fields with matching types.
  92. For example schema {"name":"", "age":0} will validate
  93. {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"},
  94. but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}.
  95. In case of arrays, only first value in schema is checked against all values in tested array.
  96. Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays,
  97. null validates values of every type.
  98. */
  99. JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value);
  100. /*
  101. * JSON Object
  102. */
  103. JSON_Value * json_object_get_value (const JSON_Object *object, const char *name);
  104. const char * json_object_get_string (const JSON_Object *object, const char *name);
  105. size_t json_object_get_string_len(const JSON_Object *object, const char *name); /* doesn't account for last null character */
  106. JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
  107. JSON_Array * json_object_get_array (const JSON_Object *object, const char *name);
  108. double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
  109. int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
  110. /* dotget functions enable addressing values with dot notation in nested objects,
  111. just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
  112. Because valid names in JSON can contain dots, some values may be inaccessible
  113. this way. */
  114. JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name);
  115. const char * json_object_dotget_string (const JSON_Object *object, const char *name);
  116. size_t json_object_dotget_string_len(const JSON_Object *object, const char *name); /* doesn't account for last null character */
  117. JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
  118. JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name);
  119. double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
  120. int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
  121. /* Functions to get available names */
  122. size_t json_object_get_count (const JSON_Object *object);
  123. const char * json_object_get_name (const JSON_Object *object, size_t index);
  124. JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index);
  125. JSON_Value * json_object_get_wrapping_value(const JSON_Object *object);
  126. /* Functions to check if object has a value with a specific name. Returned value is 1 if object has
  127. * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */
  128. int json_object_has_value (const JSON_Object *object, const char *name);
  129. int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
  130. int json_object_dothas_value (const JSON_Object *object, const char *name);
  131. int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
  132. /* Creates new name-value pair or frees and replaces old value with a new one.
  133. * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
  134. JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
  135. JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
  136. JSON_Status json_object_set_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len); /* length shouldn't include last null character */
  137. JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
  138. JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
  139. JSON_Status json_object_set_null(JSON_Object *object, const char *name);
  140. /* Works like dotget functions, but creates whole hierarchy if necessary.
  141. * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */
  142. JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value);
  143. JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string);
  144. JSON_Status json_object_dotset_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len); /* length shouldn't include last null character */
  145. JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number);
  146. JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean);
  147. JSON_Status json_object_dotset_null(JSON_Object *object, const char *name);
  148. /* Frees and removes name-value pair */
  149. JSON_Status json_object_remove(JSON_Object *object, const char *name);
  150. /* Works like dotget function, but removes name-value pair only on exact match. */
  151. JSON_Status json_object_dotremove(JSON_Object *object, const char *key);
  152. /* Removes all name-value pairs in object */
  153. JSON_Status json_object_clear(JSON_Object *object);
  154. /*
  155. *JSON Array
  156. */
  157. JSON_Value * json_array_get_value (const JSON_Array *array, size_t index);
  158. const char * json_array_get_string (const JSON_Array *array, size_t index);
  159. size_t json_array_get_string_len(const JSON_Array *array, size_t index); /* doesn't account for last null character */
  160. JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
  161. JSON_Array * json_array_get_array (const JSON_Array *array, size_t index);
  162. double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */
  163. int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */
  164. size_t json_array_get_count (const JSON_Array *array);
  165. JSON_Value * json_array_get_wrapping_value(const JSON_Array *array);
  166. /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
  167. * Order of values in array may change during execution. */
  168. JSON_Status json_array_remove(JSON_Array *array, size_t i);
  169. /* Frees and removes from array value at given index and replaces it with given one.
  170. * Does nothing and returns JSONFailure if index doesn't exist.
  171. * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */
  172. JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value);
  173. JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string);
  174. JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, const char *string, size_t len); /* length shouldn't include last null character */
  175. JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number);
  176. JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean);
  177. JSON_Status json_array_replace_null(JSON_Array *array, size_t i);
  178. /* Frees and removes all values from array */
  179. JSON_Status json_array_clear(JSON_Array *array);
  180. /* Appends new value at the end of array.
  181. * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */
  182. JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value);
  183. JSON_Status json_array_append_string(JSON_Array *array, const char *string);
  184. JSON_Status json_array_append_string_with_len(JSON_Array *array, const char *string, size_t len); /* length shouldn't include last null character */
  185. JSON_Status json_array_append_number(JSON_Array *array, double number);
  186. JSON_Status json_array_append_boolean(JSON_Array *array, int boolean);
  187. JSON_Status json_array_append_null(JSON_Array *array);
  188. /*
  189. *JSON Value
  190. */
  191. JSON_Value * json_value_init_object (void);
  192. JSON_Value * json_value_init_array (void);
  193. JSON_Value * json_value_init_string (const char *string); /* copies passed string */
  194. JSON_Value * json_value_init_string_with_len(const char *string, size_t length); /* copies passed string, length shouldn't include last null character */
  195. JSON_Value * json_value_init_number (double number);
  196. JSON_Value * json_value_init_boolean(int boolean);
  197. JSON_Value * json_value_init_null (void);
  198. JSON_Value * json_value_deep_copy (const JSON_Value *value);
  199. void json_value_free (JSON_Value *value);
  200. JSON_Value_Type json_value_get_type (const JSON_Value *value);
  201. JSON_Object * json_value_get_object (const JSON_Value *value);
  202. JSON_Array * json_value_get_array (const JSON_Value *value);
  203. const char * json_value_get_string (const JSON_Value *value);
  204. size_t json_value_get_string_len(const JSON_Value *value); /* doesn't account for last null character */
  205. double json_value_get_number (const JSON_Value *value);
  206. int json_value_get_boolean(const JSON_Value *value);
  207. JSON_Value * json_value_get_parent (const JSON_Value *value);
  208. /* Same as above, but shorter */
  209. JSON_Value_Type json_type (const JSON_Value *value);
  210. JSON_Object * json_object (const JSON_Value *value);
  211. JSON_Array * json_array (const JSON_Value *value);
  212. const char * json_string (const JSON_Value *value);
  213. size_t json_string_len(const JSON_Value *value); /* doesn't account for last null character */
  214. double json_number (const JSON_Value *value);
  215. int json_boolean(const JSON_Value *value);
  216. #ifdef __cplusplus
  217. }
  218. #endif
  219. #endif