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.

222 lines
4.7 KiB

  1. /* vim: set et ts=3 sw=3 ft=c:
  2. *
  3. * Copyright (C) 2012 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. #ifdef __cplusplus
  35. #include <string.h>
  36. extern "C"
  37. {
  38. #endif
  39. typedef struct
  40. {
  41. unsigned long max_memory;
  42. int settings;
  43. } json_settings;
  44. #define json_relaxed_commas 1
  45. typedef enum
  46. {
  47. json_none,
  48. json_object,
  49. json_array,
  50. json_integer,
  51. json_double,
  52. json_string,
  53. json_boolean,
  54. json_null
  55. } json_type;
  56. extern const struct _json_value json_value_none;
  57. typedef struct _json_value
  58. {
  59. struct _json_value * parent;
  60. json_type type;
  61. union
  62. {
  63. int boolean;
  64. long integer;
  65. double dbl;
  66. struct
  67. {
  68. unsigned int length;
  69. json_char * ptr; /* null terminated */
  70. } string;
  71. struct
  72. {
  73. unsigned int length;
  74. struct
  75. {
  76. json_char * name;
  77. struct _json_value * value;
  78. } * values;
  79. } object;
  80. struct
  81. {
  82. unsigned int length;
  83. struct _json_value ** values;
  84. } array;
  85. } u;
  86. union
  87. {
  88. struct _json_value * next_alloc;
  89. void * object_mem;
  90. } _reserved;
  91. /* Some C++ operator sugar */
  92. #ifdef __cplusplus
  93. public:
  94. inline _json_value ()
  95. { memset (this, 0, sizeof (_json_value));
  96. }
  97. inline const struct _json_value &operator [] (int index) const
  98. {
  99. if (type != json_array || index < 0
  100. || ((unsigned int) index) >= u.array.length)
  101. {
  102. return json_value_none;
  103. }
  104. return *u.array.values [index];
  105. }
  106. inline const struct _json_value &operator [] (const char * index) const
  107. {
  108. if (type != json_object)
  109. return json_value_none;
  110. for (unsigned int i = 0; i < u.object.length; ++ i)
  111. if (!strcmp (u.object.values [i].name, index))
  112. return *u.object.values [i].value;
  113. return json_value_none;
  114. }
  115. inline operator const char * () const
  116. {
  117. switch (type)
  118. {
  119. case json_string:
  120. return u.string.ptr;
  121. default:
  122. return "";
  123. };
  124. }
  125. inline operator long () const
  126. {
  127. switch (type)
  128. {
  129. case json_integer:
  130. return u.integer;
  131. case json_double:
  132. return (long) u.dbl;
  133. default:
  134. return 0;
  135. };
  136. }
  137. inline operator bool () const
  138. {
  139. if (type != json_boolean)
  140. return false;
  141. return u.boolean != 0;
  142. }
  143. inline operator double () const
  144. {
  145. switch (type)
  146. {
  147. case json_integer:
  148. return u.integer;
  149. case json_double:
  150. return u.dbl;
  151. default:
  152. return 0;
  153. };
  154. }
  155. #endif
  156. } json_value;
  157. json_value * json_parse
  158. (const json_char * json);
  159. json_value * json_parse_ex
  160. (json_settings * settings, const json_char * json, char * error);
  161. void json_value_free (json_value *);
  162. #ifdef __cplusplus
  163. } /* extern "C" */
  164. #endif
  165. #endif