autojson  0.1
A JSON parser base on the automaton provided by json.org
 All Classes Files Functions Typedefs Enumerations Enumerator Pages
Classes | Typedefs | Enumerations | Functions
JSON_checker.h File Reference

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)
 

Detailed Description

Main header of the C interface of autojson.

This file is adapted from http://www.json.org/JSON_checker/ with modification.

Typedef Documentation

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().

Parameters
userA user-provided pointer by JSON_checker_char(). The parser will not interpret its value. It will be pass as-is.
typeType of event encountered.
dataPointer 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.
lenThe 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

Enumeration Type Documentation

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.

Enumerator
JSON_array_start 

Start of a JSON array.

JSON_array_end 

End of a JSON array.

JSON_object_start 

Start of a JSON object.

JSON_object_key 

A key inside a JSON object. The value of the key is given by data with len bytes.

JSON_object_end 

End of a JSON object.

JSON_string 

String as a value. The value is given by data and len.

JSON_number 

Number as a value. It's provided as a string. You need to call atoi() to obtain the real number.

JSON_null 

Literal value "null".

JSON_true 

Literal value "true".

JSON_false 

Literal value "false".

Enum to indicate success/failure of the function

Function Documentation

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.

Parameters
jcA parser created by new_JSON_checker()
charsA block of JSON data to be parsed. It does not need to be a complete JSON document. JSON_checker_char() can parse incrementally.
lenNumber of bytes in the buffer pointed by chars.
cbA pointer to a callback function to be called when the parser encounter an event. See JSON_event for more details about the events.
userA 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.