jsmn.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2010 Serge A. Zaitsev
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file jsmn.h
  24. * @brief Definition of the JSMN (Jasmine) JSON parser.
  25. *
  26. * For more information on JSMN:
  27. * @see http://zserge.com/jsmn.html
  28. */
  29. #ifndef __JSMN_H_
  30. #define __JSMN_H_
  31. #include <stddef.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * JSON type identifier. Basic types are:
  37. * o Object
  38. * o Array
  39. * o String
  40. * o Other primitive: number, boolean (true/false) or null
  41. */
  42. typedef enum {
  43. JSMN_UNDEFINED = 0,
  44. JSMN_OBJECT = 1,
  45. JSMN_ARRAY = 2,
  46. JSMN_STRING = 3,
  47. JSMN_PRIMITIVE = 4
  48. } jsmntype_t;
  49. enum jsmnerr {
  50. /* Not enough tokens were provided */
  51. JSMN_ERROR_NOMEM = -1,
  52. /* Invalid character inside JSON string */
  53. JSMN_ERROR_INVAL = -2,
  54. /* The string is not a full JSON packet, more bytes expected */
  55. JSMN_ERROR_PART = -3
  56. };
  57. /**
  58. * JSON token description.
  59. * @param type type (object, array, string etc.)
  60. * @param start start position in JSON data string
  61. * @param end end position in JSON data string
  62. */
  63. typedef struct {
  64. jsmntype_t type;
  65. int start;
  66. int end;
  67. int size;
  68. #ifdef JSMN_PARENT_LINKS
  69. int parent;
  70. #endif
  71. } jsmntok_t;
  72. /**
  73. * JSON parser. Contains an array of token blocks available. Also stores
  74. * the string being parsed now and current position in that string
  75. */
  76. typedef struct {
  77. unsigned int pos; /* offset in the JSON string */
  78. unsigned int toknext; /* next token to allocate */
  79. int toksuper; /* superior token node, e.g parent object or array */
  80. } jsmn_parser;
  81. /**
  82. * Create JSON parser over an array of tokens
  83. */
  84. void jsmn_init(jsmn_parser *parser);
  85. /**
  86. * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
  87. * a single JSON object.
  88. */
  89. int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
  90. jsmntok_t *tokens, unsigned int num_tokens);
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif /* __JSMN_H_ */