1 #if !defined(JANSSONXX_H)
11 // represents any JSON value
14 // construct new Value with an undefined value
15 Value() : _value(0) {}
17 // free Value resources
18 ~Value() { json_decref(_value); }
20 // copy an existing Value
21 Value(const Value& e) : _value(json_incref(e._value)) {}
23 // make a reference to an existing json_t value
24 explicit Value(json_t* value) : _value(json_incref(value)) {}
26 // copy an existing Value
27 Value& operator=(const Value& e) {
29 _value = json_incref(e._value);
33 // take ownership of a json_t (does not increase reference count)
34 Value& take_ownership(json_t* value) {
40 // load a file as a JSON value
41 static Value load_file(const char* path, json_error_t* error = 0) {
42 return Value().take_ownership(json_load_file(path, error));
45 // load a string as a JSON value
46 static Value load_string(const char* string, json_error_t* error = 0) {
47 return Value().take_ownership(json_loads(string, error));
50 // get the underlying json_t
51 json_t* as_json_t() const { return _value; }
53 // check Value value type
54 bool is_undefined() const { return _value == 0; }
55 bool is_object() const { return json_is_object(_value); }
56 bool is_array() const { return json_is_array(_value); }
57 bool is_string() const { return json_is_string(_value); }
58 bool is_integer() const { return json_is_integer(_value); }
59 bool is_real() const { return json_is_real(_value); }
60 bool is_number() const { return json_is_number(_value); }
61 bool is_true() const { return json_is_true(_value); }
62 bool is_false() const { return json_is_false(_value); }
63 bool is_boolean() const { return json_is_boolean(_value); }
64 bool is_null() const { return json_is_null(_value); }
66 // get size of array or object
67 unsigned int size() const {
69 return json_object_size(_value);
71 return json_array_size(_value);
76 // get value at array index
77 const Value at(unsigned int index) const {
79 return Value(json_array_get(_value, index));
84 const Value operator[](signed int index) const { return at(index); }
85 const Value operator[](unsigned int index) const { return at(index); }
86 const Value operator[](signed short index) const { return at(index); }
87 const Value operator[](unsigned short index) const { return at(index); }
88 const Value operator[](signed long index) const { return at(index); }
89 const Value operator[](unsigned long index) const { return at(index); }
91 // get object property
92 const Value get(const char* key) const {
94 return Value(json_object_get(_value, key));
99 const Value get(const std::string& key) const { return get(key.c_str()); }
100 const Value operator[](const char* key) const { return get(key); }
101 const Value operator[](const std::string& key) const { return get(key.c_str()); }
103 // clear all array/object values
106 json_object_clear(_value);
108 json_array_clear(_value);
111 // get value cast to specified type
112 const char* as_cstring() const { return json_string_value(_value); }
113 std::string as_string() const { return as_cstring(); }
114 int as_integer() const { return json_integer_value(_value); }
115 double as_real() const { return json_real_value(_value); }
116 double as_number() const { return json_number_value(_value); }
117 bool as_boolean() const { return is_true(); }
119 // assign new string value
120 Value& operator=(const char* value) {
122 _value = json_string(value);
126 // assign new integer value
127 Value& operator=(int value) {
129 _value = json_integer(value);
133 // assign new real/double/number value
134 Value& operator=(double value) {
136 _value = json_real(value);
140 // assign new boolean value
141 Value& operator=(bool value) {
143 _value = value ? json_true() : json_false();
148 // internal value pointer
152 // iterators over a JSON object
155 // construct a new iterator for a given object
156 Iterator(const Value& value) : _object(value), _iter(0) {
157 _iter = json_object_iter(_object.as_json_t());
160 // increment iterator
163 _iter = json_object_iter_next(_object.as_json_t(), _iter);
166 Iterator& operator++() { next(); return *this; }
168 // test if iterator is still valid
169 bool valid() const { return _iter != 0; }
170 operator bool() const { return valid(); }
173 const char* ckey() const {
175 return json_object_iter_key(_iter);
180 std::string key() const { return ckey(); }
183 const Value value() const {
185 return Value(json_object_iter_value(_iter));
191 const Value operator*() const { return value(); }
195 Iterator(const Iterator&);
196 Iterator& operator=(const Iterator&);
198 // object being iterated over
205 } // namespace jansson