You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
5.8 KiB

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