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 /**
24  * @file jsmn.h
25  * @brief Definition of the JSMN (Jasmine) JSON parser.
26  *
27  * For more information on JSMN:
28  * @see http://zserge.com/jsmn.html
29  */
30 
31 #ifndef __JSMN_H_
32 #define __JSMN_H_
33 
34 #include <stddef.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * JSON type identifier. Basic types are:
42  * 	o Object
43  * 	o Array
44  * 	o String
45  * 	o Other primitive: number, boolean (true/false) or null
46  */
47 typedef enum {
48 	JSMN_UNDEFINED = 0,
49 	JSMN_OBJECT = 1,
50 	JSMN_ARRAY = 2,
51 	JSMN_STRING = 3,
52 	JSMN_PRIMITIVE = 4
53 } jsmntype_t;
54 
55 enum jsmnerr {
56 	/* Not enough tokens were provided */
57 	JSMN_ERROR_NOMEM = -1,
58 	/* Invalid character inside JSON string */
59 	JSMN_ERROR_INVAL = -2,
60 	/* The string is not a full JSON packet, more bytes expected */
61 	JSMN_ERROR_PART = -3
62 };
63 
64 /**
65  * JSON token description.
66  * @param		type	type (object, array, string etc.)
67  * @param		start	start position in JSON data string
68  * @param		end		end position in JSON data string
69  */
70 typedef struct {
71 	jsmntype_t type;
72 	int start;
73 	int end;
74 	int size;
75 #ifdef JSMN_PARENT_LINKS
76 	int parent;
77 #endif
78 } jsmntok_t;
79 
80 /**
81  * JSON parser. Contains an array of token blocks available. Also stores
82  * the string being parsed now and current position in that string
83  */
84 typedef struct {
85 	unsigned int pos; /* offset in the JSON string */
86 	unsigned int toknext; /* next token to allocate */
87 	int toksuper; /* superior token node, e.g parent object or array */
88 } jsmn_parser;
89 
90 /**
91  * Create JSON parser over an array of tokens
92  */
93 void jsmn_init(jsmn_parser *parser);
94 
95 /**
96  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
97  * a single JSON object.
98  */
99 int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
100 		jsmntok_t *tokens, unsigned int num_tokens);
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif /* __JSMN_H_ */
107