Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-16 09:44:29

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