1 // janssonxx - C++ wrapper for jansson
3 // author: Sean Middleditch <sean@middleditch.us>
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.
8 #if !defined(JANSSON_HPP)
18 // include Jansson C library in the jansson namespace
24 // implementation details; do not use directly
29 // base class for JSON value interface
30 template <typename _Base>
31 class ValueBase : public _Base {
34 ValueBase() : _Base() {}
37 ValueBase(const _Base& base) : _Base(base) {}
39 // create reference to value
40 ValueBase(json_t* json) : _Base(json) {}
42 // assignment operator
43 inline ValueBase& operator=(const Value& value);
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;
58 // get size of array or object
59 inline unsigned int size() const;
61 // get value at array index (const version)
62 inline const Value at(unsigned int index) const;
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;
71 // get value at array index (non-const version)
72 inline ValueBase<ElementProxy> at(unsigned int index);
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);
81 // get object property (const version)
82 inline const Value get(const char* key) const;
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;
88 // get object property (non-const version)
89 inline ValueBase<PropertyProxy> get(const char* key);
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);
95 // clear all array/object values
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;
106 // set an object property (converts value to object is not one already)
107 inline _Base& set_key(const char* key, const Value& value);
109 inline _Base& set_key(const std::string& key, const Value& value);
111 // set an array index (converts value to object is not one already)
112 inline _Base& set_at(unsigned int index, const Value& value);
114 // delete an object key
115 inline _Base& del_key(const char* key);
117 inline _Base& del_key(const std::string& key);
119 // delete an item from an array by index
120 inline _Base& del_at(unsigned int index);
122 // insert an item into an array at a given index
123 inline _Base& insert_at(unsigned int index, const Value& value);
125 // write the value to a file
126 inline int save_file(const char* path, int flags = 0) const;
128 // write the value to a string (caller must deallocate with free()!)
129 inline char* save_string(int flags = 0) const;
132 // represents any JSON value, private base
135 // construct new Value with an undefined value
136 Basic() : _value(0) {}
139 Basic(const Basic& value) : _value(json_incref(value._value)) {}
141 // make a reference to an existing json_t value
142 explicit Basic(json_t* value) : _value(json_incref(value)) {}
144 // free Value resources
147 // copy an existing Value
148 inline Basic& operator=(const Basic& e);
150 // get the underlying json_t
151 inline json_t* as_json() const;
154 // take ownership of a json_t (does not increase reference count)
155 static inline Basic _take(json_t* json);
158 // internal value pointer
162 // proxies an array element
166 ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
168 // assign to the proxied element
169 inline ElementProxy& operator=(const Value& value);
171 // get the proxied element
172 inline json_t* as_json() const;
175 // array object we wrap
182 // proxies an object property
183 class PropertyProxy {
186 PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
188 // assign to the proxied element
189 inline PropertyProxy& operator=(const Value& value);
191 // get the proxied element
192 inline json_t* as_json() const;
195 // array object we wrap
202 } // namespace jansson::_private
204 // represents any JSON value
205 class Value : public _private::ValueBase<_private::Basic> {
208 Value() : _private::ValueBase<_private::Basic>() {}
210 // copy constructor for base
211 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
213 // copy constructor for base
214 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
217 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
219 // create reference to value
220 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
222 // construct Value from input
223 static inline Value from(const char* value);
224 static inline Value from(const std::string& value);
225 static inline Value from(bool value);
226 static inline Value from(signed int value);
227 static inline Value from(unsigned int value);
228 static inline Value from(signed short value);
229 static inline Value from(unsigned short value);
230 static inline Value from(signed long value);
231 static inline Value from(unsigned long value);
232 static inline Value from(float value);
233 static inline Value from(double value);
235 // create a new empty object
236 static inline Value object();
238 // create a new empty array
239 static inline Value array();
241 // create a new null value
242 static inline Value null();
244 // load a file as a JSON value
245 static inline Value load_file(const char* path, json_error_t* error = 0);
247 // load a string as a JSON value
248 static inline Value load_string(const char* string, json_error_t* error = 0);
251 // iterators over a JSON object
254 // construct a new iterator for a given object
255 inline Iterator(const Value& value);
257 // construct a new iterator for a given object
258 inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
260 // increment iterator
263 inline Iterator& operator++();
265 // test if iterator is still valid
266 inline bool valid() const;
268 inline operator bool() const;
271 inline const char* ckey() const;
273 inline std::string key() const;
276 inline const Value value() const;
279 inline const Value operator*() const;
283 Iterator(const Iterator&);
284 Iterator& operator=(const Iterator&);
286 // object being iterated over
293 } // namespace jansson
295 // stream JSON value out -- inefficient and not recommended for production use
296 inline std::ostream& operator<<(std::ostream& os, const jansson::Value& value);
298 // read JSON value -- inefficient and not recommended for production use
299 inline std::istream& operator>>(std::istream& is, jansson::Value& value);
301 // include implementation code
302 #define IN_JANSSON_HPP 1
303 #include "jansson-impl.hpp"
304 #undef IN_JANSSON_HPP
306 #endif // defined(JANSSON_HPP)