1 // Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
2 // Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
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.
8 #error "jansson.ipp may only be included from jansson.hpp"
15 // assignment operator
16 template <typename _Base>
17 ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
18 _Base::operator=(value);
23 template <typename _Base>
24 bool ValueBase<_Base>::is_undefined() const {
25 return _Base::as_json() == 0;
28 template <typename _Base>
29 bool ValueBase<_Base>::is_object() const {
30 return json_is_object(_Base::as_json());
33 template <typename _Base>
34 bool ValueBase<_Base>::is_array() const {
35 return json_is_array(_Base::as_json());
38 template <typename _Base>
39 bool ValueBase<_Base>::is_string() const {
40 return json_is_string(_Base::as_json());
43 template <typename _Base>
44 bool ValueBase<_Base>::is_integer() const {
45 return json_is_integer(_Base::as_json());
48 template <typename _Base>
49 bool ValueBase<_Base>::is_real() const {
50 return json_is_real(_Base::as_json());
53 template <typename _Base>
54 bool ValueBase<_Base>::is_number() const {
55 return json_is_number(_Base::as_json());
58 template <typename _Base>
59 bool ValueBase<_Base>::is_true() const {
60 return json_is_true(_Base::as_json());
63 template <typename _Base>
64 bool ValueBase<_Base>::is_false() const {
65 return json_is_false(_Base::as_json());
68 template <typename _Base>
69 bool ValueBase<_Base>::is_boolean() const {
70 return json_is_boolean(_Base::as_json());
73 template <typename _Base>
74 bool ValueBase<_Base>::is_null() const {
75 return json_is_null(_Base::as_json());
78 // get size of array or object
79 template <typename _Base>
80 unsigned int ValueBase<_Base>::size() const {
82 return json_object_size(_Base::as_json());
84 return json_array_size(_Base::as_json());
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));
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); }
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);
112 template <typename _Base>
113 ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
117 template <typename _Base>
118 ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
122 template <typename _Base>
123 ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
127 template <typename _Base>
128 ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
132 template <typename _Base>
133 ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
137 template <typename _Base>
138 ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
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));
148 template <typename _Base>
149 const Value ValueBase<_Base>::get(const std::string& key) const {
150 return get(key.c_str());
153 template <typename _Base>
154 const Value ValueBase<_Base>::operator[](const char* key) const {
158 template <typename _Base>
159 const Value ValueBase<_Base>::operator[](const std::string& key) const {
160 return get(key.c_str());
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);
169 template <typename _Base>
170 ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
171 return get(key.c_str());
174 template <typename _Base>
175 ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
179 template <typename _Base>
180 ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
181 return get(key.c_str());
184 // clear all array/object values
185 template <typename _Base>
186 void ValueBase<_Base>::clear() {
188 json_object_clear(_Base::as_json());
190 json_array_clear(_Base::as_json());
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());
199 template <typename _Base>
200 std::string ValueBase<_Base>::as_string() const {
201 const char* tmp = as_cstring();
202 return tmp == 0 ? "" : tmp;
205 template <typename _Base>
206 int ValueBase<_Base>::as_integer() const {
207 return json_integer_value(_Base::as_json());
210 template <typename _Base>
211 double ValueBase<_Base>::as_real() const {
212 return json_real_value(_Base::as_json());
215 template <typename _Base>
216 double ValueBase<_Base>::as_number() const {
217 return json_number_value(_Base::as_json());
220 template <typename _Base>
221 bool ValueBase<_Base>::as_boolean() const {
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());
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);
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) {
241 json_array_append(_Base::as_json(), value._Base::as_json());
243 json_array_set(_Base::as_json(), index, value._Base::as_json());
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);
254 template <typename _Base>
255 _Base& ValueBase<_Base>::del_key(const std::string& key) {
256 return del_key(key.c_str());
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);
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());
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);
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);
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);
294 // copy an existing Value
295 Basic& Basic::operator=(const Basic& e) {
298 _value = json_incref(e._value);
303 // get the underlying json_t
304 json_t* Basic::as_json() const {
308 // take ownership of a json_t (does not increase reference count)
309 Basic Basic::take_ownership(json_t* json) {
315 ElementProxy::ElementProxy(json_t* array, unsigned int index)
316 : _array(array), _index(index) {
320 ElementProxy::ElementProxy(const ElementProxy& other)
321 : _array(other._array), _index(other._index) {
325 ElementProxy::~ElementProxy() {
329 // assign value to proxied array element
330 ElementProxy& ElementProxy::operator=(const Value& value) {
331 json_array_set(_array, _index, value.as_json());
335 // get the proxied element
336 json_t* ElementProxy::as_json() const {
337 return json_array_get(_array, _index);
340 PropertyProxy::PropertyProxy(json_t* object, const char* key)
343 json_incref(_object);
346 PropertyProxy::PropertyProxy(const PropertyProxy& other)
347 : _object(other._object) {
348 _key = strdup(other._key);
349 json_incref(_object);
352 PropertyProxy::~PropertyProxy() {
354 json_decref(_object);
357 // assign value to proxied object property
358 PropertyProxy& PropertyProxy::operator=(const Value& value) {
359 json_object_set(_object, _key, value.as_json());
363 json_t* PropertyProxy::as_json() const {
364 return json_object_get(_object, _key);
367 } // namespace json::detail
369 // construct Value::Value input
370 Value::Value(const char* value) {
371 _value = json_string(value);
374 Value::Value(const std::string& value) {
375 _value = json_string(value.c_str());
378 Value::Value(bool value) {
379 _value = value ? json_true() : json_false();
382 Value::Value(signed int value) {
383 _value = json_integer(value);
386 Value::Value(unsigned int value) {
387 _value = json_integer(value);
390 Value::Value(signed short value) {
391 _value = json_integer(value);
394 Value::Value(unsigned short value) {
395 _value = json_integer(value);
398 Value::Value(signed long value) {
399 _value = json_integer(value);
402 Value::Value(unsigned long value) {
403 _value = json_integer(value);
406 Value::Value(float value) {
407 _value = json_real(value);
410 Value::Value(double value) {
411 _value = json_real(value);
414 // construct a new iterator for a given object
415 Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
416 _iter = json_object_iter(_object.as_json());
419 // construct a new iterator for a given object
420 Iterator::Iterator(const detail::ValueBase<detail::PropertyProxy>& value) :
421 _object(value.as_json()), _iter(0) {
422 _iter = json_object_iter(_object.as_json());
425 // increment iterator
426 void Iterator::next() {
427 _iter = json_object_iter_next(_object.as_json(), _iter);
430 Iterator& Iterator::operator++() { next(); return *this; }
432 // test if iterator is still valid
433 bool Iterator::valid() const {
437 Iterator::operator bool() const {
442 const char* Iterator::ckey() const {
443 return json_object_iter_key(_iter);
446 std::string Iterator::key() const {
451 const Value Iterator::value() const {
452 return Value(json_object_iter_value(_iter));
456 const Value Iterator::operator*() const {
460 // create a new empty object
462 return Value::take_ownership(json_object());
465 // create a new empty array
467 return Value::take_ownership(json_array());
470 // create a new null value
472 return Value::take_ownership(json_null());
475 // load a file as a JSON value
476 Value load_file(const char* path, json_error_t* error) {
477 return Value::take_ownership(json_load_file(path, error));
480 Value load_file(const std::string& path, json_error_t* error) {
481 return load_file(path.c_str(), error);
484 // load a string as a JSON value
485 Value loads(const char* string, json_error_t* error) {
486 return Value::take_ownership(json_loads(string, error));
489 Value loads(const std::string& string, json_error_t* error) {
490 return loads(string.c_str(), error);
495 // stream JSON value out
496 std::ostream& operator<<(std::ostream& os, const json::Value& value) {
497 // get the temporary serialize string
498 char* tmp = value.dumps();
500 // stream temp string out and release it
508 std::istream& operator>>(std::istream& is, json::Value& value) {
509 // buffer the remaining bytes into a single string for Jansson
510 std::stringstream tmp;
512 tmp << static_cast<char>(is.get());
513 // parse the buffered string
514 value = json::loads(tmp.str().c_str());