blob: 5a5200ee2fb8a7ce6dac7e4864b34eaadb9a917b [file] [log] [blame]
Edwin Wong27b5a352014-05-28 15:36:44 -07001#ifndef __JSMN_H_
2#define __JSMN_H_
3
Haibo Huang8e498432018-09-06 14:52:29 -07004#include <stddef.h>
5
Edwin Wong27b5a352014-05-28 15:36:44 -07006#ifdef __cplusplus
7extern "C" {
8#endif
9
10/**
11 * JSON type identifier. Basic types are:
Haibo Huang8e498432018-09-06 14:52:29 -070012 * o Object
13 * o Array
14 * o String
15 * o Other primitive: number, boolean (true/false) or null
Edwin Wong27b5a352014-05-28 15:36:44 -070016 */
17typedef enum {
Haibo Huang8e498432018-09-06 14:52:29 -070018 JSMN_UNDEFINED = 0,
19 JSMN_OBJECT = 1,
20 JSMN_ARRAY = 2,
21 JSMN_STRING = 3,
22 JSMN_PRIMITIVE = 4
Edwin Wong27b5a352014-05-28 15:36:44 -070023} jsmntype_t;
24
Haibo Huang8e498432018-09-06 14:52:29 -070025enum jsmnerr {
26 /* Not enough tokens were provided */
27 JSMN_ERROR_NOMEM = -1,
28 /* Invalid character inside JSON string */
29 JSMN_ERROR_INVAL = -2,
30 /* The string is not a full JSON packet, more bytes expected */
31 JSMN_ERROR_PART = -3
32};
Edwin Wong27b5a352014-05-28 15:36:44 -070033
34/**
35 * JSON token description.
Haibo Huang8e498432018-09-06 14:52:29 -070036 * type type (object, array, string etc.)
37 * start start position in JSON data string
38 * end end position in JSON data string
Edwin Wong27b5a352014-05-28 15:36:44 -070039 */
40typedef struct {
Haibo Huang8e498432018-09-06 14:52:29 -070041 jsmntype_t type;
42 int start;
43 int end;
44 int size;
Edwin Wong27b5a352014-05-28 15:36:44 -070045#ifdef JSMN_PARENT_LINKS
Haibo Huang8e498432018-09-06 14:52:29 -070046 int parent;
Edwin Wong27b5a352014-05-28 15:36:44 -070047#endif
48} jsmntok_t;
49
50/**
51 * JSON parser. Contains an array of token blocks available. Also stores
52 * the string being parsed now and current position in that string
53 */
54typedef struct {
Haibo Huang8e498432018-09-06 14:52:29 -070055 unsigned int pos; /* offset in the JSON string */
56 unsigned int toknext; /* next token to allocate */
57 int toksuper; /* superior token node, e.g parent object or array */
Edwin Wong27b5a352014-05-28 15:36:44 -070058} jsmn_parser;
59
60/**
61 * Create JSON parser over an array of tokens
62 */
63void jsmn_init(jsmn_parser *parser);
64
65/**
66 * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
67 * a single JSON object.
68 */
Haibo Huang8e498432018-09-06 14:52:29 -070069int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
70 jsmntok_t *tokens, unsigned int num_tokens);
Edwin Wong27b5a352014-05-28 15:36:44 -070071
72#ifdef __cplusplus
73}
74#endif
75
76#endif /* __JSMN_H_ */