aboutsummaryrefslogtreecommitdiff
path: root/src/jansson.hpp
blob: f435493c453c8e5a85421132733568db73081b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
// Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
//
// Jansson is free software; you can redistribute it and/or modify
// it under the terms of the MIT license. See LICENSE for details.

#ifndef JANSSON_HPP
#define JANSSON_HPP

#include <string>
#include <ostream>
#include <istream>
#include <sstream>

// Included so that standard functions don't end up in namespace json
#include <cstdio>

// For free()
#include <cstdlib>

namespace json {
    // include Jansson C library into the json namespace
#   include <jansson.h>

    class Iterator;
    class Value;

    // implementation details; do not use directly
    namespace detail {
        class ElementProxy;
        class PropertyProxy;

        // base class for JSON value interface
        template <typename _Base>
        class ValueBase : public _Base {
        public:
            // empty constructor
            ValueBase() : _Base() {}

            // copy constructor
            ValueBase(const _Base& base) : _Base(base) {}

            // create reference to value
            ValueBase(json_t* json) : _Base(json) {}

            // assignment operator
            inline ValueBase& operator=(const Value& value);

            // check value type
            inline bool is_undefined() const;
            inline bool is_object() const;
            inline bool is_array() const;
            inline bool is_string() const;
            inline bool is_integer() const;
            inline bool is_real() const;
            inline bool is_number() const;
            inline bool is_true() const;
            inline bool is_false() const;
            inline bool is_boolean() const;
            inline bool is_null() const;

            // get size of array or object
            inline unsigned int size() const;

            // get value at array index (const version)
            inline const Value at(unsigned int index) const;

            inline const Value operator[](signed int index) const;
            inline const Value operator[](unsigned int index) const;
            inline const Value operator[](signed short index) const;
            inline const Value operator[](unsigned short index) const;
            inline const Value operator[](signed long index) const;
            inline const Value operator[](unsigned long index) const;

            // get value at array index (non-const version)
            inline ValueBase<ElementProxy> at(unsigned int index);

            inline ValueBase<ElementProxy> operator[](signed int index);
            inline ValueBase<ElementProxy> operator[](unsigned int index);
            inline ValueBase<ElementProxy> operator[](signed short index);
            inline ValueBase<ElementProxy> operator[](unsigned short index);
            inline ValueBase<ElementProxy> operator[](signed long index);
            inline ValueBase<ElementProxy> operator[](unsigned long index);

            // get object property (const version)
            inline const Value get(const char* key) const;

            inline const Value get(const std::string& key) const;
            inline const Value operator[](const char* key) const;
            inline const Value operator[](const std::string& key) const;

            // get object property (non-const version)
            inline ValueBase<PropertyProxy> get(const char* key);

            inline ValueBase<PropertyProxy> get(const std::string& key);
            inline ValueBase<PropertyProxy> operator[](const char* key);
            inline ValueBase<PropertyProxy> operator[](const std::string& key);

            // clear all array/object values
            inline void clear();

            // get value cast to specified type
            inline const char* as_cstring() const;
            inline std::string as_string() const;
            inline int as_integer() const;
            inline double as_real() const;
            inline double as_number() const;
            inline bool as_boolean() const;

            // set an object property (converts value to object is not one already)
            inline _Base& set_key(const char* key, const Value& value);

            inline _Base& set_key(const std::string& key, const Value& value);

            // set an array index (converts value to object is not one already)
            inline _Base& set_at(unsigned int index, const Value& value);

            // delete an object key
            inline _Base& del_key(const char* key);

            inline _Base& del_key(const std::string& key);

            // delete an item from an array by index
            inline _Base& del_at(unsigned int index);

            // insert an item into an array at a given index
            inline _Base& insert_at(unsigned int index, const Value& value);

            // write the value to a file
            inline int save_file(const char* path, int flags = 0) const;

            // write the value to a string (caller must deallocate with free()!)
            inline char* save_string(int flags = 0) const;
        };

