2 * $Id: json_tokener.h,v 1.10 2006/07/25 03:24:50 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
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.
14 * @brief Methods to parse an input string into a tree of json_object objects.
16 #ifndef _json_tokener_h_
17 #define _json_tokener_h_
19 #include "json_object.h"
26 enum json_tokener_error
29 json_tokener_continue,
30 json_tokener_error_depth,
31 json_tokener_error_parse_eof,
32 json_tokener_error_parse_unexpected,
33 json_tokener_error_parse_null,
34 json_tokener_error_parse_boolean,
35 json_tokener_error_parse_number,
36 json_tokener_error_parse_array,
37 json_tokener_error_parse_object_key_name,
38 json_tokener_error_parse_object_key_sep,
39 json_tokener_error_parse_object_value_sep,
40 json_tokener_error_parse_string,
41 json_tokener_error_parse_comment,
42 json_tokener_error_parse_utf8_string,
43 json_tokener_error_memory,
44 json_tokener_error_size
48 * @deprecated Don't use this outside of json_tokener.c, it will be made private in a future release.
50 enum json_tokener_state
52 json_tokener_state_eatws,
53 json_tokener_state_start,
54 json_tokener_state_finish,
55 json_tokener_state_null,
56 json_tokener_state_comment_start,
57 json_tokener_state_comment,
58 json_tokener_state_comment_eol,
59 json_tokener_state_comment_end,
60 json_tokener_state_string,
61 json_tokener_state_string_escape,
62 json_tokener_state_escape_unicode,
63 json_tokener_state_escape_unicode_need_escape,
64 json_tokener_state_escape_unicode_need_u,
65 json_tokener_state_boolean,
66 json_tokener_state_number,
67 json_tokener_state_array,
68 json_tokener_state_array_add,
69 json_tokener_state_array_sep,
70 json_tokener_state_object_field_start,
71 json_tokener_state_object_field,
72 json_tokener_state_object_field_end,
73 json_tokener_state_object_value,
74 json_tokener_state_object_value_add,
75 json_tokener_state_object_sep,
76 json_tokener_state_array_after_sep,
77 json_tokener_state_object_field_start_after_sep,
78 json_tokener_state_inf
82 * @deprecated Don't use this outside of json_tokener.c, it will be made private in a future release.
84 struct json_tokener_srec
86 enum json_tokener_state state, saved_state;
87 struct json_object *obj;
88 struct json_object *current;
92 #define JSON_TOKENER_DEFAULT_DEPTH 32
95 * Internal state of the json parser.
96 * Do not access any fields of this structure directly.
97 * Its definition is published due to historical limitations
98 * in the json tokener API, and will be changed to be an opaque
104 * @deprecated Do not access any of these fields outside of json_tokener.c
108 int max_depth, depth, is_double, st_pos;
110 * @deprecated See json_tokener_get_parse_end() instead.
114 * @deprecated See json_tokener_get_error() instead.
116 enum json_tokener_error err;
117 unsigned int ucs_char, high_surrogate;
119 struct json_tokener_srec *stack;
124 * Return the offset of the byte after the last byte parsed
125 * relative to the start of the most recent string passed in
126 * to json_tokener_parse_ex(). i.e. this is where parsing
127 * would start again if the input contains another JSON object
128 * after the currently parsed one.
130 * Note that when multiple parse calls are issued, this is *not* the
131 * total number of characters parsed.
133 * In the past this would have been accessed as tok->char_offset.
135 * See json_tokener_parse_ex() for an example of how to use this.
137 JSON_EXPORT size_t json_tokener_get_parse_end(struct json_tokener *tok);
140 * @deprecated Unused in json-c code
142 typedef struct json_tokener json_tokener;
145 * Be strict when parsing JSON input. Use caution with
146 * this flag as what is considered valid may become more
147 * restrictive from one release to the next, causing your
148 * code to fail on previously working input.
150 * Note that setting this will also effectively disable parsing
151 * of multiple json objects in a single character stream
152 * (e.g. {"foo":123}{"bar":234}); if you want to allow that
153 * also set JSON_TOKENER_ALLOW_TRAILING_CHARS
155 * This flag is not set by default.
157 * @see json_tokener_set_flags()
159 #define JSON_TOKENER_STRICT 0x01
162 * Use with JSON_TOKENER_STRICT to allow trailing characters after the
163 * first parsed object.
165 * @see json_tokener_set_flags()
167 #define JSON_TOKENER_ALLOW_TRAILING_CHARS 0x02
170 * Cause json_tokener_parse_ex() to validate that input is UTF8.
171 * If this flag is specified and validation fails, then
172 * json_tokener_get_error(tok) will return
173 * json_tokener_error_parse_utf8_string
175 * This flag is not set by default.
177 * @see json_tokener_set_flags()
179 #define JSON_TOKENER_VALIDATE_UTF8 0x10
182 * Given an error previously returned by json_tokener_get_error(),
183 * return a human readable description of the error.
185 * @return a generic error message is returned if an invalid error value is provided.
187 JSON_EXPORT const char *json_tokener_error_desc(enum json_tokener_error jerr);
190 * Retrieve the error caused by the last call to json_tokener_parse_ex(),
191 * or json_tokener_success if there is no error.
193 * When parsing a JSON string in pieces, if the tokener is in the middle
194 * of parsing this will return json_tokener_continue.
196 * @see json_tokener_error_desc().
198 JSON_EXPORT enum json_tokener_error json_tokener_get_error(struct json_tokener *tok);
201 * Allocate a new json_tokener.
202 * When done using that to parse objects, free it with json_tokener_free().
203 * See json_tokener_parse_ex() for usage details.
205 JSON_EXPORT struct json_tokener *json_tokener_new(void);
208 * Allocate a new json_tokener with a custom max nesting depth.
209 * @see JSON_TOKENER_DEFAULT_DEPTH
211 JSON_EXPORT struct json_tokener *json_tokener_new_ex(int depth);
214 * Free a json_tokener previously allocated with json_tokener_new().
216 JSON_EXPORT void json_tokener_free(struct json_tokener *tok);
219 * Reset the state of a json_tokener, to prepare to parse a
220 * brand new JSON object.
222 JSON_EXPORT void json_tokener_reset(struct json_tokener *tok);
225 * Parse a json_object out of the string `str`.
227 * If you need more control over how the parsing occurs,
228 * see json_tokener_parse_ex().
230 JSON_EXPORT struct json_object *json_tokener_parse(const char *str);
233 * Parser a json_object out of the string `str`, but if it fails
234 * return the error in `*error`.
235 * @see json_tokener_parse()
236 * @see json_tokener_parse_ex()
238 JSON_EXPORT struct json_object *json_tokener_parse_verbose(const char *str,
239 enum json_tokener_error *error);
242 * Set flags that control how parsing will be done.
244 JSON_EXPORT void json_tokener_set_flags(struct json_tokener *tok, int flags);
247 * Parse a string and return a non-NULL json_object if a valid JSON value
248 * is found. The string does not need to be a JSON object or array;
249 * it can also be a string, number or boolean value.
251 * A partial JSON string can be parsed. If the parsing is incomplete,
252 * NULL will be returned and json_tokener_get_error() will return
253 * json_tokener_continue.
254 * json_tokener_parse_ex() can then be called with additional bytes in str
255 * to continue the parsing.
257 * If json_tokener_parse_ex() returns NULL and the error is anything other than
258 * json_tokener_continue, a fatal error has occurred and parsing must be
259 * halted. Then, the tok object must not be reused until json_tokener_reset()
262 * When a valid JSON value is parsed, a non-NULL json_object will be
263 * returned, with a reference count of one which belongs to the caller. Also,
264 * json_tokener_get_error() will return json_tokener_success. Be sure to check
265 * the type with json_object_is_type() or json_object_get_type() before using
268 * Trailing characters after the parsed value do not automatically cause an
269 * error. It is up to the caller to decide whether to treat this as an
270 * error or to handle the additional characters, perhaps by parsing another
271 * json value starting from that point.
273 * If the caller knows that they are at the end of their input, the length
274 * passed MUST include the final '\0' character, so values with no inherent
275 * end (i.e. numbers) can be properly parsed, rather than just returning
276 * json_tokener_continue.
278 * Extra characters can be detected by comparing the value returned by
279 * json_tokener_get_parse_end() against
280 * the length of the last len parameter passed in.
282 * The tokener does \b not maintain an internal buffer so the caller is
283 * responsible for a subsequent call to json_tokener_parse_ex with an
284 * appropriate str parameter starting with the extra characters.
286 * This interface is presently not 64-bit clean due to the int len argument
287 * so the function limits the maximum string size to INT32_MAX (2GB).
288 * If the function is called with len == -1 then strlen is called to check
289 * the string length is less than INT32_MAX (2GB)
293 json_object *jobj = NULL;
294 const char *mystring = NULL;
296 enum json_tokener_error jerr;
298 mystring = ... // get JSON string, e.g. read from file, etc...
299 stringlen = strlen(mystring);
301 stringlen++; // Include the '\0' if we know we're at the end of input
302 jobj = json_tokener_parse_ex(tok, mystring, stringlen);
303 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
304 if (jerr != json_tokener_success)
306 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
307 // Handle errors, as appropriate for your application.
309 if (json_tokener_get_parse_end(tok) < stringlen)
311 // Handle extra characters after parsed object as desired.
312 // e.g. issue an error, parse another object from that point, etc...
314 // Success, use jobj here.
318 * @param tok a json_tokener previously allocated with json_tokener_new()
319 * @param str an string with any valid JSON expression, or portion of. This does not need to be null terminated.
320 * @param len the length of str
322 JSON_EXPORT struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str,