1 // janssonxx - C++ wrapper for jansson
3 // author: Sean Middleditch <sean@middleditch.us>
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.
9 #if !defined(JANSSONXX_H)
19 // include in the jansson namespace
29 // base class for JSON value interface
30 template <typename _Base>
31 class ValueBase : public _Base {
34 ValueBase() : _Base() {}
37 ValueBase(const _Base& base) : _Base(base) {}
39 // create reference to value
40 ValueBase(json_t* json) : _Base(json) {}
42 // assignment operator
43 ValueBase& operator=(const Value& value) { _Base::operator=(value); return *this; }
46 bool is_undefined() const { return _Base::as_json() == 0; }
47 bool is_object() const { return json_is_object(_Base::as_json()); }
48 bool is_array() const { return json_is_array(_Base::as_json()); }
49 bool is_string() const { return json_is_string(_Base::as_json()); }
50 bool is_integer() const { return json_is_integer(_Base::as_json()); }
51 bool is_real() const { return json_is_real(_Base::as_json()); }
52 bool is_number() const { return json_is_number(_Base::as_json()); }
53 bool is_true() const { return json_is_true(_Base::as_json()); }
54 bool is_false() const { return json_is_false(_Base::as_json()); }
55 bool is_boolean() const { return json_is_boolean(_Base::as_json()); }
56 bool is_null() const { return json_is_null(_Base::as_json()); }
58 // get size of array or object
59 inline unsigned int size() const;
61 // get value at array index (const version)
62 inline const Value at(unsigned int index) const;
64 inline const Value operator[](signed int index) const;
65 inline const Value operator[](unsigned int index) const;
66 inline const Value operator[](signed short index) const;
67 inline const Value operator[](unsigned short index) const;
68 inline const Value operator[](signed long index) const;
69 inline const Value operator[](unsigned long index) const;
71 // get value at array index (non-const version)
72 inline ValueBase<ArrayProxy> at(unsigned int index);
74 inline ValueBase<ArrayProxy> operator[](signed int index);
75 inline ValueBase<ArrayProxy> operator[](unsigned int index);
76 inline ValueBase<ArrayProxy> operator[](signed short index);
77 inline ValueBase<ArrayProxy> operator[](unsigned short index);
78 inline ValueBase<ArrayProxy> operator[](signed long index);
79 inline ValueBase<ArrayProxy> operator[](unsigned long index);
81 // get object property (const version)
82 inline const Value get(const char* key) const;
84 inline const Value get(const std::string& key) const;
85 inline const Value operator[](const char* key) const;
86 inline const Value operator[](const std::string& key) const;
88 // get object property (non-const version)
89 inline ValueBase<ObjectProxy> get(const char* key);
91 inline ValueBase<ObjectProxy> get(const std::string& key);
92 inline ValueBase<ObjectProxy> operator[](const char* key);
93 inline ValueBase<ObjectProxy> operator[](const std::string& key);
95 // clear all array/object values
98 // get value cast to specified type
99 inline const char* as_cstring() const;
100 inline std::string as_string() const;
101 inline int as_integer() const;
102 inline double as_real() const;
103 inline double as_number() const;
104 inline bool as_boolean() const;
106 // set an object property (converts value to object is not one already)
107 inline _Base& set_key(const char* key, const Value& value);
109 inline _Base& set_key(const std::string& key, const Value& value);
111 // set an array index (converts value to object is not one already)
112 inline _Base& set_at(unsigned int index, const Value& value);
114 // delete an object key
115 inline _Base& del_key(const char* key);
117 inline _Base& del_key(const std::string& key);
119 // delete an item from an array by index
120 inline _Base& del_at(unsigned int index);
122 // insert an item into an array at a given index
123 inline _Base& insert_at(unsigned int index, const Value& value);
126 // represents any JSON value, private base
129 // construct new Value with an undefined value
130 Basic() : _value(0) {}
133 Basic(const Basic& value) : _value(json_incref(value._value)) {}
135 // make a reference to an existing json_t value
136 explicit Basic(json_t* value) : _value(json_incref(value)) {}
138 // free Value resources
139 ~Basic() { json_decref(_value); }
141 // copy an existing Value
142 Basic& operator=(const Basic& e) {
145 _value = json_incref(e._value);
150 // get the underlying json_t
151 json_t* as_json() const { return _value; }
154 // take ownership of a json_t (does not increase reference count)
155 static Basic _take(json_t* json) {
162 // internal value pointer
166 // proxies an array element
170 ArrayProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
172 // assign to the proxied element
173 inline ArrayProxy& operator=(const Value& value);
175 // get the proxied element
176 json_t* as_json() const { return json_array_get(_array, _index); }
179 // array object we wrap
186 // proxies an object property
190 ObjectProxy(json_t* array, const char* key) : _object(array), _key(key) {}
192 // assign to the proxied element
193 inline ObjectProxy& operator=(const Value& value);
195 // get the proxied element
196 json_t* as_json() const { return json_object_get(_object, _key); }
199 // array object we wrap
206 } // namespace jansson::_private
208 // represents any JSON value
209 class Value : public _private::ValueBase<_private::Basic> {
212 Value() : _private::ValueBase<_private::Basic>() {}
214 // copy constructor for base
215 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
217 // copy constructor for base
218 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
221 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
223 // create reference to value
224 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
226 // construct Value from input
227 static inline Value from(const char* value) { return Value::_take(json_string(value)); }
228 static inline Value from(const std::string& value) { return from(value.c_str()); }
229 static inline Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
230 static inline Value from(signed int value) { return Value::_take(json_integer(value)); }
231 static inline Value from(unsigned int value) { return Value::_take(json_integer(value)); }
232 static inline Value from(signed short value) { return Value::_take(json_integer(value)); }
233 static inline Value from(unsigned short value) { return Value::_take(json_integer(value)); }
234 static inline Value from(signed long value) { return Value::_take(json_integer(value)); }
235 static inline Value from(unsigned long value) { return Value::_take(json_integer(value)); }
236 static inline Value from(float value) { return Value::_take(json_real(value)); }
237 static inline Value from(double value) { return Value::_take(json_real(value)); }
239 // create a new empty object
240 static inline Value object() { return Value::_take(json_object()); }
242 // create a new empty array
243 static inline Value array() { return Value::_take(json_array()); }
245 // create a new null value
246 static inline Value null() { return Value::_take(json_null()); }
248 // load a file as a JSON value
249 static Value load_file(const char* path, json_error_t* error = 0) {
250 return Value::_take(json_load_file(path, error));
253 // load a string as a JSON value
254 static Value load_string(const char* string, json_error_t* error = 0) {
255 return Value::_take(json_loads(string, error));
258 // write the value to a file
259 int save_file(const char* path, int flags = 0) const {
260 return json_dump_file(as_json(), path, flags);
263 // write the value to a string (caller must deallocate with free()!)
264 char* save_string(int flags = 0) const {
265 return json_dumps(as_json(), flags);
269 // iterators over a JSON object
272 // construct a new iterator for a given object
273 Iterator(const Value& value) : _object(value), _iter(0) {
274 _iter = json_object_iter(_object.as_json());
277 // construct a new iterator for a given object
278 Iterator(const _private::ValueBase<_private::ObjectProxy>& value) : _object(value.as_json()), _iter(0) {
279 _iter = json_object_iter(_object.as_json());
282 // increment iterator
284 _iter = json_object_iter_next(_object.as_json(), _iter);
287 Iterator& operator++() { next(); return *this; }
289 // test if iterator is still valid
290 bool valid() const { return _iter != 0; }
291 operator bool() const { return valid(); }
294 const char* ckey() const {
295 return json_object_iter_key(_iter);
298 std::string key() const { return ckey(); }
301 const Value value() const {
302 return Value(json_object_iter_value(_iter));
306 const Value operator*() const { return value(); }
310 Iterator(const Iterator&);
311 Iterator& operator=(const Iterator&);
313 // object being iterated over
320 } // namespace jansson
322 // stream JSON value out
323 std::ostream& operator<<(std::ostream& os, const jansson::Value& value) {
324 char* tmp = value.save_string();
333 std::istream& operator>>(std::istream& is, jansson::Value& value) {
334 std::stringstream tmp;
336 tmp << static_cast<char>(is.get());
337 value = jansson::Value::load_string(tmp.str().c_str());
341 #include "janssonxx.tcc"