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