jsmn.h 1.6 KB

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