1 // Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
3 // Jansson is free software; you can redistribute it and/or modify
4 // it under the terms of the MIT license. See LICENSE for details.
6 #if !defined(JANSSON_HPP)
16 // include Jansson C library into the json namespace
22 // implementation details; do not use directly
27 // base class for JSON value interface
28 template <typename _Base>
29 class ValueBase : public _Base {
32 ValueBase() : _Base() {}
35 ValueBase(const _Base& base) : _Base(base) {}
37 // create reference to value
38 ValueBase(json_t* json) : _Base(json) {}
40 // assignment operator
41 inline ValueBase& operator=(const Value& value);
44 inline bool is_undefined() const;
45 inline bool is_object() const;
46 inline bool is_array() const;
47 inline bool is_string() const;
48 inline bool is_integer() const;
49 inline bool is_real() const;
50 inline bool is_number() const;
51 inline bool is_true() const;
52 inline bool is_false() const;
53 inline bool is_boolean() const;
54 inline bool is_null() const;
56 // get size of array or object
57 inline unsigned int size() const;
59 // get value at array index (const version)
60 inline const Value at(unsigned int index) const;
62 inline const Value operator[](signed int index) const;
63 inline const Value operator[](unsigned int index) const;
64 inline const Value operator[](signed short index) const;
65 inline const Value operator[](unsigned short index) const;
66 inline const Value operator[](signed long index) const;
67 inline const Value operator[](unsigned long index) const;
69 // get value at array index (non-const version)
70 inline ValueBase<ElementProxy> at(unsigned int index);
72 inline ValueBase<ElementProxy> operator[](signed int index);
73 inline ValueBase<ElementProxy> operator[](unsigned int index);
74 inline ValueBase<ElementProxy> operator[](signed short index);
75 inline ValueBase<ElementProxy> operator[](unsigned short index);
76 inline ValueBase<ElementProxy> operator[](signed long index);
77 inline ValueBase<ElementProxy> operator[](unsigned long index);
79 // get object property (const version)
80 inline const Value get(const char* key) const;
82 inline const Value get(const std::string& key) const;
83 inline const Value operator[](const char* key) const;
84 inline const Value operator[](const std::string& key) const;
86 // get object property (non-const version)
87 inline ValueBase<PropertyProxy> get(const char* key);
89 inline ValueBase<PropertyProxy> get(const std::string& key);
90 inline ValueBase<PropertyProxy> operator[](const char* key);
91 inline ValueBase<PropertyProxy> operator[](const std::string& key);
93 // clear all array/object values
96 // get value cast to specified type
97 inline const char* as_cstring() const;
98 inline std::string as_string() const;
99 inline int as_integer() const;
100 inline double as_real() const;
101 inline double as_number() const;
102 inline bool as_boolean() const;
104 // set an object property (converts value to object is not one already)
105 inline _Base& set_key(const char* key, const Value& value);
107 inline _Base& set_key(const std::string& key, const Value& value);
109 // set an array index (converts value to object is not one already)
110 inline _Base& set_at(unsigned int index, const Value& value);
112 // delete an object key
113 inline _Base& del_key(const char* key);
115 inline _Base& del_key(const std::string& key);
117 // delete an item from an array by index
118 inline _Base& del_at(unsigned int index);
120 // insert an item into an array at a given index
121 inline _Base& insert_at(unsigned int index, const Value& value);
123 // write the value to a file
124 inline int save_file(const char* path, int flags = 0) const;
126 // write the value to a string (caller must deallocate with free()!)
127 inline char* save_string(int flags = 0) const;
130 // represents any JSON value, private base
133 // construct new Value with an undefined value
134 Basic() : _value(0) {}
137 Basic(const Basic& value) : _value(json_incref(value._value)) {}
139 // make a reference to an existing json_t value
140 explicit Basic(json_t* value) : _value(json_incref(value)) {}
142 // free Value resources
145 // copy an existing Value
146 inline Basic& operator=(const Basic& e);
148 // get the underlying json_t
149 inline json_t* as_json() const;
151 // take ownership of a json_t (does not increase reference count)
152 inline static Basic take_ownership(json_t* json);
155 // internal value pointer
159 // proxies an array element
163 ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
165 // assign to the proxied element
166 inline ElementProxy& operator=(const Value& value);
168 // get the proxied element
169 inline json_t* as_json() const;
172 // array object we wrap
179 // proxies an object property
180 class PropertyProxy {
183 PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
185 // assign to the proxied element
186 inline PropertyProxy& operator=(const Value& value);
188 // get the proxied element
189 inline json_t* as_json() const;
192 // array object we wrap
199 } // namespace json::_private
201 // represents any JSON value
202 class Value : public _private::ValueBase<_private::Basic> {
204 // construct Value from input
205 explicit inline Value(const char* value);
206 explicit inline Value(const std::string& value);
207 explicit inline Value(bool value);
208 explicit inline Value(signed int value);
209 explicit inline Value(unsigned int value);
210 explicit inline Value(signed short value);
211 explicit inline Value(unsigned short value);
212 explicit inline Value(signed long value);
213 explicit inline Value(unsigned long value);
214 explicit inline Value(float value);
215 explicit inline Value(double value);
218 Value() : _private::ValueBase<_private::Basic>() {}
220 // copy constructor for base
221 Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
223 // copy constructor for base
224 Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
227 Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
229 // create reference to value
230 explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
233 // iterators over a JSON object
236 // construct a new iterator for a given object
237 inline Iterator(const Value& value);
239 // construct a new iterator for a given object
240 inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
242 // increment iterator
245 inline Iterator& operator++();
247 // test if iterator is still valid
248 inline bool valid() const;
250 inline operator bool() const;
253 inline const char* ckey() const;
255 inline std::string key() const;
258 inline const Value value() const;
261 inline const Value operator*() const;
265 Iterator(const Iterator&);
266 Iterator& operator=(const Iterator&);
268 // object being iterated over
275 // create a new empty object
276 inline Value object();
278 // create a new empty array
279 inline Value array();
281 // create a new null value
284 // load a file as a JSON value
285 inline Value load_file(const char* path, json_error_t* error = 0);
287 // load a string as a JSON value
288 inline Value load_string(const char* string, json_error_t* error = 0);
292 // stream JSON value out -- inefficient and not recommended for production use
293 inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
295 // read JSON value -- inefficient and not recommended for production use
296 inline std::istream& operator>>(std::istream& is, json::Value& value);
298 // include implementation code
299 #define IN_JANSSON_HPP 1
300 #include "jansson-impl.hpp"
301 #undef IN_JANSSON_HPP
303 #endif // defined(JANSSON_HPP)