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 object is not a container"); \
55 throw new std::runtime_error(s); \
58 #define JSON_CHECK_OBJECT() do { \
59 if (!json_is_object(m_obj)) { \
60 std::string s("JSONObject object is not a dictionary"); \
61 throw new std::runtime_error(s); \
65 #define JSON_CHECK_ARRAY() do { \
66 if (!json_is_array(m_obj)) { \
67 std::string s("JSONObject object is not an array"); \
68 throw new std::runtime_error(s); \
72 #define JSON_CHECK(s) do { \
74 throw new std::bad_alloc; \
78 JSONObject::load(const char *input, size_t flags, json_error_t *error)
82 obj = json_loads(input, flags, error);
84 return JSONObject(obj, false);
88 JSONObject::load(FILE *fp, size_t flags, json_error_t *error)
92 obj = json_loadf(fp, flags, error);
94 return JSONObject(obj, false);
98 JSONObject::dump(size_t flags) const
100 char *s = json_dumps(m_obj, flags);
103 throw new std::bad_alloc;
109 JSONObject::dump(FILE *fp, size_t flags) const
111 int r = json_dumpf(m_obj, fp, flags);
114 throw new std::bad_alloc;
118 JSONObject::size(void) const
120 if (json_is_object(m_obj))
121 return json_object_size(m_obj);
122 else if (json_is_array(m_obj))
123 return json_array_size(m_obj);
128 JSONObject::JSONObject(json_t *obj, bool retain)
135 JSONObject::JSONObject(const char *value)
137 json_t *obj = json_string(value);
142 JSONObject::JSONObject(json_int_t value)
144 json_t *obj = json_integer(value);
149 JSONObject::JSONObject(double value)
151 json_t *obj = json_real(value);
156 JSONObject::JSONObject(bool value)
158 json_t *obj = value ? json_true() : json_false();
163 JSONObject::JSONObject(void)
165 json_t *obj = json_object();
171 JSONObject::object(void)
177 JSONObject::null(void)
179 return JSONObject(json_null(), false);
183 JSONObject::array(void)
185 return JSONObject(json_array(), false);
189 JSONObject::set(const char *key, JSONObject &value)
192 JSON_CHECK(json_object_set_new(m_obj, key, value.get()));
196 JSONObject::set(const char *key, const char *value)
198 JSONObject jobj(value);
203 JSONObject::set(const char *key, json_int_t value)
205 JSONObject jobj(value);
210 JSONObject::del(const char *key)
212 json_object_del(m_obj, key);
216 JSONObject::get(const char *key) const
220 obj = json_object_get(m_obj, key);
222 return JSONObject::null();
224 return JSONObject(obj, true);
228 JSONObject::get(size_t index) const
232 obj = json_array_get(m_obj, index);
234 return JSONObject::null();
236 return JSONObject(obj, true);
240 JSONObject::update(JSONObject &value)
243 json_t *other = value.get();
244 JSON_CHECK(json_object_update(m_obj, other));
249 JSONObject::operator[](size_t index) const
255 JSONObject::operator[](const char *key) const
261 JSONObject::append(JSONObject &value)
264 JSON_CHECK(json_array_append_new(m_obj, value.get()));
268 JSONObject::insert(size_t index, JSONObject &value)
271 JSON_CHECK(json_array_insert_new(m_obj, index, value.get()));
275 JSONObject::remove(size_t index)
278 JSON_CHECK(json_array_remove(m_obj, index));
282 JSONObject::clear(void)
284 JSON_CHECK_CONTAINER();
286 if (json_is_object(m_obj)) {
287 JSON_CHECK(json_object_clear(m_obj));
288 } else if (json_is_array(m_obj)) {
289 JSON_CHECK(json_array_clear(m_obj));
294 JSONObject::extend(JSONObject &value)
297 json_t *other = value.get();
298 JSON_CHECK(json_array_extend(m_obj, other));
303 JSONObject::string(void) const
305 return json_string_value(m_obj);
309 JSONObject::integer(void) const
311 return json_integer_value(m_obj);
315 JSONObject::real(void) const
317 return json_real_value(m_obj);
321 JSONObject::number(void) const
323 return json_number_value(m_obj);
327 JSONObject::isnull(void) const
329 return json_is_null(m_obj);
332 JSONObject::JSONObject(DDF &ddf)
334 if (ddf.isstruct()) {
335 DDF elem = ddf.first();
336 JSONObject jobj = JSONObject::array();
338 while (!elem.isnull()) {
339 JSONObject jtmp(elem);
343 } else if (ddf.islist()) {
344 DDF elem = ddf.first();
345 JSONObject jobj = JSONObject::object();
347 while (!elem.isnull()) {
348 JSONObject jtmp(elem);
349 jobj.set(elem.name(), jtmp);
352 } else if (ddf.isstring()) {
353 JSONObject(ddf.string());
354 } else if (ddf.isint()) {
355 JSONObject((json_int_t)ddf.integer());
356 } else if (ddf.isfloat()) {
357 JSONObject(ddf.floating());
358 } else if (ddf.isempty() || ddf.ispointer()) {
359 JSONObject::object();
360 } else if (ddf.isnull()) {
364 std::string s("Unbridgeable DDF object");
365 throw new std::runtime_error(s);
369 JSONObject::ddf(void) const
375 JSONIterator iter = iterator();
378 const char *key = iter.key();
379 DDF value = iter.value().ddf();
380 ddf.add(value.name(key));
381 } while (iter.next());
385 size_t i, nelems = size();
387 for (i = 0; i < nelems; i++) {
388 DDF value = get(i).ddf();
394 ddf.string(string());
397 ddf.integer(integer());
400 ddf.floating(real());
415 JSONIterator::JSONIterator(const JSONObject &obj)
418 m_iter = json_object_iter(m_obj);
421 JSONIterator::~JSONIterator(void)
427 JSONIterator::key(void) const
429 return json_object_iter_key(m_iter);
433 JSONIterator::value(void) const
435 return JSONObject(json_object_iter_value(m_iter));
439 JSONIterator::next(void)
441 m_iter = json_object_iter_next(m_obj, m_iter);
442 return m_iter != NULL;