Dumb-ways-to-memorize
2D game
Data Structures | Enumerations | Functions
jsmn.h File Reference
#include <stddef.h>
+ Include dependency graph for jsmn.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  jsmntok_t
 
struct  jsmn_parser
 

Enumerations

enum  jsmntype_t {
  JSMN_UNDEFINED = 0,
  JSMN_OBJECT = 1,
  JSMN_ARRAY = 2,
  JSMN_STRING = 3,
  JSMN_PRIMITIVE = 4
}
 
enum  jsmnerr {
  JSMN_ERROR_NOMEM = -1,
  JSMN_ERROR_INVAL = -2,
  JSMN_ERROR_PART = -3
}
 

Functions

void jsmn_init (jsmn_parser *parser)
 
int jsmn_parse (jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens)
 

Enumeration Type Documentation

enum jsmnerr
Enumerator
JSMN_ERROR_NOMEM 
JSMN_ERROR_INVAL 
JSMN_ERROR_PART 

Definition at line 25 of file jsmn.h.

25  {
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 };
enum jsmntype_t

JSON type identifier. Basic types are: o Object o Array o String o Other primitive: number, boolean (true/false) or null

Enumerator
JSMN_UNDEFINED 
JSMN_OBJECT 
JSMN_ARRAY 
JSMN_STRING 
JSMN_PRIMITIVE 

Definition at line 17 of file jsmn.h.

17  {
18  JSMN_UNDEFINED = 0,
19  JSMN_OBJECT = 1,
20  JSMN_ARRAY = 2,
21  JSMN_STRING = 3,
22  JSMN_PRIMITIVE = 4
23 } jsmntype_t;
jsmntype_t
Definition: jsmn.h:17

Function Documentation

void jsmn_init ( jsmn_parser parser)

Create JSON parser over an array of tokens

Creates a new parser based over a given buffer with an array of tokens available.

Definition at line 306 of file jsmn.c.

References jsmn_parser::pos, jsmn_parser::toknext, and jsmn_parser::toksuper.

Referenced by ConvertFileToUseable().

306  {
307  parser->pos = 0;
308  parser->toknext = 0;
309  parser->toksuper = -1;
310 }
unsigned int toknext
Definition: jsmn.h:56
int toksuper
Definition: jsmn.h:57
unsigned int pos
Definition: jsmn.h:55
int jsmn_parse ( jsmn_parser parser,
const char *  js,
size_t  len,
jsmntok_t tokens,
unsigned int  num_tokens 
)

Run JSON parser. It parses a JSON data string into and array of tokens, each describing a single JSON object.

Parse JSON string and fill tokens.

Definition at line 151 of file jsmn.c.

References jsmntok_t::end, jsmn_alloc_token(), JSMN_ARRAY, JSMN_ERROR_INVAL, JSMN_ERROR_NOMEM, JSMN_ERROR_PART, JSMN_OBJECT, jsmn_parse_primitive(), jsmn_parse_string(), JSMN_STRING, jsmn_parser::pos, jsmntok_t::size, jsmntok_t::start, jsmn_parser::toknext, jsmn_parser::toksuper, and jsmntok_t::type.

Referenced by ConvertFileToUseable().

