source: lib/json.h @ 434a2d0

Last change on this file since 434a2d0 was 5d749ad, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-09T23:48:22Z

One difference in json.h: use "long long" instead of "long" for integer
types, or it might be too small to contain Twitter message IDs on for
example GCC+i386.

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