json.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* vim: set et ts=3 sw=3 sts=3 ft=c:
  2. *
  3. * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
  4. * https://github.com/udp/json-parser
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _JSON_H
  30. #define _JSON_H
  31. #ifndef json_char
  32. #define json_char char
  33. #endif
  34. #ifndef json_int_t
  35. #ifndef _MSC_VER
  36. #include <inttypes.h>
  37. #define json_int_t int64_t
  38. #else
  39. #define json_int_t __int64
  40. #endif
  41. #endif
  42. #include <stdlib.h>
  43. #ifdef __cplusplus
  44. #include <string.h>
  45. extern "C"
  46. {
  47. #endif
  48. typedef struct
  49. {
  50. unsigned long max_memory;
  51. int settings;
  52. /* Custom allocator support (leave null to use malloc/free)
  53. */
  54. void * (* mem_alloc) (size_t, int zero, void * user_data);
  55. void (* mem_free) (void *, void * user_data);
  56. void * user_data; /* will be passed to mem_alloc and mem_free */
  57. size_t value_extra; /* how much extra space to allocate for values? */
  58. } json_settings;
  59. #define json_enable_comments 0x01
  60. typedef enum
  61. {
  62. json_none,
  63. json_object,
  64. json_array,
  65. json_integer,
  66. json_double,
  67. json_string,
  68. json_boolean,
  69. json_null
  70. } json_type;
  71. extern const struct _json_value json_value_none;
  72. typedef struct _json_object_entry
  73. {
  74. json_char * name;
  75. unsigned int name_length;
  76. struct _json_value * value;
  77. } json_object_entry;
  78. typedef struct _json_value
  79. {
  80. struct _json_value * parent;
  81. json_type type;
  82. union
  83. {
  84. int boolean;
  85. json_int_t integer;
  86. double dbl;
  87. struct
  88. {
  89. unsigned int length;
  90. json_char * ptr; /* null terminated */
  91. } string;
  92. struct
  93. {
  94. unsigned int length;
  95. json_object_entry * values;
  96. #if defined(__cplusplus) && __cplusplus >= 201103L
  97. decltype(values) begin () const
  98. { return values;
  99. }
  100. decltype(values) end () const
  101. { return values + length;
  102. }
  103. #endif
  104. } object;
  105. struct
  106. {
  107. unsigned int length;
  108. struct _json_value ** values;
  109. #if defined(__cplusplus) && __cplusplus >= 201103L
  110. decltype(values) begin () const
  111. { return values;
  112. }
  113. decltype(values) end () const
  114. { return values + length;
  115. }
  116. #endif
  117. } array;
  118. } u;
  119. union
  120. {
  121. struct _json_value * next_alloc;
  122. void * object_mem;
  123. } _reserved;
  124. #ifdef JSON_TRACK_SOURCE
  125. /* Location of the value in the source JSON
  126. */
  127. unsigned int line, col;
  128. #endif
  129. /* Some C++ operator sugar */
  130. #ifdef __cplusplus
  131. public:
  132. inline _json_value ()
  133. { memset (this, 0, sizeof (_json_value));
  134. }
  135. inline const struct _json_value &operator [] (int index) const
  136. {
  137. if (type != json_array || index < 0
  138. || ((unsigned int) index) >= u.array.length)
  139. {
  140. return json_value_none;
  141. }
  142. return *u.array.values [index];
  143. }
  144. inline const struct _json_value &operator [] (const char * index) const
  145. {
  146. if (type != json_object)
  147. return json_value_none;
  148. for (unsigned int i = 0; i < u.object.length; ++ i)
  149. if (!strcmp (u.object.values [i].name, index))
  150. return *u.object.values [i].value;
  151. return json_value_none;
  152. }
  153. inline operator const char * () const
  154. {
  155. switch (type)
  156. {
  157. case json_string:
  158. return u.string.ptr;
  159. default:
  160. return "";
  161. };
  162. }
  163. inline operator json_int_t () const
  164. {
  165. switch (type)
  166. {
  167. case json_integer:
  168. return u.integer;
  169. case json_double:
  170. return (json_int_t) u.dbl;
  171. default:
  172. return 0;
  173. };
  174. }
  175. inline operator bool () const
  176. {
  177. if (type != json_boolean)
  178. return false;
  179. return u.boolean != 0;
  180. }
  181. inline operator double () const
  182. {
  183. switch (type)
  184. {
  185. case json_integer:
  186. return (double) u.integer;
  187. case json_double:
  188. return u.dbl;
  189. default:
  190. return 0;
  191. };
  192. }
  193. #endif
  194. } json_value;
  195. json_value * json_parse (const json_char * json,
  196. size_t length);
  197. #define json_error_max 128
  198. json_value * json_parse_ex (json_settings * settings,
  199. const json_char * json,
  200. size_t length,
  201. char * error);
  202. void json_value_free (json_value *);
  203. /* Not usually necessary, unless you used a custom mem_alloc and now want to
  204. * use a custom mem_free.
  205. */
  206. void json_value_free_ex (json_settings * settings,
  207. json_value *);
  208. #ifdef __cplusplus
  209. } /* extern "C" */
  210. #endif
  211. #endif