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