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"
13 // assignment operator
14 template <typename _Base>
15 ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
16 _Base::operator=(value);
21 template <typename _Base>
22 bool ValueBase<_Base>::is_undefined() const {
23 return _Base::as_json() == 0;
26 template <typename _Base>
27 bool ValueBase<_Base>::is_object() const {
28 return json_is_object(_Base::as_json());
31 template <typename _Base>
32 bool ValueBase<_Base>::is_array() const {
33 return json_is_array(_Base::as_json());
36 template <typename _Base>
37 bool ValueBase<_Base>::is_string() const {
38 return json_is_string(_Base::as_json());
41 template <typename _Base>
42 bool ValueBase<_Base>::is_integer() const {
43 return json_is_integer(_Base::as_json());
46 template <typename _Base>
47 bool ValueBase<_Base>::is_real() const {
48 return json_is_real(_Base::as_json());
51 template <typename _Base>
52 bool ValueBase<_Base>::is_number() const {
53 return json_is_number(_Base::as_json());
56 template <typename _Base>
57 bool ValueBase<_Base>::is_true() const {
58 return json_is_true(_Base::as_json());
61 template <typename _Base>
62 bool ValueBase<_Base>::is_false() const {
63 return json_is_false(_Base::as_json());
66 template <typename _Base>
67 bool ValueBase<_Base>::is_boolean() const {
68 return json_is_boolean(_Base::as_json());
71 template <typename _Base>
72 bool ValueBase<_Base>::is_null() const {
73 return json_is_null(_Base::as_json());
76 // get size of array or object
77 template <typename _Base>
78 unsigned int ValueBase<_Base>::size() const {
80 return json_object_size(_Base::as_json());
82 return json_array_size(_Base::as_json());
85 // get value at array index (const version)
86 template <typename _Base>
87 const Value ValueBase<_Base>::at(unsigned int index) const {
88 return Value(json_array_get(_Base::as_json(), index));
91 template <typename _Base>
92 const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
93 template <typename _Base>
94 const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
95 template <typename _Base>
96 const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
97 template <typename _Base>
98 const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
99 template <typename _Base>
100 const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
101 template <typename _Base>
102 const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
104 // get value at array index (non-const version)
105 template <typename _Base>
106 ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
107 return ElementProxy(_Base::as_json(), index);
110 template <typename _Base>
111 ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
115 template <typename _Base>
116 ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
120 template <typename _Base>
121 ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
125 template <typename _Base>
126 ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
130 template <typename _Base>
131 ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
135 template <typename _Base>
136 ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
140 // get object property (const version)
141 template <typename _Base>
142 const Value ValueBase<_Base>::get(const char* key) const {
143 return Value(json_object_get(_Base::as_json(), key));
146 template <typename _Base>
147 const Value ValueBase<_Base>::get(const std::string& key) const {
148 return get(key.c_str());
151 template <typename _Base>
152 const Value ValueBase<_Base>::operator[](const char* key) const {
156 template <typename _Base>
157 const Value ValueBase<_Base>::operator[](const std::string& key) const {
158 return get(key.c_str());
161 // get object property (non-const version)
162 template <typename _Base>
163 ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
164 return PropertyProxy(_Base::as_json(), key);
167 template <typename _Base>
168 ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
169 return get(key.c_str());
172 template <typename _Base>
173 ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
177 template <typename _Base>
178 ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
179 return get(key.c_str());
182 // clear all array/object values
183 template <typename _Base>
184 void ValueBase<_Base>::clear() {
186 json_object_clear(_Base::as_json());
188 json_array_clear(_Base::as_json());
191 // get value cast to specified type
192 template <typename _Base>
193 const char* ValueBase<_Base>::as_cstring() const {
194 return json_string_value(_Base::as_json());
197 template <typename _Base>
198 std::string ValueBase<_Base>::as_string() const {
199 const char* tmp = as_cstring();
200 return tmp == 0 ? "" : tmp;
203 template <typename _Base>
204 int ValueBase<_Base>::as_integer() const {
205 return json_integer_value(_Base::as_json());
208 template <typename _Base>
209 double ValueBase<_Base>::as_real() const {
210 return json_real_value(_Base::as_json());
213 template <typename _Base>
214 double ValueBase<_Base>::as_number() const {
215 return json_number_value(_Base::as_json());
218 template <typename _Base>
219 bool ValueBase<_Base>::as_boolean() const {
223 // set an object property (converts value to object is not one already)
224 template <typename _Base>
225 _Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
226 json_object_set(_Base::as_json(), key, value._Base::as_json());
230 template <typename _Base>
231 _Base& ValueBase<_Base>::set_key(const std::string& key, const Value& value) {
232 return set_key(key.c_str(), value);
235 // set an array index (converts value to object is not one already)
236 template <typename _Base>
237 _Base& ValueBase<_Base>::set_at(unsigned int index, const Value& value) {
239 json_array_append(_Base::as_json(), value._Base::as_json());
241 json_array_set(_Base::as_json(), index, value._Base::as_json());
245 // delete an object key
246 template <typename _Base>
247 _Base& ValueBase<_Base>::del_key(const char* key) {
248 json_object_del(_Base::as_json(), key);
252 template <typename _Base>
253 _Base& ValueBase<_Base>::del_key(const std::string& key) {
254 return del_key(key.c_str());
257 // delete an item from an array by index
258 template <typename _Base>
259 _Base& ValueBase<_Base>::del_at(unsigned int index) {
260 json_array_remove(_Base::as_json(), index);
264 // insert an item into an array at a given index
265 template <typename _Base>
266 _Base& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
267 json_array_insert(_Base::as_json(), index, value._Base::as_json());
271 // write the value to a file
272 template <typename _Base>
273 int ValueBase<_Base>::save_file(const char* path, int flags) const {
274 return json_dump_file(_Base::as_json(), path, flags);
277 // write the value to a string (caller must deallocate with free()!)
278 template <typename _Base>
279 char* ValueBase<_Base>::save_string(int flags) const {
280 return json_dumps(_Base::as_json(), flags);
287 // copy an existing Value
288 Basic& Basic::operator=(const Basic& e) {
291 _value = json_incref(e._value);
296 // get the underlying json_t
297 json_t* Basic::as_json() const {
301 // take ownership of a json_t (does not increase reference count)
302 Basic Basic::take_ownership(json_t* json) {
308 // assign value to proxied array element
309 ElementProxy& ElementProxy::operator=(const Value& value) {
310 json_array_set(_array, _index, value.as_json());
314 // get the proxied element
315 json_t* ElementProxy::as_json() const {
316 return json_array_get(_array, _index);
319 // assign value to proxied object property
320 PropertyProxy& PropertyProxy::operator=(const Value& value) {
321 json_object_set(_object, _key, value.as_json());
325 json_t* PropertyProxy::as_json() const {
326 return json_object_get(_object, _key);
329 } // namespace json::detail
331 // construct Value::Value input
332 Value::Value(const char* value) {
333 _value = json_string(value);
336 Value::Value(const std::string& value) {
337 _value = json_string(value.c_str());
340 Value::Value(bool value) {
341 _value = value ? json_true() : json_false();
344 Value::Value(signed int value) {
345 _value = json_integer(value);
348 Value::Value(unsigned int value) {
349 _value = json_integer(value);
352 Value::Value(signed short value) {
353 _value = json_integer(value);
356 Value::Value(unsigned short value) {
357 _value = json_integer(value);
360 Value::Value(signed long value) {
361 _value = json_integer(value);
364 Value::Value(unsigned long value) {
365 _value = json_integer(value);
368 Value::Value(float value) {
369 _value = json_real(value);
372 Value::Value(double value) {
373 _value = json_real(value);
376 // construct a new iterator for a given object
377 Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
378 _iter = json_object_iter(_object.as_json());
381 // construct a new iterator for a given object
382 Iterator::Iterator(const detail::ValueBase<detail::PropertyProxy>& value) :
383 _object(value.as_json()), _iter(0) {
384 _iter = json_object_iter(_object.as_json());
387 // increment iterator
388 void Iterator::next() {
389 _iter = json_object_iter_next(_object.as_json(), _iter);
392 Iterator& Iterator::operator++() { next(); return *this; }
394 // test if iterator is still valid
395 bool Iterator::valid() const {
399 Iterator::operator bool() const {
404 const char* Iterator::ckey() const {
405 return json_object_iter_key(_iter);
408 std::string Iterator::key() const {
413 const Value Iterator::value() const {
414 return Value(json_object_iter_value(_iter));
418 const Value Iterator::operator*() const {
422 // create a new empty object
424 return Value::take_ownership(json_object());
427 // create a new empty array
429 return Value::take_ownership(json_array());
432 // create a new null value
434 return Value::take_ownership(json_null());
437 // load a file as a JSON value
438 Value load_file(const char* path, json_error_t* error) {
439 return Value::take_ownership(json_load_file(path, error));
442 // load a string as a JSON value
443 Value load_string(const char* string, json_error_t* error) {
444 return Value::take_ownership(json_loads(string, error));
449 // stream JSON value out
450 std::ostream& operator<<(std::ostream& os, const json::Value& value) {
451 // get the temporary serialize string
452 char* tmp = value.save_string();
454 // stream temp string out and release it
462 std::istream& operator>>(std::istream& is, json::Value& value) {
463 // buffer the remaining bytes into a single string for Jansson
464 std::stringstream tmp;
466 tmp << static_cast<char>(is.get());
467 // parse the buffered string
468 value = json::load_string(tmp.str().c_str());