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:c1cd8e6ecdc9, 2017-05-25 (annotated)
- Committer:
- Amod Amatya amodamatya@gmail.com
- Date:
- Thu May 25 15:14:50 2017 +0545
- Revision:
- 0:c1cd8e6ecdc9
ini
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 1 | /* Author: Faheem Inayat |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 2 | * Created by "Night Crue" Team @ TechShop San Jose, CA |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 3 | * |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 4 | * --- DISCLAIMER --- |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 5 | * This code is a modified version of original JSMN lirary, written by |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 6 | * *** Serge A. Zaitsev *** |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 7 | * and hosted at https://github.com/zserge/jsmn |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 8 | * Any modification to the original source is not guaranteed to be included |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 9 | * in this version. As of writing of this file, the original source is |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 10 | * licensed under MIT License |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 11 | * (http://www.opensource.org/licenses/mit-license.php). |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 12 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 13 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 14 | #ifndef __JSMN_H_ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 15 | #define __JSMN_H_ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 16 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 17 | #include <stddef.h> |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 18 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 19 | #ifdef __cplusplus |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 20 | extern "C" |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 21 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 22 | #endif |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 23 | /* |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 24 | Modified version of original jsmn lib ... added a type "JSMN_KEY" and enabled |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 25 | parent-pointers and strict JSON check. |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 26 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 27 | /** |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 28 | * JSON type identifier. Basic types are: |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 29 | * o Object |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 30 | * o Array |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 31 | * o String |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 32 | * o Other primitive: number, boolean (true/false) or null |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 33 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 34 | typedef enum |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 35 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 36 | JSMN_UNDEFINED = 0, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 37 | JSMN_OBJECT = 1, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 38 | JSMN_ARRAY = 2, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 39 | JSMN_STRING = 3, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 40 | JSMN_PRIMITIVE = 4, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 41 | JSMN_KEY = 5 |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 42 | } jsmntype_t; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 43 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 44 | enum jsmnerr |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 45 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 46 | /* Not enough tokens were provided */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 47 | JSMN_ERROR_NOMEM = -1, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 48 | /* Invalid character inside JSON string */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 49 | JSMN_ERROR_INVAL = -2, |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 50 | /* The string is not a full JSON packet, more bytes expected */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 51 | JSMN_ERROR_PART = -3 |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 52 | }; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 53 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 54 | /** |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 55 | * JSON token description. |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 56 | * @param type type (object, array, string etc.) |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 57 | * @param start start position in JSON data string |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 58 | * @param end end position in JSON data string |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 59 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 60 | typedef struct |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 61 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 62 | jsmntype_t type; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 63 | int start; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 64 | int end; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 65 | int parent; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 66 | int childCount; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 67 | } jsmntok_t; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 68 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 69 | /** |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 70 | * JSON parser. Contains an array of token blocks available. Also stores |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 71 | * the string being parsed now and current position in that string |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 72 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 73 | typedef struct |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 74 | { |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 75 | unsigned int pos; /* offset in the JSON string */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 76 | unsigned int toknext; /* next token to allocate */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 77 | int toksuper; /* superior token node, e.g parent object or array */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 78 | } jsmn_parser; |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 79 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 80 | /** |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 81 | * Create JSON parser over an array of tokens |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 82 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 83 | void jsmn_init ( jsmn_parser *parser ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 84 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 85 | /** |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 86 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 87 | * a single JSON object. |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 88 | */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 89 | int jsmn_parse ( jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens ); |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 90 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 91 | #ifdef __cplusplus |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 92 | } |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 93 | #endif |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 94 | |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 95 | #endif /* __JSMN_H_ */ |
| Amod Amatya amodamatya@gmail.com | 0:c1cd8e6ecdc9 | 96 |