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)
341 : _object(object), _key(0) {
342 _iter = json_object_iter_at(object, key);
345 json_incref(_object);
348 PropertyProxy::PropertyProxy(const PropertyProxy& other)
349 : _object(other._object), _iter(other._iter), _key(0) {
351 _key = strdup(other._key);
352 json_incref(_object);
355 PropertyProxy::~PropertyProxy() {
357 json_decref(_object);
360 // assign value to proxied object property
361 PropertyProxy& PropertyProxy::operator=(const Value& value) {
363 json_object_iter_set(_object, _iter, value.as_json());
365 json_object_set(_object, _key, value.as_json());
369 json_t* PropertyProxy::as_json() const {
371 return json_object_iter_value(_iter);
373 return json_object_get(_object, _key);
376 } // namespace json::detail
378 // construct Value::Value input
379 Value::Value(const char* value) {
380 _value = json_string(value);
383 Value::Value(const std::string& value) {
384 _value = json_string(value.c_str());
387 Value::Value(bool value) {
388 _value = value ? json_true() : json_false();
391 Value::Value(signed int value) {
392 _value = json_integer(value);
395 Value::Value(unsigned int value) {
396 _value = json_integer(value);
399 Value::Value(signed short value) {
400 _value = json_integer(value);
403 Value::Value(unsigned short value) {
404 _value = json_integer(value);
407 Value::Value(signed long value) {
408 _value = json_integer(value);
411 Value::Value(unsigned long value) {
412 _value = json_integer(value);
415 Value::Value(float value) {
416 _value = json_real(value);
419 Value::Value(double value) {
420 _value = json_real(value);
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());
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());
434 // increment iterator
435 void Iterator::next() {
436 _iter = json_object_iter_next(_object.as_json(), _iter);
439 Iterator& Iterator::operator++() { next(); return *this; }
441 // test if iterator is still valid
442 bool Iterator::valid() const {
446 Iterator::operator bool() const {
451 const char* Iterator::ckey() const {
452 return json_object_iter_key(_iter);
455 std::string Iterator::key() const {
460 const Value Iterator::value() const {
461 return Value(json_object_iter_value(_iter));
465 const Value Iterator::operator*() const {
469 // create a new empty object
471 return Value::take_ownership(json_object());
474 // create a new empty array
476 return Value::take_ownership(json_array());
479 // create a new null value
481 return Value::take_ownership(json_null());
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));
489 Value load_file(const std::string& path, json_error_t* error) {
490 return load_file(path.c_str(), error);
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));
498 Value loads(const std::string& string, json_error_t* error) {
499 return loads(string.c_str(), error);
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();
509 // stream temp string out and release it
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;
521 tmp << static_cast<char>(is.get());
522 // parse the buffered string
523 value = json::loads(tmp.str().c_str());