w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

json.h
Go to the documentation of this file.
1 
2 /* vim: set et ts=3 sw=3 sts=3 ft=c:
3  *
4  * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
5  * https://github.com/udp/json-parser
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef _JSON_H
32 #define _JSON_H
33 
34 #ifndef json_char
35  #define json_char char
36 #endif
37 
38 #ifdef _CARYLL_USE_PRE_SERIALIZED
39  #define CARYLL_USE_PRE_SERIALIZED
40 #endif
41 
42 #ifndef json_int_t
43  #include <stdint.h>
44  #ifndef _MSC_VER
45  #include <inttypes.h>
46  #define json_int_t int64_t
47  #else
48  #define json_int_t __int64
49  #endif
50 #endif
51 
52 #include <stdlib.h>
53 
54 #ifdef __cplusplus
55 
56  #include <string.h>
57 
58  extern "C"
59  {
60 
61 #endif
62 
63 typedef struct
64 {
65  unsigned long max_memory;
66  int settings;
67 
68  /* Custom allocator support (leave null to use malloc/free)
69  */
70 
71  void * (* mem_alloc) (size_t, int zero, void * user_data);
72  void (* mem_free) (void *, void * user_data);
73 
74  void * user_data; /* will be passed to mem_alloc and mem_free */
75 
76  size_t value_extra; /* how much extra space to allocate for values? */
77 
79 
80 #define json_enable_comments 0x01
81 
82 typedef enum
83 {
91  json_null
92 #ifdef CARYLL_USE_PRE_SERIALIZED
93 ,
95 #endif
97 
98 extern const struct _json_value json_value_none;
99 
100 typedef struct _json_object_entry
101 {
103  unsigned int name_length;
104 
105  struct _json_value * value;
106 
108 
109 typedef struct _json_value
110 {
111  struct _json_value * parent;
112 
114 
115  union
116  {
117  int boolean;
119  double dbl;
120 
121  struct
122  {
123  unsigned int length;
124  json_char * ptr; /* null terminated */
125 
127 
128  struct
129  {
130  unsigned int length;
131 
133 
134  #if defined(__cplusplus) && __cplusplus >= 201103L
135  decltype(values) begin () const
136  { return values;
137  }
138  decltype(values) end () const
139  { return values + length;
140  }
141  #endif
142 
144 
145  struct
146  {
147  unsigned int length;
148  struct _json_value ** values;
149 
150  #if defined(__cplusplus) && __cplusplus >= 201103L
151  decltype(values) begin () const
152  { return values;
153  }
154  decltype(values) end () const
155  { return values + length;
156  }
157  #endif
158 
159  } array;
160 
161  } u;
162 
163  union
164  {
166  void * object_mem;
167 
169 
170  #ifdef JSON_TRACK_SOURCE
171 
172  /* Location of the value in the source JSON
173  */
174  unsigned int line, col;
175 
176  #endif
177 
178 
179  /* Some C++ operator sugar */
180 
181  #ifdef __cplusplus
182 
183  public:
184 
185  inline _json_value ()
186  { memset (this, 0, sizeof (_json_value));
187  }
188 
189  inline const struct _json_value &operator [] (int index) const
190  {
191  if (type != json_array || index < 0
192  || ((unsigned int) index) >= u.array.length)
193  {
194  return json_value_none;
195  }
196 
197  return *u.array.values [index];
198  }
199 
200  inline const struct _json_value &operator [] (const char * index) const
201  {
202  if (type != json_object)
203  return json_value_none;
204 
205  for (unsigned int i = 0; i < u.object.length; ++ i)
206  if (!strcmp (u.object.values [i].name, index))
207  return *u.object.values [i].value;
208 
209  return json_value_none;
210  }
211 
212  inline operator const char * () const
213  {
214  switch (type)
215  {
216  case json_string:
217  return u.string.ptr;
218 
219  default:
220  return "";
221  };
222  }
223 
224  inline operator json_int_t () const
225  {
226  switch (type)
227  {
228  case json_integer:
229  return u.integer;
230 
231  case json_double:
232  return (json_int_t) u.dbl;
233 
234  default:
235  return 0;
236  };
237  }
238 
239  inline operator bool () const
240  {
241  if (type != json_boolean)
242  return false;
243 
244  return u.boolean != 0;
245  }
246 
247  inline operator double () const
248  {
249  switch (type)
250  {
251  case json_integer:
252  return (double) u.integer;
253 
254  case json_double:
255  return u.dbl;
256 
257  default:
258  return 0;
259  };
260  }
261 
262  #endif
263 
265 
267  size_t length);
268 
269 #define json_error_max 128
271  const json_char * json,
272  size_t length,
273  char * error);
274 
276 
277 
278 /* Not usually necessary, unless you used a custom mem_alloc and now want to
279  * use a custom mem_free.
280  */
282  json_value *);
283 
284 
285 #ifdef __cplusplus
286  } /* extern "C" */
287 #endif
288 
289 #endif
290 
291 
@ json_pre_serialized
Definition: CPAL.d:8692
json_settings
Definition: CPAL.d:8676
#define bool
Definition: autosp.c:101
int strcmp()
Definition: coll.cpp:143
#define error(a)
Definition: dviinfo.c:48
static void
Definition: fpif.c:118
static FIELD_PTR begin
Definition: genind.c:37
int col
Definition: gsftopk.c:443
small capitals from c petite p scientific i
Definition: afcover.h:80
#define length(c)
Definition: ctangleboot.c:65
#define size_t
Definition: glob.c:257
#define zero
#define index(s, c)
Definition: plain2.h:351
void mem_free(struct pixrect *p)
Definition: libpnm4.c:62
char line[1024]
Definition: process_score.c:29
bstring c int memset(void *s, int c, int length)
void json_value_free_ex(json_settings *settings, json_value *)
json_value * json_parse(const char *json, size_t length)
const struct _json_value json_value_none
void json_value_free(json_value *)
struct _json_value json_value
json_value * json_parse_ex(json_settings *settings, const char *json, size_t length, char *error)
json_type
Definition: json.h:83
@ json_null
Definition: json.h:91
@ json_none
Definition: json.h:84
@ json_double
Definition: json.h:88
@ json_string
Definition: json.h:89
@ json_array
Definition: json.h:86
@ json_boolean
Definition: json.h:90
@ json_integer
Definition: json.h:87
@ json_object
Definition: json.h:85
struct _json_object_entry json_object_entry
#define json_char
Definition: json.h:35
#define json_int_t
Definition: json.h:46
Definition: json.h:101
unsigned int name_length
Definition: json.h:103
char * name
Definition: json.h:102
struct _json_value * value
Definition: json.h:105
json_type type
Definition: json.h:113
char * ptr
Definition: json.h:124
union _json_value::@1796 _reserved
struct _json_value * parent
Definition: json.h:111
unsigned int length
Definition: json.h:123
struct _json_value::@1795::@1798 object
int boolean
Definition: json.h:117
json_object_entry * values
Definition: json.h:132
union _json_value::@1795 u
struct _json_value * next_alloc
Definition: json.h:165
int64_t integer
Definition: json.h:118
struct _json_value ** values
Definition: json.h:148
struct _json_value::@1795::@1799 array
struct _json_value::@1795::@1797 string
double dbl
Definition: json.h:119
void * object_mem
Definition: json.h:166
Definition: mendex.h:20
unsigned long max_memory
Definition: json.h:65
size_t value_extra
Definition: json.h:76
void * user_data
Definition: json.h:74
int settings
Definition: json.h:66
#define end(cp)
Definition: zic.c:71