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