xref: /freebsd/contrib/ntp/libjsmn/jsmn.h (revision 276da39af92f48350aa01091a2b8b3e735217eea)
12b15cb3dSCy Schubert #ifndef __JSMN_H_
22b15cb3dSCy Schubert #define __JSMN_H_
32b15cb3dSCy Schubert 
4*276da39aSCy Schubert #include <stddef.h>
5*276da39aSCy Schubert 
6*276da39aSCy Schubert #ifdef __cplusplus
7*276da39aSCy Schubert extern "C" {
8*276da39aSCy Schubert #endif
9*276da39aSCy Schubert 
102b15cb3dSCy Schubert /**
112b15cb3dSCy Schubert  * JSON type identifier. Basic types are:
122b15cb3dSCy Schubert  * 	o Object
132b15cb3dSCy Schubert  * 	o Array
142b15cb3dSCy Schubert  * 	o String
152b15cb3dSCy Schubert  * 	o Other primitive: number, boolean (true/false) or null
162b15cb3dSCy Schubert  */
172b15cb3dSCy Schubert typedef enum {
182b15cb3dSCy Schubert 	JSMN_PRIMITIVE = 0,
192b15cb3dSCy Schubert 	JSMN_OBJECT = 1,
202b15cb3dSCy Schubert 	JSMN_ARRAY = 2,
212b15cb3dSCy Schubert 	JSMN_STRING = 3
222b15cb3dSCy Schubert } jsmntype_t;
232b15cb3dSCy Schubert 
242b15cb3dSCy Schubert typedef enum {
252b15cb3dSCy Schubert 	/* Not enough tokens were provided */
262b15cb3dSCy Schubert 	JSMN_ERROR_NOMEM = -1,
272b15cb3dSCy Schubert 	/* Invalid character inside JSON string */
282b15cb3dSCy Schubert 	JSMN_ERROR_INVAL = -2,
292b15cb3dSCy Schubert 	/* The string is not a full JSON packet, more bytes expected */
30*276da39aSCy Schubert 	JSMN_ERROR_PART = -3
312b15cb3dSCy Schubert } jsmnerr_t;
322b15cb3dSCy Schubert 
332b15cb3dSCy Schubert /**
342b15cb3dSCy Schubert  * JSON token description.
352b15cb3dSCy Schubert  * @param		type	type (object, array, string etc.)
362b15cb3dSCy Schubert  * @param		start	start position in JSON data string
372b15cb3dSCy Schubert  * @param		end		end position in JSON data string
382b15cb3dSCy Schubert  */
392b15cb3dSCy Schubert typedef struct {
402b15cb3dSCy Schubert 	jsmntype_t type;
412b15cb3dSCy Schubert 	int start;
422b15cb3dSCy Schubert 	int end;
432b15cb3dSCy Schubert 	int size;
442b15cb3dSCy Schubert #ifdef JSMN_PARENT_LINKS
452b15cb3dSCy Schubert 	int parent;
462b15cb3dSCy Schubert #endif
472b15cb3dSCy Schubert } jsmntok_t;
482b15cb3dSCy Schubert 
492b15cb3dSCy Schubert /**
502b15cb3dSCy Schubert  * JSON parser. Contains an array of token blocks available. Also stores
512b15cb3dSCy Schubert  * the string being parsed now and current position in that string
522b15cb3dSCy Schubert  */
532b15cb3dSCy Schubert typedef struct {
542b15cb3dSCy Schubert 	unsigned int pos; /* offset in the JSON string */
55*276da39aSCy Schubert 	unsigned int toknext; /* next token to allocate */
562b15cb3dSCy Schubert 	int toksuper; /* superior token node, e.g parent object or array */
572b15cb3dSCy Schubert } jsmn_parser;
582b15cb3dSCy Schubert 
592b15cb3dSCy Schubert /**
602b15cb3dSCy Schubert  * Create JSON parser over an array of tokens
612b15cb3dSCy Schubert  */
622b15cb3dSCy Schubert void jsmn_init(jsmn_parser *parser);
632b15cb3dSCy Schubert 
642b15cb3dSCy Schubert /**
652b15cb3dSCy Schubert  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
662b15cb3dSCy Schubert  * a single JSON object.
672b15cb3dSCy Schubert  */
68*276da39aSCy Schubert jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
692b15cb3dSCy Schubert 		jsmntok_t *tokens, unsigned int num_tokens);
702b15cb3dSCy Schubert 
71*276da39aSCy Schubert #ifdef __cplusplus
72*276da39aSCy Schubert }
73*276da39aSCy Schubert #endif
74*276da39aSCy Schubert 
752b15cb3dSCy Schubert #endif /* __JSMN_H_ */
76