json_tokener.h 6.9 KB

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