Vergil Cola
/
MQTTGateway2
Fork of my original MQTTGateway
jsmn/jsmn.h@0:a1734fe1ec4b, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:43:14 2017 +0000
- Revision:
- 0:a1734fe1ec4b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:a1734fe1ec4b | 1 | #ifndef __JSMN_H_ |
vpcola | 0:a1734fe1ec4b | 2 | #define __JSMN_H_ |
vpcola | 0:a1734fe1ec4b | 3 | |
vpcola | 0:a1734fe1ec4b | 4 | #include <stddef.h> |
vpcola | 0:a1734fe1ec4b | 5 | |
vpcola | 0:a1734fe1ec4b | 6 | #ifdef __cplusplus |
vpcola | 0:a1734fe1ec4b | 7 | extern "C" { |
vpcola | 0:a1734fe1ec4b | 8 | #endif |
vpcola | 0:a1734fe1ec4b | 9 | |
vpcola | 0:a1734fe1ec4b | 10 | /** |
vpcola | 0:a1734fe1ec4b | 11 | * JSON type identifier. Basic types are: |
vpcola | 0:a1734fe1ec4b | 12 | * o Object |
vpcola | 0:a1734fe1ec4b | 13 | * o Array |
vpcola | 0:a1734fe1ec4b | 14 | * o String |
vpcola | 0:a1734fe1ec4b | 15 | * o Other primitive: number, boolean (true/false) or null |
vpcola | 0:a1734fe1ec4b | 16 | */ |
vpcola | 0:a1734fe1ec4b | 17 | typedef enum { |
vpcola | 0:a1734fe1ec4b | 18 | JSMN_UNDEFINED = 0, |
vpcola | 0:a1734fe1ec4b | 19 | JSMN_OBJECT = 1, |
vpcola | 0:a1734fe1ec4b | 20 | JSMN_ARRAY = 2, |
vpcola | 0:a1734fe1ec4b | 21 | JSMN_STRING = 3, |
vpcola | 0:a1734fe1ec4b | 22 | JSMN_PRIMITIVE = 4 |
vpcola | 0:a1734fe1ec4b | 23 | } jsmntype_t; |
vpcola | 0:a1734fe1ec4b | 24 | |
vpcola | 0:a1734fe1ec4b | 25 | enum jsmnerr { |
vpcola | 0:a1734fe1ec4b | 26 | /* Not enough tokens were provided */ |
vpcola | 0:a1734fe1ec4b | 27 | JSMN_ERROR_NOMEM = -1, |
vpcola | 0:a1734fe1ec4b | 28 | /* Invalid character inside JSON string */ |
vpcola | 0:a1734fe1ec4b | 29 | JSMN_ERROR_INVAL = -2, |
vpcola | 0:a1734fe1ec4b | 30 | /* The string is not a full JSON packet, more bytes expected */ |
vpcola | 0:a1734fe1ec4b | 31 | JSMN_ERROR_PART = -3 |
vpcola | 0:a1734fe1ec4b | 32 | }; |
vpcola | 0:a1734fe1ec4b | 33 | |
vpcola | 0:a1734fe1ec4b | 34 | /** |
vpcola | 0:a1734fe1ec4b | 35 | * JSON token description. |
vpcola | 0:a1734fe1ec4b | 36 | * type type (object, array, string etc.) |
vpcola | 0:a1734fe1ec4b | 37 | * start start position in JSON data string |
vpcola | 0:a1734fe1ec4b | 38 | * end end position in JSON data string |
vpcola | 0:a1734fe1ec4b | 39 | */ |
vpcola | 0:a1734fe1ec4b | 40 | typedef struct { |
vpcola | 0:a1734fe1ec4b | 41 | jsmntype_t type; |
vpcola | 0:a1734fe1ec4b | 42 | int start; |
vpcola | 0:a1734fe1ec4b | 43 | int end; |
vpcola | 0:a1734fe1ec4b | 44 | int size; |
vpcola | 0:a1734fe1ec4b | 45 | #ifdef JSMN_PARENT_LINKS |
vpcola | 0:a1734fe1ec4b | 46 | int parent; |
vpcola | 0:a1734fe1ec4b | 47 | #endif |
vpcola | 0:a1734fe1ec4b | 48 | } jsmntok_t; |
vpcola | 0:a1734fe1ec4b | 49 | |
vpcola | 0:a1734fe1ec4b | 50 | /** |
vpcola | 0:a1734fe1ec4b | 51 | * JSON parser. Contains an array of token blocks available. Also stores |
vpcola | 0:a1734fe1ec4b | 52 | * the string being parsed now and current position in that string |
vpcola | 0:a1734fe1ec4b | 53 | */ |
vpcola | 0:a1734fe1ec4b | 54 | typedef struct { |
vpcola | 0:a1734fe1ec4b | 55 | unsigned int pos; /* offset in the JSON string */ |
vpcola | 0:a1734fe1ec4b | 56 | unsigned int toknext; /* next token to allocate */ |
vpcola | 0:a1734fe1ec4b | 57 | int toksuper; /* superior token node, e.g parent object or array */ |
vpcola | 0:a1734fe1ec4b | 58 | } jsmn_parser; |
vpcola | 0:a1734fe1ec4b | 59 | |
vpcola | 0:a1734fe1ec4b | 60 | /** |
vpcola | 0:a1734fe1ec4b | 61 | * Create JSON parser over an array of tokens |
vpcola | 0:a1734fe1ec4b | 62 | */ |
vpcola | 0:a1734fe1ec4b | 63 | void jsmn_init(jsmn_parser *parser); |
vpcola | 0:a1734fe1ec4b | 64 | |
vpcola | 0:a1734fe1ec4b | 65 | /** |
vpcola | 0:a1734fe1ec4b | 66 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing |
vpcola | 0:a1734fe1ec4b | 67 | * a single JSON object. |
vpcola | 0:a1734fe1ec4b | 68 | */ |
vpcola | 0:a1734fe1ec4b | 69 | int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, |
vpcola | 0:a1734fe1ec4b | 70 | jsmntok_t *tokens, unsigned int num_tokens); |
vpcola | 0:a1734fe1ec4b | 71 | |
vpcola | 0:a1734fe1ec4b | 72 | #ifdef __cplusplus |
vpcola | 0:a1734fe1ec4b | 73 | } |
vpcola | 0:a1734fe1ec4b | 74 | #endif |
vpcola | 0:a1734fe1ec4b | 75 | |
vpcola | 0:a1734fe1ec4b | 76 | #endif /* __JSMN_H_ */ |