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
25 // base class for JSON value interface
26 template <typename _Base>
27 class _ValueBase : public _Base {
30 _ValueBase() : _Base() {}
33 _ValueBase(const _Base& base) : _Base(base) {}
35 // create reference to value
36 _ValueBase(json_t* json) : _Base(json) {}
39 bool is_undefined() const { return _Base::as_json() == 0; }
40 bool is_object() const { return json_is_object(_Base::as_json()); }
41 bool is_array() const { return json_is_array(_Base::as_json()); }
42 bool is_string() const { return json_is_string(_Base::as_json()); }
43 bool is_integer() const { return json_is_integer(_Base::as_json()); }
44 bool is_real() const { return json_is_real(_Base::as_json()); }
45 bool is_number() const { return json_is_number(_Base::as_json()); }
46 bool is_true() const { return json_is_true(_Base::as_json()); }
47 bool is_false() const { return json_is_false(_Base::as_json()); }
48 bool is_boolean() const { return json_is_boolean(_Base::as_json()); }
49 bool is_null() const { return json_is_null(_Base::as_json()); }
51 // get size of array or object
52 inline unsigned int size() const;
54 // get value at array index (const version)
55 inline const Value at(unsigned int index) const;
57 inline const Value operator[](signed int index) const;
58 inline const Value operator[](unsigned int index) const;
59 inline const Value operator[](signed short index) const;
60 inline const Value operator[](unsigned short index) const;
61 inline const Value operator[](signed long index) const;
62 inline const Value operator[](unsigned long index) const;
64 // get value at array index (non-const version)
65 inline Value at(unsigned int index);
67 inline Value operator[](signed int index);
68 inline Value operator[](unsigned int index);
69 inline Value operator[](signed short index);
70 inline Value operator[](unsigned short index);
71 inline Value operator[](signed long index);
72 inline Value operator[](unsigned long index);
74 // get object property
75 inline const Value get(const char* key) const;
77 inline const Value get(const std::string& key) const;
78 inline const Value operator[](const char* key) const;
79 inline const Value operator[](const std::string& key) const;
81 // clear all array/object values
84 // get value cast to specified type
85 inline const char* as_cstring() const;
86 inline std::string as_string() const;
87 inline int as_integer() const;
88 inline double as_real() const;
89 inline double as_number() const;
90 inline bool as_boolean() const;
92 // set an object property (converts value to object is not one already)
93 inline _Base& set_key(const char* key, const Value& value);
95 inline _Base& set_key(const std::string& key, const Value& value);
97 // set an array index (converts value to object is not one already)
98 inline _Base& set_at(unsigned int index, const Value& value);
100 // delete an object key
101 inline _Base& del_key(const char* key);
103 inline _Base& del_key(const std::string& key);
105 // delete an item from an array by index
106 inline _Base& del_at(unsigned int index);
108 // insert an item into an array at a given index
109 inline _Base& insert_at(unsigned int index, const Value& value);
112 // represents any JSON value, private base
115 // construct new Value with an undefined value
116 _Value() : _value(0) {}
119 _Value(const _Value& value) : _value(json_incref(value._value)) {}
121 // make a reference to an existing json_t value
122 explicit _Value(json_t* value) : _value(json_incref(value)) {}
124 // free Value resources
125 ~_Value() { json_decref(_value); }
127 // copy an existing Value
128 _Value& operator=(const _Value& e) {
131 _value = json_incref(e._value);
136 // get the underlying json_t
137 json_t* as_json() const { return _value; }
140 // take ownership of a json_t (does not increase reference count)
141 static _Value _take(json_t* json) {
148 // internal value pointer
152 // represents any JSON value
153 class Value : public _ValueBase<_Value> {
156 Value() : _ValueBase<_Value>() {}
158 // copy constructor for base
159 Value(const _Value& value) : _ValueBase<_Value>(value) {}
161 // copy constructor for base
162 Value(const _ValueBase<_Value>& value) : _ValueBase<_Value>(value) {}
165 Value(const Value& value) : _ValueBase<_Value>(value) {}
167 // create reference to value
168 explicit Value(json_t* json) : _ValueBase<_Value>(json) {}
170 // construct Value from input
171 static inline Value from(const char* value) { return Value::_take(json_string(value)); }
172 static inline Value from(const std::string& value) { return from(value.c_str()); }
173 static inline Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
174 static inline Value from(signed int value) { return Value::_take(json_integer(value)); }
175 static inline Value from(unsigned int value) { return Value::_take(json_integer(value)); }
176 static inline Value from(signed short value) { return Value::_take(json_integer(value)); }
177 static inline Value from(unsigned short value) { return Value::_take(json_integer(value)); }
178 static inline Value from(signed long value) { return Value::_take(json_integer(value)); }
179 static inline Value from(unsigned long value) { return Value::_take(json_integer(value)); }
180 static inline Value from(float value) { return Value::_take(json_real(value)); }
181 static inline Value from(double value) { return Value::_take(json_real(value)); }
183 // create a new empty object
184 static inline Value object() { return Value::_take(json_object()); }
186 // create a new empty array
187 static inline Value array() { return Value::_take(json_array()); }
189 // create a new null value
190 static inline Value null() { return Value::_take(json_null()); }
192 // load a file as a JSON value
193 static Value load_file(const char* path, json_error_t* error = 0) {
194 return Value::_take(json_load_file(path, error));
197 // load a string as a JSON value
198 static Value load_string(const char* string, json_error_t* error = 0) {
199 return Value::_take(json_loads(string, error));
202 // write the value to a file
203 int save_file(const char* path, int flags = 0) const {
204 return json_dump_file(as_json(), path, flags);
207 // write the value to a string (caller must deallocate with free()!)
208 char* save_string(int flags = 0) const {
209 return json_dumps(as_json(), flags);
213 // iterators over a JSON object
216 // construct a new iterator for a given object
217 Iterator(const Value& value) : _object(value), _iter(0) {
218 _iter = json_object_iter(_object.as_json());
221 // increment iterator
223 _iter = json_object_iter_next(_object.as_json(), _iter);
226 Iterator& operator++() { next(); return *this; }
228 // test if iterator is still valid
229 bool valid() const { return _iter != 0; }
230 operator bool() const { return valid(); }
233 const char* ckey() const {
234 return json_object_iter_key(_iter);
237 std::string key() const { return ckey(); }
240 const Value value() const {
241 return Value(json_object_iter_value(_iter));
245 const Value operator*() const { return value(); }
249 Iterator(const Iterator&);
250 Iterator& operator=(const Iterator&);
252 // object being iterated over
259 } // namespace jansson
261 // stream JSON value out
262 std::ostream& operator<<(std::ostream& os, const jansson::Value& value) {
263 char* tmp = value.save_string();
272 std::istream& operator>>(std::istream& is, jansson::Value& value) {
273 std::stringstream tmp;
275 tmp << static_cast<char>(is.get());
276 value = jansson::Value::load_string(tmp.str().c_str());
280 #include "janssonxx.tcc"