ignore test-bin output file
[jansson.git] / janssonxx.h
1 #if !defined(JANSSONXX_H)
2 #define JANSSONXX_H 1
3
4 #include <string>
5 #include <jansson.h>
6
7 namespace jansson {
8
9 class Iterator;
10
11 // represents any JSON value
12 class Value {
13 public:
14         // construct new Value with an undefined value
15         Value() : _value(0) {}
16
17         // free Value resources
18         ~Value() { json_decref(_value); }
19
20         // copy an existing Value
21         Value(const Value& e) : _value(json_incref(e._value)) {}
22
23         // make a reference to an existing json_t value
24         explicit Value(json_t* value) : _value(json_incref(value)) {}
25
26         // copy an existing Value
27         Value& operator=(const Value& e) {
28                 json_decref(_value);
29                 _value = json_incref(e._value);
30                 return *this;
31         }
32
33         // take ownership of a json_t (does not increase reference count)
34         Value& take_ownership(json_t* value) {
35                 json_decref(_value);
36                 _value = value;
37                 return *this;
38         }
39
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));
43         }
44
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));
48         }
49
50         // get the underlying json_t
51         json_t* as_json_t() const { return _value; }
52
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); }
65
66         // get size of array or object
67         unsigned int size() const {
68                 if (is_object())
69                         return json_object_size(_value);
70                 else if (is_array())
71                         return json_array_size(_value);
72                 else
73                         return 0;
74         }
75
76         // get value at array index
77         const Value at(unsigned int index) const {
78                 if (is_array())
79                         return Value(json_array_get(_value, index));
80                 else
81                         return Value();
82         }
83
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); }
90
91         // get object property
92         const Value get(const char* key) const {
93                 if (is_object())
94                         return Value(json_object_get(_value, key));
95                 else
96                         return Value();
97         }
98
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()); }
102
103         // clear all array/object values
104         void clear() {
105                 if (is_object())
106                         json_object_clear(_value);
107                 else if (is_array())
108                         json_array_clear(_value);
109         }
110
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(); }
118
119         // assign new string value
120         Value& operator=(const char* value) {
121                 json_decref(_value);
122                 _value = json_string(value);
123                 return *this;
124         }
125
126         // assign new integer value
127         Value& operator=(int value) {
128                 json_decref(_value);
129                 _value = json_integer(value);
130                 return *this;
131         }
132
133         // assign new real/double/number value
134         Value& operator=(double value) {
135                 json_decref(_value);
136                 _value = json_real(value);
137                 return *this;
138         }
139
140         // assign new boolean value
141         Value& operator=(bool value) {
142                 json_decref(_value);
143                 _value = value ? json_true() : json_false();
144                 return *this;
145         }
146
147 private:
148         // internal value pointer
149         json_t* _value;
150 };
151
152 // iterators over a JSON object
153 class Iterator {
154 public:
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());
158         }
159
160         // increment iterator
161         void next() {
162                 if (_iter != 0)
163                         _iter = json_object_iter_next(_object.as_json_t(), _iter);
164         }
165
166         Iterator& operator++() { next(); return *this; }
167
168         // test if iterator is still valid
169         bool valid() const { return _iter != 0; }
170         operator bool() const { return valid(); }
171
172         // get key
173         const char* ckey() const {
174                 if (_iter != 0)
175                         return json_object_iter_key(_iter);
176                 else
177                         return "";
178         }
179
180         std::string key() const { return ckey(); }
181
182         // get value
183         const Value value() const {
184                 if (_iter != 0)
185                         return Value(json_object_iter_value(_iter));
186                 else
187                         return Value();
188         }
189
190         // dereference value
191         const Value operator*() const { return value(); }
192
193 private:
194         // disallow copying
195         Iterator(const Iterator&);
196         Iterator& operator=(const Iterator&);
197
198         // object being iterated over
199         Value _object;
200
201         // iterator value
202         void* _iter;
203 };
204
205 } // namespace jansson
206
207 #endif