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 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifndef _json_tokener_h_
  12. #define _json_tokener_h_
  13. #include <stddef.h>
  14. #include "json_object.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. enum json_tokener_error {
  19. json_tokener_success,
  20. json_tokener_continue,
  21. json_tokener_error_depth,
  22. json_tokener_error_parse_eof,
  23. json_tokener_error_parse_unexpected,
  24. json_tokener_error_parse_null,
  25. json_tokener_error_parse_boolean,
  26. json_tokener_error_parse_number,
  27. json_tokener_error_parse_array,
  28. json_tokener_error_parse_object_key_name,
  29. json_tokener_error_parse_object_key_sep,
  30. json_tokener_error_parse_object_value_sep,
  31. json_tokener_error_parse_string,
  32. json_tokener_error_parse_comment
  33. };
  34. enum json_tokener_state {
  35. json_tokener_state_eatws,
  36. json_tokener_state_start,
  37. json_tokener_state_finish,
  38. json_tokener_state_null,
  39. json_tokener_state_comment_start,
  40. json_tokener_state_comment,
  41. json_tokener_state_comment_eol,
  42. json_tokener_state_comment_end,
  43. json_tokener_state_string,
  44. json_tokener_state_string_escape,
  45. json_tokener_state_escape_unicode,
  46. json_tokener_state_boolean,
  47. json_tokener_state_number,
  48. json_tokener_state_array,
  49. json_tokener_state_array_add,
  50. json_tokener_state_array_sep,
  51. json_tokener_state_object_field_start,
  52. json_tokener_state_object_field,
  53. json_tokener_state_object_field_end,
  54. json_tokener_state_object_value,
  55. json_tokener_state_object_value_add,
  56. json_tokener_state_object_sep,
  57. json_tokener_state_array_after_sep,
  58. json_tokener_state_object_field_start_after_sep
  59. };
  60. struct json_tokener_srec
  61. {
  62. enum json_tokener_state state, saved_state;
  63. struct json_object *obj;
  64. struct json_object *current;
  65. char *obj_field_name;
  66. };
  67. #define JSON_TOKENER_DEFAULT_DEPTH 32
  68. struct json_tokener
  69. {
  70. char *str;
  71. struct printbuf *pb;
  72. int max_depth, depth, is_double, st_pos, char_offset;
  73. enum json_tokener_error err;
  74. unsigned int ucs_char;
  75. char quote_char;
  76. struct json_tokener_srec *stack;
  77. int flags;
  78. };
  79. /**
  80. * Be strict when parsing JSON input. Use caution with
  81. * this flag as what is considered valid may become more
  82. * restrictive from one release to the next, causing your
  83. * code to fail on previously working input.
  84. *
  85. * This flag is not set by default.
  86. *
  87. * @see json_tokener_set_flags()
  88. */
  89. #define JSON_TOKENER_STRICT 0x01
  90. /**
  91. * Given an error previously returned by json_tokener_get_error(),
  92. * return a human readable description of the error.
  93. *
  94. * @return a generic error message is returned if an invalid error value is provided.
  95. */
  96. const char *json_tokener_error_desc(enum json_tokener_error jerr);
  97. /**
  98. * @b XXX do not use json_tokener_errors directly.
  99. * After v0.10 this will be removed.
  100. *
  101. * See json_tokener_error_desc() instead.
  102. */
  103. extern const char* json_tokener_errors[];
  104. /**
  105. * Retrieve the error caused by the last call to json_tokener_parse_ex(),
  106. * or json_tokener_success if there is no error.
  107. *
  108. * When parsing a JSON string in pieces, if the tokener is in the middle
  109. * of parsing this will return json_tokener_continue.
  110. *
  111. * See also json_tokener_error_desc().
  112. */
  113. enum json_tokener_error json_tokener_get_error(struct json_tokener *tok);
  114. extern struct json_tokener* json_tokener_new(void);
  115. extern struct json_tokener* json_tokener_new_ex(int depth);
  116. extern void json_tokener_free(struct json_tokener *tok);
  117. extern void json_tokener_reset(struct json_tokener *tok);
  118. extern struct json_object* json_tokener_parse(const char *str);
  119. extern struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error);
  120. /**
  121. * Set flags that control how parsing will be done.
  122. */
  123. extern void json_tokener_set_flags(struct json_tokener *tok, int flags);
  124. /**
  125. * Parse a string and return a non-NULL json_object if a valid JSON value
  126. * is found. The string does not need to be a JSON object or array;
  127. * it can also be a string, number or boolean value.
  128. *
  129. * A partial JSON string can be parsed. If the parsing is incomplete,
  130. * NULL will be returned and json_tokener_get_error() will be return
  131. * json_tokener_continue.
  132. * json_tokener_parse_ex() can then be called with additional bytes in str
  133. * to continue the parsing.
  134. *
  135. * If json_tokener_parse_ex() returns NULL and the error anything other than
  136. * json_tokener_continue, a fatal error has occurred and parsing must be
  137. * halted. Then tok object must not be re-used until json_tokener_reset() is
  138. * called.
  139. *
  140. * When a valid JSON value is parsed, a non-NULL json_object will be
  141. * returned. Also, json_tokener_get_error() will return json_tokener_success.
  142. * Be sure to check the type with json_object_is_type() or
  143. * json_object_get_type() before using the object.
  144. *
  145. * @b XXX this shouldn't use internal fields:
  146. * Trailing characters after the parsed value do not automatically cause an
  147. * error. It is up to the caller to decide whether to treat this as an
  148. * error or to handle the additional characters, perhaps by parsing another
  149. * json value starting from that point.
  150. *
  151. * Extra characters can be detected by comparing the tok->char_offset against
  152. * the length of the last len parameter passed in.
  153. *
  154. * The tokener does \b not maintain an internal buffer so the caller is
  155. * responsible for calling json_tokener_parse_ex with an appropriate str
  156. * parameter starting with the extra characters.
  157. *
  158. * Example:
  159. * @code
  160. json_object *jobj = NULL;
  161. const char *mystring = NULL;
  162. int stringlen = 0;
  163. enum json_tokener_error jerr;
  164. do {
  165. mystring = ... // get JSON string, e.g. read from file, etc...
  166. stringlen = strlen(mystring);
  167. jobj = json_tokener_parse_ex(tok, mystring, stringlen);
  168. } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
  169. if (jerr != json_tokener_success)
  170. {
  171. fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
  172. // Handle errors, as appropriate for your application.
  173. }
  174. if (tok->char_offset < stringlen) // XXX shouldn't access internal fields
  175. {
  176. // Handle extra characters after parsed object as desired.
  177. // e.g. issue an error, parse another object from that point, etc...
  178. }
  179. // Success, use jobj here.
  180. @endcode
  181. *
  182. * @param tok a json_tokener previously allocated with json_tokener_new()
  183. * @param str an string with any valid JSON expression, or portion of. This does not need to be null terminated.
  184. * @param len the length of str
  185. */
  186. extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
  187. const char *str, int len);
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif