Annotation of parser3/src/lib/json/pa_json.h, revision 1.3
1.1 moko 1: /*
1.2 moko 2: * Copyright (C) 2009-2011 Vincent Hanquez <vincent@snarc.org>
1.1 moko 3: *
4: * This program is free software; you can redistribute it and/or modify
5: * it under the terms of the GNU Lesser General Public License as published
6: * by the Free Software Foundation; version 2.1 or version 3.0 only.
7: *
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: * GNU General Public License for more details.
12: */
13:
14: #ifndef JSON_H
15: #define JSON_H
16:
17: #include "pa_config_includes.h"
18:
19: #ifdef __cplusplus
20: extern "C" {
21: #endif
22:
23: typedef enum
24: {
25: JSON_NONE,
26: JSON_ARRAY_BEGIN,
27: JSON_OBJECT_BEGIN,
28: JSON_ARRAY_END,
29: JSON_OBJECT_END,
30: JSON_INT,
31: JSON_FLOAT,
32: JSON_STRING,
33: JSON_KEY,
34: JSON_TRUE,
35: JSON_FALSE,
1.3 ! moko 36: JSON_NULL
1.1 moko 37: } json_type;
38:
39: typedef enum
40: {
41: /* SUCCESS = 0 */
42: /* running out of memory */
43: JSON_ERROR_NO_MEMORY = 1,
44: /* character < 32, except space newline tab */
45: JSON_ERROR_BAD_CHAR,
46: /* trying to pop more object/array than pushed on the stack */
47: JSON_ERROR_POP_EMPTY,
48: /* trying to pop wrong type of mode. popping array in object mode, vice versa */
49: JSON_ERROR_POP_UNEXPECTED_MODE,
50: /* reach nesting limit on stack */
51: JSON_ERROR_NESTING_LIMIT,
52: /* reach data limit on buffer */
53: JSON_ERROR_DATA_LIMIT,
54: /* comment are not allowed with current configuration */
55: JSON_ERROR_COMMENT_NOT_ALLOWED,
56: /* unexpected char in the current parser context */
57: JSON_ERROR_UNEXPECTED_CHAR,
58: /* unicode low surrogate missing after high surrogate */
59: JSON_ERROR_UNICODE_MISSING_LOW_SURROGATE,
60: /* unicode low surrogate missing without previous high surrogate */
61: JSON_ERROR_UNICODE_UNEXPECTED_LOW_SURROGATE,
62: /* found a comma not in structure (array/object) */
63: JSON_ERROR_COMMA_OUT_OF_STRUCTURE,
64: /* callback returns error */
1.3 ! moko 65: JSON_ERROR_CALLBACK
1.1 moko 66: } json_error;
67:
68: #define LIBJSON_DEFAULT_STACK_SIZE 256
69: #define LIBJSON_DEFAULT_BUFFER_SIZE 4096
70:
71: typedef int (*json_parser_callback)(void *userdata, int type, const char *data, uint32_t length);
72:
73: typedef struct {
74: uint32_t buffer_initial_size;
75: uint32_t max_nesting;
76: uint32_t max_data;
77: int allow_c_comments;
78: int allow_yaml_comments;
79: void * (*user_malloc)(size_t size);
80: void * (*user_realloc)(void *ptr, size_t size);
81: void (*user_free)(void *ptr);
82: } json_config;
83:
84: typedef struct json_parser {
85: json_config config;
86:
87: /* SAJ callback */
88: json_parser_callback callback;
89: void *userdata;
90:
91: /* parser state */
92: uint8_t state;
93: uint8_t save_state;
94: uint8_t expecting_key;
95: uint16_t unicode_multi;
96: json_type type;
97:
98: /* state stack */
99: uint8_t *stack;
100: uint32_t stack_offset;
101: uint32_t stack_size;
102:
103: /* parse buffer */
104: char *buffer;
105: uint32_t buffer_size;
106: uint32_t buffer_offset;
107: } json_parser;
108:
109: /** json_parser_init initialize a parser structure taking a config,
110: * a config and its userdata.
111: * return JSON_ERROR_NO_MEMORY if memory allocation failed or SUCCESS. */
112: int json_parser_init(json_parser *parser, json_config *cfg,
113: json_parser_callback callback, void *userdata);
114:
115: /** json_parser_free freed memory structure allocated by the parser */
116: int json_parser_free(json_parser *parser);
117:
118: /** json_parser_string append a string s with a specific length to the parser
119: * return 0 if everything went ok, a JSON_ERROR_* otherwise.
120: * the user can supplied a valid processed pointer that will
121: * be fill with the number of processed characters before returning */
122: int json_parser_string(json_parser *parser, const char *string,
123: uint32_t length, uint32_t *processed);
124:
125: /** json_parser_char append one single char to the parser
126: * return 0 if everything went ok, a JSON_ERROR_* otherwise */
127: int json_parser_char(json_parser *parser, unsigned char next_char);
128:
129: /** json_parser_is_done return 0 is the parser isn't in a finish state. !0 if it is */
130: int json_parser_is_done(json_parser *parser);
131:
132: #ifdef __cplusplus
133: }
134: #endif
135:
136: #endif /* JSON_H */
E-mail: