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