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