json-parser.h revision 662bb64be818407c6719a69780411f7ca8d6c96f
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen#ifndef JSON_PARSER_H
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen#define JSON_PARSER_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenenum json_type {
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen /* { key: */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen JSON_TYPE_OBJECT_KEY,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen /* : { new object */
137ea7ca34005345aa2304a940149b7f3774d727Timo Sirainen JSON_TYPE_OBJECT,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen /* } (not returned for the root object) */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_OBJECT_END,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_ARRAY,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_ARRAY_END,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
137ea7ca34005345aa2304a940149b7f3774d727Timo Sirainen JSON_TYPE_STRING,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_NUMBER,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_TRUE,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_FALSE,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_TYPE_NULL
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen};
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenenum json_parser_flags {
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen /* By default we assume that the input is an object and parsing skips
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen the root level "{" and "}". If this flag is set, it's possible to
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen parse any other type of JSON values directly. */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen JSON_PARSER_NO_ROOT_OBJECT = 0x01
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen};
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Parse JSON tokens from the input stream. */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenstruct json_parser *json_parser_init(struct istream *input);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenstruct json_parser *json_parser_init_flags(struct istream *input,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen enum json_parser_flags flags);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenint json_parser_deinit(struct json_parser **parser, const char **error_r);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Parse the next token. Returns 1 if found, 0 if more input stream is
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen non-blocking and needs more input, -1 if input stream is at EOF. */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenint json_parse_next(struct json_parser *parser, enum json_type *type_r,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen const char **value_r);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Skip the next object value. If it's an object, its members are also
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen skipped. */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenvoid json_parse_skip_next(struct json_parser *parser);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Return the following string as input stream. Returns 1 if ok, 0 if
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen input stream is non-blocking and needs more input, -1 if the next token
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen isn't a string (call json_parse_next()). */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenint json_parse_next_stream(struct json_parser *parser,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen struct istream **input_r);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Append data to already opened JSON string. src should be valid UTF-8 data. */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenvoid json_append_escaped(string_t *dest, const char *src);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Same as json_append_escaped(), but append non-\0 terminated input. */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenvoid json_append_escaped_data(string_t *dest, const unsigned char *src, size_t size);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainenvoid ostream_escaped_json_format(string_t *dest, unsigned char src);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen#endif
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen