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 * JSON object wrapper with not-entirely-toll-free DDF bridging.
38 #define _UTIL_JSON_H_ 1
45 #include <shibsp/remoting/ddf.h>
47 using namespace shibsp;
49 namespace gss_eap_util {
52 class JSONException : public std::exception {
54 JSONException(json_t *obj = NULL, json_type type = JSON_NULL);
56 ~JSONException(void) throw() {
60 virtual const char *what(void) const throw() {
61 return m_reason.c_str();
72 JSONIterator(const JSONObject &obj);
74 const char *key(void) const;
75 JSONObject value(void) const;
85 static JSONObject load(const char *input, size_t flags, json_error_t *error);
86 static JSONObject load(FILE *, size_t flags, json_error_t *error);
88 static JSONObject object(void);
89 static JSONObject array(void);
90 static JSONObject null(void);
91 static JSONObject ddf(DDF &value);
93 char *dump(size_t flags = 0) const;
94 void dump(FILE *fp, size_t flags = JSON_INDENT(4)) const;
96 json_type type(void) const { return json_typeof(m_obj); }
97 size_t size(void) const;
100 JSONObject(const char *value);
101 JSONObject(json_int_t value);
102 JSONObject(double value);
103 JSONObject(bool value);
105 void set(const char *key, JSONObject &value);
106 void set(const char *key, const char *value);
107 void set(const char *key, json_int_t value);
108 void del(const char *key);
109 void update(JSONObject &value);
110 JSONIterator iterator(void) const { return JSONIterator(*this); }
111 JSONObject get(const char *key) const;
112 JSONObject operator[](const char *key) const;
114 JSONObject get(size_t index) const;
115 JSONObject operator[](size_t index) const;
116 void append(JSONObject &value);
117 void insert(size_t index, JSONObject &value);
118 void remove(size_t index);
120 void extend(JSONObject &value);
122 const char *string(void) const;
123 json_int_t integer(void) const;
124 double real(void) const;
125 double number(void) const;
128 bool isObject(void) const;
129 bool isArray(void) const;
130 bool isString(void) const;
131 bool isInteger(void) const;
132 bool isNumber(void) const;
133 bool isBoolean(void) const;
134 bool isNull(void) const;
142 JSONObject(const JSONObject &obj)
144 m_obj = json_incref(obj.m_obj);
147 JSONObject& operator=(const JSONObject &obj)
155 friend class JSONIterator;
157 json_t *get(void) const {
158 return json_incref(m_obj);
161 void set(json_t *obj) {
164 m_obj = json_incref(m_obj);
168 JSONObject(json_t *obj, bool retain = true);
174 #endif /* __cplusplus */
176 #endif /* _UTIL_JSON_H_ */