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