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