You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_tokener.h 9.3 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * $Id: json_tokener.h,v 1.10 2006/07/25 03:24:50 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. /**
  12. * @file
  13. * @brief Methods to parse an input string into a tree of json_object objects.
  14. */
  15. #ifndef _json_tokener_h_
  16. #define _json_tokener_h_
  17. #include "json_object.h"
  18. #include <stddef.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. enum json_tokener_error
  23. {
  24. json_tokener_success,
  25. json_tokener_continue,
  26. json_tokener_error_depth,
  27. json_tokener_error_parse_eof,
  28. json_tokener_error_parse_unexpected,
  29. json_tokener_error_parse_null,
  30. json_tokener_error_parse_boolean,
  31. json_tokener_error_parse_number,
  32. json_tokener_error_parse_array,
  33. json_tokener_error_parse_object_key_name,
  34. json_tokener_error_parse_object_key_sep,
  35. json_tokener_error_parse_object_value_sep,
  36. json_tokener_error_parse_string,
  37. json_tokener_error_parse_comment,
  38. json_tokener_error_parse_utf8_string,
  39. json_tokener_error_size
  40. };
  41. /**
  42. * @deprecated Don't use this outside of json_tokener.c, it will be made private in a future release.
  43. */
  44. enum json_tokener_state
  45. {
  46. json_tokener_state_eatws,
  47. json_tokener_state_start,
  48. json_tokener_state_finish,
  49. json_tokener_state_null,
  50. json_tokener_state_comment_start,
  51. json_tokener_state_comment,
  52. json_tokener_state_comment_eol,
  53. json_tokener_state_comment_end,
  54. json_tokener_state_string,
  55. json_tokener_state_string_escape,
  56. json_tokener_state_escape_unicode,
  57. json_tokener_state_boolean,
  58. json_tokener_state_number,
  59. json_tokener_state_array,
  60. json_tokener_state_array_add,
  61. json_tokener_state_array_sep,
  62. json_tokener_state_object_field_start,
  63. json_tokener_state_object_field,
  64. json_tokener_state_object_field_end,
  65. json_tokener_state_object_value,
  66. json_tokener_state_object_value_add,
  67. json_tokener_state_object_sep,
  68. json_tokener_state_array_after_sep,
  69. json_tokener_state_object_field_start_after_sep,
  70. json_tokener_state_inf
  71. };
  72. /**
  73. * @deprecated Don't use this outside of json_tokener.c, it will be made private in a future release.
  74. */
  75. struct json_tokener_srec
  76. {
  77. enum json_tokener_state state, saved_state;
  78. struct json_object *obj;
  79. struct json_object *current;
  80. char *obj_field_name;
  81. };
  82. #define JSON_TOKENER_DEFAULT_DEPTH 32
  83. /**
  84. * Internal state of the json parser.
  85. * Do not access any fields of this structure directly.
  86. * Its definition is published due to historical limitations
  87. * in the json tokener API, and will be changed to be an opaque
  88. * type in the future.
  89. */
  90. struct json_tokener
  91. {
  92. /**
  93. * @deprecated Do not access any of these fields outside of json_tokener.c
  94. */
  95. char *str;
  96. struct printbuf *pb;
  97. int max_depth, depth, is_double, st_pos;
  98. /**
  99. * @deprecated See json_tokener_get_parse_end() instead.
  100. */
  101. int char_offset;
  102. /**
  103. * @deprecated See json_tokener_get_error() instead.
  104. */
  105. enum json_tokener_error err;
  106. unsigned int ucs_char;
  107. char quote_char;
  108. struct json_tokener_srec *stack;
  109. int flags;
  110. };
  111. /**
  112. * Return the offset of the byte after the last byte parsed
  113. * relative to the start of the most recent string passed in
  114. * to json_tokener_parse_ex(). i.e. this is where parsing
  115. * would start again if the input contains another JSON object
  116. * after the currently parsed one.
  117. *
  118. * Note that when multiple parse calls are issued, this is *not* the
  119. * total number of characters parsed.
  120. *
  121. * In the past this would have been accessed as tok->char_offset.
  122. *
  123. * See json_tokener_parse_ex() for an example of how to use this.
  124. */
  125. JSON_EXPORT size_t json_tokener_get_parse_end(struct json_tokener *tok);
  126. /**
  127. * @deprecated Unused in json-c code
  128. */
  129. typedef struct json_tokener json_tokener;
  130. /**
  131. * Be strict when parsing JSON input. Use caution with
  132. * this flag as what is considered valid may become more
  133. * restrictive from one release to the next, causing your
  134. * code to fail on previously working input.
  135. *
  136. * Note that setting this will also effectively disable parsing
  137. * of multiple json objects in a single character stream
  138. * (e.g. {"foo":123}{"bar":234}); if you want to allow that
  139. * also set JSON_TOKENER_ALLOW_TRAILING_CHARS
  140. *
  141. * This flag is not set by default.
  142. *
  143. * @see json_tokener_set_flags()
  144. */
  145. #define JSON_TOKENER_STRICT 0x01
  146. /**
  147. * Use with JSON_TOKENER_STRICT to allow trailing characters after the
  148. * first parsed object.
  149. *
  150. * @see json_tokener_set_flags()
  151. */
  152. #define JSON_TOKENER_ALLOW_TRAILING_CHARS 0x02
  153. /**
  154. * Cause json_tokener_parse_ex() to validate that input is UTF8.
  155. * If this flag is specified and validation fails, then
  156. * json_tokener_get_error(tok) will return
  157. * json_tokener_error_parse_utf8_string
  158. *
  159. * This flag is not set by default.
  160. *
  161. * @see json_tokener_set_flags()
  162. */
  163. #define JSON_TOKENER_VALIDATE_UTF8 0x10
  164. /**
  165. * Given an error previously returned by json_tokener_get_error(),
  166. * return a human readable description of the error.
  167. *
  168. * @return a generic error message is returned if an invalid error value is provided.
  169. */
  170. JSON_EXPORT const char *json_tokener_error_desc(enum json_tokener_error jerr);
  171. /**
  172. * Retrieve the error caused by the last call to json_tokener_parse_ex(),
  173. * or json_tokener_success if there is no error.
  174. *
  175. * When parsing a JSON string in pieces, if the tokener is in the middle
  176. * of parsing this will return json_tokener_continue.
  177. *
  178. * @see json_tokener_error_desc().
  179. */
  180. JSON_EXPORT enum json_tokener_error json_tokener_get_error(struct json_tokener *tok);
  181. JSON_EXPORT struct json_tokener *json_tokener_new(void);
  182. JSON_EXPORT struct json_tokener *json_tokener_new_ex(int depth);
  183. JSON_EXPORT void json_tokener_free(struct json_tokener *tok);
  184. JSON_EXPORT void json_tokener_reset(struct json_tokener *tok);
  185. JSON_EXPORT struct json_object *json_tokener_parse(const char *str);
  186. JSON_EXPORT struct json_object *json_tokener_parse_verbose(const char *str,
  187. enum json_tokener_error *error);
  188. /**
  189. * Set flags that control how parsing will be done.
  190. */
  191. JSON_EXPORT void json_tokener_set_flags(struct json_tokener *tok, int flags);
  192. /**
  193. * Parse a string and return a non-NULL json_object if a valid JSON value
  194. * is found. The string does not need to be a JSON object or array;
  195. * it can also be a string, number or boolean value.
  196. *
  197. * A partial JSON string can be parsed. If the parsing is incomplete,
  198. * NULL will be returned and json_tokener_get_error() will return
  199. * json_tokener_continue.
  200. * json_tokener_parse_ex() can then be called with additional bytes in str
  201. * to continue the parsing.
  202. *
  203. * If json_tokener_parse_ex() returns NULL and the error is anything other than
  204. * json_tokener_continue, a fatal error has occurred and parsing must be
  205. * halted. Then, the tok object must not be reused until json_tokener_reset() is
  206. * called.
  207. *
  208. * When a valid JSON value is parsed, a non-NULL json_object will be
  209. * returned, with a reference count of one which belongs to the caller. Also,
  210. * json_tokener_get_error() will return json_tokener_success. Be sure to check
  211. * the type with json_object_is_type() or json_object_get_type() before using
  212. * the object.
  213. *
  214. * Trailing characters after the parsed value do not automatically cause an
  215. * error. It is up to the caller to decide whether to treat this as an
  216. * error or to handle the additional characters, perhaps by parsing another
  217. * json value starting from that point.
  218. *
  219. * Extra characters can be detected by comparing the value returned by
  220. * json_tokener_get_parse_end() against
  221. * the length of the last len parameter passed in.
  222. *
  223. * The tokener does \b not maintain an internal buffer so the caller is
  224. * responsible for calling json_tokener_parse_ex with an appropriate str
  225. * parameter starting with the extra characters.
  226. *
  227. * This interface is presently not 64-bit clean due to the int len argument
  228. * so the function limits the maximum string size to INT32_MAX (2GB).
  229. * If the function is called with len == -1 then strlen is called to check
  230. * the string length is less than INT32_MAX (2GB)
  231. *
  232. * Example:
  233. * @code
  234. json_object *jobj = NULL;
  235. const char *mystring = NULL;
  236. int stringlen = 0;
  237. enum json_tokener_error jerr;
  238. do {
  239. mystring = ... // get JSON string, e.g. read from file, etc...
  240. stringlen = strlen(mystring);
  241. jobj = json_tokener_parse_ex(tok, mystring, stringlen);
  242. } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
  243. if (jerr != json_tokener_success)
  244. {
  245. fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
  246. // Handle errors, as appropriate for your application.
  247. }
  248. if (json_tokener_get_parse_end(tok) < stringlen)
  249. {
  250. // Handle extra characters after parsed object as desired.
  251. // e.g. issue an error, parse another object from that point, etc...
  252. }
  253. // Success, use jobj here.
  254. @endcode
  255. *
  256. * @param tok a json_tokener previously allocated with json_tokener_new()
  257. * @param str an string with any valid JSON expression, or portion of. This does not need to be null terminated.
  258. * @param len the length of str
  259. */
  260. JSON_EXPORT struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str,
  261. int len);
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif