]> git.sesse.net Git - vlc/blob - modules/misc/webservices/json.h
live555: retrieve RTSP track languages from SDP
[vlc] / modules / misc / webservices / json.h
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 #ifndef json_int_t
39 #include <inttypes.h>
40 #define json_int_t int64_t
41 #endif
42
43 #ifdef __cplusplus
44
45    #include <string.h>
46
47    extern "C"
48    {
49
50 #endif
51
52 typedef struct
53 {
54    unsigned long max_memory;
55    int settings;
56
57 } json_settings;
58
59 #define json_relaxed_commas 1
60
61 typedef enum
62 {
63    json_none,
64    json_object,
65    json_array,
66    json_integer,
67    json_double,
68    json_string,
69    json_boolean,
70    json_null
71
72 } json_type;
73
74 extern const struct _json_value json_value_none;
75
76 typedef struct _json_value
77 {
78    struct _json_value * parent;
79
80    json_type type;
81
82    union
83    {
84       int boolean;
85       json_int_t integer;
86       double dbl;
87
88       struct
89       {
90          unsigned int length;
91          json_char * ptr; /* null terminated */
92
93       } string;
94
95       struct
96       {
97          unsigned int length;
98
99          struct
100          {
101             json_char * name;
102             struct _json_value * value;
103
104          } * values;
105
106       } object;
107
108       struct
109       {
110          unsigned int length;
111          struct _json_value ** values;
112
113       } array;
114
115    } u;
116
117    union
118    {
119       struct _json_value * next_alloc;
120       void * object_mem;
121
122    } _reserved;
123
124
125    /* Some C++ operator sugar */
126
127    #ifdef __cplusplus
128
129       public:
130
131          inline _json_value ()
132          {  memset (this, 0, sizeof (_json_value));
133          }
134
135          inline const struct _json_value &operator [] (int index) const
136          {
137             if (type != json_array || index < 0
138                      || ((unsigned int) index) >= u.array.length)
139             {
140                return json_value_none;
141             }
142
143             return *u.array.values [index];
144          }
145
146          inline const struct _json_value &operator [] (const char * index) const
147          {
148             if (type != json_object)
149                return json_value_none;
150
151             for (unsigned int i = 0; i < u.object.length; ++ i)
152                if (!strcmp (u.object.values [i].name, index))
153                   return *u.object.values [i].value;
154
155             return json_value_none;
156          }
157
158          inline operator const char * () const
159          {
160             switch (type)
161             {
162                case json_string:
163                   return u.string.ptr;
164
165                default:
166                   return "";
167             };
168          }
169
170          inline operator json_int_t () const
171          {
172             switch (type)
173             {
174                case json_integer:
175                   return u.integer;
176
177                case json_double:
178                   return (json_int_t) u.dbl;
179
180                default:
181                   return 0;
182             };
183          }
184
185          inline operator bool () const
186          {
187             if (type != json_boolean)
188                return false;
189
190             return u.boolean != 0;
191          }
192
193          inline operator double () const
194          {
195             switch (type)
196             {
197                case json_integer:
198                   return (double) u.integer;
199
200                case json_double:
201                   return u.dbl;
202
203                default:
204                   return 0;
205             };
206          }
207
208    #endif
209
210 } json_value;
211
212 json_value * json_parse
213    (const json_char * json);
214
215 json_value * json_parse_ex
216    (json_settings * settings, const json_char * json, char * error);
217
218 void json_value_free (json_value *);
219
220
221 #ifdef __cplusplus
222    } /* extern "C" */
223 #endif
224
225 #endif