Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
external_libs/jsmn.h@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | /* |
JMF | 0:082731ede69f | 2 | * Copyright (c) 2010 Serge A. Zaitsev |
JMF | 0:082731ede69f | 3 | * |
JMF | 0:082731ede69f | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
JMF | 0:082731ede69f | 5 | * of this software and associated documentation files (the "Software"), to deal |
JMF | 0:082731ede69f | 6 | * in the Software without restriction, including without limitation the rights |
JMF | 0:082731ede69f | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
JMF | 0:082731ede69f | 8 | * copies of the Software, and to permit persons to whom the Software is |
JMF | 0:082731ede69f | 9 | * furnished to do so, subject to the following conditions: |
JMF | 0:082731ede69f | 10 | * |
JMF | 0:082731ede69f | 11 | * The above copyright notice and this permission notice shall be included in |
JMF | 0:082731ede69f | 12 | * all copies or substantial portions of the Software. |
JMF | 0:082731ede69f | 13 | * |
JMF | 0:082731ede69f | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
JMF | 0:082731ede69f | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
JMF | 0:082731ede69f | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
JMF | 0:082731ede69f | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
JMF | 0:082731ede69f | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
JMF | 0:082731ede69f | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
JMF | 0:082731ede69f | 20 | * THE SOFTWARE. |
JMF | 0:082731ede69f | 21 | */ |
JMF | 0:082731ede69f | 22 | |
JMF | 0:082731ede69f | 23 | /** |
JMF | 0:082731ede69f | 24 | * @file jsmn.h |
JMF | 0:082731ede69f | 25 | * @brief Definition of the JSMN (Jasmine) JSON parser. |
JMF | 0:082731ede69f | 26 | * |
JMF | 0:082731ede69f | 27 | * For more information on JSMN: |
JMF | 0:082731ede69f | 28 | * @see http://zserge.com/jsmn.html |
JMF | 0:082731ede69f | 29 | */ |
JMF | 0:082731ede69f | 30 | |
JMF | 0:082731ede69f | 31 | #ifndef __JSMN_H_ |
JMF | 0:082731ede69f | 32 | #define __JSMN_H_ |
JMF | 0:082731ede69f | 33 | |
JMF | 0:082731ede69f | 34 | #include <stddef.h> |
JMF | 0:082731ede69f | 35 | |
JMF | 0:082731ede69f | 36 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 37 | extern "C" { |
JMF | 0:082731ede69f | 38 | #endif |
JMF | 0:082731ede69f | 39 | |
JMF | 0:082731ede69f | 40 | /** |
JMF | 0:082731ede69f | 41 | * JSON type identifier. Basic types are: |
JMF | 0:082731ede69f | 42 | * o Object |
JMF | 0:082731ede69f | 43 | * o Array |
JMF | 0:082731ede69f | 44 | * o String |
JMF | 0:082731ede69f | 45 | * o Other primitive: number, boolean (true/false) or null |
JMF | 0:082731ede69f | 46 | */ |
JMF | 0:082731ede69f | 47 | typedef enum { |
JMF | 0:082731ede69f | 48 | JSMN_UNDEFINED = 0, |
JMF | 0:082731ede69f | 49 | JSMN_OBJECT = 1, |
JMF | 0:082731ede69f | 50 | JSMN_ARRAY = 2, |
JMF | 0:082731ede69f | 51 | JSMN_STRING = 3, |
JMF | 0:082731ede69f | 52 | JSMN_PRIMITIVE = 4 |
JMF | 0:082731ede69f | 53 | } jsmntype_t; |
JMF | 0:082731ede69f | 54 | |
JMF | 0:082731ede69f | 55 | enum jsmnerr { |
JMF | 0:082731ede69f | 56 | /* Not enough tokens were provided */ |
JMF | 0:082731ede69f | 57 | JSMN_ERROR_NOMEM = -1, |
JMF | 0:082731ede69f | 58 | /* Invalid character inside JSON string */ |
JMF | 0:082731ede69f | 59 | JSMN_ERROR_INVAL = -2, |
JMF | 0:082731ede69f | 60 | /* The string is not a full JSON packet, more bytes expected */ |
JMF | 0:082731ede69f | 61 | JSMN_ERROR_PART = -3 |
JMF | 0:082731ede69f | 62 | }; |
JMF | 0:082731ede69f | 63 | |
JMF | 0:082731ede69f | 64 | /** |
JMF | 0:082731ede69f | 65 | * JSON token description. |
JMF | 0:082731ede69f | 66 | * type type (object, array, string etc.) |
JMF | 0:082731ede69f | 67 | * start start position in JSON data string |
JMF | 0:082731ede69f | 68 | * end end position in JSON data string |
JMF | 0:082731ede69f | 69 | */ |
JMF | 0:082731ede69f | 70 | typedef struct { |
JMF | 0:082731ede69f | 71 | jsmntype_t type; |
JMF | 0:082731ede69f | 72 | int start; |
JMF | 0:082731ede69f | 73 | int end; |
JMF | 0:082731ede69f | 74 | int size; |
JMF | 0:082731ede69f | 75 | #ifdef JSMN_PARENT_LINKS |
JMF | 0:082731ede69f | 76 | int parent; |
JMF | 0:082731ede69f | 77 | #endif |
JMF | 0:082731ede69f | 78 | } jsmntok_t; |
JMF | 0:082731ede69f | 79 | |
JMF | 0:082731ede69f | 80 | /** |
JMF | 0:082731ede69f | 81 | * JSON parser. Contains an array of token blocks available. Also stores |
JMF | 0:082731ede69f | 82 | * the string being parsed now and current position in that string |
JMF | 0:082731ede69f | 83 | */ |
JMF | 0:082731ede69f | 84 | typedef struct { |
JMF | 0:082731ede69f | 85 | unsigned int pos; /* offset in the JSON string */ |
JMF | 0:082731ede69f | 86 | unsigned int toknext; /* next token to allocate */ |
JMF | 0:082731ede69f | 87 | int toksuper; /* superior token node, e.g parent object or array */ |
JMF | 0:082731ede69f | 88 | } jsmn_parser; |
JMF | 0:082731ede69f | 89 | |
JMF | 0:082731ede69f | 90 | /** |
JMF | 0:082731ede69f | 91 | * Create JSON parser over an array of tokens |
JMF | 0:082731ede69f | 92 | */ |
JMF | 0:082731ede69f | 93 | void jsmn_init(jsmn_parser *parser); |
JMF | 0:082731ede69f | 94 | |
JMF | 0:082731ede69f | 95 | /** |
JMF | 0:082731ede69f | 96 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing |
JMF | 0:082731ede69f | 97 | * a single JSON object. |
JMF | 0:082731ede69f | 98 | */ |
JMF | 0:082731ede69f | 99 | int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, |
JMF | 0:082731ede69f | 100 | jsmntok_t *tokens, unsigned int num_tokens); |
JMF | 0:082731ede69f | 101 | |
JMF | 0:082731ede69f | 102 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 103 | } |
JMF | 0:082731ede69f | 104 | #endif |
JMF | 0:082731ede69f | 105 | |
JMF | 0:082731ede69f | 106 | #endif /* __JSMN_H_ */ |