cleanup code
[jansson.git] / janssonxx.h
1 // janssonxx - C++ wrapper for jansson
2 //
3 // author: Sean Middleditch <sean@middleditch.us>
4 //
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.
7
8
9 #if !defined(JANSSONXX_H)
10 #define JANSSONXX_H 1
11
12 #include <string>
13 #include <ostream>
14 #include <istream>
15 #include <sstream>
16 #include <cstdlib>
17
18 namespace jansson {
19         // include Jansson C library in the jansson namespace
20         #include <jansson.h>
21
22         class Iterator;
23         class Value;
24
25         // implementation details; do not use directly
26         namespace _private {
27                 class ElementProxy;
28                 class PropertyProxy;
29
30                 // base class for JSON value interface
31                 template <typename _Base>
32                 class ValueBase : public _Base {
33                 public:
34                         // empty constructor
35                         ValueBase() : _Base() {}
36
37                         // copy constructor
38                         ValueBase(const _Base& base) : _Base(base) {}
39
40                         // create reference to value
41                         ValueBase(json_t* json) : _Base(json) {}
42
43                         // assignment operator
44                         inline ValueBase& operator=(const Value& value);
45
46                         // check value type
47                         inline bool is_undefined() const;
48                         inline bool is_object() const;
49                         inline bool is_array() const;
50                         inline bool is_string() const;
51                         inline bool is_integer() const;
52                         inline bool is_real() const;
53                         inline bool is_number() const;
54                         inline bool is_true() const;
55                         inline bool is_false() const;
56                         inline bool is_boolean() const;
57                         inline bool is_null() const;
58
59                         // get size of array or object
60                         inline unsigned int size() const;
61
62                         // get value at array index (const version)
63                         inline const Value at(unsigned int index) const;
64
65                         inline const Value operator[](signed int index) const;
66                         inline const Value operator[](unsigned int index) const;
67                         inline const Value operator[](signed short index) const;
68                         inline const Value operator[](unsigned short index) const;
69                         inline const Value operator[](signed long index) const;
70                         inline const Value operator[](unsigned long index) const;
71
72                         // get value at array index (non-const version)
73                         inline ValueBase<ElementProxy> at(unsigned int index);
74
75                         inline ValueBase<ElementProxy> operator[](signed int index);
76                         inline ValueBase<ElementProxy> operator[](unsigned int index);
77                         inline ValueBase<ElementProxy> operator[](signed short index);
78                         inline ValueBase<ElementProxy> operator[](unsigned short index);
79                         inline ValueBase<ElementProxy> operator[](signed long index);
80                         inline ValueBase<ElementProxy> operator[](unsigned long index);
81
82                         // get object property (const version)
83                         inline const Value get(const char* key) const;
84
85                         inline const Value get(const std::string& key) const;
86                         inline const Value operator[](const char* key) const;
87                         inline const Value operator[](const std::string& key) const;
88
89                         // get object property (non-const version)
90                         inline ValueBase<PropertyProxy> get(const char* key);
91
92                         inline ValueBase<PropertyProxy> get(const std::string& key);
93                         inline ValueBase<PropertyProxy> operator[](const char* key);
94                         inline ValueBase<PropertyProxy> operator[](const std::string& key);
95
96                         // clear all array/object values
97                         inline void clear();
98
99                         // get value cast to specified type
100                         inline const char* as_cstring() const;
101                         inline std::string as_string() const;
102                         inline int as_integer() const;
103                         inline double as_real() const;
104                         inline double as_number() const;
105                         inline bool as_boolean() const;
106
107                         // set an object property (converts value to object is not one already)
108                         inline _Base& set_key(const char* key, const Value& value);
109
110                         inline _Base& set_key(const std::string& key, const Value& value);
111
112                         // set an array index (converts value to object is not one already)
113                         inline _Base& set_at(unsigned int index, const Value& value);
114
115                         // delete an object key
116                         inline _Base& del_key(const char* key);
117
118                         inline _Base& del_key(const std::string& key);
119
120                         // delete an item from an array by index
121                         inline _Base& del_at(unsigned int index);
122
123                         // insert an item into an array at a given index
124                         inline _Base& insert_at(unsigned int index, const Value& value);
125
126                         // write the value to a file
127                         inline int save_file(const char* path, int flags = 0) const;
128
129                         // write the value to a string (caller must deallocate with free()!)
130                         inline char* save_string(int flags = 0) const;
131                 };
132
133                 // represents any JSON value, private base
134                 class Basic {
135                 public:
136                         // construct new Value with an undefined value
137                         Basic() : _value(0) {}
138
139                         // copy constructor
140                         Basic(const Basic& value) : _value(json_incref(value._value)) {}
141
142                         // make a reference to an existing json_t value
143                         explicit Basic(json_t* value) : _value(json_incref(value)) {}
144
145                         // free Value resources
146                         inline ~Basic();
147
148                         // copy an existing Value
149                         inline Basic& operator=(const Basic& e);
150
151                         // get the underlying json_t
152                         inline json_t* as_json() const;
153
154                 protected:
155                         // take ownership of a json_t (does not increase reference count)
156                         static inline Basic _take(json_t* json);
157
158                 private:
159                         // internal value pointer
160                         json_t* _value;
161                 };
162
163                 // proxies an array element
164                 class ElementProxy {
165                 public:
166                         // constructor
167                         ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
168
169                         // assign to the proxied element
170                         inline ElementProxy& operator=(const Value& value);
171
172                         // get the proxied element
173                         inline json_t* as_json() const;
174
175                 private:
176                         // array object we wrap
177                         json_t* _array;
178
179                         // index of property
180                         unsigned int _index;
181                 };
182
183                 // proxies an object property
184                 class PropertyProxy {
185                 public:
186                         // constructor
187                         PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
188
189                         // assign to the proxied element
190                         inline PropertyProxy& operator=(const Value& value);
191
192                         // get the proxied element
193                         inline json_t* as_json() const;
194
195                 private:
196                         // array object we wrap
197                         json_t* _object;
198
199                         // key of property
200                         const char* _key;
201                 };
202
203         } // namespace jansson::_private
204
205         // represents any JSON value
206         class Value : public _private::ValueBase<_private::Basic> {
207         public:
208                 // empty constructor
209                 Value() : _private::ValueBase<_private::Basic>() {}
210
211                 // copy constructor for base
212                 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
213         
214                 // copy constructor for base
215                 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
216
217                 // copy constructor
218                 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
219
220                 // create reference to value
221                 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
222
223                 // construct Value from input
224                 static inline Value from(const char* value);
225                 static inline Value from(const std::string& value);
226                 static inline Value from(bool value);
227                 static inline Value from(signed int value);
228                 static inline Value from(unsigned int value);
229                 static inline Value from(signed short value);
230                 static inline Value from(unsigned short value);
231                 static inline Value from(signed long value);
232                 static inline Value from(unsigned long value);
233                 static inline Value from(float value);
234                 static inline Value from(double value);
235
236                 // create a new empty object
237                 static inline Value object();
238
239                 // create a new empty array
240                 static inline Value array();
241
242                 // create a new null value
243                 static inline Value null();
244
245                 // load a file as a JSON value
246                 static inline Value load_file(const char* path, json_error_t* error = 0);
247
248                 // load a string as a JSON value
249                 static inline Value load_string(const char* string, json_error_t* error = 0);
250         };
251
252         // iterators over a JSON object
253         class Iterator {
254         public:
255                 // construct a new iterator for a given object
256                 inline Iterator(const Value& value);
257
258                 // construct a new iterator for a given object
259                 inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
260
261                 // increment iterator
262                 inline void next();
263
264                 inline Iterator& operator++();
265
266                 // test if iterator is still valid
267                 inline bool valid() const;
268
269                 inline operator bool() const;
270
271                 // get key
272                 inline const char* ckey() const;
273
274                 inline std::string key() const;
275
276                 // get value
277                 inline const Value value() const;
278
279                 // dereference value
280                 inline const Value operator*() const;
281
282         private:
283                 // disallow copying
284                 Iterator(const Iterator&);
285                 Iterator& operator=(const Iterator&);
286
287                 // object being iterated over
288                 Value _object;
289
290                 // iterator value
291                 void* _iter;
292         };
293
294 } // namespace jansson
295
296 // stream JSON value out
297 inline std::ostream& operator<<(std::ostream& os, const jansson::Value& value);
298
299 // read JSON value
300 inline std::istream& operator>>(std::istream& is, jansson::Value& value);
301
302 #include "janssonxx.tcc"
303
304 #endif