1 // Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
2 // Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
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.
15 // Included so that standard functions don't end up in namespace json
22 // include Jansson C library into the json namespace
28 // implementation details; do not use directly
33 // base class for JSON value interface
34 template <typename _Base>
35 class ValueBase : public _Base {
38 ValueBase() : _Base() {}
41 ValueBase(const _Base& base) : _Base(base) {}
43 // create reference to value
44 ValueBase(json_t* json) : _Base(json) {}
46 // assignment operator
47 inline ValueBase& operator=(const Value& value);
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;
62 // get size of array or object
63 inline unsigned int size() const;
65 // get value at array index (const version)
66 inline const Value at(unsigned int index) const;
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;
75 // get value at array index (non-const version)
76 inline ValueBase<ElementProxy> at(unsigned int index);
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);
85 // get object property (const version)
86 inline const Value get(const char* key) const;
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;
92 // get object property (non-const version)
93 inline ValueBase<PropertyProxy> get(const char* key);
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);
99 // clear all array/object values
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;
110 // set an object property (converts value to object is not one already)
111 inline _Base& set_key(const char* key, const Value& value);
113 inline _Base& set_key(const std::string& key, const Value& value);
115 // set an array index (converts value to object is not one already)
116 inline _Base& set_at(unsigned int index, const Value& value);
118 // delete an object key
119 inline _Base& del_key(const char* key);
121 inline _Base& del_key(const std::string& key);
123 // delete an item from an array by index
124 inline _Base& del_at(unsigned int index);
126 // insert an item into an array at a given index
127 inline _Base& insert_at(unsigned int index, const Value& value);
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;
133 // write the value to a string (caller must deallocate with free()!)
134 inline char* dumps(int flags = 0) const;
137 // represents any JSON value, private base
140 // construct new Value with an undefined value
141 Basic() : _value(0) {}
144 Basic(const Basic& value) : _value(json_incref(value._value)) {}
146 // make a reference to an existing json_t value
147 explicit Basic(json_t* value) : _value(json_incref(value)) {}
149 // free Value resources
152 // copy an existing Value
153 inline Basic& operator=(const Basic& e);
155 // get the underlying json_t
156 inline json_t* as_json() const;
158 // take ownership of a json_t (does not increase reference count)
159 inline static Basic take_ownership(json_t* json);
162 // internal value pointer
166 // proxies an array element
169 inline ElementProxy(json_t* array, unsigned int index);
170 inline ElementProxy(const ElementProxy& other);
171 inline ~ElementProxy();
173 // assign to the proxied element
174 inline ElementProxy& operator=(const Value& value);
176 // get the proxied element
177 inline json_t* as_json() const;
180 // array object we wrap
187 // proxies an object property
188 class PropertyProxy {
190 inline PropertyProxy(json_t* object, const char *key);
191 inline PropertyProxy(const PropertyProxy& other);
192 inline ~PropertyProxy();
194 // assign to the proxied element
195 inline PropertyProxy& operator=(const Value& value);
197 // get the proxied element
198 inline json_t* as_json() const;
201 // array object we wrap
204 // iterator pointing to property
211 } // namespace json::detail
213 // represents any JSON value
214 class Value : public detail::ValueBase<detail::Basic> {
216 // construct Value from input
217 explicit inline Value(const char* value);
218 explicit inline Value(const std::string& value);
219 explicit inline Value(bool value);
220 explicit inline Value(signed int value);
221 explicit inline Value(unsigned int value);
222 explicit inline Value(signed short value);
223 explicit inline Value(unsigned short value);
224 explicit inline Value(signed long value);
225 explicit inline Value(unsigned long value);
226 explicit inline Value(float value);
227 explicit inline Value(double value);
230 Value() : detail::ValueBase<detail::Basic>() {}
232 // copy constructor for base
233 Value(const detail::Basic& value) : detail::ValueBase<detail::Basic>(value) {}
235 // copy constructor for base
236 Value(const detail::ValueBase<detail::Basic>& value) : detail::ValueBase<detail::Basic>(value) {}
239 Value(const Value& value) : detail::ValueBase<detail::Basic>(value) {}
241 // create reference to value
242 explicit Value(json_t* json) : detail::ValueBase<detail::Basic>(json) {}
245 // iterators over a JSON object
248 // construct a new iterator for a given object
249 inline Iterator(const Value& value);
251 // construct a new iterator for a given object
252 inline Iterator(const detail::ValueBase<detail::PropertyProxy>& value);
254 // increment iterator
257 inline Iterator& operator++();
259 // test if iterator is still valid
260 inline bool valid() const;
262 inline operator bool() const;
265 inline const char* ckey() const;
267 inline std::string key() const;
270 inline const Value value() const;
273 inline const Value operator*() const;
277 Iterator(const Iterator&);
278 Iterator& operator=(const Iterator&);
280 // object being iterated over
287 // create a new empty object
288 inline Value object();
290 // create a new empty array
291 inline Value array();
293 // create a new null value
296 // load a file as a JSON value
297 inline Value load_file(const char* path, json_error_t* error = 0);
298 inline Value load_file(const std::string& path, json_error_t* error = 0);
300 // load a string as a JSON value
301 inline Value loads(const char* string, json_error_t* error = 0);
302 inline Value loads(const std::string& string, json_error_t* error = 0);
306 // stream JSON value out -- inefficient and not recommended for production use
307 inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
309 // read JSON value -- inefficient and not recommended for production use
310 inline std::istream& operator>>(std::istream& is, json::Value& value);
312 // include implementation code
313 #define IN_JANSSON_HPP
314 #include "jansson.ipp"
315 #undef IN_JANSSON_HPP
317 #endif // defined(JANSSON_HPP)