1275970Scy#ifndef __JSMN_H_
2275970Scy#define __JSMN_H_
3275970Scy
4285169Scy#include <stddef.h>
5285169Scy
6285169Scy#ifdef __cplusplus
7285169Scyextern "C" {
8285169Scy#endif
9285169Scy
10275970Scy/**
11275970Scy * JSON type identifier. Basic types are:
12275970Scy * 	o Object
13275970Scy * 	o Array
14275970Scy * 	o String
15275970Scy * 	o Other primitive: number, boolean (true/false) or null
16275970Scy */
17275970Scytypedef enum {
18275970Scy	JSMN_PRIMITIVE = 0,
19275970Scy	JSMN_OBJECT = 1,
20275970Scy	JSMN_ARRAY = 2,
21275970Scy	JSMN_STRING = 3
22275970Scy} jsmntype_t;
23275970Scy
24275970Scytypedef enum {
25275970Scy	/* Not enough tokens were provided */
26275970Scy	JSMN_ERROR_NOMEM = -1,
27275970Scy	/* Invalid character inside JSON string */
28275970Scy	JSMN_ERROR_INVAL = -2,
29275970Scy	/* The string is not a full JSON packet, more bytes expected */
30285169Scy	JSMN_ERROR_PART = -3
31275970Scy} jsmnerr_t;
32275970Scy
33275970Scy/**
34275970Scy * JSON token description.
35275970Scy * @param		type	type (object, array, string etc.)
36275970Scy * @param		start	start position in JSON data string
37275970Scy * @param		end		end position in JSON data string
38275970Scy */
39275970Scytypedef struct {
40275970Scy	jsmntype_t type;
41275970Scy	int start;
42275970Scy	int end;
43275970Scy	int size;
44275970Scy#ifdef JSMN_PARENT_LINKS
45275970Scy	int parent;
46275970Scy#endif
47275970Scy} jsmntok_t;
48275970Scy
49275970Scy/**
50275970Scy * JSON parser. Contains an array of token blocks available. Also stores
51275970Scy * the string being parsed now and current position in that string
52275970Scy */
53275970Scytypedef struct {
54275970Scy	unsigned int pos; /* offset in the JSON string */
55285169Scy	unsigned int toknext; /* next token to allocate */
56275970Scy	int toksuper; /* superior token node, e.g parent object or array */
57275970Scy} jsmn_parser;
58275970Scy
59275970Scy/**
60275970Scy * Create JSON parser over an array of tokens
61275970Scy */
62275970Scyvoid jsmn_init(jsmn_parser *parser);
63275970Scy
64275970Scy/**
65275970Scy * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
66275970Scy * a single JSON object.
67275970Scy */
68285169Scyjsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
69275970Scy		jsmntok_t *tokens, unsigned int num_tokens);
70275970Scy
71285169Scy#ifdef __cplusplus
72285169Scy}
73285169Scy#endif
74285169Scy
75275970Scy#endif /* __JSMN_H_ */
76