2 * Copyright (c) 2010 Serge A. Zaitsev
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * Slightly modified by AK to not assume 0 terminated input.
29 * Allocates a fresh unused token from the token pool.
31 static jsmntok_t
*jsmn_alloc_token(jsmn_parser
*parser
,
32 jsmntok_t
*tokens
, size_t num_tokens
)
36 if ((unsigned)parser
->toknext
>= num_tokens
)
38 tok
= &tokens
[parser
->toknext
++];
39 tok
->start
= tok
->end
= -1;
45 * Fills token type and boundaries.
47 static void jsmn_fill_token(jsmntok_t
*token
, jsmntype_t type
,
57 * Fills next available token with JSON primitive.
59 static jsmnerr_t
jsmn_parse_primitive(jsmn_parser
*parser
, const char *js
,
61 jsmntok_t
*tokens
, size_t num_tokens
)
68 for (; parser
->pos
< len
; parser
->pos
++) {
69 switch (js
[parser
->pos
]) {
72 * In strict mode primitive must be followed by ","
88 if (js
[parser
->pos
] < 32 || js
[parser
->pos
] >= 127) {
90 return JSMN_ERROR_INVAL
;
95 * In strict mode primitive must be followed by a
99 return JSMN_ERROR_PART
;
103 token
= jsmn_alloc_token(parser
, tokens
, num_tokens
);
106 return JSMN_ERROR_NOMEM
;
108 jsmn_fill_token(token
, JSMN_PRIMITIVE
, start
, parser
->pos
);
109 parser
->pos
--; /* parent sees closing brackets */
114 * Fills next token with JSON string.
116 static jsmnerr_t
jsmn_parse_string(jsmn_parser
*parser
, const char *js
,
118 jsmntok_t
*tokens
, size_t num_tokens
)
121 int start
= parser
->pos
;
123 /* Skip starting quote */
126 for (; parser
->pos
< len
; parser
->pos
++) {
127 char c
= js
[parser
->pos
];
129 /* Quote: end of string */
131 token
= jsmn_alloc_token(parser
, tokens
, num_tokens
);
134 return JSMN_ERROR_NOMEM
;
136 jsmn_fill_token(token
, JSMN_STRING
, start
+1,
141 /* Backslash: Quoted symbol expected */
144 switch (js
[parser
->pos
]) {
145 /* Allowed escaped symbols */
155 /* Allows escaped symbol \uXXXX */
159 /* Unexpected symbol */
162 return JSMN_ERROR_INVAL
;
167 return JSMN_ERROR_PART
;
171 * Parse JSON string and fill tokens.
173 jsmnerr_t
jsmn_parse(jsmn_parser
*parser
, const char *js
, size_t len
,
174 jsmntok_t
*tokens
, unsigned int num_tokens
)
180 for (; parser
->pos
< len
; parser
->pos
++) {
188 token
= jsmn_alloc_token(parser
, tokens
, num_tokens
);
190 return JSMN_ERROR_NOMEM
;
191 if (parser
->toksuper
!= -1)
192 tokens
[parser
->toksuper
].size
++;
193 token
->type
= (c
== '{' ? JSMN_OBJECT
: JSMN_ARRAY
);
194 token
->start
= parser
->pos
;
195 parser
->toksuper
= parser
->toknext
- 1;
199 type
= (c
== '}' ? JSMN_OBJECT
: JSMN_ARRAY
);
200 for (i
= parser
->toknext
- 1; i
>= 0; i
--) {
202 if (token
->start
!= -1 && token
->end
== -1) {
203 if (token
->type
!= type
)
204 return JSMN_ERROR_INVAL
;
205 parser
->toksuper
= -1;
206 token
->end
= parser
->pos
+ 1;
210 /* Error if unmatched closing bracket */
212 return JSMN_ERROR_INVAL
;
213 for (; i
>= 0; i
--) {
215 if (token
->start
!= -1 && token
->end
== -1) {
216 parser
->toksuper
= i
;
222 r
= jsmn_parse_string(parser
, js
, len
, tokens
,
226 if (parser
->toksuper
!= -1)
227 tokens
[parser
->toksuper
].size
++;
238 * In strict mode primitives are:
239 * numbers and booleans.
257 * In non-strict mode every unquoted value
263 r
= jsmn_parse_primitive(parser
, js
, len
, tokens
,
267 if (parser
->toksuper
!= -1)
268 tokens
[parser
->toksuper
].size
++;
272 /* Unexpected char in strict mode */
274 return JSMN_ERROR_INVAL
;
279 for (i
= parser
->toknext
- 1; i
>= 0; i
--) {
280 /* Unmatched opened object or array */
281 if (tokens
[i
].start
!= -1 && tokens
[i
].end
== -1)
282 return JSMN_ERROR_PART
;
289 * Creates a new parser based over a given buffer with an array of tokens
292 void jsmn_init(jsmn_parser
*parser
)
296 parser
->toksuper
= -1;
299 const char *jsmn_strerror(jsmnerr_t err
)
302 case JSMN_ERROR_NOMEM
:
303 return "No enough tokens";
304 case JSMN_ERROR_INVAL
:
305 return "Invalid character inside JSON string";
306 case JSMN_ERROR_PART
:
307 return "The string is not a full JSON packet, more bytes expected";
311 return "Unknown json error";