autojson
0.1
A JSON parser base on the automaton provided by json.org
|
Main header of the C interface of autojson. More...
#include <stddef.h>
Go to the source code of this file.
Classes | |
struct | JSON_checker_struct |
Typedefs | |
typedef enum JSON_event | JSON_event |
typedef enum JSON_result | JSON_result |
typedef void(* | JSON_callback )(void *user, JSON_event type, const char *data, size_t len) |
typedef struct JSON_checker_struct * | JSON_checker |
Enumerations | |
enum | JSON_event { JSON_array_start, JSON_array_end, JSON_object_start, JSON_object_key, JSON_object_end, JSON_string, JSON_number, JSON_null, JSON_true, JSON_false } |
enum | JSON_result { JSON_error, JSON_ok } |
Functions | |
JSON_checker | new_JSON_checker (int depth) |
int | JSON_checker_char (JSON_checker jc, const char *chars, size_t len, JSON_callback cb, void *user) |
int | JSON_checker_done (JSON_checker jc) |
Main header of the C interface of autojson.
This file is adapted from http://www.json.org/JSON_checker/ with modification.
typedef void(* JSON_callback)(void *user, JSON_event type, const char *data, size_t len) |
A callback function to be invoked when the parser encounter a event. See JSON_event for a detailed list of events available. You need to provide this function when you call JSON_checker_char().
user | A user-provided pointer by JSON_checker_char(). The parser will not interpret its value. It will be pass as-is. |
type | Type of event encountered. |
data | Pointer to parsed data. It's only meaningful for certain events, such as JSON_string. Note that this buffer is not null-terminated. You need to refer to len to know how many bytes are there. |
len | The number of bytes in the buffer pointed by data . Note that the unit is bytes, not characters, as unicode characters many need more than 1 byte to represent in UTF-8/UTF-16. |
typedef enum JSON_event JSON_event |
The type of JSON parse events. It is used to indicate what the parser has encountered. It is a parameter to the JSON_callback() function.
typedef enum JSON_result JSON_result |
Enum to indicate success/failure of the function
enum JSON_event |
The type of JSON parse events. It is used to indicate what the parser has encountered. It is a parameter to the JSON_callback() function.
enum JSON_result |
Enum to indicate success/failure of the function
int JSON_checker_char | ( | JSON_checker | jc, |
const char * | chars, | ||
size_t | len, | ||
JSON_callback | cb, | ||
void * | user | ||
) |
Parse JSON data. Call this function to parse some JSON data. The data does not need to be a complete JSON file. It can be a fragment of the file. The parser will parse it progressively and invoke the callback function it is can successfully parse something.
jc | A parser created by new_JSON_checker() |
chars | A block of JSON data to be parsed. It does not need to be a complete JSON document. JSON_checker_char() can parse incrementally. |
len | Number of bytes in the buffer pointed by chars . |
cb | A pointer to a callback function to be called when the parser encounter an event. See JSON_event for more details about the events. |
user | A pointer to user data. The parser will not try to interpret this pointer. It will be passed as-is to the callback function cb . |
JSON_checker new_JSON_checker | ( | int | depth | ) |
Create a new parser. new_JSON_checker() starts the checking process by constructing a JSON_checker object. It takes the depth
parameter that restricts the level of maximum nesting.
To continue the process, call JSON_checker_char() for each block of characters in the JSON text, and then call JSON_checker_done() to obtain the final result. These functions are fully reentrant.
The JSON_checker object will be deleted by JSON_checker_done(). JSON_checker_char() will auto delete the JSON_checker object if it sees an error.