jsmn.h 1.6 KB

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