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 into the json 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;
153 // take ownership of a json_t (does not increase reference count)
154 inline static Basic take_ownership(json_t* json);
157 // internal value pointer
161 // proxies an array element
165 ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
167 // assign to the proxied element
168 inline ElementProxy& operator=(const Value& value);
170 // get the proxied element
171 inline json_t* as_json() const;
174 // array object we wrap
181 // proxies an object property
182 class PropertyProxy {
185 PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
187 // assign to the proxied element
188 inline PropertyProxy& operator=(const Value& value);
190 // get the proxied element
191 inline json_t* as_json() const;
194 // array object we wrap
201 } // namespace json::_private
203 // represents any JSON value
204 class Value : public _private::ValueBase<_private::Basic> {
206 // construct Value from input
207 explicit inline Value(const char* value);
208 explicit inline Value(const std::string& value);
209 explicit inline Value(bool value);
210 explicit inline Value(signed int value);
211 explicit inline Value(unsigned int value);
212 explicit inline Value(signed short value);
213 explicit inline Value(unsigned short value);
214 explicit inline Value(signed long value);
215 explicit inline Value(unsigned long value);
216 explicit inline Value(float value);
217 explicit inline Value(double value);
220 Value() : _private::ValueBase<_private::Basic>() {}
222 // copy constructor for base
223 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
225 // copy constructor for base
226 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
229 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
231 // create reference to value
232 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
235 // iterators over a JSON object
238 // construct a new iterator for a given object
239 inline Iterator(const Value& value);
241 // construct a new iterator for a given object
242 inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
244 // increment iterator
247 inline Iterator& operator++();
249 // test if iterator is still valid
250 inline bool valid() const;
252 inline operator bool() const;
255 inline const char* ckey() const;
257 inline std::string key() const;
260 inline const Value value() const;
263 inline const Value operator*() const;
267 Iterator(const Iterator&);
268 Iterator& operator=(const Iterator&);
270 // object being iterated over
277 // create a new empty object
278 inline Value object();
280 // create a new empty array
281 inline Value array();
283 // create a new null value
286 // load a file as a JSON value
287 inline Value load_file(const char* path, json_error_t* error = 0);
289 // load a string as a JSON value
290 inline Value load_string(const char* string, json_error_t* error = 0);
294 // stream JSON value out -- inefficient and not recommended for production use
295 inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
297 // read JSON value -- inefficient and not recommended for production use
298 inline std::istream& operator>>(std::istream& is, json::Value& value);
300 // include implementation code
301 #define IN_JANSSON_HPP 1
302 #include "jansson-impl.hpp"
303 #undef IN_JANSSON_HPP
305 #endif // defined(JANSSON_HPP)