|
|
|||
File indexing completed on 2026-11-02 10:22:12
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 * The depth must be at least 1. 0210 * @see JSON_TOKENER_DEFAULT_DEPTH 0211 */ 0212 JSON_EXPORT struct json_tokener *json_tokener_new_ex(int depth); 0213 0214 /** 0215 * Free a json_tokener previously allocated with json_tokener_new(). 0216 */ 0217 JSON_EXPORT void json_tokener_free(struct json_tokener *tok); 0218 0219 /** 0220 * Reset the state of a json_tokener, to prepare to parse a 0221 * brand new JSON object. 0222 */ 0223 JSON_EXPORT void json_tokener_reset(struct json_tokener *tok); 0224 0225 /** 0226 * Parse a json_object out of the string `str`. 0227 * 0228 * If you need more control over how the parsing occurs, 0229 * see json_tokener_parse_ex(). 0230 */ 0231 JSON_EXPORT struct json_object *json_tokener_parse(const char *str); 0232 0233 /** 0234 * Parse a json_object out of the string `str`, but if it fails 0235 * return the error in `*error`. 0236 * @see json_tokener_parse() 0237 * @see json_tokener_parse_ex() 0238 */ 0239 JSON_EXPORT struct json_object *json_tokener_parse_verbose(const char *str, 0240 enum json_tokener_error *error); 0241 0242 /** 0243 * Set flags that control how parsing will be done. 0244 */ 0245 JSON_EXPORT void json_tokener_set_flags(struct json_tokener *tok, int flags); 0246 0247 /** 0248 * Parse a string and return a non-NULL json_object if a valid JSON value 0249 * is found. The string does not need to be a JSON object or array; 0250 * it can also be a string, number or boolean value. 0251 * 0252 * A partial JSON string can be parsed. If the parsing is incomplete, 0253 * NULL will be returned and json_tokener_get_error() will return 0254 * json_tokener_continue. 0255 * json_tokener_parse_ex() can then be called with additional bytes in str 0256 * to continue the parsing. 0257 * 0258 * If json_tokener_parse_ex() returns NULL and the error is anything other than 0259 * json_tokener_continue, a fatal error has occurred and parsing must be 0260 * halted. Then, the tok object must not be reused until json_tokener_reset() 0261 * is called. 0262 * 0263 * When a valid JSON value is parsed, a non-NULL json_object will be 0264 * returned, with a reference count of one which belongs to the caller. Also, 0265 * json_tokener_get_error() will return json_tokener_success. Be sure to check 0266 * the type with json_object_is_type() or json_object_get_type() before using 0267 * the object. 0268 * 0269 * Trailing characters after the parsed value do not automatically cause an 0270 * error. It is up to the caller to decide whether to treat this as an 0271 * error or to handle the additional characters, perhaps by parsing another 0272 * json value starting from that point. 0273 * 0274 * If the caller knows that they are at the end of their input, the length 0275 * passed MUST include the final '\0' character, so values with no inherent 0276 * end (i.e. numbers) can be properly parsed, rather than just returning 0277 * json_tokener_continue. 0278 * 0279 * Extra characters can be detected by comparing the value returned by 0280 * json_tokener_get_parse_end() against 0281 * the length of the last len parameter passed in. 0282 * 0283 * The tokener does \b not maintain an internal buffer so the caller is 0284 * responsible for a subsequent call to json_tokener_parse_ex with an 0285 * appropriate str parameter starting with the extra characters. 0286 * 0287 * This interface is presently not 64-bit clean due to the int len argument 0288 * so the function limits the maximum string size to INT32_MAX (2GB). 0289 * If the function is called with len == -1 then strlen is called to check 0290 * the string length is less than INT32_MAX (2GB) 0291 * 0292 * Example: 0293 * @code 0294 json_object *jobj = NULL; 0295 const char *mystring = NULL; 0296 int stringlen = 0; 0297 enum json_tokener_error jerr; 0298 do { 0299 mystring = ... // get JSON string, e.g. read from file, etc... 0300 stringlen = strlen(mystring); 0301 if (end_of_input) 0302 stringlen++; // Include the '\0' if we know we're at the end of input 0303 jobj = json_tokener_parse_ex(tok, mystring, stringlen); 0304 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 0305 if (jerr != json_tokener_success) 0306 { 0307 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 0308 // Handle errors, as appropriate for your application. 0309 } 0310 if (json_tokener_get_parse_end(tok) < stringlen) 0311 { 0312 // Handle extra characters after parsed object as desired. 0313 // e.g. issue an error, parse another object from that point, etc... 0314 } 0315 // Success, use jobj here. 0316 0317 @endcode 0318 * 0319 * @param tok a json_tokener previously allocated with json_tokener_new() 0320 * @param str an string with any valid JSON expression, or portion of. This does not need to be null terminated. 0321 * @param len the length of str 0322 */ 0323 JSON_EXPORT struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, 0324 int len); 0325 0326 #ifdef __cplusplus 0327 } 0328 #endif 0329 0330 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|