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.
9 #if !defined(JANSSONXX_H)
19 // include Jansson C library in the jansson namespace
25 // implementation details; do not use directly
30 // base class for JSON value interface
31 template <typename _Base>
32 class ValueBase : public _Base {
35 ValueBase() : _Base() {}
38 ValueBase(const _Base& base) : _Base(base) {}
40 // create reference to value
41 ValueBase(json_t* json) : _Base(json) {}
43 // assignment operator
44 inline ValueBase& operator=(const Value& value);
47 inline bool is_undefined() const;
48 inline bool is_object() const;
49 inline bool is_array() const;
50 inline bool is_string() const;
51 inline bool is_integer() const;
52 inline bool is_real() const;
53 inline bool is_number() const;
54 inline bool is_true() const;
55 inline bool is_false() const;
56 inline bool is_boolean() const;
57 inline bool is_null() const;
59 // get size of array or object
60 inline unsigned int size() const;
62 // get value at array index (const version)
63 inline const Value at(unsigned int index) const;
65 inline const Value operator[](signed int index) const;
66 inline const Value operator[](unsigned int index) const;
67 inline const Value operator[](signed short index) const;
68 inline const Value operator[](unsigned short index) const;
69 inline const Value operator[](signed long index) const;
70 inline const Value operator[](unsigned long index) const;
72 // get value at array index (non-const version)
73 inline ValueBase<ElementProxy> at(unsigned int index);
75 inline ValueBase<ElementProxy> operator[](signed int index);
76 inline ValueBase<ElementProxy> operator[](unsigned int index);
77 inline ValueBase<ElementProxy> operator[](signed short index);
78 inline ValueBase<ElementProxy> operator[](unsigned short index);
79 inline ValueBase<ElementProxy> operator[](signed long index);
80 inline ValueBase<ElementProxy> operator[](unsigned long index);
82 // get object property (const version)
83 inline const Value get(const char* key) const;
85 inline const Value get(const std::string& key) const;
86 inline const Value operator[](const char* key) const;
87 inline const Value operator[](const std::string& key) const;
89 // get object property (non-const version)
90 inline ValueBase<PropertyProxy> get(const char* key);
92 inline ValueBase<PropertyProxy> get(const std::string& key);
93 inline ValueBase<PropertyProxy> operator[](const char* key);
94 inline ValueBase<PropertyProxy> operator[](const std::string& key);
96 // clear all array/object values
99 // get value cast to specified type
100 inline const char* as_cstring() const;
101 inline std::string as_string() const;
102 inline int as_integer() const;
103 inline double as_real() const;
104 inline double as_number() const;
105 inline bool as_boolean() const;
107 // set an object property (converts value to object is not one already)
108 inline _Base& set_key(const char* key, const Value& value);
110 inline _Base& set_key(const std::string& key, const Value& value);
112 // set an array index (converts value to object is not one already)
113 inline _Base& set_at(unsigned int index, const Value& value);
115 // delete an object key
116 inline _Base& del_key(const char* key);
118 inline _Base& del_key(const std::string& key);
120 // delete an item from an array by index
121 inline _Base& del_at(unsigned int index);
123 // insert an item into an array at a given index
124 inline _Base& insert_at(unsigned int index, const Value& value);
126 // write the value to a file
127 inline int save_file(const char* path, int flags = 0) const;
129 // write the value to a string (caller must deallocate with free()!)
130 inline char* save_string(int flags = 0) const;
133 // represents any JSON value, private base
136 // construct new Value with an undefined value
137 Basic() : _value(0) {}
140 Basic(const Basic& value) : _value(json_incref(value._value)) {}
142 // make a reference to an existing json_t value
143 explicit Basic(json_t* value) : _value(json_incref(value)) {}
145 // free Value resources
148 // copy an existing Value
149 inline Basic& operator=(const Basic& e);
151 // get the underlying json_t
152 inline json_t* as_json() const;
155 // take ownership of a json_t (does not increase reference count)
156 static inline Basic _take(json_t* json);
159 // internal value pointer
163 // proxies an array element
167 ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
169 // assign to the proxied element
170 inline ElementProxy& operator=(const Value& value);
172 // get the proxied element
173 inline json_t* as_json() const;
176 // array object we wrap
183 // proxies an object property
184 class PropertyProxy {
187 PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
189 // assign to the proxied element
190 inline PropertyProxy& operator=(const Value& value);
192 // get the proxied element
193 inline json_t* as_json() const;
196 // array object we wrap
203 } // namespace jansson::_private
205 // represents any JSON value
206 class Value : public _private::ValueBase<_private::Basic> {
209 Value() : _private::ValueBase<_private::Basic>() {}
211 // copy constructor for base
212 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
214 // copy constructor for base
215 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
218 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
220 // create reference to value
221 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
223 // construct Value from input
224 static inline Value from(const char* value);
225 static inline Value from(const std::string& value);
226 static inline Value from(bool value);
227 static inline Value from(signed int value);
228 static inline Value from(unsigned int value);
229 static inline Value from(signed short value);
230 static inline Value from(unsigned short value);
231 static inline Value from(signed long value);
232 static inline Value from(unsigned long value);
233 static inline Value from(float value);
234 static inline Value from(double value);
236 // create a new empty object
237 static inline Value object();
239 // create a new empty array
240 static inline Value array();
242 // create a new null value
243 static inline Value null();
245 // load a file as a JSON value
246 static inline Value load_file(const char* path, json_error_t* error = 0);
248 // load a string as a JSON value
249 static inline Value load_string(const char* string, json_error_t* error = 0);
252 // iterators over a JSON object
255 // construct a new iterator for a given object
256 inline Iterator(const Value& value);
258 // construct a new iterator for a given object
259 inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
261 // increment iterator
264 inline Iterator& operator++();
266 // test if iterator is still valid
267 inline bool valid() const;
269 inline operator bool() const;
272 inline const char* ckey() const;
274 inline std::string key() const;
277 inline const Value value() const;
280 inline const Value operator*() const;
284 Iterator(const Iterator&);
285 Iterator& operator=(const Iterator&);
287 // object being iterated over
294 } // namespace jansson
296 // stream JSON value out
297 inline std::ostream& operator<<(std::ostream& os, const jansson::Value& value);
300 inline std::istream& operator>>(std::istream& is, jansson::Value& value);
302 #include "janssonxx.tcc"