1 /* $NetBSD: jsmn.h,v 1.5 2020/05/25 20:47:24 christos Exp $ */ 2 3 #ifndef __JSMN_H_ 4 #define __JSMN_H_ 5 6 #include <stddef.h> 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 /** 13 * JSON type identifier. Basic types are: 14 * o Object 15 * o Array 16 * o String 17 * o Other primitive: number, boolean (true/false) or null 18 */ 19 typedef enum { 20 JSMN_PRIMITIVE = 0, 21 JSMN_OBJECT = 1, 22 JSMN_ARRAY = 2, 23 JSMN_STRING = 3 24 } jsmntype_t; 25 26 typedef enum { 27 /* Not enough tokens were provided */ 28 JSMN_ERROR_NOMEM = -1, 29 /* Invalid character inside JSON string */ 30 JSMN_ERROR_INVAL = -2, 31 /* The string is not a full JSON packet, more bytes expected */ 32 JSMN_ERROR_PART = -3 33 } jsmnerr_t; 34 35 /** 36 * JSON token description. 37 * @param type type (object, array, string etc.) 38 * @param start start position in JSON data string 39 * @param end end position in JSON data string 40 */ 41 typedef struct { 42 jsmntype_t type; 43 int start; 44 int end; 45 int size; 46 #ifdef JSMN_PARENT_LINKS 47 int parent; 48 #endif 49 } jsmntok_t; 50 51 /** 52 * JSON parser. Contains an array of token blocks available. Also stores 53 * the string being parsed now and current position in that string 54 */ 55 typedef struct { 56 unsigned int pos; /* offset in the JSON string */ 57 unsigned int toknext; /* next token to allocate */ 58 int toksuper; /* superior token node, e.g parent object or array */ 59 } jsmn_parser; 60 61 /** 62 * Create JSON parser over an array of tokens 63 */ 64 void jsmn_init(jsmn_parser *parser); 65 66 /** 67 * Run JSON parser. It parses a JSON data string into and array of tokens, each describing 68 * a single JSON object. 69 */ 70 jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, 71 jsmntok_t *tokens, unsigned int num_tokens); 72 73 #ifdef __cplusplus 74 } 75 #endif 76 77 #endif /* __JSMN_H_ */ 78