diff options
| author | 2021-12-23 03:09:07 +0100 | |
|---|---|---|
| committer | 2021-12-23 03:09:07 +0100 | |
| commit | 429cbc660ae14d4998715803b44c71abf0e4a339 (patch) | |
| tree | 12fe08f5dcb00a380536198bac3fffd4eb7dd19b /src/jsmn.h | |
| parent | 70002721443dabaa99b56301b537980e137b6249 (diff) | |
| download | libplist-429cbc660ae14d4998715803b44c71abf0e4a339.tar.gz libplist-429cbc660ae14d4998715803b44c71abf0e4a339.tar.bz2 | |
Add support for JSON format
Diffstat (limited to 'src/jsmn.h')
| -rw-r--r-- | src/jsmn.h | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/jsmn.h b/src/jsmn.h new file mode 100644 index 0000000..f12dc5a --- /dev/null +++ b/src/jsmn.h | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | /* | ||
| 2 | * jsmn.h | ||
| 3 | * Simple JSON parser (header file) | ||
| 4 | * | ||
| 5 | * Copyright (c) 2010 Serge A. Zaitsev | ||
| 6 | * | ||
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 8 | * of this software and associated documentation files (the "Software"), to deal | ||
| 9 | * in the Software without restriction, including without limitation the rights | ||
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 11 | * copies of the Software, and to permit persons to whom the Software is | ||
| 12 | * furnished to do so, subject to the following conditions: | ||
| 13 | * | ||
| 14 | * The above copyright notice and this permission notice shall be included in | ||
| 15 | * all copies or substantial portions of the Software. | ||
| 16 | * | ||
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 23 | * THE SOFTWARE. | ||
| 24 | */ | ||
| 25 | #ifndef __JSMN_H_ | ||
| 26 | #define __JSMN_H_ | ||
| 27 | |||
| 28 | /** | ||
| 29 | * JSON type identifier. Basic types are: | ||
| 30 | * o Object | ||
| 31 | * o Array | ||
| 32 | * o String | ||
| 33 | * o Other primitive: number, boolean (true/false) or null | ||
| 34 | */ | ||
| 35 | typedef enum { | ||
| 36 | JSMN_PRIMITIVE = 0, | ||
| 37 | JSMN_OBJECT = 1, | ||
| 38 | JSMN_ARRAY = 2, | ||
| 39 | JSMN_STRING = 3 | ||
| 40 | } jsmntype_t; | ||
| 41 | |||
| 42 | typedef enum { | ||
| 43 | /* Not enough tokens were provided */ | ||
| 44 | JSMN_ERROR_NOMEM = -1, | ||
| 45 | /* Invalid character inside JSON string */ | ||
| 46 | JSMN_ERROR_INVAL = -2, | ||
| 47 | /* The string is not a full JSON packet, more bytes expected */ | ||
| 48 | JSMN_ERROR_PART = -3, | ||
| 49 | /* Everything was fine */ | ||
| 50 | JSMN_SUCCESS = 0 | ||
| 51 | } jsmnerr_t; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * JSON token description. | ||
| 55 | * @param type type (object, array, string etc.) | ||
| 56 | * @param start start position in JSON data string | ||
| 57 | * @param end end position in JSON data string | ||
| 58 | */ | ||
| 59 | typedef struct { | ||
| 60 | jsmntype_t type; | ||
| 61 | int start; | ||
| 62 | int end; | ||
| 63 | int size; | ||
| 64 | #ifdef JSMN_PARENT_LINKS | ||
| 65 | int parent; | ||
| 66 | #endif | ||
| 67 | } jsmntok_t; | ||
| 68 | |||
| 69 | /** | ||
| 70 | * JSON parser. Contains an array of token blocks available. Also stores | ||
| 71 | * the string being parsed now and current position in that string | ||
| 72 | */ | ||
| 73 | typedef struct { | ||
| 74 | unsigned int pos; /* offset in the JSON string */ | ||
| 75 | int toknext; /* next token to allocate */ | ||
| 76 | int toksuper; /* superior token node, e.g parent object or array */ | ||
| 77 | } jsmn_parser; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Create JSON parser over an array of tokens | ||
| 81 | */ | ||
| 82 | void jsmn_init(jsmn_parser *parser); | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing | ||
| 86 | * a single JSON object. | ||
| 87 | */ | ||
| 88 | jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, | ||
| 89 | jsmntok_t *tokens, unsigned int num_tokens); | ||
| 90 | |||
| 91 | #endif /* __JSMN_H_ */ | ||
