source: lib/json.h

Last change on this file was 82149f4, checked in by GitHub <noreply@…>, at 2023-03-04T19:59:09Z

Fixes related to external-json-parser (#165)

  • configure: append conditionally json-parser to pc file

json-parser needs to be appended to Requires section of pkg-config file
when bitlbee is configured to use external-json-parser. This change is
needed for external plugins like bitlbee-steam.

  • Makefile: do not install json.h when system json-parser is used

install-dev target should not install bundled json.h when bitlbee is
configured to use external-json-parser.

  • Use correct json.h header file with respect to external_json_parser value

The preprocessor must include correct json.h header file with respect to
external_json_parser value, otherwise function prototypes and other
definitions do not need to correspond with object used for linking.

The state before this commit is that local version lib/json.h is used
always for compilation and external_json_parser variable controls if
local lib/json.o or global libjsonparser.so will be linked.

In order to fix this problem, #include directives in lib/json_util.h and
lib/oauth2.c were changed from "json.h" to <json.h> and preprocessor -I
flags were moved after conditional json-parser flags, which is enough
for solving the issue. Additionally, USE_EXTERNAL_JSON_PARSER macro is
exported when external-json-parser is used and it is used in lib/json.h
to trigger an error message, which should prevent similar mistakes in
future.

  • Property mode set to 100644
File size: 5.2 KB
Line 
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#ifdef USE_EXTERNAL_JSON_PARSER
32#error Bitlbee was configured to use system json-parser, this header file should not be used.
33#endif
34
35#ifndef _JSON_H
36#define _JSON_H
37
38#ifndef json_char
39   #define json_char char
40#endif
41
42#ifndef json_int_t
43   #ifndef _MSC_VER
44      #include <inttypes.h>
45      #define json_int_t int64_t
46   #else
47      #define json_int_t __int64
48   #endif
49#endif
50
51#include <stdlib.h>
52
53#ifdef __cplusplus
54
55   #include <string.h>
56
57extern "C"
58{
59
60#endif
61
62typedef struct {
63        unsigned long max_memory;
64        int settings;
65
66        /* Custom allocator support (leave null to use malloc/free)
67         */
68
69        void * (*mem_alloc)(size_t, int zero, void * user_data);
70        void (* mem_free)(void *, void * user_data);
71
72        void * user_data; /* will be passed to mem_alloc and mem_free */
73
74} json_settings;
75
76#define json_enable_comments  0x01
77
78typedef enum {
79        json_none,
80        json_object,
81        json_array,
82        json_integer,
83        json_double,
84        json_string,
85        json_boolean,
86        json_null
87
88} json_type;
89
90extern const struct _json_value json_value_none;
91
92typedef struct _json_value {
93        struct _json_value * parent;
94
95        json_type type;
96
97        union {
98                int boolean;
99                json_int_t integer;
100                double dbl;
101
102                struct {
103                        unsigned int length;
104                        json_char * ptr; /* null terminated */
105
106                } string;
107
108                struct {
109                        unsigned int length;
110
111                        struct {
112                                json_char * name;
113                                unsigned int name_length;
114
115                                struct _json_value * value;
116
117                        } * values;
118
119         #if defined(__cplusplus) && __cplusplus >= 201103L
120                        decltype(values) begin() const
121                        {  return values; }
122                        decltype(values) end() const
123                        {  return values + length; }
124         #endif
125
126                } object;
127
128                struct {
129                        unsigned int length;
130                        struct _json_value ** values;
131
132         #if defined(__cplusplus) && __cplusplus >= 201103L
133                        decltype(values) begin() const
134                        {  return values; }
135                        decltype(values) end() const
136                        {  return values + length; }
137         #endif
138
139                } array;
140
141        } u;
142
143        union {
144                struct _json_value * next_alloc;
145                void * object_mem;
146
147        } _reserved;
148
149
150        /* Some C++ operator sugar */
151
152   #ifdef __cplusplus
153
154public:
155
156        inline _json_value ()
157        {
158                memset(this, 0, sizeof(_json_value));
159        }
160
161        inline const struct _json_value &operator [](int index) const
162        {
163                if (type != json_array || index < 0
164                    || ((unsigned int)index) >= u.array.length) {
165                        return json_value_none;
166                }
167
168                return *u.array.values [index];
169        }
170
171        inline const struct _json_value &operator [](const char * index) const
172        {
173                if (type != json_object) {
174                        return json_value_none;
175                }
176
177                for (unsigned int i = 0; i < u.object.length; ++i) {
178                        if (!strcmp(u.object.values [i].name, index)) {
179                                return *u.object.values [i].value;
180                        }
181                }
182
183                return json_value_none;
184        }
185
186        inline operator const char *() const
187        {
188                switch (type) {
189                case json_string:
190                        return u.string.ptr;
191
192                default:
193                        return "";
194                }
195                ;
196        }
197
198        inline operator json_int_t() const
199        {
200                switch (type) {
201                case json_integer:
202                        return u.integer;
203
204                case json_double:
205                        return (json_int_t)u.dbl;
206
207                default:
208                        return 0;
209                }
210                ;
211        }
212
213        inline operator bool() const
214        {
215                if (type != json_boolean) {
216                        return false;
217                }
218
219                return u.boolean != 0;
220        }
221
222        inline operator double() const
223        {
224                switch (type) {
225                case json_integer:
226                        return (double)u.integer;
227
228                case json_double:
229                        return u.dbl;
230
231                default:
232                        return 0;
233                }
234                ;
235        }
236
237   #endif
238
239} json_value;
240
241json_value * json_parse(const json_char * json,
242                        size_t length);
243
244#define json_error_max 128
245json_value * json_parse_ex(json_settings * settings,
246                           const json_char * json,
247                           size_t length,
248                           char * error);
249
250void json_value_free(json_value *);
251
252
253/* Not usually necessary, unless you used a custom mem_alloc and now want to
254 * use a custom mem_free.
255 */
256void json_value_free_ex(json_settings * settings,
257                        json_value *);
258
259
260#ifdef __cplusplus
261}    /* extern "C" */
262#endif
263
264#endif
265
266
Note: See TracBrowser for help on using the repository browser.