jsmn.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2010 Serge Zaitsev
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifndef JSMN_H
  25. #define JSMN_H
  26. #include <stddef.h>
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #ifdef JSMN_STATIC
  31. #define JSMN_API static
  32. #else
  33. #define JSMN_API extern
  34. #endif
  35. #define JSMN_STRICT 1
  36. /**
  37. * JSON type identifier. Basic types are:
  38. * o Object
  39. * o Array
  40. * o String
  41. * o Other primitive: number, boolean (true/false) or null
  42. */
  43. typedef enum {
  44. JSMN_UNDEFINED = 0,
  45. JSMN_OBJECT = 1 << 0,
  46. JSMN_ARRAY = 1 << 1,
  47. JSMN_STRING = 1 << 2,
  48. JSMN_PRIMITIVE = 1 << 3
  49. } jsmntype_t;
  50. enum jsmnerr {
  51. /* Not enough tokens were provided */
  52. JSMN_ERROR_NOMEM = -1,
  53. /* Invalid character inside JSON string */
  54. JSMN_ERROR_INVAL = -2,
  55. /* The string is not a full JSON packet, more bytes expected */
  56. JSMN_ERROR_PART = -3
  57. };
  58. /**
  59. * JSON token description.
  60. * type type (object, array, string etc.)
  61. * start start position in JSON data string
  62. * end end position in JSON data string
  63. */
  64. typedef struct jsmntok {
  65. jsmntype_t type;
  66. int start;
  67. int end;
  68. int size;
  69. #ifdef JSMN_PARENT_LINKS
  70. int parent;
  71. #endif
  72. } jsmntok_t;
  73. /**
  74. * JSON parser. Contains an array of token blocks available. Also stores
  75. * the string being parsed now and current position in that string.
  76. */
  77. typedef struct jsmn_parser {
  78. unsigned int pos; /* offset in the JSON string */
  79. unsigned int toknext; /* next token to allocate */
  80. int toksuper; /* superior token node, e.g. parent object or array */
  81. } jsmn_parser;
  82. /**
  83. * Create JSON parser over an array of tokens
  84. */
  85. JSMN_API void jsmn_init(jsmn_parser* parser);
  86. /**
  87. * Run JSON parser. It parses a JSON data string into and array of tokens,
  88. * each describing a single JSON object.
  89. */
  90. JSMN_API int jsmn_parse(jsmn_parser* parser,
  91. const char* js,
  92. const size_t len,
  93. jsmntok_t* tokens,
  94. const unsigned int num_tokens);
  95. #ifndef JSMN_HEADER
  96. /**
  97. * Allocates a fresh unused token from the token pool.
  98. */
  99. static jsmntok_t* jsmn_alloc_token(jsmn_parser* parser,
  100. jsmntok_t* tokens,
  101. const size_t num_tokens) {
  102. jsmntok_t* tok;
  103. if (parser->toknext >= num_tokens) {
  104. return NULL;
  105. }
  106. tok = &tokens[parser->toknext++];
  107. tok->start = tok->end = -1;
  108. tok->size = 0;
  109. #ifdef JSMN_PARENT_LINKS
  110. tok->parent = -1;
  111. #endif
  112. return tok;
  113. }
  114. /**
  115. * Fills token type and boundaries.
  116. */
  117. static void jsmn_fill_token(jsmntok_t* token,
  118. const jsmntype_t type,
  119. const int start,
  120. const int end) {
  121. token->type = type;
  122. token->start = start;
  123. token->end = end;
  124. token->size = 0;
  125. }
  126. /**
  127. * Fills next available token with JSON primitive.
  128. */
  129. static int jsmn_parse_primitive(jsmn_parser* parser,
  130. const char* js,
  131. const size_t len,
  132. jsmntok_t* tokens,
  133. const size_t num_tokens) {
  134. jsmntok_t* token;
  135. int start;
  136. start = parser->pos;
  137. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
  138. switch (js[parser->pos]) {
  139. #ifndef JSMN_STRICT
  140. /* In strict mode primitive must be followed by "," or "}" or
  141. * "]" */
  142. case ':':
  143. #endif
  144. case '\t':
  145. case '\r':
  146. case '\n':
  147. case ' ':
  148. case ',':
  149. case ']':
  150. case '}':
  151. goto found;
  152. default:
  153. /* to quiet a warning from gcc*/
  154. break;
  155. }
  156. if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
  157. parser->pos = start;
  158. return JSMN_ERROR_INVAL;
  159. }
  160. }
  161. #ifdef JSMN_STRICT
  162. /* In strict mode primitive must be followed by a comma/object/array */
  163. parser->pos = start;
  164. return JSMN_ERROR_PART;
  165. #endif
  166. found:
  167. if (tokens == NULL) {
  168. parser->pos--;
  169. return 0;
  170. }
  171. token = jsmn_alloc_token(parser, tokens, num_tokens);
  172. if (token == NULL) {
  173. parser->pos = start;
  174. return JSMN_ERROR_NOMEM;
  175. }
  176. jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
  177. #ifdef JSMN_PARENT_LINKS
  178. token->parent = parser->toksuper;
  179. #endif
  180. parser->pos--;
  181. return 0;
  182. }
  183. /**
  184. * Fills next token with JSON string.
  185. */
  186. static int jsmn_parse_string(jsmn_parser* parser,
  187. const char* js,
  188. const size_t len,
  189. jsmntok_t* tokens,
  190. const size_t num_tokens) {
  191. jsmntok_t* token;
  192. int start = parser->pos;
  193. /* Skip starting quote */
  194. parser->pos++;
  195. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
  196. char c = js[parser->pos];
  197. /* Quote: end of string */
  198. if (c == '\"') {
  199. if (tokens == NULL) {
  200. return 0;
  201. }
  202. token = jsmn_alloc_token(parser, tokens, num_tokens);
  203. if (token == NULL) {
  204. parser->pos = start;
  205. return JSMN_ERROR_NOMEM;
  206. }
  207. jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
  208. #ifdef JSMN_PARENT_LINKS
  209. token->parent = parser->toksuper;
  210. #endif
  211. return 0;
  212. }
  213. /* Backslash: Quoted symbol expected */
  214. if (c == '\\' && parser->pos + 1 < len) {
  215. int i;
  216. parser->pos++;
  217. switch (js[parser->pos]) {
  218. /* Allowed escaped symbols */
  219. case '\"':
  220. case '/':
  221. case '\\':
  222. case 'b':
  223. case 'f':
  224. case 'r':
  225. case 'n':
  226. case 't':
  227. break;
  228. /* Allows escaped symbol \uXXXX */
  229. case 'u':
  230. parser->pos++;
  231. for (i = 0;
  232. i < 4 && parser->pos < len && js[parser->pos] != '\0';
  233. i++) {
  234. /* If it isn't a hex character we have an error */
  235. if (!((js[parser->pos] >= 48 &&
  236. js[parser->pos] <= 57) || /* 0-9 */
  237. (js[parser->pos] >= 65 &&
  238. js[parser->pos] <= 70) || /* A-F */
  239. (js[parser->pos] >= 97 &&
  240. js[parser->pos] <= 102))) { /* a-f */
  241. parser->pos = start;
  242. return JSMN_ERROR_INVAL;
  243. }
  244. parser->pos++;
  245. }
  246. parser->pos--;
  247. break;
  248. /* Unexpected symbol */
  249. default:
  250. parser->pos = start;
  251. return JSMN_ERROR_INVAL;
  252. }
  253. }
  254. }
  255. parser->pos = start;
  256. return JSMN_ERROR_PART;
  257. }
  258. /**
  259. * Parse JSON string and fill tokens.
  260. */
  261. JSMN_API int jsmn_parse(jsmn_parser* parser,
  262. const char* js,
  263. const size_t len,
  264. jsmntok_t* tokens,
  265. const unsigned int num_tokens) {
  266. int r;
  267. int i;
  268. jsmntok_t* token;
  269. int count = parser->toknext;
  270. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
  271. char c;
  272. jsmntype_t type;
  273. c = js[parser->pos];
  274. switch (c) {
  275. case '{':
  276. case '[':
  277. count++;
  278. if (tokens == NULL) {
  279. break;
  280. }
  281. token = jsmn_alloc_token(parser, tokens, num_tokens);
  282. if (token == NULL) {
  283. return JSMN_ERROR_NOMEM;
  284. }
  285. if (parser->toksuper != -1) {
  286. jsmntok_t* t = &tokens[parser->toksuper];
  287. #ifdef JSMN_STRICT
  288. /* In strict mode an object or array can't become a key
  289. */
  290. if (t->type == JSMN_OBJECT) {
  291. return JSMN_ERROR_INVAL;
  292. }
  293. #endif
  294. t->size++;
  295. #ifdef JSMN_PARENT_LINKS
  296. token->parent = parser->toksuper;
  297. #endif
  298. }
  299. token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
  300. token->start = parser->pos;
  301. parser->toksuper = parser->toknext - 1;
  302. break;
  303. case '}':
  304. case ']':
  305. if (tokens == NULL) {
  306. break;
  307. }
  308. type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
  309. #ifdef JSMN_PARENT_LINKS
  310. if (parser->toknext < 1) {
  311. return JSMN_ERROR_INVAL;
  312. }
  313. token = &tokens[parser->toknext - 1];
  314. for (;;) {
  315. if (token->start != -1 && token->end == -1) {
  316. if (token->type != type) {
  317. return JSMN_ERROR_INVAL;
  318. }
  319. token->end = parser->pos + 1;
  320. parser->toksuper = token->parent;
  321. break;
  322. }
  323. if (token->parent == -1) {
  324. if (token->type != type || parser->toksuper == -1) {
  325. return JSMN_ERROR_INVAL;
  326. }
  327. break;
  328. }
  329. token = &tokens[token->parent];
  330. }
  331. #else
  332. for (i = parser->toknext - 1; i >= 0; i--) {
  333. token = &tokens[i];
  334. if (token->start != -1 && token->end == -1) {
  335. if (token->type != type) {
  336. return JSMN_ERROR_INVAL;
  337. }
  338. parser->toksuper = -1;
  339. token->end = parser->pos + 1;
  340. break;
  341. }
  342. }
  343. /* Error if unmatched closing bracket */
  344. if (i == -1) {
  345. return JSMN_ERROR_INVAL;
  346. }
  347. for (; i >= 0; i--) {
  348. token = &tokens[i];
  349. if (token->start != -1 && token->end == -1) {
  350. parser->toksuper = i;
  351. break;
  352. }
  353. }
  354. #endif
  355. break;
  356. case '\"':
  357. r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
  358. if (r < 0) {
  359. return r;
  360. }
  361. count++;
  362. if (parser->toksuper != -1 && tokens != NULL) {
  363. tokens[parser->toksuper].size++;
  364. }
  365. break;
  366. case '\t':
  367. case '\r':
  368. case '\n':
  369. case ' ':
  370. break;
  371. case ':':
  372. parser->toksuper = parser->toknext - 1;
  373. break;
  374. case ',':
  375. if (tokens != NULL && parser->toksuper != -1 &&
  376. tokens[parser->toksuper].type != JSMN_ARRAY &&
  377. tokens[parser->toksuper].type != JSMN_OBJECT) {
  378. #ifdef JSMN_PARENT_LINKS
  379. parser->toksuper = tokens[parser->toksuper].parent;
  380. #else
  381. for (i = parser->toknext - 1; i >= 0; i--) {
  382. if (tokens[i].type == JSMN_ARRAY ||
  383. tokens[i].type == JSMN_OBJECT) {
  384. if (tokens[i].start != -1 && tokens[i].end == -1) {
  385. parser->toksuper = i;
  386. break;
  387. }
  388. }
  389. }
  390. #endif
  391. }
  392. break;
  393. #ifdef JSMN_STRICT
  394. /* In strict mode primitives are: numbers and booleans */
  395. case '-':
  396. case '0':
  397. case '1':
  398. case '2':
  399. case '3':
  400. case '4':
  401. case '5':
  402. case '6':
  403. case '7':
  404. case '8':
  405. case '9':
  406. case 't':
  407. case 'f':
  408. case 'n':
  409. /* And they must not be keys of the object */
  410. if (tokens != NULL && parser->toksuper != -1) {
  411. const jsmntok_t* t = &tokens[parser->toksuper];
  412. if (t->type == JSMN_OBJECT ||
  413. (t->type == JSMN_STRING && t->size != 0)) {
  414. return JSMN_ERROR_INVAL;
  415. }
  416. }
  417. #else
  418. /* In non-strict mode every unquoted value is a primitive */
  419. default:
  420. #endif
  421. r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
  422. if (r < 0) {
  423. return r;
  424. }
  425. count++;
  426. if (parser->toksuper != -1 && tokens != NULL) {
  427. tokens[parser->toksuper].size++;
  428. }
  429. break;
  430. #ifdef JSMN_STRICT
  431. /* Unexpected char in strict mode */
  432. default:
  433. return JSMN_ERROR_INVAL;
  434. #endif
  435. }
  436. }
  437. if (tokens != NULL) {
  438. for (i = parser->toknext - 1; i >= 0; i--) {
  439. /* Unmatched opened object or array */
  440. if (tokens[i].start != -1 && tokens[i].end == -1) {
  441. return JSMN_ERROR_PART;
  442. }
  443. }
  444. }
  445. return count;
  446. }
  447. /**
  448. * Creates a new parser based over a given buffer with an array of tokens
  449. * available.
  450. */
  451. JSMN_API void jsmn_init(jsmn_parser* parser) {
  452. parser->pos = 0;
  453. parser->toknext = 0;
  454. parser->toksuper = -1;
  455. }
  456. #endif /* JSMN_HEADER */
  457. #ifdef __cplusplus
  458. }
  459. #endif
  460. #endif /* JSMN_H */