d3d11b3fab5bffea42019748ecaee00717c66e09
[jansson.git] / src / 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 json {
18         // include Jansson C library into the json 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                         // take ownership of a json_t (does not increase reference count)
154                         inline static Basic take_ownership(json_t* json);
155
156                 protected:
157                         // internal value pointer
158                         json_t* _value;
159                 };
160
161                 // proxies an array element
162                 class ElementProxy {
163                 public:
164                         // constructor
165                         ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
166
167                         // assign to the proxied element
168                         inline ElementProxy& operator=(const Value& value);
169
170                         // get the proxied element
171                         inline json_t* as_json() const;
172
173                 private:
174                         // array object we wrap
175                         json_t* _array;
176
177                         // index of property
178                         unsigned int _index;
179                 };
180
181                 // proxies an object property
182                 class PropertyProxy {
183                 public:
184                         // constructor
185                         PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
186
187                         // assign to the proxied element
188                         inline PropertyProxy& operator=(const Value& value);
189
190                         // get the proxied element
191                         inline json_t* as_json() const;
192
193                 private:
194                         // array object we wrap
195                         json_t* _object;
196
197                         // key of property
198                         const char* _key;
199                 };
200
201         } // namespace json::_private
202
203         // represents any JSON value
204         class Value : public _private::ValueBase<_private::Basic> {
205         public:
206                 // construct Value from input
207                 explicit inline Value(const char* value);
208                 explicit inline Value(const std::string& value);
209                 explicit inline Value(bool value);
210                 explicit inline Value(signed int value);
211                 explicit inline Value(unsigned int value);
212                 explicit inline Value(signed short value);
213                 explicit inline Value(unsigned short value);
214                 explicit inline Value(signed long value);
215                 explicit inline Value(unsigned long value);
216                 explicit inline Value(float value);
217                 explicit inline Value(double value);
218
219                 // empty constructor
220                 Value() : _private::ValueBase<_private::Basic>() {}
221
222                 // copy constructor for base
223                 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
224         
225                 // copy constructor for base
226                 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
227
228                 // copy constructor
229                 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
230
231                 // create reference to value
232                 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
233         };
234
235         // iterators over a JSON object
236         class Iterator {
237         public:
238                 // construct a new iterator for a given object
239                 inline Iterator(const Value& value);
240
241                 // construct a new iterator for a given object
242                 inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
243
244                 // increment iterator
245                 inline void next();
246
247                 inline Iterator& operator++();
248
249                 // test if iterator is still valid
250                 inline bool valid() const;
251
252                 inline operator bool() const;
253
254                 // get key
255                 inline const char* ckey() const;
256
257                 inline std::string key() const;
258
259                 // get value
260                 inline const Value value() const;
261
262                 // dereference value
263                 inline const Value operator*() const;
264
265         private:
266                 // disallow copying
267                 Iterator(const Iterator&);
268                 Iterator& operator=(const Iterator&);
269
270                 // object being iterated over
271                 Value _object;
272
273                 // iterator value
274                 void* _iter;
275         };
276
277         // create a new empty object
278         inline Value object();
279
280         // create a new empty array
281         inline Value array();
282
283         // create a new null value
284         inline Value null();
285
286         // load a file as a JSON value
287         inline Value load_file(const char* path, json_error_t* error = 0);
288
289         // load a string as a JSON value
290         inline Value load_string(const char* string, json_error_t* error = 0);
291
292 } // namespace json 
293
294 // stream JSON value out -- inefficient and not recommended for production use
295 inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
296
297 // read JSON value -- inefficient and not recommended for production use
298 inline std::istream& operator>>(std::istream& is, json::Value& value);
299
300 // include implementation code
301 #define IN_JANSSON_HPP 1
302 #include "jansson-impl.hpp"
303 #undef IN_JANSSON_HPP
304
305 #endif // defined(JANSSON_HPP)