1
0

json.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. } json_settings;
  58. #define json_enable_comments 0x01
  59. typedef enum
  60. {
  61. json_none,
  62. json_object,
  63. json_array,
  64. json_integer,
  65. json_double,
  66. json_string,
  67. json_boolean,
  68. json_null
  69. } json_type;
  70. extern const struct _json_value json_value_none;
  71. typedef struct _json_value
  72. {
  73. struct _json_value * parent;
  74. json_type type;
  75. union
  76. {
  77. int boolean;
  78. json_int_t integer;
  79. double dbl;
  80. struct
  81. {
  82. unsigned int length;
  83. json_char * ptr; /* null terminated */
  84. } string;
  85. struct
  86. {
  87. unsigned int length;
  88. struct
  89. {
  90. json_char * name;
  91. unsigned int name_length;
  92. struct _json_value * value;
  93. } * values;
  94. #if defined(__cplusplus) && __cplusplus >= 201103L
  95. decltype(values) begin () const
  96. { return values;
  97. }
  98. decltype(values) end () const
  99. { return values + length;
  100. }
  101. #endif
  102. } object;
  103. struct
  104. {
  105. unsigned int length;
  106. struct _json_value ** values;
  107. #if defined(__cplusplus) && __cplusplus >= 201103L
  108. decltype(values) begin () const
  109. { return values;
  110. }
  111. decltype(values) end () const
  112. { return values + length;
  113. }
  114. #endif
  115. } array;
  116. } u;
  117. union
  118. {
  119. struct _json_value * next_alloc;
  120. void * object_mem;
  121. } _reserved;
  122. /* Some C++ operator sugar */
  123. #ifdef __cplusplus
  124. public:
  125. inline _json_value ()
  126. { memset (this, 0, sizeof (_json_value));
  127. }
  128. inline const struct _json_value &operator [] (int index) const
  129. {
  130. if (type != json_array || index < 0
  131. || ((unsigned int) index) >= u.array.length)
  132. {
  133. return json_value_none;
  134. }
  135. return *u.array.values [index];
  136. }
  137. inline const struct _json_value &operator [] (const char * index) const
  138. {
  139. if (type != json_object)
  140. return json_value_none;
  141. for (unsigned int i = 0; i < u.object.length; ++ i)
  142. if (!strcmp (u.object.values [i].name, index))
  143. return *u.object.values [i].value;
  144. return json_value_none;
  145. }
  146. inline operator const char * () const
  147. {
  148. switch (type)
  149. {
  150. case json_string:
  151. return u.string.ptr;
  152. default:
  153. return "";
  154. };
  155. }
  156. inline operator json_int_t () const
  157. {
  158. switch (type)
  159. {
  160. case json_integer:
  161. return u.integer;
  162. case json_double:
  163. return (json_int_t) u.dbl;
  164. default:
  165. return 0;
  166. };
  167. }
  168. inline operator bool () const
  169. {
  170. if (type != json_boolean)
  171. return false;
  172. return u.boolean != 0;
  173. }
  174. inline operator double () const
  175. {
  176. switch (type)
  177. {
  178. case json_integer:
  179. return (double) u.integer;
  180. case json_double:
  181. return u.dbl;
  182. default:
  183. return 0;
  184. };
  185. }
  186. #endif
  187. } json_value;
  188. json_value * json_parse (const json_char * json,
  189. size_t length);
  190. #define json_error_max 128
  191. json_value * json_parse_ex (json_settings * settings,
  192. const json_char * json,
  193. size_t length,
  194. char * error);
  195. void json_value_free (json_value *);
  196. /* Not usually necessary, unless you used a custom mem_alloc and now want to
  197. * use a custom mem_free.
  198. */
  199. void json_value_free_ex (json_settings * settings,
  200. json_value *);
  201. #ifdef __cplusplus
  202. } /* extern "C" */
  203. #endif
  204. #endif