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;
132 // write the value to a string (caller must deallocate with free()!)
133 inline char* dumps(int flags = 0) const;
136 // represents any JSON value, private base
139 // construct new Value with an undefined value
140 Basic() : _value(0) {}
143 Basic(const Basic& value) : _value(json_incref(value._value)) {}
145 // make a reference to an existing json_t value
146 explicit Basic(json_t* value) : _value(json_incref(value)) {}
148 // free Value resources
151 // copy an existing Value
152 inline Basic& operator=(const Basic& e);
154 // get the underlying json_t
155 inline json_t* as_json() const;
157 // take ownership of a json_t (does not increase reference count)
158 inline static Basic take_ownership(json_t* json);
161 // internal value pointer
165 // proxies an array element
169 ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
171 // assign to the proxied element
172 inline ElementProxy& operator=(const Value& value);
174 // get the proxied element
175 inline json_t* as_json() const;
178 // array object we wrap
185 // proxies an object property
186 class PropertyProxy {
189 PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
191 // assign to the proxied element
192 inline PropertyProxy& operator=(const Value& value);
194 // get the proxied element
195 inline json_t* as_json() const;
198 // array object we wrap
205 } // namespace json::detail
207 // represents any JSON value
208 class Value : public detail::ValueBase<detail::Basic> {
210 // construct Value from input
211 explicit inline Value(const char* value);
212 explicit inline Value(const std::string& value);
213 explicit inline Value(bool value);
214 explicit inline Value(signed int value);
215 explicit inline Value(unsigned int value);
216 explicit inline Value(signed short value);
217 explicit inline Value(unsigned short value);
218 explicit inline Value(signed long value);
219 explicit inline Value(unsigned long value);
220 explicit inline Value(float value);
221 explicit inline Value(double value);
224 Value() : detail::ValueBase<detail::Basic>() {}
226 // copy constructor for base
227 Value(const detail::Basic& value) : detail::ValueBase<detail::Basic>(value) {}
229 // copy constructor for base
230 Value(const detail::ValueBase<detail::Basic>& value) : detail::ValueBase<detail::Basic>(value) {}
233 Value(const Value& value) : detail::ValueBase<detail::Basic>(value) {}
235 // create reference to value
236 explicit Value(json_t* json) : detail::ValueBase<detail::Basic>(json) {}
239 // iterators over a JSON object
242 // construct a new iterator for a given object
243 inline Iterator(const Value& value);
245 // construct a new iterator for a given object
246 inline Iterator(const detail::ValueBase<detail::PropertyProxy>& value);
248 // increment iterator
251 inline Iterator& operator++();
253 // test if iterator is still valid
254 inline bool valid() const;
256 inline operator bool() const;
259 inline const char* ckey() const;
261 inline std::string key() const;
264 inline const Value value() const;
267 inline const Value operator*() const;
271 Iterator(const Iterator&);
272 Iterator& operator=(const Iterator&);
274 // object being iterated over
281 // create a new empty object
282 inline Value object();
284 // create a new empty array
285 inline Value array();
287 // create a new null value
290 // load a file as a JSON value
291 inline Value load_file(const char* path, json_error_t* error = 0);
293 // load a string as a JSON value
294 inline Value loads(const char* string, json_error_t* error = 0);
298 // stream JSON value out -- inefficient and not recommended for production use
299 inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
301 // read JSON value -- inefficient and not recommended for production use
302 inline std::istream& operator>>(std::istream& is, json::Value& value);
304 // include implementation code
305 #define IN_JANSSON_HPP
306 #include "jansson.ipp"
307 #undef IN_JANSSON_HPP
309 #endif // defined(JANSSON_HPP)