json.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2009-2011 Vincent Hanquez <vincent@snarc.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation; version 2.1 or version 3.0 only.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * SPDX-License-Identifier: GPL-3.0+
  14. * License-Filename: LICENSE
  15. */
  16. #ifndef JSON_H
  17. #define JSON_H
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include <stdlib.h>
  22. #if defined(_MSC_VER) && (_MSC_VER < 1600)
  23. // MSVC does not include stdint.h before version 10.0
  24. typedef unsigned __int8 uint8_t;
  25. typedef unsigned __int16 uint16_t;
  26. typedef unsigned __int32 uint32_t;
  27. #else
  28. #include <stdint.h>
  29. #endif
  30. #define JSON_MAJOR 1
  31. #define JSON_MINOR 0
  32. #define JSON_VERSION (JSON_MAJOR * 100 + JSON_MINOR)
  33. typedef enum {
  34. JSON_NONE,
  35. JSON_ARRAY_BEGIN,
  36. JSON_OBJECT_BEGIN,
  37. JSON_ARRAY_END,
  38. JSON_OBJECT_END,
  39. JSON_INT,
  40. JSON_FLOAT,
  41. JSON_STRING,
  42. JSON_KEY,
  43. JSON_TRUE,
  44. JSON_FALSE,
  45. JSON_NULL,
  46. JSON_BSTRING,
  47. } json_type;
  48. typedef enum {
  49. /* SUCCESS = 0 */
  50. /* running out of memory */
  51. JSON_ERROR_NO_MEMORY = 1,
  52. /* character < 32, except space newline tab */
  53. JSON_ERROR_BAD_CHAR,
  54. /* trying to pop more object/array than pushed on the stack */
  55. JSON_ERROR_POP_EMPTY,
  56. /* trying to pop wrong type of mode. popping array in object mode, vice versa */
  57. JSON_ERROR_POP_UNEXPECTED_MODE,
  58. /* reach nesting limit on stack */
  59. JSON_ERROR_NESTING_LIMIT,
  60. /* reach data limit on buffer */
  61. JSON_ERROR_DATA_LIMIT,
  62. /* comment are not allowed with current configuration */
  63. JSON_ERROR_COMMENT_NOT_ALLOWED,
  64. /* unexpected char in the current parser context */
  65. JSON_ERROR_UNEXPECTED_CHAR,
  66. /* unicode low surrogate missing after high surrogate */
  67. JSON_ERROR_UNICODE_MISSING_LOW_SURROGATE,
  68. /* unicode low surrogate missing without previous high surrogate */
  69. JSON_ERROR_UNICODE_UNEXPECTED_LOW_SURROGATE,
  70. /* found a comma not in structure (array/object) */
  71. JSON_ERROR_COMMA_OUT_OF_STRUCTURE,
  72. /* callback returns error */
  73. JSON_ERROR_CALLBACK,
  74. /* utf8 stream is invalid */
  75. JSON_ERROR_UTF8,
  76. } json_error;
  77. #define LIBJSON_DEFAULT_STACK_SIZE 256
  78. #define LIBJSON_DEFAULT_BUFFER_SIZE 4096
  79. typedef int (*json_parser_callback)(void *userdata, int type, const char *data, uint32_t length);
  80. typedef int (*json_printer_callback)(void *userdata, const char *s, uint32_t length);
  81. typedef struct {
  82. uint32_t buffer_initial_size;
  83. uint32_t max_nesting;
  84. uint32_t max_data;
  85. int allow_c_comments;
  86. int allow_yaml_comments;
  87. void (*user_free)(void *ptr);
  88. void *(*user_calloc)(size_t nmemb, size_t size);
  89. void *(*user_realloc)(void *ptr, size_t size);
  90. } json_config;
  91. typedef struct json_parser {
  92. json_config config;
  93. /* SAJ callback */
  94. json_parser_callback callback;
  95. void *userdata;
  96. /* parser state */
  97. uint8_t state;
  98. uint8_t save_state;
  99. uint8_t expecting_key;
  100. uint8_t utf8_multibyte_left;
  101. uint16_t unicode_multi;
  102. json_type type;
  103. /* state stack */
  104. uint8_t *stack;
  105. uint32_t stack_offset;
  106. uint32_t stack_size;
  107. /* parse buffer */
  108. char *buffer;
  109. uint32_t buffer_size;
  110. uint32_t buffer_offset;
  111. } json_parser;
  112. typedef struct json_printer {
  113. json_printer_callback callback;
  114. void *userdata;
  115. char *indentstr;
  116. int indentlevel;
  117. int afterkey;
  118. int enter_object;
  119. int first;
  120. } json_printer;
  121. /** json_parser_init initialize a parser structure taking a config,
  122. * a config and its userdata.
  123. * return JSON_ERROR_NO_MEMORY if memory allocation failed or SUCCESS. */
  124. int json_parser_init(json_parser * parser, json_config * cfg, json_parser_callback callback, void *userdata);
  125. /** json_parser_free freed memory structure allocated by the parser */
  126. int json_parser_free(json_parser * parser);
  127. /** json_parser_string append a string s with a specific length to the parser
  128. * return 0 if everything went ok, a JSON_ERROR_* otherwise.
  129. * the user can supplied a valid processed pointer that will
  130. * be fill with the number of processed characters before returning */
  131. int json_parser_string(json_parser * parser, const char *string, uint32_t length, uint32_t * processed);
  132. /** json_parser_char append one single char to the parser
  133. * return 0 if everything went ok, a JSON_ERROR_* otherwise */
  134. int json_parser_char(json_parser * parser, unsigned char next_char);
  135. /** json_parser_is_done return 0 is the parser isn't in a finish state. !0 if it is */
  136. int json_parser_is_done(json_parser * parser);
  137. /** json_print_init initialize a printer context. always succeed */
  138. int json_print_init(json_printer * printer, json_printer_callback callback, void *userdata);
  139. /** json_print_free free a printer context
  140. * doesn't do anything now, but in future print_init could allocate memory */
  141. int json_print_free(json_printer * printer);
  142. /** json_print_pretty pretty print the passed argument (type/data/length). */
  143. int json_print_pretty(json_printer * printer, int type, const char *data, uint32_t length);
  144. /** json_print_raw prints without eye candy the passed argument (type/data/length). */
  145. int json_print_raw(json_printer * printer, int type, const char *data, uint32_t length);
  146. /** json_print_args takes multiple types and pass them to the printer function
  147. * array, object and constants doesn't take a string and length argument.
  148. * int, float, key, string need to be followed by a pointer to char and then a length.
  149. * if the length argument is -1, then the strlen function will use on the string argument.
  150. * the function call should always be terminated by -1 */
  151. int json_print_args(json_printer *, int (*f)(json_printer *, int, const char *, uint32_t), ...);
  152. /** callback from the parser_dom callback to create object and array */
  153. typedef void *(*json_parser_dom_create_structure)(int, int);
  154. /** callback from the parser_dom callback to create data values */
  155. typedef void *(*json_parser_dom_create_data)(int, const char *, uint32_t);
  156. /** callback from the parser helper callback to append a value to an object or array value
  157. * append(parent, key, key_length, val); */
  158. typedef int (*json_parser_dom_append)(void *, char *, uint32_t, void *);
  159. /** the json_parser_dom permits to create a DOM like tree easily through the
  160. * use of 3 callbacks where the user can choose the representation of the JSON values */
  161. typedef struct json_parser_dom {
  162. /* object stack */
  163. struct stack_elem {
  164. void *val;
  165. char *key;
  166. uint32_t key_length;
  167. } *stack;
  168. uint32_t stack_size;
  169. uint32_t stack_offset;
  170. /* overridable memory allocator */
  171. void *(*user_calloc)(size_t nmemb, size_t size);
  172. void *(*user_realloc)(void *ptr, size_t size);
  173. /* returned root structure (object or array) */
  174. void *root_structure;
  175. /* callbacks */
  176. json_parser_dom_create_structure create_structure;
  177. json_parser_dom_create_data create_data;
  178. json_parser_dom_append append;
  179. } json_parser_dom;
  180. /** initialize a parser dom structure with the necessary callbacks */
  181. int json_parser_dom_init(json_parser_dom * helper,
  182. json_parser_dom_create_structure create_structure,
  183. json_parser_dom_create_data create_data, json_parser_dom_append append);
  184. /** free memory allocated by the DOM callback helper */
  185. int json_parser_dom_free(json_parser_dom * ctx);
  186. /** helper to parser callback that arrange parsing events into comprehensive JSON data structure */
  187. int json_parser_dom_callback(void *userdata, int type, const char *data, uint32_t length);
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif /* JSON_H */
  192. /* end. */