2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * JSONObject utilities.
37 #include "gssapiP_eap.h"
46 #define JSON_INIT(obj) do { \
48 throw new std::bad_alloc; \
52 #define JSON_CHECK_CONTAINER() do { \
53 if (!json_is_object(m_obj) && !json_is_array(m_obj)) { \
54 std::string s("JSONObject is not a container"); \
55 throw new std::runtime_error(s); \
59 #define JSON_CHECK_OBJECT() do { \
60 if (!json_is_object(m_obj)) { \
61 std::string s("JSONObject is not a dictionary"); \
62 throw new std::runtime_error(s); \
66 #define JSON_CHECK_ARRAY() do { \
67 if (!json_is_array(m_obj)) { \
68 std::string s("JSONObject is not an array"); \
69 throw new std::runtime_error(s); \
73 #define JSON_CHECK(s) do { \
75 throw new std::bad_alloc; \
79 JSONObject::load(const char *input, size_t flags, json_error_t *error)
83 obj = json_loads(input, flags, error);
85 return JSONObject(obj, false);
89 JSONObject::load(FILE *fp, size_t flags, json_error_t *error)
93 obj = json_loadf(fp, flags, error);
95 return JSONObject(obj, false);
99 JSONObject::dump(size_t flags) const
101 char *s = json_dumps(m_obj, flags);
104 throw new std::bad_alloc;
110 JSONObject::dump(FILE *fp, size_t flags) const
112 int r = json_dumpf(m_obj, fp, flags);
115 throw new std::bad_alloc;
119 JSONObject::size(void) const
121 if (json_is_object(m_obj))
122 return json_object_size(m_obj);
123 else if (json_is_array(m_obj))
124 return json_array_size(m_obj);
129 JSONObject::JSONObject(json_t *obj, bool retain)
136 JSONObject::JSONObject(const char *value)
138 json_t *obj = json_string(value);
143 JSONObject::JSONObject(json_int_t value)
145 json_t *obj = json_integer(value);
150 JSONObject::JSONObject(double value)
152 json_t *obj = json_real(value);
157 JSONObject::JSONObject(bool value)
159 json_t *obj = value ? json_true() : json_false();
164 JSONObject::JSONObject(void)
166 json_t *obj = json_object();
172 JSONObject::object(void)
178 JSONObject::null(void)
180 return JSONObject(json_null(), false);
184 JSONObject::array(void)
186 return JSONObject(json_array(), false);
190 JSONObject::set(const char *key, JSONObject &value)
193 JSON_CHECK(json_object_set_new(m_obj, key, value.get()));
197 JSONObject::set(const char *key, const char *value)
199 JSONObject jobj(value);
204 JSONObject::set(const char *key, json_int_t value)
206 JSONObject jobj(value);
211 JSONObject::del(const char *key)
213 json_object_del(m_obj, key);
217 JSONObject::get(const char *key) const
221 obj = json_object_get(m_obj, key);
223 return JSONObject::null();
225 return JSONObject(obj, true);
229 JSONObject::get(size_t index) const
233 obj = json_array_get(m_obj, index);
235 return JSONObject::null();
237 return JSONObject(obj, true);
241 JSONObject::update(JSONObject &value)
244 json_t *other = value.get();
245 JSON_CHECK(json_object_update(m_obj, other));
250 JSONObject::operator[](size_t index) const
256 JSONObject::operator[](const char *key) const
262 JSONObject::append(JSONObject &value)
265 JSON_CHECK(json_array_append_new(m_obj, value.get()));
269 JSONObject::insert(size_t index, JSONObject &value)
272 JSON_CHECK(json_array_insert_new(m_obj, index, value.get()));
276 JSONObject::remove(size_t index)
279 JSON_CHECK(json_array_remove(m_obj, index));
283 JSONObject::clear(void)
285 JSON_CHECK_CONTAINER();
287 if (json_is_object(m_obj)) {
288 JSON_CHECK(json_object_clear(m_obj));
289 } else if (json_is_array(m_obj)) {
290 JSON_CHECK(json_array_clear(m_obj));
295 JSONObject::extend(JSONObject &value)
298 json_t *other = value.get();
299 JSON_CHECK(json_array_extend(m_obj, other));
304 JSONObject::string(void) const
306 return json_string_value(m_obj);
310 JSONObject::integer(void) const
312 return json_integer_value(m_obj);
316 JSONObject::real(void) const
318 return json_real_value(m_obj);
322 JSONObject::number(void) const
324 return json_number_value(m_obj);
328 JSONObject::isnull(void) const
330 return json_is_null(m_obj);
333 JSONObject::JSONObject(DDF &ddf)
335 if (ddf.isstruct()) {
336 DDF elem = ddf.first();
337 JSONObject jobj = JSONObject::array();
339 while (!elem.isnull()) {
340 JSONObject jtmp(elem);
344 } else if (ddf.islist()) {
345 DDF elem = ddf.first();
346 JSONObject jobj = JSONObject::object();
348 while (!elem.isnull()) {
349 JSONObject jtmp(elem);
350 jobj.set(elem.name(), jtmp);
353 } else if (ddf.isstring()) {
354 JSONObject(ddf.string());
355 } else if (ddf.isint()) {
356 JSONObject((json_int_t)ddf.integer());
357 } else if (ddf.isfloat()) {
358 JSONObject(ddf.floating());
359 } else if (ddf.isempty() || ddf.ispointer()) {
360 JSONObject::object();
361 } else if (ddf.isnull()) {
365 std::string s("Unbridgeable DDF object");
366 throw new std::runtime_error(s);
370 JSONObject::ddf(void) const
376 JSONIterator iter = iterator();
379 const char *key = iter.key();
380 DDF value = iter.value().ddf();
381 ddf.add(value.name(key));
382 } while (iter.next());
386 size_t i, nelems = size();
388 for (i = 0; i < nelems; i++) {
389 DDF value = get(i).ddf();
395 ddf.string(string());
398 ddf.integer(integer());
401 ddf.floating(real());
416 JSONIterator::JSONIterator(const JSONObject &obj)
419 m_iter = json_object_iter(m_obj);
422 JSONIterator::~JSONIterator(void)
428 JSONIterator::key(void) const
430 return json_object_iter_key(m_iter);
434 JSONIterator::value(void) const
436 return JSONObject(json_object_iter_value(m_iter));
440 JSONIterator::next(void)
442 m_iter = json_object_iter_next(m_obj, m_iter);
443 return m_iter != NULL;