        // represents any JSON value, private base
        class Basic {
        public:
            // construct new Value with an undefined value
            Basic() : _value(0) {}

            // copy constructor
            Basic(const Basic& value) : _value(json_incref(value._value)) {}

            // make a reference to an existing json_t value
            explicit Basic(json_t* value) : _value(json_incref(value)) {}

            // free Value resources
            inline ~Basic();

            // copy an existing Value
            inline Basic& operator=(const Basic& e);

            // get the underlying json_t
            inline json_t* as_json() const;

            // take ownership of a json_t (does not increase reference count)
            inline static Basic take_ownership(json_t* json);

        protected:
            // internal value pointer
            json_t* _value;
        };

        // proxies an array element
        class ElementProxy {
        public:
            // constructor
            ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}

            // assign to the proxied element
            inline ElementProxy& operator=(const Value& value);

            // get the proxied element
            inline json_t* as_json() const;

        private:
            // array object we wrap
            json_t* _array;

            // index of property
            unsigned int _index;
        };

        // proxies an object property
        class PropertyProxy {
        public:
            // constructor
            PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}

            // assign to the proxied element
            inline PropertyProxy& operator=(const Value& value);

            // get the proxied element
            inline json_t* as_json() const;

        private:
            // array object we wrap
            json_t* _object;

            // key of property
            const char* _key;
        };

    } // namespace json::detail

    // represents any JSON value
    class Value : public detail::ValueBase<detail::Basic> {
    public:
        // construct Value from input
        explicit inline Value(const char* value);
        explicit inline Value(const std::string& value);
        explicit inline Value(bool value);
        explicit inline Value(signed int value);
        explicit inline Value(unsigned int value);
        explicit inline Value(signed short value);
        explicit inline Value(unsigned short value);
        explicit inline Value(signed long value);
        explicit inline Value(unsigned long value);
        explicit inline Value(float value);
        explicit inline Value(double value);

        // empty constructor
        Value() : detail::ValueBase<detail::Basic>() {}

        // copy constructor for base
        Value(const detail::Basic& value) : detail::ValueBase<detail::Basic>(value) {}

        // copy constructor for base
        Value(const detail::ValueBase<detail::Basic>& value) : detail::ValueBase<detail::Basic>(value) {}

        // copy constructor
        Value(const Value& value) : detail::ValueBase<detail::Basic>(value) {}

        // create reference to value
        explicit Value(json_t* json) : detail::ValueBase<detail::Basic>(json) {}
    };

    // iterators over a JSON object
    class Iterator {
    public:
        // construct a new iterator for a given object
        inline Iterator(const Value& value);

        // construct a new iterator for a given object
        inline Iterator(const detail::ValueBase<detail::PropertyProxy>& value);

        // increment iterator
        inline void next();

        inline Iterator& operator++();

        // test if iterator is still valid
        inline bool valid() const;

        inline operator bool() const;

        // get key
        inline const char* ckey() const;

        inline std::string key() const;

        // get value
        inline const Value value() const;

        // dereference value
        inline const Value operator*() const;

    private:
        // disallow copying
        Iterator(const Iterator&);
        Iterator& operator=(const Iterator&);

        // object being iterated over
        Value _object;

        // iterator value
        void* _iter;
    };

    // create a new empty object
    inline Value object();

    // create a new empty array
    inline Value array();

    // create a new null value
    inline Value null();

    // load a file as a JSON value
    inline Value load_file(const char* path, json_error_t* error = 0);

    // load a string as a JSON value
    inline Value load_string(const char* string, json_error_t* error = 0);

} // namespace json

// stream JSON value out -- inefficient and not recommended for production use
inline std::ostream& operator<<(std::ostream& os, const json::Value& value);

// read JSON value -- inefficient and not recommended for production use
inline std::istream& operator>>(std::istream& is, json::Value& value);

// include implementation code
#define IN_JANSSON_HPP
#include "jansson.ipp"
#undef IN_JANSSON_HPP

#endif // defined(JANSSON_HPP)