1/*-
2 * Copyright (c) 2010 Serge A. Zaitsev
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * THE SOFTWARE.
21 */
22
23#ifndef _BMK_CORE_JSMN_H_
24#define _BMK_CORE_JSMN_H_
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 * JSON type identifier. Basic types are:
32 * 	o Object
33 * 	o Array
34 * 	o String
35 * 	o Other primitive: number, boolean (true/false) or null
36 */
37typedef enum {
38	JSMN_PRIMITIVE = 0,
39	JSMN_OBJECT = 1,
40	JSMN_ARRAY = 2,
41	JSMN_STRING = 3
42} jsmntype_t;
43
44typedef enum {
45	/* Not enough tokens were provided */
46	JSMN_ERROR_NOMEM = -1,
47	/* Invalid character inside JSON string */
48	JSMN_ERROR_INVAL = -2,
49	/* The string is not a full JSON packet, more bytes expected */
50	JSMN_ERROR_PART = -3
51} jsmnerr_t;
52
53/**
54 * JSON token description.
55 * @param		type	type (object, array, string etc.)
56 * @param		start	start position in JSON data string
57 * @param		end		end position in JSON data string
58 */
59typedef struct {
60	jsmntype_t type;
61	int start;
62	int end;
63	int size;
64#ifdef JSMN_PARENT_LINKS
65	int parent;
66#endif
67} jsmntok_t;
68
69/**
70 * JSON parser. Contains an array of token blocks available. Also stores
71 * the string being parsed now and current position in that string
72 */
73typedef struct {
74	unsigned int pos; /* offset in the JSON string */
75	unsigned int toknext; /* next token to allocate */
76	int toksuper; /* superior token node, e.g parent object or array */
77} jsmn_parser;
78
79/**
80 * Create JSON parser over an array of tokens
81 */
82void jsmn_init(jsmn_parser *parser);
83
84/**
85 * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
86 * a single JSON object.
87 */
88jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned long len,
89		jsmntok_t *tokens, unsigned int num_tokens);
90
91#ifdef __cplusplus
92}
93#endif
94
95#endif /* _BMK_CORE_JSMN_H_ */
96