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