templatize janssonxx functionality to prepare for proxy setters
[jansson.git] / janssonxx.h
1 // janssonxx - C++ wrapper for jansson
2 //
3 // author: Sean Middleditch <sean@middleditch.us>
4 //
5 // janssonxx is free software; you can redistribute it and/or modify
6 // it under the terms of the MIT license. See LICENSE for details.
7
8
9 #if !defined(JANSSONXX_H)
10 #define JANSSONXX_H 1
11
12 #include <string>
13 #include <ostream>
14 #include <istream>
15 #include <sstream>
16 #include <cstdlib>
17
18 namespace jansson {
19         // include in the jansson namespace
20 #       include <jansson.h>
21
22         class Iterator;
23         class Value;
24
25         // base class for JSON value interface
26         template <typename _Base>
27         class _ValueBase : public _Base {
28         public:
29                 // empty constructor
30                 _ValueBase() : _Base() {}
31
32                 // copy constructor
33                 _ValueBase(const _Base& base) : _Base(base) {}
34
35                 // create reference to value
36                 _ValueBase(json_t* json) : _Base(json) {}
37
38                 // check value type
39                 bool is_undefined() const { return _Base::as_json() == 0; }
40                 bool is_object() const { return json_is_object(_Base::as_json()); }
41                 bool is_array() const { return json_is_array(_Base::as_json()); }
42                 bool is_string() const { return json_is_string(_Base::as_json()); }
43                 bool is_integer() const { return json_is_integer(_Base::as_json()); }
44                 bool is_real() const { return json_is_real(_Base::as_json()); }
45                 bool is_number() const { return json_is_number(_Base::as_json()); }
46                 bool is_true() const { return json_is_true(_Base::as_json()); }
47                 bool is_false() const { return json_is_false(_Base::as_json()); }
48                 bool is_boolean() const { return json_is_boolean(_Base::as_json()); }
49                 bool is_null() const { return json_is_null(_Base::as_json()); }
50
51                 // get size of array or object
52                 inline unsigned int size() const;
53
54                 // get value at array index (const version)
55                 inline const Value at(unsigned int index) const;
56
57                 inline const Value operator[](signed int index) const;
58                 inline const Value operator[](unsigned int index) const;
59                 inline const Value operator[](signed short index) const;
60                 inline const Value operator[](unsigned short index) const;
61                 inline const Value operator[](signed long index) const;
62                 inline const Value operator[](unsigned long index) const;
63
64                 // get value at array index (non-const version)
65                 inline Value at(unsigned int index);
66
67                 inline Value operator[](signed int index);
68                 inline Value operator[](unsigned int index);
69                 inline Value operator[](signed short index);
70                 inline Value operator[](unsigned short index);
71                 inline Value operator[](signed long index);
72                 inline Value operator[](unsigned long index);
73
74                 // get object property
75                 inline const Value get(const char* key) const;
76
77                 inline const Value get(const std::string& key) const;
78                 inline const Value operator[](const char* key) const;
79                 inline const Value operator[](const std::string& key) const;
80
81                 // clear all array/object values
82                 inline void clear();
83
84                 // get value cast to specified type
85                 inline const char* as_cstring() const;
86                 inline std::string as_string() const;
87                 inline int as_integer() const;
88                 inline double as_real() const;
89                 inline double as_number() const;
90                 inline bool as_boolean() const;
91
92                 // set an object property (converts value to object is not one already)
93                 inline _Base& set_key(const char* key, const Value& value);
94
95                 inline _Base& set_key(const std::string& key, const Value& value);
96
97                 // set an array index (converts value to object is not one already)
98                 inline _Base& set_at(unsigned int index, const Value& value);
99
100                 // delete an object key
101                 inline _Base& del_key(const char* key);
102
103                 inline _Base& del_key(const std::string& key);
104
105                 // delete an item from an array by index
106                 inline _Base& del_at(unsigned int index);
107
108                 // insert an item into an array at a given index
109                 inline _Base& insert_at(unsigned int index, const Value& value);
110         };
111
112         // represents any JSON value, private base
113         class _Value {
114         public:
115                 // construct new Value with an undefined value
116                 _Value() : _value(0) {}
117
118                 // copy constructor
119                 _Value(const _Value& value) : _value(json_incref(value._value)) {}
120
121                 // make a reference to an existing json_t value
122                 explicit _Value(json_t* value) : _value(json_incref(value)) {}
123
124                 // free Value resources
125                 ~_Value() { json_decref(_value); }
126
127                 // copy an existing Value
128                 _Value& operator=(const _Value& e) {
129                         if (&e != this) {
130                                 json_decref(_value);
131                                 _value = json_incref(e._value);
132                         }
133                         return *this;
134                 }
135
136                 // get the underlying json_t
137                 json_t* as_json() const { return _value; }
138
139         protected:
140                 // take ownership of a json_t (does not increase reference count)
141                 static _Value _take(json_t* json) {
142                         _Value v;
143                         v._value = json;
144                         return v;
145                 }
146
147         private:
148                 // internal value pointer
149                 json_t* _value;
150         };
151
152         // represents any JSON value
153         class Value : public _ValueBase<_Value> {
154         public:
155                 // empty constructor
156                 Value() : _ValueBase<_Value>() {}
157
158                 // copy constructor for base
159                 Value(const _Value& value) : _ValueBase<_Value>(value) {}
160         
161                 // copy constructor for base
162                 Value(const _ValueBase<_Value>& value) : _ValueBase<_Value>(value) {}
163
164                 // copy constructor
165                 Value(const Value& value) : _ValueBase<_Value>(value) {}
166
167                 // create reference to value
168                 explicit Value(json_t* json) : _ValueBase<_Value>(json) {}
169
170                 // construct Value from input
171                 static inline Value from(const char* value) { return Value::_take(json_string(value)); }
172                 static inline Value from(const std::string& value) { return from(value.c_str()); }
173                 static inline Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
174                 static inline Value from(signed int value) { return Value::_take(json_integer(value)); }
175                 static inline Value from(unsigned int value) { return Value::_take(json_integer(value)); }
176                 static inline Value from(signed short value) { return Value::_take(json_integer(value)); }
177                 static inline Value from(unsigned short value) { return Value::_take(json_integer(value)); }
178                 static inline Value from(signed long value) { return Value::_take(json_integer(value)); }
179                 static inline Value from(unsigned long value) { return Value::_take(json_integer(value)); }
180                 static inline Value from(float value) { return Value::_take(json_real(value)); }
181                 static inline Value from(double value) { return Value::_take(json_real(value)); }
182
183                 // create a new empty object
184                 static inline Value object() { return Value::_take(json_object()); }
185
186                 // create a new empty array
187                 static inline Value array() { return Value::_take(json_array()); }
188
189                 // create a new null value
190                 static inline Value null() { return Value::_take(json_null()); }
191
192                 // load a file as a JSON value
193                 static Value load_file(const char* path, json_error_t* error = 0) {
194                         return Value::_take(json_load_file(path, error));
195                 }
196
197                 // load a string as a JSON value
198                 static Value load_string(const char* string, json_error_t* error = 0) {
199                         return Value::_take(json_loads(string, error));
200                 }
201
202                 // write the value to a file
203                 int save_file(const char* path, int flags = 0) const {
204                         return json_dump_file(as_json(), path, flags);
205                 }
206
207                 // write the value to a string (caller must deallocate with free()!)
208                 char* save_string(int flags = 0) const {
209                         return json_dumps(as_json(), flags);
210                 }
211         };
212
213         // iterators over a JSON object
214         class Iterator {
215         public:
216                 // construct a new iterator for a given object
217                 Iterator(const Value& value) : _object(value), _iter(0) {
218                         _iter = json_object_iter(_object.as_json());
219                 }
220
221                 // increment iterator
222                 void next() {
223                         _iter = json_object_iter_next(_object.as_json(), _iter);
224                 }
225
226                 Iterator& operator++() { next(); return *this; }
227
228                 // test if iterator is still valid
229                 bool valid() const { return _iter != 0; }
230                 operator bool() const { return valid(); }
231
232                 // get key
233                 const char* ckey() const {
234                         return json_object_iter_key(_iter);
235                 }
236
237                 std::string key() const { return ckey(); }
238
239                 // get value
240                 const Value value() const {
241                         return Value(json_object_iter_value(_iter));
242                 }
243
244                 // dereference value
245                 const Value operator*() const { return value(); }
246
247         private:
248                 // disallow copying
249                 Iterator(const Iterator&);
250                 Iterator& operator=(const Iterator&);
251
252                 // object being iterated over
253                 Value _object;
254
255                 // iterator value
256                 void* _iter;
257         };
258
259 } // namespace jansson
260
261 // stream JSON value out
262 std::ostream& operator<<(std::ostream& os, const jansson::Value& value) {
263         char* tmp = value.save_string();
264         if (tmp != 0) {
265                 os << tmp;
266                 free(tmp);
267         }
268         return os;
269 }
270
271 // read JSON value
272 std::istream& operator>>(std::istream& is, jansson::Value& value) {
273         std::stringstream tmp;
274         while (is)
275                 tmp << static_cast<char>(is.get());
276         value = jansson::Value::load_string(tmp.str().c_str());
277         return is;
278 }
279
280 #include "janssonxx.tcc"
281
282 #endif