aboutsummaryrefslogtreecommitdiff
path: root/src/jansson.ipp
blob: b80d1af58c0ae25fd38f67cfa035cbdb9f0ffe50 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// 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 IN_JANSSON_HPP
#error "jansson.ipp may only be included from jansson.hpp"
#endif

namespace json {
    namespace detail {
        // assignment operator
        template <typename _Base>
        ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
            _Base::operator=(value);
            return *this;
        }

        // check value type
        template <typename _Base>
        bool ValueBase<_Base>::is_undefined() const {
            return _Base::as_json() == 0;
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_object() const {
            return json_is_object(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_array() const {
            return json_is_array(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_string() const {
            return json_is_string(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_integer() const {
            return json_is_integer(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_real() const {
            return json_is_real(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_number() const {
            return json_is_number(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_true() const {
            return json_is_true(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_false() const {
            return json_is_false(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_boolean() const {
            return json_is_boolean(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::is_null() const {
            return json_is_null(_Base::as_json());
        }

        // get size of array or object
        template <typename _Base>
        unsigned int ValueBase<_Base>::size() const {
            if (is_object())
                return json_object_size(_Base::as_json());
            else
                return json_array_size(_Base::as_json());
        }

        // get value at array index (const version)
        template <typename _Base>
        const Value ValueBase<_Base>::at(unsigned int index) const {
            return Value(json_array_get(_Base::as_json(), index));
        }

        template <typename _Base>
        const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
        template <typename _Base>
        const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
        template <typename _Base>
        const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
        template <typename _Base>
        const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
        template <typename _Base>
        const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
        template <typename _Base>
        const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }

        // get value at array index (non-const version)
        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
            return ElementProxy(_Base::as_json(), index);
        }

        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
            return at(index);
        }

        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
            return at(index);
        }

        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
            return at(index);
        }

        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
            return at(index);
        }

        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
            return at(index);
        }

        template <typename _Base>
        ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
            return at(index);
        }

        // get object property (const version)
        template <typename _Base>
        const Value ValueBase<_Base>::get(const char* key) const {
            return Value(json_object_get(_Base::as_json(), key));
        }

        template <typename _Base>
        const Value ValueBase<_Base>::get(const std::string& key) const {
            return get(key.c_str());
        }

        template <typename _Base>
        const Value ValueBase<_Base>::operator[](const char* key) const {
            return get(key);
        }

        template <typename _Base>
        const Value ValueBase<_Base>::operator[](const std::string& key) const {
            return get(key.c_str());
        }

        // get object property (non-const version)
        template <typename _Base>
        ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
            return PropertyProxy(_Base::as_json(), key);
        }

        template <typename _Base>
        ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
            return get(key.c_str());
        }

        template <typename _Base>
        ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
            return get(key);
        }

        template <typename _Base>
        ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
            return get(key.c_str());
        }

        // clear all array/object values
        template <typename _Base>
        void ValueBase<_Base>::clear() {
            if (is_object())
                json_object_clear(_Base::as_json());
            else
                json_array_clear(_Base::as_json());
        }

        // get value cast to specified type
        template <typename _Base>
        const char* ValueBase<_Base>::as_cstring() const {
            return json_string_value(_Base::as_json());
        }

        template <typename _Base>
        std::string ValueBase<_Base>::as_string() const {
            const char* tmp = as_cstring();
            return tmp == 0 ? "" : tmp;
        }

        template <typename _Base>
        int ValueBase<_Base>::as_integer() const {
            return json_integer_value(_Base::as_json());
        }

        template <typename _Base>
        double ValueBase<_Base>::as_real() const {
            return json_real_value(_Base::as_json());
        }

        template <typename _Base>
        double ValueBase<_Base>::as_number() const {
            return json_number_value(_Base::as_json());
        }

        template <typename _Base>
        bool ValueBase<_Base>::as_boolean() const {
            return is_true();
        }

        // set an object property (converts value to object is not one already)
        template <typename _Base>
        _Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
            json_object_set(_Base::as_json(), key, value._Base::as_json());
            return *this;
        }

        template <typename _Base>
        _Base& ValueBase<_Base>::set_key(const std::string& key, const Value& value) {
            return set_key(key.c_str(), value);
        }

        // set an array index (converts value to object is not one already)
        template <typename _Base>
        _Base& ValueBase<_Base>::set_at(unsigned int index, const Value& value) {
            if (index == size())
                json_array_append(_Base::as_json(), value._Base::as_json());
            else
                json_array_set(_Base::as_json(), index, value._Base::as_json());
            return *this;
        }

        // delete an object key
        template <typename _Base>
        _Base& ValueBase<_Base>::del_key(const char* key) {
            json_object_del(_Base::as_json(), key);
            return *this;
        }

        template <typename _Base>
        _Base& ValueBase<_Base>::del_key(const std::string& key) {
            return del_key(key.c_str());
        }

        // delete an item from an array by index
        template <typename _Base>
        _Base& ValueBase<_Base>::del_at(unsigned int index) {
            json_array_remove(_Base::as_json(), index);
            return *this;
        }

        // insert an item into an array at a given index
        template <typename _Base>
        _Base& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
            json_array_insert(_Base::as_json(), index, value._Base::as_json());
            return *this;
        }

        // write the value to a file
        template <typename _Base>
        int ValueBase<_Base>::save_file(const char* path, int flags) const {
            return json_dump_file(_Base::as_json(), path, flags);
        }

        // write the value to a string (caller must deallocate with free()!)
        template <typename _Base>
        char* ValueBase<_Base>::save_string(int flags) const {
            return json_dumps(_Base::as_json(), flags);
        }

        Basic::~Basic() {
            json_decref(_value);
        }

        // copy an existing Value
        Basic& Basic::operator=(const Basic& e) {
            if (&e != this) {
                json_decref(_value);
                _value = json_incref(e._value);
            }
            return *this;
        }

        // get the underlying json_t
        json_t* Basic::as_json() const {
            return _value;
        }

        // take ownership of a json_t (does not increase reference count)
        Basic Basic::take_ownership(json_t* json) {
            Basic v;
            v._value = json;
            return v;
        }

        // assign value to proxied array element
        ElementProxy& ElementProxy::operator=(const Value& value) {
            json_array_set(_array, _index, value.as_json());
            return *this;
        }

        // get the proxied element
        json_t* ElementProxy::as_json() const {
            return json_array_get(_array, _index);
        }

        // assign value to proxied object property
        PropertyProxy& PropertyProxy::operator=(const Value& value) {
            json_object_set(_object, _key, value.as_json());
            return *this;
        }

        json_t* PropertyProxy::as_json() const {
            return json_object_get(_object, _key);
        }

    } // namespace json::detail

    // construct Value::Value input
    Value::Value(const char* value) {
        _value = json_string(value);
    }

    Value::Value(const std::string& value) {
        _value = json_string(value.c_str());
    }

    Value::Value(bool value) {
        _value = value ? json_true() : json_false();
    }

    Value::Value(signed int value) {
        _value = json_integer(value);
    }

    Value::Value(unsigned int value) {
        _value = json_integer(value);
    }

    Value::Value(signed short value) {
        _value = json_integer(value);
    }

    Value::Value(unsigned short value) {
        _value = json_integer(value);
    }

    Value::Value(signed long value) {
        _value = json_integer(value);
    }

    Value::Value(unsigned long value) {
        _value = json_integer(value);
    }

    Value::Value(float value) {
        _value = json_real(value);
    }

    Value::Value(double value) {
        _value = json_real(value);
    }

    // construct a new iterator for a given object
    Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
        _iter = json_object_iter(_object.as_json());
    }

    // construct a new iterator for a given object
    Iterator::Iterator(const detail::ValueBase<detail::PropertyProxy>& value) :
        _object(value.as_json()), _iter(0) {
        _iter = json_object_iter(_object.as_json());
    }

    // increment iterator
    void Iterator::next() {
        _iter = json_object_iter_next(_object.as_json(), _iter);
    }

    Iterator& Iterator::operator++() { next(); return *this; }

    // test if iterator is still valid
    bool Iterator::valid() const {
        return _iter != 0;
    }

    Iterator::operator bool() const {
        return valid();
    }

    // get key
    const char* Iterator::ckey() const {
        return json_object_iter_key(_iter);
    }

    std::string Iterator::key() const {
        return ckey();
    }

    // get value
    const Value Iterator::value() const {
        return Value(json_object_iter_value(_iter));
    }

    // dereference value
    const Value Iterator::operator*() const {
        return value();
    }

    // create a new empty object
    Value object() {
        return Value::take_ownership(json_object());
    }

    // create a new empty array
    Value array() {
        return Value::take_ownership(json_array());
    }

    // create a new null value
    Value null() {
        return Value::take_ownership(json_null());
    }

    // load a file as a JSON value
    Value load_file(const char* path, json_error_t* error) {
        return Value::take_ownership(json_load_file(path, error));
    }

    // load a string as a JSON value
    Value load_string(const char* string, json_error_t* error) {
        return Value::take_ownership(json_loads(string, error));
    }

} // namespace json

// stream JSON value out
std::ostream& operator<<(std::ostream& os, const json::Value& value) {
    // get the temporary serialize string
    char* tmp = value.save_string();
    if (tmp != 0) {
        // stream temp string out and release it
        os << tmp;
        free(tmp);
    }
    return os;
}

// read JSON value
std::istream& operator>>(std::istream& is, json::Value& value) {
    // buffer the remaining bytes into a single string for Jansson
    std::stringstream tmp;
    while (is)
        tmp << static_cast<char>(is.get());
    // parse the buffered string
    value = json::load_string(tmp.str().c_str());
    return is;
}