![]() |
|
|||
File indexing completed on 2025-09-17 09:02:33
0001 /* 0002 * $Id: json_tokener.h,v 1.10 2006/07/25 03:24:50 mclark Exp $ 0003 * 0004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. 0005 * Michael Clark <michael@metaparadigm.com> 0006 * 0007 * This library is free software; you can redistribute it and/or modify 0008 * it under the terms of the MIT license. See COPYING for details. 0009 * 0010 */ 0011 0012 /** 0013 * @file 0014 * @brief Methods to parse an input string into a tree of json_object objects. 0015 */ 0016 #ifndef _json_tokener_h_ 0017 #define _json_tokener_h_ 0018 0019 #include "json_object.h" 0020 #include <stddef.h> 0021 0022 #ifdef __cplusplus 0023 extern "C" { 0024 #endif 0025 0026 enum json_tokener_error 0027 { 0028 json_tokener_success, 0029 json_tokener_continue, 0030 json_tokener_error_depth, 0031 json_tokener_error_parse_eof, 0032 json_tokener_error_parse_unexpected, 0033 json_tokener_error_parse_null, 0034 json_tokener_error_parse_boolean, 0035 json_tokener_error_parse_number, 0036 json_tokener_error_parse_array, 0037 json_tokener_error_parse_object_key_name, 0038 json_tokener_error_parse_object_key_sep, 0039 json_tokener_error_parse_object_value_sep, 0040 json_tokener_error_parse_string, 0041 json_tokener_error_parse_comment, 0042 json_tokener_error_parse_utf8_string, 0043 json_tokener_error_size, /* A string longer than INT32_MAX was passed as input */ 0044 json_tokener_error_memory /* Failed to allocate memory */ 0045 }; 0046 0047 /** 0048 * @deprecated Don't use this outside of json_tokener.c, it will be made private in a future release. 0049 */ 0050 enum json_tokener_state 0051 { 0052 json_tokener_state_eatws, 0053 json_tokener_state_start, 0054 json_tokener_state_finish, 0055 json_tokener_state_null, 0056 json_tokener_state_comment_start, 0057 json_tokener_state_comment, 0058 json_tokener_state_comment_eol, 0059 json_tokener_state_comment_end, 0060 json_tokener_state_string, 0061 json_tokener_state_string_escape, 0062 json_tokener_state_escape_unicode, 0063 json_tokener_state_escape_unicode_need_escape, 0064 json_tokener_state_escape_unicode_need_u, 0065 json_tokener_state_boolean, 0066 json_tokener_state_number, 0067 json_tokener_state_array, 0068 json_tokener_state_array_add, 0069 json_tokener_state_array_sep, 0070 json_tokener_state_object_field_start, 0071 json_tokener_state_object_field, 0072 json_tokener_state_object_field_end, 0073 json_tokener_state_object_value, 0074 json_tokener_state_object_value_add, 0075 json_tokener_state_object_sep, 0076 json_tokener_state_array_after_sep, 0077 json_tokener_state_object_field_start_after_sep, 0078 json_tokener_state_inf 0079 }; 0080 0081 /** 0082 * @deprecated Don't use this outside of json_tokener.c, it will be made private in a future release. 0083 */ 0084 struct json_tokener_srec 0085 { 0086 enum json_tokener_state state, saved_state; 0087 struct json_object *obj; 0088 struct json_object *current; 0089 char *obj_field_name; 0090 }; 0091 0092 #define JSON_TOKENER_DEFAULT_DEPTH 32 0093 0094 /** 0095 * Internal state of the json parser. 0096 * Do not access any fields of this structure directly. 0097 * Its definition is published due to historical limitations 0098 * in the json tokener API, and will be changed to be an opaque 0099 * type in the future. 0100 */ 0101 struct json_tokener 0102 { 0103 /** 0104 * @deprecated Do not access any of these fields outside of json_tokener.c 0105 */ 0106 char *str; 0107 struct printbuf *pb; 0108 int max_depth, depth, is_double, st_pos; 0109 /** 0110 * @deprecated See json_tokener_get_parse_end() instead. 0111 */ 0112 int char_offset; 0113 /** 0114 * @deprecated See json_tokener_get_error() instead. 0115 */ 0116 enum json_tokener_error err; 0117 unsigned int ucs_char, high_surrogate; 0118 char quote_char; 0119 struct json_tokener_srec *stack; 0120 int flags; 0121 }; 0122 0123 /** 0124 * Return the offset of the byte after the last byte parsed 0125 * relative to the start of the most recent string passed in 0126 * to json_tokener_parse_ex(). i.e. this is where parsing 0127 * would start again if the input contains another JSON object 0128 * after the currently parsed one. 0129 * 0130 * Note that when multiple parse calls are issued, this is *not* the 0131 * total number of characters parsed. 0132 * 0133 * In the past this would have been accessed as tok->char_offset. 0134 * 0135 * See json_tokener_parse_ex() for an example of how to use this. 0136 */ 0137 JSON_EXPORT size_t json_tokener_get_parse_end(struct json_tokener *tok); 0138 0139 /** 0140 * @deprecated Unused in json-c code 0141 */ 0142 typedef struct json_tokener json_tokener; 0143 0144 /** 0145 * Be strict when parsing JSON input. Use caution with 0146 * this flag as what is considered valid may become more 0147 * restrictive from one release to the next, causing your 0148 * code to fail on previously working input. 0149 * 0150 * Note that setting this will also effectively disable parsing 0151 * of multiple json objects in a single character stream 0152 * (e.g. {"foo":123}{"bar":234}); if you want to allow that 0153 * also set JSON_TOKENER_ALLOW_TRAILING_CHARS 0154 * 0155 * This flag is not set by default. 0156 * 0157 * @see json_tokener_set_flags() 0158 */ 0159 #define JSON_TOKENER_STRICT 0x01 0160 0161 /** 0162 * Use with JSON_TOKENER_STRICT to allow trailing characters after the 0163 * first parsed object. 0164 * 0165 * @see json_tokener_set_flags() 0166 */ 0167 #define JSON_TOKENER_ALLOW_TRAILING_CHARS 0x02 0168 0169 /** 0170 * Cause json_tokener_parse_ex() to validate that input is UTF8. 0171 * If this flag is specified and validation fails, then 0172 * json_tokener_get_error(tok) will return 0173 * json_tokener_error_parse_utf8_string 0174 * 0175 * This flag is not set by default. 0176 * 0177 * @see json_tokener_set_flags() 0178 */ 0179 #define JSON_TOKENER_VALIDATE_UTF8 0x10 0180 0181 /** 0182 * Given an error previously returned by json_tokener_get_error(), 0183 * return a human readable description of the error. 0184 * 0185 * @return a generic error message is returned if an invalid error value is provided. 0186 */ 0187 JSON_EXPORT const char *json_tokener_error_desc(enum json_tokener_error jerr); 0188 0189 /** 0190 * Retrieve the error caused by the last call to json_tokener_parse_ex(), 0191 * or json_tokener_success if there is no error. 0192 * 0193 * When parsing a JSON string in pieces, if the tokener is in the middle 0194 * of parsing this will return json_tokener_continue. 0195 * 0196 * @see json_tokener_error_desc(). 0197 */ 0198 JSON_EXPORT enum json_tokener_error json_tokener_get_error(struct json_tokener *tok); 0199 0200 /** 0201 * Allocate a new json_tokener. 0202 * When done using that to parse objects, free it with json_tokener_free(). 0203 * See json_tokener_parse_ex() for usage details. 0204 */ 0205 JSON_EXPORT struct json_tokener *json_tokener_new(void); 0206 0207 /** 0208 * Allocate a new json_tokener with a custom max nesting depth. 0209 * @see JSON_TOKENER_DEFAULT_DEPTH 0210 */ 0211 JSON_EXPORT struct json_tokener *json_tokener_new_ex(int depth); 0212 0213 /** 0214 * Free a json_tokener previously allocated with json_tokener_new(). 0215 */ 0216 JSON_EXPORT void json_tokener_free(struct json_tokener *tok); 0217 0218 /** 0219 * Reset the state of a json_tokener, to prepare to parse a 0220 * brand new JSON object. 0221 */ 0222 JSON_EXPORT void json_tokener_reset(struct json_tokener *tok); 0223 0224 /** 0225 * Parse a json_object out of the string `str`. 0226 * 0227 * If you need more control over how the parsing occurs, 0228 * see json_tokener_parse_ex(). 0229 */ 0230 JSON_EXPORT struct json_object *json_tokener_parse(const char *str); 0231 0232 /** 0233 * Parse a json_object out of the string `str`, but if it fails 0234 * return the error in `*error`. 0235 * @see json_tokener_parse() 0236 * @see json_tokener_parse_ex() 0237 */ 0238 JSON_EXPORT struct json_object *json_tokener_parse_verbose(const char *str, 0239 enum json_tokener_error *error); 0240 0241 /** 0242 * Set flags that control how parsing will be done. 0243 */ 0244 JSON_EXPORT void json_tokener_set_flags(struct json_tokener *tok, int flags); 0245 0246 /** 0247 * Parse a string and return a non-NULL json_object if a valid JSON value 0248 * is found. The string does not need to be a JSON object or array; 0249 * it can also be a string, number or boolean value. 0250 * 0251 * A partial JSON string can be parsed. If the parsing is incomplete, 0252 * NULL will be returned and json_tokener_get_error() will return 0253 * json_tokener_continue. 0254 * json_tokener_parse_ex() can then be called with additional bytes in str 0255 * to continue the parsing. 0256 * 0257 * If json_tokener_parse_ex() returns NULL and the error is anything other than 0258 * json_tokener_continue, a fatal error has occurred and parsing must be 0259 * halted. Then, the tok object must not be reused until json_tokener_reset() 0260 * is called. 0261 * 0262 * When a valid JSON value is parsed, a non-NULL json_object will be 0263 * returned, with a reference count of one which belongs to the caller. Also, 0264 * json_tokener_get_error() will return json_tokener_success. Be sure to check 0265 * the type with json_object_is_type() or json_object_get_type() before using 0266 * the object. 0267 * 0268 * Trailing characters after the parsed value do not automatically cause an 0269 * error. It is up to the caller to decide whether to treat this as an 0270 * error or to handle the additional characters, perhaps by parsing another 0271 * json value starting from that point. 0272 * 0273 * If the caller knows that they are at the end of their input, the length 0274 * passed MUST include the final '\0' character, so values with no inherent 0275 * end (i.e. numbers) can be properly parsed, rather than just returning 0276 * json_tokener_continue. 0277 * 0278 * Extra characters can be detected by comparing the value returned by 0279 * json_tokener_get_parse_end() against 0280 * the length of the last len parameter passed in. 0281 * 0282 * The tokener does \b not maintain an internal buffer so the caller is 0283 * responsible for a subsequent call to json_tokener_parse_ex with an 0284 * appropriate str parameter starting with the extra characters. 0285 * 0286 * This interface is presently not 64-bit clean due to the int len argument 0287 * so the function limits the maximum string size to INT32_MAX (2GB). 0288 * If the function is called with len == -1 then strlen is called to check 0289 * the string length is less than INT32_MAX (2GB) 0290 * 0291 * Example: 0292 * @code 0293 json_object *jobj = NULL; 0294 const char *mystring = NULL; 0295 int stringlen = 0; 0296 enum json_tokener_error jerr; 0297 do { 0298 mystring = ... // get JSON string, e.g. read from file, etc... 0299 stringlen = strlen(mystring); 0300 if (end_of_input) 0301 stringlen++; // Include the '\0' if we know we're at the end of input 0302 jobj = json_tokener_parse_ex(tok, mystring, stringlen); 0303 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 0304 if (jerr != json_tokener_success) 0305 { 0306 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 0307 // Handle errors, as appropriate for your application. 0308 } 0309 if (json_tokener_get_parse_end(tok) < stringlen) 0310 { 0311 // Handle extra characters after parsed object as desired. 0312 // e.g. issue an error, parse another object from that point, etc... 0313 } 0314 // Success, use jobj here. 0315 0316 @endcode 0317 * 0318 * @param tok a json_tokener previously allocated with json_tokener_new() 0319 * @param str an string with any valid JSON expression, or portion of. This does not need to be null terminated. 0320 * @param len the length of str 0321 */ 0322 JSON_EXPORT struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, 0323 int len); 0324 0325 #ifdef __cplusplus 0326 } 0327 #endif 0328 0329 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |