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