forked from zserge/jsmn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsmn.h
191 lines (169 loc) · 10 KB
/
jsmn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef __JSMN_H_
#define __JSMN_H_
#include <stddef.h>
#ifdef JSMN_EMITTER
#define JSMN_DOM
#define JSMN_STRICT
#endif
#ifdef JSMN_DOM
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* JSON type identifier. Basic types are:
* o Object
* o Array
* o String
* o Other primitive: number, boolean (true/false) or null
*/
typedef enum {
JSMN_UNDEFINED = 0,
JSMN_OBJECT = 1,
JSMN_ARRAY = 2,
JSMN_STRING = 3,
JSMN_PRIMITIVE = 4
} jsmntype_t;
enum jsmnerr {
/* Not enough tokens were provided */
JSMN_ERROR_NOMEM = -1,
/* Invalid character inside JSON string */
JSMN_ERROR_INVAL = -2,
/* The string is not a full JSON packet, more bytes expected */
JSMN_ERROR_PART = -3
};
#ifdef JSMN_DOM
struct children_t {
int first;
int last;
};
struct siblings_t {
int prev;
int next;
};
struct family_t {
int parent;
struct siblings_t siblings;
struct children_t children;
};
#endif
/**
* JSON token description.
* @param type type (object, array, string etc.)
* @param start start position in JSON data string
* @param end end position in JSON data string
*/
typedef struct jsmntok_s {
jsmntype_t type;
int start;
int end;
#ifdef JSMN_DOM
struct family_t family;
#else
int size;
#ifdef JSMN_PARENT_LINKS
int parent;
#endif
#endif
} jsmntok_t;
/**
* JSON parser. Contains an array of token blocks available. Also stores
* the string being parsed now and current position in that string
*/
typedef struct {
unsigned int pos; /* offset in the JSON string */
unsigned int toknext; /* next token to allocate */
int toksuper; /* superior token node, e.g parent object or array */
} jsmn_parser;
/**
* Create JSON parser over an array of tokens
*/
void jsmn_init(jsmn_parser *parser);
/**
* Run JSON parser. It parses a JSON data string into and array of tokens, each describing
* a single JSON object.
*/
int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
jsmntok_t *tokens, unsigned int num_tokens);
#ifdef JSMN_DOM
int jsmn_dom_rollback( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_is_null( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_is_bool( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_is_true( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_is_false( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_integer( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i, int *value_ptr);
int jsmn_dom_get_double( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i, double *value_ptr);
int jsmn_dom_get_utf8( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i, char *val8, size_t val8_len);
size_t jsmn_dom_get_utf8len( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_utf32( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i, wchar_t *val32, size_t val32_len);
size_t jsmn_dom_get_utf32len( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_value( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int i, char *buf, size_t buflen);
int jsmn_dom_get_start( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
size_t jsmn_dom_get_strlen( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
size_t jsmn_dom_get_count( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
jsmntype_t jsmn_dom_get_type( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_parent( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_child( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_sibling( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_is_open( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_get_open( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_add( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int parent_i, int i);
int jsmn_dom_delete( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_move( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int parent_i, int i);
int jsmn_dom_replace( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int prior_i, int i);
int jsmn_dom_set( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i, jsmntype_t type, int start, int end);
int jsmn_dom_set_from( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int to_i, int i);
int jsmn_dom_close( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i, int end);
int jsmn_dom_new( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens);
int jsmn_dom_new_as( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, jsmntype_t type, int start, int end);
int jsmn_dom_new_object( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens);
int jsmn_dom_new_array( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens);
int jsmn_dom_new_primitive( jsmn_parser *parser, char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, const char *value);
int jsmn_dom_dup_primitive( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int i);
int jsmn_dom_new_integer( jsmn_parser *parser, char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int value);
int jsmn_dom_new_double( jsmn_parser *parser, char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, double value);
int jsmn_dom_new_string( jsmn_parser *parser, char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, const char *value);
int jsmn_dom_new_utf8( jsmn_parser *parser, char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, const char *value, size_t value_len);
#define TRUE_EXPR "true "
#define FALSE_EXPR "false "
#define NULL_EXPR "null "
int jsmn_dom_eval( jsmn_parser *parser, char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, const char *value);
int jsmn_dom_get_utf8_name( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int object_i, char *utf8_name, size_t utf8_len);
int jsmn_dom_get_by_utf8_name(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens, int object_i, char *utf8_name, size_t utf8_len);
int jsmn_dom_insert_name( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int object_i, int name_i, int value_i);
int jsmn_dom_insert_value( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int array_i, int value_i);
int jsmn_dom_delete_name( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int object_i, int name_i);
int jsmn_dom_delete_value( jsmn_parser *parser, jsmntok_t *tokens, unsigned int num_tokens, int array_i, int value_i);
#endif
#ifdef JSMN_EMITTER
enum tokphase {
PHASE_UNOPENED = 0,
PHASE_OPENED,
PHASE_UNCLOSED,
PHASE_CLOSED
};
typedef struct {
int cursor_i;
enum tokphase cursor_phase;
} jsmn_emitter;
void jsmn_emit_seek( jsmn_emitter *emitter, int object_i);
void jsmn_init_emitter(jsmn_emitter *emitter);
/*
* Emit as many remaining tokens as possible into the buffer `outjs` of length `outlen`.
* There will always be a null terminator because a token will only be emitted if there is enough room for the string including its null terminator.
* Call iteratively to fill multiple or extended buffers.
*
* Returns the number of bytes written into `outjs` when >= 0:
* Returns JSMN_ERROR_* when < 0;
*
* When emission is complete: `emitter->cursor_i == -1`.
* Emission starts at emitter state, which defaults to `{0, PHASE_UNOPENED}`.
*/
int jsmn_emit(jsmn_parser *parser, char *js, size_t len,
jsmntok_t *tokens, unsigned int num_tokens,
jsmn_emitter *emitter, char *outjs, size_t outlen);
#endif
#ifdef __cplusplus
}
#endif
#endif /* __JSMN_H_ */