jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. For more information visit http://zserge.bitbucket.org/jsmn.html
jsmn.h@0:625bfa0ff591, 2014-01-22 (annotated)
- Committer:
- trisjph
- Date:
- Wed Jan 22 15:21:09 2014 +0000
- Revision:
- 0:625bfa0ff591
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
trisjph | 0:625bfa0ff591 | 1 | #ifndef __JSMN_H_ |
trisjph | 0:625bfa0ff591 | 2 | #define __JSMN_H_ |
trisjph | 0:625bfa0ff591 | 3 | |
trisjph | 0:625bfa0ff591 | 4 | /** |
trisjph | 0:625bfa0ff591 | 5 | * JSON type identifier. Basic types are: |
trisjph | 0:625bfa0ff591 | 6 | * o Object |
trisjph | 0:625bfa0ff591 | 7 | * o Array |
trisjph | 0:625bfa0ff591 | 8 | * o String |
trisjph | 0:625bfa0ff591 | 9 | * o Other primitive: number, boolean (true/false) or null |
trisjph | 0:625bfa0ff591 | 10 | */ |
trisjph | 0:625bfa0ff591 | 11 | typedef enum { |
trisjph | 0:625bfa0ff591 | 12 | JSMN_PRIMITIVE = 0, |
trisjph | 0:625bfa0ff591 | 13 | JSMN_OBJECT = 1, |
trisjph | 0:625bfa0ff591 | 14 | JSMN_ARRAY = 2, |
trisjph | 0:625bfa0ff591 | 15 | JSMN_STRING = 3 |
trisjph | 0:625bfa0ff591 | 16 | } jsmntype_t; |
trisjph | 0:625bfa0ff591 | 17 | |
trisjph | 0:625bfa0ff591 | 18 | typedef enum { |
trisjph | 0:625bfa0ff591 | 19 | /* Not enough tokens were provided */ |
trisjph | 0:625bfa0ff591 | 20 | JSMN_ERROR_NOMEM = -1, |
trisjph | 0:625bfa0ff591 | 21 | /* Invalid character inside JSON string */ |
trisjph | 0:625bfa0ff591 | 22 | JSMN_ERROR_INVAL = -2, |
trisjph | 0:625bfa0ff591 | 23 | /* The string is not a full JSON packet, more bytes expected */ |
trisjph | 0:625bfa0ff591 | 24 | JSMN_ERROR_PART = -3, |
trisjph | 0:625bfa0ff591 | 25 | /* Everything was fine */ |
trisjph | 0:625bfa0ff591 | 26 | JSMN_SUCCESS = 0 |
trisjph | 0:625bfa0ff591 | 27 | } jsmnerr_t; |
trisjph | 0:625bfa0ff591 | 28 | |
trisjph | 0:625bfa0ff591 | 29 | /** |
trisjph | 0:625bfa0ff591 | 30 | * JSON token description. |
trisjph | 0:625bfa0ff591 | 31 | * @param type type (object, array, string etc.) |
trisjph | 0:625bfa0ff591 | 32 | * @param start start position in JSON data string |
trisjph | 0:625bfa0ff591 | 33 | * @param end end position in JSON data string |
trisjph | 0:625bfa0ff591 | 34 | */ |
trisjph | 0:625bfa0ff591 | 35 | typedef struct { |
trisjph | 0:625bfa0ff591 | 36 | jsmntype_t type; |
trisjph | 0:625bfa0ff591 | 37 | int start; |
trisjph | 0:625bfa0ff591 | 38 | int end; |
trisjph | 0:625bfa0ff591 | 39 | int size; |
trisjph | 0:625bfa0ff591 | 40 | #ifdef JSMN_PARENT_LINKS |
trisjph | 0:625bfa0ff591 | 41 | int parent; |
trisjph | 0:625bfa0ff591 | 42 | #endif |
trisjph | 0:625bfa0ff591 | 43 | } jsmntok_t; |
trisjph | 0:625bfa0ff591 | 44 | |
trisjph | 0:625bfa0ff591 | 45 | /** |
trisjph | 0:625bfa0ff591 | 46 | * JSON parser. Contains an array of token blocks available. Also stores |
trisjph | 0:625bfa0ff591 | 47 | * the string being parsed now and current position in that string |
trisjph | 0:625bfa0ff591 | 48 | */ |
trisjph | 0:625bfa0ff591 | 49 | typedef struct { |
trisjph | 0:625bfa0ff591 | 50 | unsigned int pos; /* offset in the JSON string */ |
trisjph | 0:625bfa0ff591 | 51 | int toknext; /* next token to allocate */ |
trisjph | 0:625bfa0ff591 | 52 | int toksuper; /* superior token node, e.g parent object or array */ |
trisjph | 0:625bfa0ff591 | 53 | } jsmn_parser; |
trisjph | 0:625bfa0ff591 | 54 | |
trisjph | 0:625bfa0ff591 | 55 | /** |
trisjph | 0:625bfa0ff591 | 56 | * Create JSON parser over an array of tokens |
trisjph | 0:625bfa0ff591 | 57 | */ |
trisjph | 0:625bfa0ff591 | 58 | void jsmn_init(jsmn_parser *parser); |
trisjph | 0:625bfa0ff591 | 59 | |
trisjph | 0:625bfa0ff591 | 60 | /** |
trisjph | 0:625bfa0ff591 | 61 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing |
trisjph | 0:625bfa0ff591 | 62 | * a single JSON object. |
trisjph | 0:625bfa0ff591 | 63 | */ |
trisjph | 0:625bfa0ff591 | 64 | jsmnerr_t jsmn_parse(jsmn_parser *parser, char *js, |
trisjph | 0:625bfa0ff591 | 65 | jsmntok_t *tokens, unsigned int num_tokens); |
trisjph | 0:625bfa0ff591 | 66 | |
trisjph | 0:625bfa0ff591 | 67 | #endif /* __JSMN_H_ */ |