comment out dumping code
[mech_eap.orig] / util_json.h
1 /*
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * JSON object wrapper with not-entirely-toll-free DDF bridging.
35  */
36
37 #ifndef _UTIL_JSON_H_
38 #define _UTIL_JSON_H_ 1
39
40 #ifdef __cplusplus
41 #include <string>
42 #include <new>
43
44 #include <jansson.h>
45 #include <shibsp/remoting/ddf.h>
46
47 using namespace shibsp;
48
49 namespace gss_eap {
50     class JSONObject;
51
52     class JSONIterator {
53     public:
54         JSONIterator(const JSONObject &obj);
55         ~JSONIterator(void);
56         const char *key(void) const;
57         JSONObject value(void) const;
58         bool next(void);
59
60     private:
61         json_t *m_obj;
62         void *m_iter;
63     };
64
65     class JSONObject {
66     public:
67         static JSONObject load(const char *input, size_t flags, json_error_t *error);
68         static JSONObject load(FILE *, size_t flags, json_error_t *error);
69
70         static JSONObject object(void);
71         static JSONObject array(void);
72         static JSONObject null(void);
73
74         char *dump(size_t flags = 0) const;
75         void dump(FILE *fp, size_t flags = 0) const;
76
77         json_type type(void) const { return json_typeof(m_obj); }
78         size_t size(void) const;
79
80         JSONObject(void);
81         JSONObject(DDF &value);
82         JSONObject(const char *value);
83         JSONObject(json_int_t value);
84         JSONObject(double value);
85         JSONObject(bool value);
86
87         void set(const char *key, JSONObject &value);
88         void set(const char *key, const char *value);
89         void set(const char *key, json_int_t value);
90         void del(const char *key);
91         void update(JSONObject &value);
92         JSONIterator iterator(void) const { return JSONIterator(*this); }
93         JSONObject get(const char *key) const;
94         JSONObject operator[](const char *key) const;
95
96         JSONObject get(size_t index) const;
97         JSONObject operator[](size_t index) const;
98         void append(JSONObject &value);
99         void insert(size_t index, JSONObject &value);
100         void remove(size_t index);
101         void clear(void);
102         void extend(JSONObject &value);
103
104         const char *string(void) const;
105         json_int_t integer(void) const;
106         double real(void) const;
107         double number(void) const;
108         bool isnull(void) const;
109         DDF ddf(void) const;
110
111         ~JSONObject(void)
112         {
113             if (m_obj != NULL)
114                 json_decref(m_obj);
115         }
116
117         JSONObject(const JSONObject &obj)
118         {
119             m_obj = json_incref(obj.m_obj);
120         }
121
122         JSONObject& operator=(const JSONObject &obj)
123         {
124             if (this != &obj)
125                 set(obj.m_obj);
126             return *this;
127         }
128
129     private:
130         friend class JSONIterator;
131
132         json_t *get(void) const {
133             return json_incref(m_obj);
134         }
135
136         void set(json_t *obj) {
137             if (m_obj != obj) {
138                 json_decref(m_obj);
139                 m_obj = json_incref(m_obj);
140             }
141         }
142
143         JSONObject(json_t *obj, bool retain);
144
145         json_t *m_obj;
146     };
147 }
148
149 #endif /* __cplusplus */
150
151 #endif /* _UTIL_JSON_H_ */