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)
20 // include in the jansson namespace
25 // represents any JSON value
28 // construct new Value with an undefined value
29 Value() : _value(0) {}
31 // free Value resources
32 ~Value() { json_decref(_value); }
34 // copy an existing Value
35 Value(const Value& e) : _value(json_incref(e._value)) {}
37 // make a reference to an existing json_t value
38 explicit Value(json_t* value) : _value(json_incref(value)) {}
40 // copy an existing Value
41 Value& operator=(const Value& e) {
44 _value = json_incref(e._value);
49 // load a file as a JSON value
50 static Value load_file(const char* path, json_error_t* error = 0) {
51 return Value::_take(json_load_file(path, error));
54 // load a string as a JSON value
55 static Value load_string(const char* string, json_error_t* error = 0) {
56 return Value::_take(json_loads(string, error));
59 // write the value to a file
60 int save_file(const char* path, int flags = JSON_INDENT(2)) const {
61 return json_dump_file(_value, path, flags);
64 // write the value to a string (caller must deallocate with free()!)
65 char* save_string(int flags = JSON_INDENT(2)) const {
66 return json_dumps(_value, flags);
69 // construct Value from input
70 static Value from(const char* value) { return Value::_take(json_string(value)); }
71 static Value from(const std::string& value) { return from(value.c_str()); }
72 static Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
73 static Value from(signed int value) { return Value::_take(json_integer(value)); }
74 static Value from(unsigned int value) { return Value::_take(json_integer(value)); }
75 static Value from(signed short value) { return Value::_take(json_integer(value)); }
76 static Value from(unsigned short value) { return Value::_take(json_integer(value)); }
77 static Value from(signed long value) { return Value::_take(json_integer(value)); }
78 static Value from(unsigned long value) { return Value::_take(json_integer(value)); }
79 static Value from(float value) { return Value::_take(json_real(value)); }
80 static Value from(double value) { return Value::_take(json_real(value)); }
82 // create a new empty object
83 static Value object() { return Value::_take(json_object()); }
85 // create a new empty array
86 static Value array() { return Value::_take(json_array()); }
88 // create a new null value
89 static Value null() { return Value::_take(json_null()); }
91 // get the underlying json_t
92 json_t* as_json() const { return _value; }
95 bool is_undefined() const { return _value == 0; }
96 bool is_object() const { return json_is_object(_value); }
97 bool is_array() const { return json_is_array(_value); }
98 bool is_string() const { return json_is_string(_value); }
99 bool is_integer() const { return json_is_integer(_value); }
100 bool is_real() const { return json_is_real(_value); }
101 bool is_number() const { return json_is_number(_value); }
102 bool is_true() const { return json_is_true(_value); }
103 bool is_false() const { return json_is_false(_value); }
104 bool is_boolean() const { return json_is_boolean(_value); }
105 bool is_null() const { return json_is_null(_value); }
107 // get size of array or object
108 unsigned int size() const {
110 return json_object_size(_value);
112 return json_array_size(_value);
115 // get value at array index (const version)
116 const Value at(unsigned int index) const {
117 return Value(json_array_get(_value, index));
120 const Value operator[](signed int index) const { return at(index); }
121 const Value operator[](unsigned int index) const { return at(index); }
122 const Value operator[](signed short index) const { return at(index); }
123 const Value operator[](unsigned short index) const { return at(index); }
124 const Value operator[](signed long index) const { return at(index); }
125 const Value operator[](unsigned long index) const { return at(index); }
127 // get value at array index (non-const version)
128 Value at(unsigned int index) {
129 return Value(json_array_get(_value, index));
132 Value operator[](signed int index) { return at(index); }
133 Value operator[](unsigned int index) { return at(index); }
134 Value operator[](signed short index) { return at(index); }
135 Value operator[](unsigned short index) { return at(index); }
136 Value operator[](signed long index) { return at(index); }
137 Value operator[](unsigned long index) { return at(index); }
139 // get object property
140 const Value get(const char* key) const {
141 return Value(json_object_get(_value, key));
144 const Value get(const std::string& key) const { return get(key.c_str()); }
145 const Value operator[](const char* key) const { return get(key); }
146 const Value operator[](const std::string& key) const { return get(key.c_str()); }
148 // clear all array/object values
151 json_object_clear(_value);
153 json_array_clear(_value);
156 // get value cast to specified type
157 const char* as_cstring() const { return json_string_value(_value); }
158 std::string as_string() const { return as_cstring(); }
159 int as_integer() const { return json_integer_value(_value); }
160 double as_real() const { return json_real_value(_value); }
161 double as_number() const { return json_number_value(_value); }
162 bool as_boolean() const { return is_true(); }
164 // set an object property (converts value to object is not one already)
165 Value& set_key(const char* key, const Value& value) {
166 json_object_set(_value, key, value.as_json());
170 Value& set_key(const std::string& key, const Value& value) {
171 return set_key(key.c_str(), value);
174 // set an array index (converts value to object is not one already)
175 Value& set_at(unsigned int index, const Value& value) {
177 json_array_append(_value, value.as_json());
179 json_array_set(_value, index, value.as_json());
183 // delete an object key
184 Value& del_key(const char* key) {
185 json_object_del(_value, key);
189 Value& del_key(const std::string& key) {
190 return del_key(key.c_str());
193 // delete an item from an array by index
194 Value& del_at(unsigned int index) {
195 json_array_remove(_value, index);
199 // insert an item into an array at a given index
200 Value& insert_at(unsigned int index, const Value& value) {
201 json_array_insert(_value, index, value.as_json());
206 // take ownership of a json_t (does not increase reference count)
207 static Value _take(json_t* json) {
213 // internal value pointer
217 // iterators over a JSON object
220 // construct a new iterator for a given object
221 Iterator(const Value& value) : _object(value), _iter(0) {
222 _iter = json_object_iter(_object.as_json());
225 // increment iterator
227 _iter = json_object_iter_next(_object.as_json(), _iter);
230 Iterator& operator++() { next(); return *this; }
232 // test if iterator is still valid
233 bool valid() const { return _iter != 0; }
234 operator bool() const { return valid(); }
237 const char* ckey() const {
238 return json_object_iter_key(_iter);
241 std::string key() const { return ckey(); }
244 const Value value() const {
245 return Value(json_object_iter_value(_iter));
249 const Value operator*() const { return value(); }
253 Iterator(const Iterator&);
254 Iterator& operator=(const Iterator&);
256 // object being iterated over
263 } // namespace jansson
265 // stream JSON value out
266 std::ostream& operator<<(std::ostream& os, const jansson::Value& value) {
267 char* tmp = value.save_string();
276 std::istream& operator>>(std::istream& is, jansson::Value& value) {
277 std::stringstream tmp;
279 tmp << static_cast<char>(is.get());
280 value = jansson::Value::load_string(tmp.str().c_str());