• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1JSMN
2====
3
4[![Build Status](https://travis-ci.org/zserge/jsmn.svg?branch=master)](https://travis-ci.org/zserge/jsmn)
5
6jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C.  It can be
7easily integrated into resource-limited or embedded projects.
8
9You can find more information about JSON format at [json.org][1]
10
11Library sources are available at https://github.com/zserge/jsmn
12
13The web page with some information about jsmn can be found at
14[http://zserge.com/jsmn.html][2]
15
16Philosophy
17----------
18
19Most JSON parsers offer you a bunch of functions to load JSON data, parse it
20and extract any value by its name. jsmn proves that checking the correctness of
21every JSON packet or allocating temporary objects to store parsed JSON fields
22often is an overkill.
23
24JSON format itself is extremely simple, so why should we complicate it?
25
26jsmn is designed to be	**robust** (it should work fine even with erroneous
27data), **fast** (it should parse data on the fly), **portable** (no superfluous
28dependencies or non-standard C extensions). And of course, **simplicity** is a
29key feature - simple code style, simple algorithm, simple integration into
30other projects.
31
32Features
33--------
34
35* compatible with C89
36* no dependencies (even libc!)
37* highly portable (tested on x86/amd64, ARM, AVR)
38* about 200 lines of code
39* extremely small code footprint
40* API contains only 2 functions
41* no dynamic memory allocation
42* incremental single-pass parsing
43* library code is covered with unit-tests
44
45Design
46------
47
48The rudimentary jsmn object is a **token**. Let's consider a JSON string:
49
50	'{ "name" : "Jack", "age" : 27 }'
51
52It holds the following tokens:
53
54* Object: `{ "name" : "Jack", "age" : 27}` (the whole object)
55* Strings: `"name"`, `"Jack"`, `"age"` (keys and some values)
56* Number: `27`
57
58In jsmn, tokens do not hold any data, but point to token boundaries in JSON
59string instead. In the example above jsmn will create tokens like: Object
60[0..31], String [3..7], String [12..16], String [20..23], Number [27..29].
61
62Every jsmn token has a type, which indicates the type of corresponding JSON
63token. jsmn supports the following token types:
64
65* Object - a container of key-value pairs, e.g.:
66	`{ "foo":"bar", "x":0.3 }`
67* Array - a sequence of values, e.g.:
68	`[ 1, 2, 3 ]`
69* String - a quoted sequence of chars, e.g.: `"foo"`
70* Primitive - a number, a boolean (`true`, `false`) or `null`
71
72Besides start/end positions, jsmn tokens for complex types (like arrays
73or objects) also contain a number of child items, so you can easily follow
74object hierarchy.
75
76This approach provides enough information for parsing any JSON data and makes
77it possible to use zero-copy techniques.
78
79Install
80-------
81
82To clone the repository you should have Git installed. Just run:
83
84	$ git clone https://github.com/zserge/jsmn
85
86Repository layout is simple: jsmn.c and jsmn.h are library files, tests are in
87the jsmn\_test.c, you will also find README, LICENSE and Makefile files inside.
88
89To build the library, run `make`. It is also recommended to run `make test`.
90Let me know, if some tests fail.
91
92If build was successful, you should get a `libjsmn.a` library.
93The header file you should include is called `"jsmn.h"`.
94
95API
96---
97
98Token types are described by `jsmntype_t`:
99
100	typedef enum {
101		JSMN_UNDEFINED = 0,
102		JSMN_OBJECT = 1,
103		JSMN_ARRAY = 2,
104		JSMN_STRING = 3,
105		JSMN_PRIMITIVE = 4
106	} jsmntype_t;
107
108**Note:** Unlike JSON data types, primitive tokens are not divided into
109numbers, booleans and null, because one can easily tell the type using the
110first character:
111
112* <code>'t', 'f'</code> - boolean
113* <code>'n'</code> - null
114* <code>'-', '0'..'9'</code> - number
115
116Token is an object of `jsmntok_t` type:
117
118	typedef struct {
119		jsmntype_t type; // Token type
120		int start;       // Token start position
121		int end;         // Token end position
122		int size;        // Number of child (nested) tokens
123	} jsmntok_t;
124
125**Note:** string tokens point to the first character after
126the opening quote and the previous symbol before final quote. This was made
127to simplify string extraction from JSON data.
128
129All job is done by `jsmn_parser` object. You can initialize a new parser using:
130
131	jsmn_parser parser;
132	jsmntok_t tokens[10];
133
134	jsmn_init(&parser);
135
136	// js - pointer to JSON string
137	// tokens - an array of tokens available
138	// 10 - number of tokens available
139	jsmn_parse(&parser, js, strlen(js), tokens, 10);
140
141This will create a parser, and then it tries to parse up to 10 JSON tokens from
142the `js` string.
143
144A non-negative return value of `jsmn_parse` is the number of tokens actually
145used by the parser.
146Passing NULL instead of the tokens array would not store parsing results, but
147instead the function will return the value of tokens needed to parse the given
148string. This can be useful if you don't know yet how many tokens to allocate.
149
150If something goes wrong, you will get an error. Error will be one of these:
151
152* `JSMN_ERROR_INVAL` - bad token, JSON string is corrupted
153* `JSMN_ERROR_NOMEM` - not enough tokens, JSON string is too large
154* `JSMN_ERROR_PART` - JSON string is too short, expecting more JSON data
155
156If you get `JSMN_ERROR_NOMEM`, you can re-allocate more tokens and call
157`jsmn_parse` once more.  If you read json data from the stream, you can
158periodically call `jsmn_parse` and check if return value is `JSMN_ERROR_PART`.
159You will get this error until you reach the end of JSON data.
160
161Other info
162----------
163
164This software is distributed under [MIT license](http://www.opensource.org/licenses/mit-license.php),
165 so feel free to integrate it in your commercial products.
166
167[1]: http://www.json.org/
168[2]: http://zserge.com/jsmn.html
169