C++: Optimize PropertyProxy
[jansson.git] / src / jansson.ipp
1 // Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
2 // Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
3 //
4 // Jansson is free software; you can redistribute it and/or modify
5 // it under the terms of the MIT license. See LICENSE for details.
6
7 #ifndef IN_JANSSON_HPP
8 #error "jansson.ipp may only be included from jansson.hpp"
9 #endif
10
11 #include <string.h>
12
13 namespace json {
14     namespace detail {
15         // assignment operator
16         template <typename _Base>
17         ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
18             _Base::operator=(value);
19             return *this;
20         }
21
22         // check value type
23         template <typename _Base>
24         bool ValueBase<_Base>::is_undefined() const {
25             return _Base::as_json() == 0;
26         }
27
28         template <typename _Base>
29         bool ValueBase<_Base>::is_object() const {
30             return json_is_object(_Base::as_json());
31         }
32
33         template <typename _Base>
34         bool ValueBase<_Base>::is_array() const {
35             return json_is_array(_Base::as_json());
36         }
37
38         template <typename _Base>
39         bool ValueBase<_Base>::is_string() const {
40             return json_is_string(_Base::as_json());
41         }
42
43         template <typename _Base>
44         bool ValueBase<_Base>::is_integer() const {
45             return json_is_integer(_Base::as_json());
46         }
47
48         template <typename _Base>
49         bool ValueBase<_Base>::is_real() const {
50             return json_is_real(_Base::as_json());
51         }
52
53         template <typename _Base>
54         bool ValueBase<_Base>::is_number() const {
55             return json_is_number(_Base::as_json());
56         }
57
58         template <typename _Base>
59         bool ValueBase<_Base>::is_true() const {
60             return json_is_true(_Base::as_json());
61         }
62
63         template <typename _Base>
64         bool ValueBase<_Base>::is_false() const {
65             return json_is_false(_Base::as_json());
66         }
67
68         template <typename _Base>
69         bool ValueBase<_Base>::is_boolean() const {
70             return json_is_boolean(_Base::as_json());
71         }
72
73         template <typename _Base>
74         bool ValueBase<_Base>::is_null() const {
75             return json_is_null(_Base::as_json());
76         }
77
78         // get size of array or object
79         template <typename _Base>
80         unsigned int ValueBase<_Base>::size() const {
81             if (is_object())
82                 return json_object_size(_Base::as_json());
83             else
84                 return json_array_size(_Base::as_json());
85         }
86
87         // get value at array index (const version)
88         template <typename _Base>
89         const Value ValueBase<_Base>::at(unsigned int index) const {
90             return Value(json_array_get(_Base::as_json(), index));
91         }
92
93         template <typename _Base>
94         const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
95         template <typename _Base>
96         const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
97         template <typename _Base>
98         const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
99         template <typename _Base>
100         const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
101         template <typename _Base>
102         const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
103         template <typename _Base>
104         const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
105
106         // get value at array index (non-const version)
107         template <typename _Base>
108         ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
109             return ElementProxy(_Base::as_json(), index);
110         }
111
112         template <typename _Base>
113         ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
114             return at(index);
115         }
116
117         template <typename _Base>
118         ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
119             return at(index);
120         }
121
122         template <typename _Base>
123         ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
124             return at(index);
125         }
126
127         template <typename _Base>
128         ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
129             return at(index);
130         }
131
132         template <typename _Base>
133         ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
134             return at(index);
135         }
136
137         template <typename _Base>
138         ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
139             return at(index);
140         }
141
142         // get object property (const version)
143         template <typename _Base>
144         const Value ValueBase<_Base>::get(const char* key) const {
145             return Value(json_object_get(_Base::as_json(), key));
146         }
147
148         template <typename _Base>
149         const Value ValueBase<_Base>::get(const std::string& key) const {
150             return get(key.c_str());
151         }
152
153         template <typename _Base>
154         const Value ValueBase<_Base>::operator[](const char* key) const {
155             return get(key);
156         }
157
158         template <typename _Base>
159         const Value ValueBase<_Base>::operator[](const std::string& key) const {
160             return get(key.c_str());
161         }
162
163         // get object property (non-const version)
164         template <typename _Base>
165         ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
166             return PropertyProxy(_Base::as_json(), key);
167         }
168
169         template <typename _Base>
170         ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
171             return get(key.c_str());
172         }
173
174         template <typename _Base>
175         ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
176             return get(key);
177         }
178
179         template <typename _Base>
180         ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
181             return get(key.c_str());
182         }
183
184         // clear all array/object values
185         template <typename _Base>
186         void ValueBase<_Base>::clear() {
187             if (is_object())
188                 json_object_clear(_Base::as_json());
189             else
190                 json_array_clear(_Base::as_json());
191         }
192
193         // get value cast to specified type
194         template <typename _Base>
195         const char* ValueBase<_Base>::as_cstring() const {
196             return json_string_value(_Base::as_json());
197         }
198
199         template <typename _Base>
200         std::string ValueBase<_Base>::as_string() const {
201             const char* tmp = as_cstring();
202             return tmp == 0 ? "" : tmp;
203         }
204
205         template <typename _Base>
206         int ValueBase<_Base>::as_integer() const {
207             return json_integer_value(_Base::as_json());
208         }
209
210         template <typename _Base>
211         double ValueBase<_Base>::as_real() const {
212             return json_real_value(_Base::as_json());
213         }
214
215         template <typename _Base>
216         double ValueBase<_Base>::as_number() const {
217             return json_number_value(_Base::as_json());
218         }
219
220         template <typename _Base>
221         bool ValueBase<_Base>::as_boolean() const {
222             return is_true();
223         }
224
225         // set an object property (converts value to object is not one already)
226         template <typename _Base>
227         _Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
228             json_object_set(_Base::as_json(), key, value._Base::as_json());
229             return *this;
230         }
231
232         template <typename _Base>
233         _Base& ValueBase<_Base>::set_key(const std::string& key, const Value& value) {
234             return set_key(key.c_str(), value);
235         }
236
237         // set an array index (converts value to object is not one already)
238         template <typename _Base>
239         _Base& ValueBase<_Base>::set_at(unsigned int index, const Value& value) {
240             if (index == size())
241                 json_array_append(_Base::as_json(), value._Base::as_json());
242             else
243                 json_array_set(_Base::as_json(), index, value._Base::as_json());
244             return *this;
245         }
246
247         // delete an object key
248         template <typename _Base>
249         _Base& ValueBase<_Base>::del_key(const char* key) {
250             json_object_del(_Base::as_json(), key);
251             return *this;
252         }
253
254         template <typename _Base>
255         _Base& ValueBase<_Base>::del_key(const std::string& key) {
256             return del_key(key.c_str());
257         }
258
259         // delete an item from an array by index
260         template <typename _Base>
261         _Base& ValueBase<_Base>::del_at(unsigned int index) {
262             json_array_remove(_Base::as_json(), index);
263             return *this;
264         }
265
266         // insert an item into an array at a given index
267         template <typename _Base>
268         _Base& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
269             json_array_insert(_Base::as_json(), index, value._Base::as_json());
270             return *this;
271         }
272
273         // write the value to a file
274         template <typename _Base>
275         int ValueBase<_Base>::dump_file(const char* path, int flags) const {
276             return json_dump_file(_Base::as_json(), path, flags);
277         }
278
279         template <typename _Base>
280         int ValueBase<_Base>::dump_file(const std::string& path, int flags) const {
281             return dump_file(path.c_str(), flags);
282         }
283
284         // write the value to a string (caller must deallocate with free()!)
285         template <typename _Base>
286         char* ValueBase<_Base>::dumps(int flags) const {
287             return json_dumps(_Base::as_json(), flags);
288         }
289
290         Basic::~Basic() {
291             json_decref(_value);
292         }
293
294         // copy an existing Value
295         Basic& Basic::operator=(const Basic& e) {
296             if (&e != this) {
297                 json_decref(_value);
298                 _value = json_incref(e._value);
299             }
300             return *this;
301         }
302
303         // get the underlying json_t
304         json_t* Basic::as_json() const {
305             return _value;
306         }
307
308         // take ownership of a json_t (does not increase reference count)
309         Basic Basic::take_ownership(json_t* json) {
310             Basic v;
311             v._value = json;
312             return v;
313         }
314
315         ElementProxy::ElementProxy(json_t* array, unsigned int index)
316             : _array(array), _index(index) {
317             json_incref(_array);
318         }
319
320         ElementProxy::ElementProxy(const ElementProxy& other) 
321             : _array(other._array), _index(other._index) {
322             json_incref(_array);
323         }
324
325         ElementProxy::~ElementProxy() {
326             json_decref(_array);
327         }
328
329         // assign value to proxied array element
330         ElementProxy& ElementProxy::operator=(const Value& value) {
331             json_array_set(_array, _index, value.as_json());
332             return *this;
333         }
334
335         // get the proxied element
336         json_t* ElementProxy::as_json() const {
337             return json_array_get(_array, _index);
338         }
339
340         PropertyProxy::PropertyProxy(json_t* object, const char* key)
341             : _object(object), _key(0) {
342             _iter = json_object_iter_at(object, key);
343             if(!_iter)
344                 _key = strdup(key);
345             json_incref(_object);
346         }
347
348         PropertyProxy::PropertyProxy(const PropertyProxy& other)
349             : _object(other._object), _iter(other._iter), _key(0) {
350             if(other._key)
351                 _key = strdup(other._key);
352             json_incref(_object);
353         }
354
355         PropertyProxy::~PropertyProxy() {
356             free(_key);
357             json_decref(_object);
358         }
359
360         // assign value to proxied object property
361         PropertyProxy& PropertyProxy::operator=(const Value& value) {
362             if(_iter)
363                 json_object_iter_set(_object, _iter, value.as_json());
364             else
365                 json_object_set(_object, _key, value.as_json());
366             return *this;
367         }
368
369         json_t* PropertyProxy::as_json() const {
370             if(_iter)
371                 return json_object_iter_value(_iter);
372             else
373                 return json_object_get(_object, _key);
374         }
375
376     } // namespace json::detail
377
378     // construct Value::Value input
379     Value::Value(const char* value) {
380         _value = json_string(value);
381     }
382
383     Value::Value(const std::string& value) {
384         _value = json_string(value.c_str());
385     }
386
387     Value::Value(bool value) {
388         _value = value ? json_true() : json_false();
389     }
390
391     Value::Value(signed int value) {
392         _value = json_integer(value);
393     }
394
395     Value::Value(unsigned int value) {
396         _value = json_integer(value);
397     }
398
399     Value::Value(signed short value) {
400         _value = json_integer(value);
401     }
402
403     Value::Value(unsigned short value) {
404         _value = json_integer(value);
405     }
406
407     Value::Value(signed long value) {
408         _value = json_integer(value);
409     }
410
411     Value::Value(unsigned long value) {
412         _value = json_integer(value);
413     }
414
415     Value::Value(float value) {
416         _value = json_real(value);
417     }
418
419     Value::Value(double value) {
420         _value = json_real(value);
421     }
422
423     // construct a new iterator for a given object
424     Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
425         _iter = json_object_iter(_object.as_json());
426     }
427
428     // construct a new iterator for a given object
429     Iterator::Iterator(const detail::ValueBase<detail::PropertyProxy>& value) :
430         _object(value.as_json()), _iter(0) {
431         _iter = json_object_iter(_object.as_json());
432     }
433
434     // increment iterator
435     void Iterator::next() {
436         _iter = json_object_iter_next(_object.as_json(), _iter);
437     }
438
439     Iterator& Iterator::operator++() { next(); return *this; }
440
441     // test if iterator is still valid
442     bool Iterator::valid() const {
443         return _iter != 0;
444     }
445
446     Iterator::operator bool() const {
447         return valid();
448     }
449
450     // get key
451     const char* Iterator::ckey() const {
452         return json_object_iter_key(_iter);
453     }
454
455     std::string Iterator::key() const {
456         return ckey();
457     }
458
459     // get value
460     const Value Iterator::value() const {
461         return Value(json_object_iter_value(_iter));
462     }
463
464     // dereference value
465     const Value Iterator::operator*() const {
466         return value();
467     }
468
469     // create a new empty object
470     Value object() {
471         return Value::take_ownership(json_object());
472     }
473
474     // create a new empty array
475     Value array() {
476         return Value::take_ownership(json_array());
477     }
478
479     // create a new null value
480     Value null() {
481         return Value::take_ownership(json_null());
482     }
483
484     // load a file as a JSON value
485     Value load_file(const char* path, json_error_t* error) {
486         return Value::take_ownership(json_load_file(path, error));
487     }
488
489     Value load_file(const std::string& path, json_error_t* error) {
490         return load_file(path.c_str(), error);
491     }
492
493     // load a string as a JSON value
494     Value loads(const char* string, json_error_t* error) {
495         return Value::take_ownership(json_loads(string, error));
496     }
497
498     Value loads(const std::string& string, json_error_t* error) {
499         return loads(string.c_str(), error);
500     }
501
502 } // namespace json
503
504 // stream JSON value out
505 std::ostream& operator<<(std::ostream& os, const json::Value& value) {
506     // get the temporary serialize string
507     char* tmp = value.dumps();
508     if (tmp != 0) {
509         // stream temp string out and release it
510         os << tmp;
511         free(tmp);
512     }
513     return os;
514 }
515
516 // read JSON value
517 std::istream& operator>>(std::istream& is, json::Value& value) {
518     // buffer the remaining bytes into a single string for Jansson
519     std::stringstream tmp;
520     while (is)
521         tmp << static_cast<char>(is.get());
522     // parse the buffered string
523     value = json::loads(tmp.str().c_str());
524     return is;
525 }