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