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