152  {
153  int r;
154  int i;
155  jsmntok_t *token;
156  int count = parser->toknext;
157 
158  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
159  char c;
160  jsmntype_t type;
161 
162  c = js[parser->pos];
163  switch (c) {
164  case '{': case '[':
165  count++;
166  if (tokens == NULL) {
167  break;
168  }
169  token = jsmn_alloc_token(parser, tokens, num_tokens);
170  if (token == NULL)
171  return JSMN_ERROR_NOMEM;
172  if (parser->toksuper != -1) {
173  tokens[parser->toksuper].size++;
174 #ifdef JSMN_PARENT_LINKS
175  token->parent = parser->toksuper;
176 #endif
177  }
178  token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
179  token->start = parser->pos;
180  parser->toksuper = parser->toknext - 1;
181  break;
182  case '}': case ']':
183  if (tokens == NULL)
184  break;
185  type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
186 #ifdef JSMN_PARENT_LINKS
187  if (parser->toknext < 1) {
188  return JSMN_ERROR_INVAL;
189  }
190  token = &tokens[parser->toknext - 1];
191  for (;;) {
192  if (token->start != -1 && token->end == -1) {
193  if (token->type != type) {
194  return JSMN_ERROR_INVAL;
195  }
196  token->end = parser->pos + 1;
197  parser->toksuper = token->parent;
198  break;
199  }
200  if (token->parent == -1) {
201  break;
202  }
203  token = &tokens[token->parent];
204  }
205 #else
206  for (i = parser->toknext - 1; i >= 0; i--) {
207  token = &tokens[i];
208  if (token->start != -1 && token->end == -1) {
209  if (token->type != type) {
210  return JSMN_ERROR_INVAL;
211  }
212  parser->toksuper = -1;
213  token->end = parser->pos + 1;
214  break;
215  }
216  }
217  /* Error if unmatched closing bracket */
218  if (i == -1) return JSMN_ERROR_INVAL;
219  for (; i >= 0; i--) {
220  token = &tokens[i];
221  if (token->start != -1 && token->end == -1) {
222  parser->toksuper = i;
223  break;
224  }
225  }
226 #endif
227  break;
228  case '\"':
229  r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
230  if (r < 0) return r;
231  count++;
232  if (parser->toksuper != -1 && tokens != NULL)
233  tokens[parser->toksuper].size++;
234  break;
235  case '\t' : case '\r' : case '\n' : case ' ':
236  break;
237  case ':':
238  parser->toksuper = parser->toknext - 1;
239  break;
240  case ',':
241  if (tokens != NULL && parser->toksuper != -1 &&
242  tokens[parser->toksuper].type != JSMN_ARRAY &&
243  tokens[parser->toksuper].type != JSMN_OBJECT) {
244 #ifdef JSMN_PARENT_LINKS
245  parser->toksuper = tokens[parser->toksuper].parent;
246 #else
247  for (i = parser->toknext - 1; i >= 0; i--) {
248  if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
249  if (tokens[i].start != -1 && tokens[i].end == -1) {
250  parser->toksuper = i;
251  break;
252  }
253  }
254  }
255 #endif
256  }
257  break;
258 #ifdef JSMN_STRICT
259  /* In strict mode primitives are: numbers and booleans */
260  case '-': case '0': case '1' : case '2': case '3' : case '4':
261  case '5': case '6': case '7' : case '8': case '9':
262  case 't': case 'f': case 'n' :
263  /* And they must not be keys of the object */
264  if (tokens != NULL && parser->toksuper != -1) {
265  jsmntok_t *t = &tokens[parser->toksuper];
266  if (t->type == JSMN_OBJECT ||
267  (t->type == JSMN_STRING && t->size != 0)) {
268  return JSMN_ERROR_INVAL;
269  }
270  }
271 #else
272  /* In non-strict mode every unquoted value is a primitive */
273  default:
274 #endif
275  r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
276  if (r < 0) return r;
277  count++;
278  if (parser->toksuper != -1 && tokens != NULL)
279  tokens[parser->toksuper].size++;
280  break;
281 
282 #ifdef JSMN_STRICT
283  /* Unexpected char in strict mode */
284  default:
285  return JSMN_ERROR_INVAL;
286 #endif
287  }
288  }
289 
290  if (tokens != NULL) {
291  for (i = parser->toknext - 1; i >= 0; i--) {
292  /* Unmatched opened object or array */
293  if (tokens[i].start != -1 && tokens[i].end == -1) {
294  return JSMN_ERROR_PART;
295  }
296  }
297  }
298 
299  return count;
300 }
Definition: jsmn.h:40
jsmntype_t
Definition: jsmn.h:17
static jsmntok_t * jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens, size_t num_tokens)
Definition: jsmn.c:6
unsigned int toknext
Definition: jsmn.h:56
jsmntype_t type
Definition: jsmn.h:41
static int jsmn_parse_string(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens)
Definition: jsmn.c:84
int toksuper
Definition: jsmn.h:57
int start
Definition: jsmn.h:42
static int jsmn_parse_primitive(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens)
Definition: jsmn.c:35
int end
Definition: jsmn.h:43
int size
Definition: jsmn.h:44
unsigned int pos
Definition: jsmn.h:55