00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _JSON_H
00032 #define _JSON_H
00033
00034 #ifndef json_char
00035 #define json_char char
00036 #endif
00037
00038 #ifndef json_int_t
00039 #ifndef _MSC_VER
00040 #include <inttypes.h>
00041 #define json_int_t int64_t
00042 #else
00043 #define json_int_t __int64
00044 #endif
00045 #endif
00046
00047 #include <stdlib.h>
00048
00049 #ifdef __cplusplus
00050
00051 #include <string.h>
00052
00053 extern "C"
00054 {
00055
00056 #endif
00057
00058 typedef struct
00059 {
00060 unsigned long max_memory;
00061 int settings;
00062
00063
00064
00065
00066 void * (* mem_alloc) (size_t, int zero, void * user_data);
00067 void (* mem_free) (void *, void * user_data);
00068
00069 void * user_data;
00070
00071 } json_settings;
00072
00073 #define json_relaxed_commas 1
00074
00075 typedef enum
00076 {
00077 json_none,
00078 json_object,
00079 json_array,
00080 json_integer,
00081 json_double,
00082 json_string,
00083 json_boolean,
00084 json_null
00085
00086 } json_type;
00087
00088 extern const struct _json_value json_value_none;
00089
00090 typedef struct _json_value
00091 {
00092 struct _json_value * parent;
00093
00094 json_type type;
00095
00096 union
00097 {
00098 int boolean;
00099 json_int_t integer;
00100 double dbl;
00101
00102 struct
00103 {
00104 unsigned int length;
00105 json_char * ptr;
00106
00107 } string;
00108
00109 struct
00110 {
00111 unsigned int length;
00112
00113 struct
00114 {
00115 json_char * name;
00116 struct _json_value * value;
00117
00118 } * values;
00119
00120 #if defined(__cplusplus) && __cplusplus >= 201103L
00121 decltype(values) begin () const
00122 { return values;
00123 }
00124 decltype(values) end () const
00125 { return values + length;
00126 }
00127 #endif
00128
00129 } object;
00130
00131 struct
00132 {
00133 unsigned int length;
00134 struct _json_value ** values;
00135
00136 #if defined(__cplusplus) && __cplusplus >= 201103L
00137 decltype(values) begin () const
00138 { return values;
00139 }
00140 decltype(values) end () const
00141 { return values + length;
00142 }
00143 #endif
00144
00145 } array;
00146
00147 } u;
00148
00149 union
00150 {
00151 struct _json_value * next_alloc;
00152 void * object_mem;
00153
00154 } _reserved;
00155
00156
00157
00158
00159 #ifdef __cplusplus
00160
00161 public:
00162
00163 inline _json_value ()
00164 { memset (this, 0, sizeof (_json_value));
00165 }
00166
00167 inline const struct _json_value &operator [] (int index) const
00168 {
00169 if (type != json_array || index < 0
00170 || ((unsigned int) index) >= u.array.length)
00171 {
00172 return json_value_none;
00173 }
00174
00175 return *u.array.values [index];
00176 }
00177
00178 inline const struct _json_value &operator [] (const char * index) const
00179 {
00180 if (type != json_object)
00181 return json_value_none;
00182
00183 for (unsigned int i = 0; i < u.object.length; ++ i)
00184 if (!strcmp (u.object.values [i].name, index))
00185 return *u.object.values [i].value;
00186
00187 return json_value_none;
00188 }
00189
00190 inline operator const char * () const
00191 {
00192 switch (type)
00193 {
00194 case json_string:
00195 return u.string.ptr;
00196
00197 default:
00198 return "";
00199 };
00200 }
00201
00202 inline operator json_int_t () const
00203 {
00204 switch (type)
00205 {
00206 case json_integer:
00207 return u.integer;
00208
00209 case json_double:
00210 return (json_int_t) u.dbl;
00211
00212 default:
00213 return 0;
00214 };
00215 }
00216
00217 inline operator bool () const
00218 {
00219 if (type != json_boolean)
00220 return false;
00221
00222 return u.boolean != 0;
00223 }
00224
00225 inline operator double () const
00226 {
00227 switch (type)
00228 {
00229 case json_integer:
00230 return (double) u.integer;
00231
00232 case json_double:
00233 return u.dbl;
00234
00235 default:
00236 return 0;
00237 };
00238 }
00239
00240 #endif
00241
00242 } json_value;
00243
00244 json_value * json_parse (const json_char * json,
00245 size_t length);
00246
00247 json_value * json_parse_ex (json_settings * settings,
00248 const json_char * json,
00249 size_t length,
00250 char * error);
00251
00252 void json_value_free (json_value *);
00253
00254
00255
00256
00257
00258 void json_value_free_ex (json_settings * settings,
00259 json_value *);
00260
00261
00262 #ifdef __cplusplus
00263 }
00264 #endif
00265
00266 #endif
00267
00268