jsmn.h 1.7 KB

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