Edwin Wong | 27b5a35 | 2014-05-28 15:36:44 -0700 | [diff] [blame] | 1 | #ifndef __JSMN_H_ |
| 2 | #define __JSMN_H_ |
| 3 | |
| 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
| 8 | /** |
| 9 | * JSON type identifier. Basic types are: |
| 10 | * o Object |
| 11 | * o Array |
| 12 | * o String |
| 13 | * o Other primitive: number, boolean (true/false) or null |
| 14 | */ |
| 15 | typedef enum { |
| 16 | JSMN_PRIMITIVE = 0, |
| 17 | JSMN_OBJECT = 1, |
| 18 | JSMN_ARRAY = 2, |
| 19 | JSMN_STRING = 3 |
| 20 | } jsmntype_t; |
| 21 | |
| 22 | typedef enum { |
| 23 | /* Not enough tokens were provided */ |
| 24 | JSMN_ERROR_NOMEM = -1, |
| 25 | /* Invalid character inside JSON string */ |
| 26 | JSMN_ERROR_INVAL = -2, |
| 27 | /* The string is not a full JSON packet, more bytes expected */ |
| 28 | JSMN_ERROR_PART = -3, |
| 29 | } jsmnerr_t; |
| 30 | |
| 31 | /** |
| 32 | * JSON token description. |
| 33 | * @param type type (object, array, string etc.) |
| 34 | * @param start start position in JSON data string |
| 35 | * @param end end position in JSON data string |
| 36 | */ |
| 37 | typedef struct { |
| 38 | jsmntype_t type; |
| 39 | int start; |
| 40 | int end; |
| 41 | int size; |
| 42 | #ifdef JSMN_PARENT_LINKS |
| 43 | int parent; |
| 44 | #endif |
| 45 | } jsmntok_t; |
| 46 | |
| 47 | /** |
| 48 | * JSON parser. Contains an array of token blocks available. Also stores |
| 49 | * the string being parsed now and current position in that string |
| 50 | */ |
| 51 | typedef struct { |
| 52 | unsigned int pos; /* offset in the JSON string */ |
| 53 | unsigned int toknext; /* next token to allocate */ |
| 54 | int toksuper; /* superior token node, e.g parent object or array */ |
| 55 | } jsmn_parser; |
| 56 | |
| 57 | /** |
| 58 | * Create JSON parser over an array of tokens |
| 59 | */ |
| 60 | void jsmn_init(jsmn_parser *parser); |
| 61 | |
| 62 | /** |
| 63 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing |
| 64 | * a single JSON object. |
| 65 | */ |
| 66 | jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, |
| 67 | jsmntok_t *tokens, unsigned int num_tokens); |
| 68 | |
| 69 | #ifdef __cplusplus |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | #endif /* __JSMN_H_ */ |