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.
17 // include Jansson C library into the json namespace
23 // implementation details; do not use directly
28 // base class for JSON value interface
29 template <typename _Base>
30 class ValueBase : public _Base {
33 ValueBase() : _Base() {}
36 ValueBase(const _Base& base) : _Base(base) {}
38 // create reference to value
39 ValueBase(json_t* json) : _Base(json) {}
41 // assignment operator
42 inline ValueBase& operator=(const Value& value);
45 inline bool is_undefined() const;
46 inline bool is_object() const;
47 inline bool is_array() const;
48 inline bool is_string() const;
49 inline bool is_integer() const;
50 inline bool is_real() const;
51 inline bool is_number() const;
52 inline bool is_true() const;
53 inline bool is_false() const;
54 inline bool is_boolean() const;
55 inline bool is_null() const;
57 // get size of array or object
58 inline unsigned int size() const;
60 // get value at array index (const version)
61 inline const Value at(unsigned int index) const;
63 inline const Value operator[](signed int index) const;
64 inline const Value operator[](unsigned int index) const;
65 inline const Value operator[](signed short index) const;
66 inline const Value operator[](unsigned short index) const;
67 inline const Value operator[](signed long index) const;
68 inline const Value operator[](unsigned long index) const;
70 // get value at array index (non-const version)
71 inline ValueBase<ElementProxy> at(unsigned int index);
73 inline ValueBase<ElementProxy> operator[](signed int index);
74 inline ValueBase<ElementProxy> operator[](unsigned int index);
75 inline ValueBase<ElementProxy> operator[](signed short index);
76 inline ValueBase<ElementProxy> operator[](unsigned short index);
77 inline ValueBase<ElementProxy> operator[](signed long index);
78 inline ValueBase<ElementProxy> operator[](unsigned long index);
80 // get object property (const version)
81 inline const Value get(const char* key) const;
83 inline const Value get(const std::string& key) const;
84 inline const Value operator[](const char* key) const;
85 inline const Value operator[](const std::string& key) const;
87 // get object property (non-const version)
88 inline ValueBase<PropertyProxy> get(const char* key);
90 inline ValueBase<PropertyProxy> get(const std::string& key);
91 inline ValueBase<PropertyProxy> operator[](const char* key);
92 inline ValueBase<PropertyProxy> operator[](const std::string& key);
94 // clear all array/object values
97 // get value cast to specified type
98 inline const char* as_cstring() const;
99 inline std::string as_string() const;
100 inline int as_integer() const;
101 inline double as_real() const;
102 inline double as_number() const;
103 inline bool as_boolean() const;
105 // set an object property (converts value to object is not one already)
106 inline _Base& set_key(const char* key, const Value& value);
108 inline _Base& set_key(const std::string& key, const Value& value);
110 // set an array index (converts value to object is not one already)
111 inline _Base& set_at(unsigned int index, const Value& value);
113 // delete an object key
114 inline _Base& del_key(const char* key);
116 inline _Base& del_key(const std::string& key);
118 // delete an item from an array by index
119 inline _Base& del_at(unsigned int index);
121 // insert an item into an array at a given index
122 inline _Base& insert_at(unsigned int index, const Value& value);
124 // write the value to a file
125 inline int save_file(const char* path, int flags = 0) const;
127 // write the value to a string (caller must deallocate with free()!)
128 inline char* save_string(int flags = 0) const;
131 // represents any JSON value, private base
134 // construct new Value with an undefined value
135 Basic() : _value(0) {}
138 Basic(const Basic& value) : _value(json_incref(value._value)) {}
140 // make a reference to an existing json_t value
141 explicit Basic(json_t* value) : _value(json_incref(value)) {}
143 // free Value resources
146 // copy an existing Value
147 inline Basic& operator=(const Basic& e);
149 // get the underlying json_t
150 inline json_t* as_json() const;
152 // take ownership of a json_t (does not increase reference count)
153 inline static Basic take_ownership(json_t* json);
156 // internal value pointer
160 // proxies an array element
164 ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
166 // assign to the proxied element
167 inline ElementProxy& operator=(const Value& value);
169 // get the proxied element
170 inline json_t* as_json() const;
173 // array object we wrap
180 // proxies an object property
181 class PropertyProxy {
184 PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
186 // assign to the proxied element
187 inline PropertyProxy& operator=(const Value& value);
189 // get the proxied element
190 inline json_t* as_json() const;
193 // array object we wrap
200 } // namespace json::detail
202 // represents any JSON value
203 class Value : public detail::ValueBase<detail::Basic> {
205 // construct Value from input
206 explicit inline Value(const char* value);
207 explicit inline Value(const std::string& value);
208 explicit inline Value(bool value);
209 explicit inline Value(signed int value);
210 explicit inline Value(unsigned int value);
211 explicit inline Value(signed short value);
212 explicit inline Value(unsigned short value);
213 explicit inline Value(signed long value);
214 explicit inline Value(unsigned long value);
215 explicit inline Value(float value);
216 explicit inline Value(double value);
219 Value() : detail::ValueBase<detail::Basic>() {}
221 // copy constructor for base
222 Value(const detail::Basic& value) : detail::ValueBase<detail::Basic>(value) {}
224 // copy constructor for base
225 Value(const detail::ValueBase<detail::Basic>& value) : detail::ValueBase<detail::Basic>(value) {}
228 Value(const Value& value) : detail::ValueBase<detail::Basic>(value) {}
230 // create reference to value
231 explicit Value(json_t* json) : detail::ValueBase<detail::Basic>(json) {}
234 // iterators over a JSON object
237 // construct a new iterator for a given object
238 inline Iterator(const Value& value);
240 // construct a new iterator for a given object
241 inline Iterator(const detail::ValueBase<detail::PropertyProxy>& value);
243 // increment iterator
246 inline Iterator& operator++();
248 // test if iterator is still valid
249 inline bool valid() const;
251 inline operator bool() const;
254 inline const char* ckey() const;
256 inline std::string key() const;
259 inline const Value value() const;
262 inline const Value operator*() const;
266 Iterator(const Iterator&);
267 Iterator& operator=(const Iterator&);
269 // object being iterated over
276 // create a new empty object
277 inline Value object();
279 // create a new empty array
280 inline Value array();
282 // create a new null value
285 // load a file as a JSON value
286 inline Value load_file(const char* path, json_error_t* error = 0);
288 // load a string as a JSON value
289 inline Value load_string(const char* string, json_error_t* error = 0);
293 // stream JSON value out -- inefficient and not recommended for production use
294 inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
296 // read JSON value -- inefficient and not recommended for production use
297 inline std::istream& operator>>(std::istream& is, json::Value& value);
299 // include implementation code
300 #define IN_JANSSON_HPP
301 #include "jansson.ipp"
302 #undef IN_JANSSON_HPP
304 #endif // defined(JANSSON_HPP)