1*aab65fc5SEd Maste /* SPDX-License-Identifier: MIT */ 2959826caSMatt Macy #ifndef __JSMN_H_ 3959826caSMatt Macy #define __JSMN_H_ 4959826caSMatt Macy 5959826caSMatt Macy /* 6959826caSMatt Macy * JSON type identifier. Basic types are: 7959826caSMatt Macy * o Object 8959826caSMatt Macy * o Array 9959826caSMatt Macy * o String 10959826caSMatt Macy * o Other primitive: number, boolean (true/false) or null 11959826caSMatt Macy */ 12959826caSMatt Macy typedef enum { 13959826caSMatt Macy JSMN_PRIMITIVE = 0, 14959826caSMatt Macy JSMN_OBJECT = 1, 15959826caSMatt Macy JSMN_ARRAY = 2, 16959826caSMatt Macy JSMN_STRING = 3 17959826caSMatt Macy } jsmntype_t; 18959826caSMatt Macy 19959826caSMatt Macy typedef enum { 20959826caSMatt Macy /* Not enough tokens were provided */ 21959826caSMatt Macy JSMN_ERROR_NOMEM = -1, 22959826caSMatt Macy /* Invalid character inside JSON string */ 23959826caSMatt Macy JSMN_ERROR_INVAL = -2, 24959826caSMatt Macy /* The string is not a full JSON packet, more bytes expected */ 25959826caSMatt Macy JSMN_ERROR_PART = -3, 26959826caSMatt Macy /* Everything was fine */ 27959826caSMatt Macy JSMN_SUCCESS = 0 28959826caSMatt Macy } jsmnerr_t; 29959826caSMatt Macy 30959826caSMatt Macy /* 31959826caSMatt Macy * JSON token description. 32959826caSMatt Macy * @param type type (object, array, string etc.) 33959826caSMatt Macy * @param start start position in JSON data string 34959826caSMatt Macy * @param end end position in JSON data string 35959826caSMatt Macy */ 36959826caSMatt Macy typedef struct { 37959826caSMatt Macy jsmntype_t type; 38959826caSMatt Macy int start; 39959826caSMatt Macy int end; 40959826caSMatt Macy int size; 41959826caSMatt Macy } jsmntok_t; 42959826caSMatt Macy 43959826caSMatt Macy /* 44959826caSMatt Macy * JSON parser. Contains an array of token blocks available. Also stores 45959826caSMatt Macy * the string being parsed now and current position in that string 46959826caSMatt Macy */ 47959826caSMatt Macy typedef struct { 48959826caSMatt Macy unsigned int pos; /* offset in the JSON string */ 49959826caSMatt Macy int toknext; /* next token to allocate */ 50959826caSMatt Macy int toksuper; /* superior token node, e.g parent object or array */ 51959826caSMatt Macy } jsmn_parser; 52959826caSMatt Macy 53959826caSMatt Macy /* 54959826caSMatt Macy * Create JSON parser over an array of tokens 55959826caSMatt Macy */ 56959826caSMatt Macy void jsmn_init(jsmn_parser *parser); 57959826caSMatt Macy 58959826caSMatt Macy /* 59959826caSMatt Macy * Run JSON parser. It parses a JSON data string into and array of tokens, 60959826caSMatt Macy * each describing a single JSON object. 61959826caSMatt Macy */ 62959826caSMatt Macy jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, 63959826caSMatt Macy size_t len, 64959826caSMatt Macy jsmntok_t *tokens, unsigned int num_tokens); 65959826caSMatt Macy 66959826caSMatt Macy const char *jsmn_strerror(jsmnerr_t err); 67959826caSMatt Macy 68959826caSMatt Macy #endif /* __JSMN_H_ */ 69