jsmn.h 1.6 KB

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