json.cc 48.8 KB
Newer Older
N
tidy up  
Niels 已提交
1 2 3 4 5 6 7 8 9 10 11
/*!
@file
@copyright The code is licensed under the MIT License
           <http://opensource.org/licenses/MIT>,
           Copyright (c) 2013-2014 Niels Lohmann.

@author Niels Lohmann <http://nlohmann.me>

@see https://github.com/nlohmann/json
*/

N
Niels 已提交
12
#include "json.h"
N
Niels 已提交
13

N
Niels 已提交
14 15 16 17
#include <cctype>     // std::isdigit, std::isspace
#include <cstddef>    // size_t
#include <stdexcept>  // std::runtime_error
#include <utility>    // std::swap, std::move
N
Niels 已提交
18

N
Niels 已提交
19 20 21 22

////////////////////
// STATIC MEMBERS //
////////////////////
N
Niels 已提交
23

N
Niels 已提交
24
std::mutex json::token_;
N
Niels 已提交
25 26 27 28 29 30


///////////////////////////////////
// CONSTRUCTORS OF UNION "value" //
///////////////////////////////////

N
Niels 已提交
31
json::value::value(array_t* _array): array(_array) {}
N
Niels 已提交
32
json::value::value(object_t* object_): object(object_) {}
N
Niels 已提交
33 34 35 36
json::value::value(string_t* _string): string(_string) {}
json::value::value(boolean_t _boolean) : boolean(_boolean) {}
json::value::value(number_t _number) : number(_number) {}
json::value::value(number_float_t _number_float) : number_float(_number_float) {}
N
Niels 已提交
37

N
Niels 已提交
38 39 40 41

/////////////////////////////////
// CONSTRUCTORS AND DESTRUCTOR //
/////////////////////////////////
N
Niels 已提交
42

N
Niels 已提交
43 44
/*!
Construct an empty JSON given the type.
N
Niels 已提交
45

N
Niels 已提交
46
@param t  the type from the @ref json::type enumeration.
N
Niels 已提交
47 48 49

@post Memory for array, object, and string are allocated.
*/
N
Niels 已提交
50
json::json(const value_type t) noexcept
N
Niels 已提交
51
    : type_(t)
N
Niels 已提交
52
{
N
Niels 已提交
53
    switch (type_)
N
Niels 已提交
54 55 56
    {
        case (value_type::array):
        {
N
Niels 已提交
57
            value_.array = new array_t();
N
Niels 已提交
58 59
            break;
        }
N
Niels 已提交
60 61
        case (value_type::object):
        {
N
Niels 已提交
62
            value_.object = new object_t();
N
Niels 已提交
63 64
            break;
        }
N
Niels 已提交
65 66
        case (value_type::string):
        {
N
Niels 已提交
67
            value_.string = new string_t();
N
Niels 已提交
68 69
            break;
        }
N
Niels 已提交
70 71
        case (value_type::boolean):
        {
N
Niels 已提交
72
            value_.boolean = boolean_t();
N
Niels 已提交
73 74
            break;
        }
N
Niels 已提交
75 76
        case (value_type::number):
        {
N
Niels 已提交
77
            value_.number = number_t();
N
Niels 已提交
78 79
            break;
        }
N
Niels 已提交
80 81
        case (value_type::number_float):
        {
N
Niels 已提交
82
            value_.number_float = number_float_t();
N
Niels 已提交
83 84
            break;
        }
N
Niels 已提交
85
        default:
N
Niels 已提交
86
        {
N
Niels 已提交
87 88 89 90 91
            break;
        }
    }
}

N
Niels 已提交
92 93 94
/*!
Construct a null JSON object.
*/
N
Niels 已提交
95
json::json(std::nullptr_t) noexcept : json()
N
Niels 已提交
96 97 98 99 100 101 102
{}

/*!
Construct a string JSON object.

@param s  a string to initialize the JSON object with
*/
N
Niels 已提交
103
json::json(const std::string& s) noexcept
N
Niels 已提交
104
    : type_(value_type::string), value_(new string_t(s))
N
Niels 已提交
105 106
{}

N
Niels 已提交
107
json::json(std::string&& s) noexcept
N
Niels 已提交
108
    : type_(value_type::string), value_(new string_t(std::move(s)))
N
Niels 已提交
109 110
{}

N
Niels 已提交
111
json::json(const char* s) noexcept
N
Niels 已提交
112
    : type_(value_type::string), value_(new string_t(s))
N
Niels 已提交
113 114
{}

N
Niels 已提交
115
json::json(const bool b) noexcept
N
Niels 已提交
116
    : type_(value_type::boolean), value_(b)
N
Niels 已提交
117 118
{}

N
Niels 已提交
119
json::json(const int i) noexcept
N
Niels 已提交
120
    : type_(value_type::number), value_(i)
N
Niels 已提交
121 122
{}

N
Niels 已提交
123
json::json(const double f) noexcept
N
Niels 已提交
124
    : type_(value_type::number_float), value_(f)
N
Niels 已提交
125 126
{}

N
Niels 已提交
127
json::json(const array_t& a) noexcept
N
Niels 已提交
128
    : type_(value_type::array), value_(new array_t(a))
N
Niels 已提交
129 130
{}

N
Niels 已提交
131
json::json(array_t&& a) noexcept
N
Niels 已提交
132
    : type_(value_type::array), value_(new array_t(std::move(a)))
N
Niels 已提交
133 134
{}

N
Niels 已提交
135
json::json(const object_t& o) noexcept
N
Niels 已提交
136
    : type_(value_type::object), value_(new object_t(o))
N
Niels 已提交
137 138
{}

N
Niels 已提交
139
json::json(object_t&& o) noexcept
N
Niels 已提交
140
    : type_(value_type::object), value_(new object_t(std::move(o)))
N
Niels 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{}

/*!
This function is a bit tricky as it uses an initializer list of JSON objects
for both arrays and objects. This is not supported by C++, so we use the
following trick. Both initializer lists for objects and arrays will transform
to a list of JSON objects. The only difference is that in case of an object,
the list will contain JSON array objects with two elements - one for the key
and one for the value. As a result, it is sufficient to check if each element
of the initializer list is an array (1) with two elements (2) whose first
element is of type string (3). If this is the case, we treat the whole
initializer list as list of pairs to construct an object. If not, we pass it
as is to create an array.

@bug With the described approach, we would fail to recognize an array whose
     first element is again an arrays as array.
*/
N
Niels 已提交
158
json::json(list_init_t a) noexcept
N
Niels 已提交
159 160 161 162 163
{
    // check if each element is an array with two elements whose first element
    // is a string
    for (const auto& element : a)
    {
N
Niels 已提交
164
        if (element.type_ != value_type::array or
N
Niels 已提交
165
                element.size() != 2 or
N
Niels 已提交
166
                element[0].type_ != value_type::string)
N
Niels 已提交
167 168 169
        {

            // the initializer list describes an array
N
Niels 已提交
170 171
            type_ = value_type::array;
            value_ = new array_t(a);
N
Niels 已提交
172 173 174 175 176
            return;
        }
    }

    // the initializer list is a list of pairs
N
Niels 已提交
177 178
    type_ = value_type::object;
    value_ = new object_t();
N
Niels 已提交
179
    for (const json& element : a)
N
Niels 已提交
180 181
    {
        const std::string k = element[0];
N
Niels 已提交
182
        value_.object->emplace(std::make_pair(std::move(k),
N
Niels 已提交
183
                                              std::move(element[1])));
N
Niels 已提交
184 185 186 187 188
    }
}

/*!
A copy constructor for the JSON class.
N
Niels 已提交
189

N
Niels 已提交
190 191
@param o  the JSON object to copy
*/
N
Niels 已提交
192
json::json(const json& o) noexcept
N
Niels 已提交
193
    : type_(o.type_)
N
Niels 已提交
194
{
N
Niels 已提交
195
    switch (type_)
N
Niels 已提交
196 197 198
    {
        case (value_type::array):
        {
N
Niels 已提交
199
            value_.array = new array_t(*o.value_.array);
N
Niels 已提交
200
            break;
N
Niels 已提交
201
        }
N
Niels 已提交
202 203
        case (value_type::object):
        {
N
Niels 已提交
204
            value_.object = new object_t(*o.value_.object);
N
Niels 已提交
205
            break;
N
Niels 已提交
206
        }
N
Niels 已提交
207 208
        case (value_type::string):
        {
N
Niels 已提交
209
            value_.string = new string_t(*o.value_.string);
N
Niels 已提交
210
            break;
N
Niels 已提交
211
        }
N
Niels 已提交
212 213
        case (value_type::boolean):
        {
N
Niels 已提交
214
            value_.boolean = o.value_.boolean;
N
Niels 已提交
215
            break;
N
Niels 已提交
216
        }
N
Niels 已提交
217 218
        case (value_type::number):
        {
N
Niels 已提交
219
            value_.number = o.value_.number;
N
Niels 已提交
220
            break;
N
Niels 已提交
221
        }
N
Niels 已提交
222 223
        case (value_type::number_float):
        {
N
Niels 已提交
224
            value_.number_float = o.value_.number_float;
N
Niels 已提交
225
            break;
N
Niels 已提交
226
        }
N
Niels 已提交
227
        default:
N
Niels 已提交
228
        {
N
Niels 已提交
229
            break;
N
Niels 已提交
230
        }
N
Niels 已提交
231 232 233
    }
}

N
Niels 已提交
234 235
/*!
A move constructor for the JSON class.
N
Niels 已提交
236

N
Niels 已提交
237 238 239 240
@param o  the JSON object to move

@post The JSON object \p o is invalidated.
*/
N
Niels 已提交
241
json::json(json&& o) noexcept
N
Niels 已提交
242
    : type_(std::move(o.type_)), value_(std::move(o.value_))
N
Niels 已提交
243 244
{
    // invalidate payload
N
Niels 已提交
245 246
    o.type_ = value_type::null;
    o.value_ = {};
N
Niels 已提交
247 248 249 250 251 252 253 254
}

/*!
A copy assignment operator for the JSON class, following the copy-and-swap
idiom.

@param o  A JSON object to assign to this object.
*/
N
Niels 已提交
255
json& json::operator=(json o) noexcept
N
Niels 已提交
256
{
N
Niels 已提交
257 258
    std::swap(type_, o.type_);
    std::swap(value_, o.value_);
N
Niels 已提交
259 260
    return *this;
}
N
Niels 已提交
261

N
Niels 已提交
262
json::~json() noexcept
N
Niels 已提交
263
{
N
Niels 已提交
264
    switch (type_)
N
Niels 已提交
265 266 267
    {
        case (value_type::array):
        {
N
Niels 已提交
268
            delete value_.array;
N
Niels 已提交
269 270
            break;
        }
N
Niels 已提交
271 272
        case (value_type::object):
        {
N
Niels 已提交
273
            delete value_.object;
N
Niels 已提交
274 275
            break;
        }
N
Niels 已提交
276 277
        case (value_type::string):
        {
N
Niels 已提交
278
            delete value_.string;
N
Niels 已提交
279 280
            break;
        }
N
Niels 已提交
281
        default:
N
Niels 已提交
282 283
        {
            // nothing to do for non-pointer types
N
Niels 已提交
284
            break;
N
Niels 已提交
285
        }
N
Niels 已提交
286 287 288
    }
}

N
Niels 已提交
289 290 291 292
/*!
@param s  a string representation of a JSON object
@return a JSON object
*/
N
Niels 已提交
293
json json::parse(const std::string& s)
N
Niels 已提交
294
{
N
Niels 已提交
295
    return parser(s).parse();
N
Niels 已提交
296 297 298 299 300 301
}

/*!
@param s  a string representation of a JSON object
@return a JSON object
*/
N
Niels 已提交
302
json json::parse(const char* s)
N
Niels 已提交
303
{
N
Niels 已提交
304
    return parser(s).parse();
N
Niels 已提交
305 306 307
}


N
Niels 已提交
308
const std::string json::type_name() const noexcept
N
Niels 已提交
309
{
N
Niels 已提交
310
    switch (type_)
N
Niels 已提交
311 312 313 314
    {
        case (value_type::array):
        {
            return "array";
N
Niels 已提交
315
        }
N
Niels 已提交
316 317 318
        case (value_type::object):
        {
            return "object";
N
Niels 已提交
319
        }
N
Niels 已提交
320 321 322
        case (value_type::null):
        {
            return "null";
N
Niels 已提交
323
        }
N
Niels 已提交
324 325 326
        case (value_type::string):
        {
            return "string";
N
Niels 已提交
327
        }
N
Niels 已提交
328 329 330
        case (value_type::boolean):
        {
            return "boolean";
N
Niels 已提交
331
        }
N
Niels 已提交
332
        default:
N
Niels 已提交
333 334
        {
            return "number";
N
Niels 已提交
335
        }
N
Niels 已提交
336 337 338 339
    }
}


N
Niels 已提交
340 341 342
///////////////////////////////
// OPERATORS AND CONVERSIONS //
///////////////////////////////
N
Niels 已提交
343

N
Niels 已提交
344 345 346 347 348
/*!
@exception std::logic_error if the function is called for JSON objects whose
    type is not string
*/
template<>
N
Niels 已提交
349
std::string json::get() const
N
Niels 已提交
350
{
N
Niels 已提交
351
    switch (type_)
N
Niels 已提交
352 353
    {
        case (value_type::string):
N
Niels 已提交
354
            return *value_.string;
N
Niels 已提交
355
        default:
N
Niels 已提交
356
            throw std::logic_error("cannot cast " + type_name() + " to JSON string");
N
Niels 已提交
357 358 359
    }
}

N
Niels 已提交
360 361 362 363 364
/*!
@exception std::logic_error if the function is called for JSON objects whose
    type is not number (int or float)
*/
template<>
N
Niels 已提交
365
int json::get() const
N
Niels 已提交
366
{
N
Niels 已提交
367
    switch (type_)
N
Niels 已提交
368 369
    {
        case (value_type::number):
N
Niels 已提交
370
            return value_.number;
N
Niels 已提交
371
        case (value_type::number_float):
N
Niels 已提交
372
            return static_cast<number_t>(value_.number_float);
N
Niels 已提交
373
        default:
N
Niels 已提交
374
            throw std::logic_error("cannot cast " + type_name() + " to JSON number");
N
Niels 已提交
375 376 377
    }
}

N
Niels 已提交
378 379 380 381 382
/*!
@exception std::logic_error if the function is called for JSON objects whose
    type is not number (int or float)
*/
template<>
N
Niels 已提交
383
double json::get() const
N
Niels 已提交
384
{
N
Niels 已提交
385
    switch (type_)
N
Niels 已提交
386 387
    {
        case (value_type::number):
N
Niels 已提交
388
            return static_cast<number_float_t>(value_.number);
N
Niels 已提交
389
        case (value_type::number_float):
N
Niels 已提交
390
            return value_.number_float;
N
Niels 已提交
391
        default:
N
Niels 已提交
392
            throw std::logic_error("cannot cast " + type_name() + " to JSON number");
N
Niels 已提交
393 394 395
    }
}

N
Niels 已提交
396 397 398 399 400
/*!
@exception std::logic_error if the function is called for JSON objects whose
    type is not boolean
*/
template<>
N
Niels 已提交
401
bool json::get() const
N
Niels 已提交
402
{
N
Niels 已提交
403
    switch (type_)
N
Niels 已提交
404 405
    {
        case (value_type::boolean):
N
Niels 已提交
406
            return value_.boolean;
N
Niels 已提交
407
        default:
N
Niels 已提交
408
            throw std::logic_error("cannot cast " + type_name() + " to JSON Boolean");
N
Niels 已提交
409 410 411
    }
}

N
Niels 已提交
412 413 414 415 416
/*!
@exception std::logic_error if the function is called for JSON objects whose
    type is an object
*/
template<>
N
Niels 已提交
417
json::array_t json::get() const
N
Niels 已提交
418
{
N
Niels 已提交
419
    if (type_ == value_type::array)
N
Niels 已提交
420
    {
N
Niels 已提交
421
        return *value_.array;
N
Niels 已提交
422
    }
N
Niels 已提交
423
    if (type_ == value_type::object)
N
Niels 已提交
424
    {
N
Niels 已提交
425
        throw std::logic_error("cannot cast " + type_name() + " to JSON array");
N
Niels 已提交
426 427
    }

N
Niels 已提交
428
    array_t result;
N
Niels 已提交
429 430 431 432
    result.push_back(*this);
    return result;
}

N
Niels 已提交
433 434 435 436 437
/*!
@exception std::logic_error if the function is called for JSON objects whose
    type is not object
*/
template<>
N
Niels 已提交
438
json::object_t json::get() const
N
Niels 已提交
439
{
N
Niels 已提交
440
    if (type_ == value_type::object)
N
Niels 已提交
441
    {
N
Niels 已提交
442
        return *value_.object;
N
Niels 已提交
443 444 445
    }
    else
    {
N
Niels 已提交
446
        throw std::logic_error("cannot cast " + type_name() + " to JSON object");
N
Niels 已提交
447 448 449
    }
}

N
Niels 已提交
450
json::operator const std::string() const
N
Niels 已提交
451 452 453 454
{
    return get<std::string>();
}

N
Niels 已提交
455
json::operator int() const
N
Niels 已提交
456 457 458 459
{
    return get<int>();
}

N
Niels 已提交
460
json::operator double() const
N
Niels 已提交
461 462 463 464
{
    return get<double>();
}

N
Niels 已提交
465
json::operator bool() const
N
Niels 已提交
466 467 468 469
{
    return get<bool>();
}

N
Niels 已提交
470
json::operator array_t() const
N
Niels 已提交
471 472 473 474
{
    return get<array_t>();
}

N
Niels 已提交
475
json::operator object_t() const
N
Niels 已提交
476 477 478 479
{
    return get<object_t>();
}

N
Niels 已提交
480
const std::string json::to_string() const noexcept
N
Niels 已提交
481
{
N
Niels 已提交
482
    switch (type_)
N
Niels 已提交
483 484 485
    {
        case (value_type::string):
        {
N
Niels 已提交
486
            return std::string("\"") + *value_.string + "\"";
N
Niels 已提交
487 488
        }

N
Niels 已提交
489 490
        case (value_type::boolean):
        {
N
Niels 已提交
491
            return value_.boolean ? "true" : "false";
N
Niels 已提交
492 493
        }

N
Niels 已提交
494 495
        case (value_type::number):
        {
N
Niels 已提交
496
            return std::to_string(value_.number);
N
Niels 已提交
497 498
        }

N
Niels 已提交
499 500
        case (value_type::number_float):
        {
N
Niels 已提交
501
            return std::to_string(value_.number_float);
N
Niels 已提交
502 503
        }

N
Niels 已提交
504 505
        case (value_type::array):
        {
N
Niels 已提交
506 507
            std::string result;

N
Niels 已提交
508
            for (array_t::const_iterator i = value_.array->begin(); i != value_.array->end(); ++i)
N
Niels 已提交
509
            {
N
Niels 已提交
510
                if (i != value_.array->begin())
N
Niels 已提交
511
                {
N
Niels 已提交
512 513
                    result += ", ";
                }
N
Niels 已提交
514
                result += i->to_string();
N
Niels 已提交
515 516 517 518 519
            }

            return "[" + result + "]";
        }

N
Niels 已提交
520 521
        case (value_type::object):
        {
N
Niels 已提交
522 523
            std::string result;

N
Niels 已提交
524
            for (object_t::const_iterator i = value_.object->begin(); i != value_.object->end(); ++i)
N
Niels 已提交
525
            {
N
Niels 已提交
526
                if (i != value_.object->begin())
N
Niels 已提交
527
                {
N
Niels 已提交
528 529
                    result += ", ";
                }
N
Niels 已提交
530
                result += "\"" + i->first + "\": " + i->second.to_string();
N
Niels 已提交
531 532 533 534
            }

            return "{" + result + "}";
        }
N
Niels 已提交
535 536 537 538 539 540

        // actually only value_type::null - but making the compiler happy
        default:
        {
            return "null";
        }
N
Niels 已提交
541 542 543 544
    }
}


N
Niels 已提交
545 546 547
///////////////////////////////////////////
// ADDING ELEMENTS TO OBJECTS AND ARRAYS //
///////////////////////////////////////////
N
Niels 已提交
548

N
Niels 已提交
549
json& json::operator+=(const json& o)
N
Niels 已提交
550
{
N
Niels 已提交
551 552 553 554
    push_back(o);
    return *this;
}

N
Niels 已提交
555
json& json::operator+=(const std::string& s)
N
Niels 已提交
556
{
N
Niels 已提交
557
    push_back(json(s));
N
Niels 已提交
558 559 560
    return *this;
}

N
Niels 已提交
561
json& json::operator+=(const char* s)
N
Niels 已提交
562
{
N
Niels 已提交
563
    push_back(json(s));
N
Niels 已提交
564 565 566
    return *this;
}

N
Niels 已提交
567
json& json::operator+=(std::nullptr_t)
N
Niels 已提交
568
{
N
Niels 已提交
569
    push_back(json());
N
Niels 已提交
570 571 572
    return *this;
}

N
Niels 已提交
573
json& json::operator+=(bool b)
N
Niels 已提交
574
{
N
Niels 已提交
575
    push_back(json(b));
N
Niels 已提交
576 577 578
    return *this;
}

N
Niels 已提交
579 580 581 582 583 584
/*!
Adds a number (int) to the current object. This is done by wrapping the number
into a JSON and call push_back for this.

@param i  A number (int) to add to the array.
*/
N
Niels 已提交
585
json& json::operator+=(int i)
N
Niels 已提交
586
{
N
Niels 已提交
587
    push_back(json(i));
N
Niels 已提交
588 589 590
    return *this;
}

N
Niels 已提交
591 592 593 594 595 596
/*!
Adds a number (float) to the current object. This is done by wrapping the
number into a JSON and call push_back for this.

@param f  A number (float) to add to the array.
*/
N
Niels 已提交
597
json& json::operator+=(double f)
N
Niels 已提交
598
{
N
Niels 已提交
599
    push_back(json(f));
N
Niels 已提交
600 601 602
    return *this;
}

N
Niels 已提交
603
/*!
N
Niels 已提交
604
@todo comment me
N
Niels 已提交
605
*/
N
Niels 已提交
606
json& json::operator+=(const object_t::value_type& p)
N
Niels 已提交
607 608 609 610 611
{
    return operator[](p.first) = p.second;
}

/*!
N
Niels 已提交
612
@todo comment me
N
Niels 已提交
613
*/
N
Niels 已提交
614
json& json::operator+=(list_init_t a)
N
Niels 已提交
615 616 617 618
{
    push_back(a);
    return *this;
}
N
Niels 已提交
619

N
Niels 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633
/*!
This function implements the actual "adding to array" function and is called
by all other push_back or operator+= functions. If the function is called for
an array, the passed element is added to the array.

@param o  The element to add to the array.

@pre  The JSON object is an array or null.
@post The JSON object is an array whose last element is the passed element o.
@exception std::runtime_error  The function was called for a JSON type that
             does not support addition to an array (e.g., int or string).

@note Null objects are silently transformed into an array before the addition.
*/
N
Niels 已提交
634
void json::push_back(const json& o)
N
Niels 已提交
635 636
{
    // push_back only works for null objects or arrays
N
Niels 已提交
637
    if (not(type_ == value_type::null or type_ == value_type::array))
N
Niels 已提交
638
    {
N
Niels 已提交
639
        throw std::runtime_error("cannot add element to " + type_name());
N
Niels 已提交
640 641
    }

N
Niels 已提交
642
    std::lock_guard<std::mutex> lg(token_);
N
Niels 已提交
643 644

    // transform null object into an array
N
Niels 已提交
645
    if (type_ == value_type::null)
N
Niels 已提交
646
    {
N
Niels 已提交
647 648
        type_ = value_type::array;
        value_.array = new array_t;
N
Niels 已提交
649 650
    }

N
Niels 已提交
651
    // add element to array
N
Niels 已提交
652
    value_.array->push_back(o);
N
Niels 已提交
653 654
}

N
Niels 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
/*!
This function implements the actual "adding to array" function and is called
by all other push_back or operator+= functions. If the function is called for
an array, the passed element is added to the array using move semantics.

@param o  The element to add to the array.

@pre  The JSON object is an array or null.
@post The JSON object is an array whose last element is the passed element o.
@post The element o is destroyed.
@exception std::runtime_error  The function was called for a JSON type that
             does not support addition to an array (e.g., int or string).

@note Null objects are silently transformed into an array before the addition.
@note This function applies move semantics for the given element.
*/
N
Niels 已提交
671
void json::push_back(json&& o)
N
Niels 已提交
672 673
{
    // push_back only works for null objects or arrays
N
Niels 已提交
674
    if (not(type_ == value_type::null or type_ == value_type::array))
N
Niels 已提交
675
    {
N
Niels 已提交
676
        throw std::runtime_error("cannot add element to " + type_name());
N
Niels 已提交
677 678
    }

N
Niels 已提交
679
    std::lock_guard<std::mutex> lg(token_);
N
Niels 已提交
680 681

    // transform null object into an array
N
Niels 已提交
682
    if (type_ == value_type::null)
N
Niels 已提交
683
    {
N
Niels 已提交
684 685
        type_ = value_type::array;
        value_.array = new array_t;
N
Niels 已提交
686 687 688
    }

    // add element to array (move semantics)
N
Niels 已提交
689
    value_.array->emplace_back(std::move(o));
N
Niels 已提交
690
    // invalidate object
N
Niels 已提交
691
    o.type_ = value_type::null;
N
Niels 已提交
692 693
}

N
Niels 已提交
694
void json::push_back(const std::string& s)
N
Niels 已提交
695
{
N
Niels 已提交
696
    push_back(json(s));
N
Niels 已提交
697 698
}

N
Niels 已提交
699
void json::push_back(const char* s)
N
Niels 已提交
700
{
N
Niels 已提交
701
    push_back(json(s));
N
Niels 已提交
702 703
}

N
Niels 已提交
704
void json::push_back(std::nullptr_t)
N
Niels 已提交
705
{
N
Niels 已提交
706
    push_back(json());
N
Niels 已提交
707 708
}

N
Niels 已提交
709
void json::push_back(bool b)
N
Niels 已提交
710
{
N
Niels 已提交
711
    push_back(json(b));
N
Niels 已提交
712 713
}

N
Niels 已提交
714 715 716 717 718 719
/*!
Adds a number (int) to the current object. This is done by wrapping the number
into a JSON and call push_back for this.

@param i  A number (int) to add to the array.
*/
N
Niels 已提交
720
void json::push_back(int i)
N
Niels 已提交
721
{
N
Niels 已提交
722
    push_back(json(i));
N
Niels 已提交
723 724
}

N
Niels 已提交
725 726 727 728 729 730
/*!
Adds a number (float) to the current object. This is done by wrapping the
number into a JSON and call push_back for this.

@param f  A number (float) to add to the array.
*/
N
Niels 已提交
731
void json::push_back(double f)
N
Niels 已提交
732
{
N
Niels 已提交
733
    push_back(json(f));
N
Niels 已提交
734 735
}

N
Niels 已提交
736
/*!
N
Niels 已提交
737
@todo comment me
N
Niels 已提交
738
*/
N
Niels 已提交
739
void json::push_back(const object_t::value_type& p)
N
Niels 已提交
740 741 742 743 744
{
    operator[](p.first) = p.second;
}

/*!
N
Niels 已提交
745
@todo comment me
N
Niels 已提交
746
*/
N
Niels 已提交
747
void json::push_back(list_init_t a)
N
Niels 已提交
748 749 750 751 752 753 754
{
    bool is_array = false;

    // check if each element is an array with two elements whose first element
    // is a string
    for (const auto& element : a)
    {
N
Niels 已提交
755
        if (element.type_ != value_type::array or
N
Niels 已提交
756
                element.size() != 2 or
N
Niels 已提交
757
                element[0].type_ != value_type::string)
N
Niels 已提交
758 759 760 761 762 763 764 765 766
        {
            // the initializer list describes an array
            is_array = true;
            break;
        }
    }

    if (is_array)
    {
N
Niels 已提交
767
        for (const json& element : a)
N
Niels 已提交
768 769 770 771 772 773
        {
            push_back(element);
        }
    }
    else
    {
N
Niels 已提交
774
        for (const json& element : a)
N
Niels 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
        {
            const object_t::value_type tmp {element[0].get<std::string>(), element[1]};
            push_back(tmp);
        }
    }
}

/*!
This operator realizes read/write access to array elements given an integer
index.  Bounds will not be checked.

@note The "index" variable should be of type size_t as it is compared against
      size() and used in the at() function. However, the compiler will have
      problems in case integer literals are used. In this case, an implicit
      conversion to both size_t and JSON is possible. Therefore, we use int as
      type and convert it to size_t where necessary.

@param index  the index of the element to return from the array
@return reference to element for the given index

@pre Object is an array.
@exception std::domain_error if object is not an array
*/
N
Niels 已提交
798
json& json::operator[](const int index)
N
Niels 已提交
799 800
{
    // this [] operator only works for arrays
N
Niels 已提交
801
    if (type_ != value_type::array)
N
Niels 已提交
802 803
    {
        throw std::domain_error("cannot add entry with index " +
N
Niels 已提交
804
                                std::to_string(index) + " to " + type_name());
N
Niels 已提交
805 806
    }

N
Niels 已提交
807
    std::lock_guard<std::mutex> lg(token_);
N
Niels 已提交
808 809

    // return reference to element from array at given index
N
Niels 已提交
810
    return (*value_.array)[static_cast<size_t>(index)];
N
Niels 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
}

/*!
This operator realizes read-only access to array elements given an integer
index.  Bounds will not be checked.

@note The "index" variable should be of type size_t as it is compared against
      size() and used in the at() function. However, the compiler will have
      problems in case integer literals are used. In this case, an implicit
      conversion to both size_t and JSON is possible. Therefore, we use int as
      type and convert it to size_t where necessary.

@param index  the index of the element to return from the array
@return read-only reference to element for the given index

@pre Object is an array.
@exception std::domain_error if object is not an array
*/
N
Niels 已提交
829
const json& json::operator[](const int index) const
N
Niels 已提交
830 831
{
    // this [] operator only works for arrays
N
Niels 已提交
832
    if (type_ != value_type::array)
N
Niels 已提交
833 834
    {
        throw std::domain_error("cannot get entry with index " +
N
Niels 已提交
835
                                std::to_string(index) + " from " + type_name());
N
Niels 已提交
836 837
    }

N
Niels 已提交
838
    // return element from array at given index
N
Niels 已提交
839
    return (*value_.array)[static_cast<size_t>(index)];
N
Niels 已提交
840 841
}

N
Niels 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
/*!
This function realizes read/write access to array elements given an integer
index. Bounds will be checked.

@note The "index" variable should be of type size_t as it is compared against
      size() and used in the at() function. However, the compiler will have
      problems in case integer literals are used. In this case, an implicit
      conversion to both size_t and JSON is possible. Therefore, we use int as
      type and convert it to size_t where necessary.

@param index  the index of the element to return from the array
@return reference to element for the given index

@pre Object is an array.
@exception std::domain_error if object is not an array
@exception std::out_of_range if index is out of range (via std::vector::at)
*/
N
Niels 已提交
859
json& json::at(const int index)
N
Niels 已提交
860 861
{
    // this function only works for arrays
N
Niels 已提交
862
    if (type_ != value_type::array)
N
Niels 已提交
863 864
    {
        throw std::domain_error("cannot add entry with index " +
N
Niels 已提交
865
                                std::to_string(index) + " to " + type_name());
N
Niels 已提交
866 867
    }

N
Niels 已提交
868
    std::lock_guard<std::mutex> lg(token_);
N
Niels 已提交
869 870

    // return reference to element from array at given index
N
Niels 已提交
871
    return value_.array->at(static_cast<size_t>(index));
N
Niels 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
}

/*!
This operator realizes read-only access to array elements given an integer
index. Bounds will be checked.

@note The "index" variable should be of type size_t as it is compared against
      size() and used in the at() function. However, the compiler will have
      problems in case integer literals are used. In this case, an implicit
      conversion to both size_t and JSON is possible. Therefore, we use int as
      type and convert it to size_t where necessary.

@param index  the index of the element to return from the array
@return read-only reference to element for the given index

@pre Object is an array.
@exception std::domain_error if object is not an array
@exception std::out_of_range if index is out of range (via std::vector::at)
*/
N
Niels 已提交
891
const json& json::at(const int index) const
N
Niels 已提交
892 893
{
    // this function only works for arrays
N
Niels 已提交
894
    if (type_ != value_type::array)
N
Niels 已提交
895 896
    {
        throw std::domain_error("cannot get entry with index " +
N
Niels 已提交
897
                                std::to_string(index) + " from " + type_name());
N
Niels 已提交
898 899
    }

N
Niels 已提交
900
    // return element from array at given index
N
Niels 已提交
901
    return value_.array->at(static_cast<size_t>(index));
N
Niels 已提交
902 903
}

N
Niels 已提交
904
/*!
N
Niels 已提交
905
@copydoc json::operator[](const char* key)
N
Niels 已提交
906
*/
N
Niels 已提交
907
json& json::operator[](const std::string& key)
N
Niels 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
{
    return operator[](key.c_str());
}

/*!
This operator realizes read/write access to object elements given a string
key.

@param key  the key index of the element to return from the object
@return reference to a JSON object for the given key (null if key does not
        exist)

@pre  Object is an object or a null object.
@post null objects are silently converted to objects.

@exception std::domain_error if object is not an object (or null)
*/
N
Niels 已提交
925
json& json::operator[](const char* key)
N
Niels 已提交
926
{
N
Niels 已提交
927
    std::lock_guard<std::mutex> lg(token_);
N
Niels 已提交
928

N
Niels 已提交
929
    // implicitly convert null to object
N
Niels 已提交
930
    if (type_ == value_type::null)
N
Niels 已提交
931
    {
N
Niels 已提交
932 933
        type_ = value_type::object;
        value_.object = new object_t;
N
Niels 已提交
934 935
    }

N
Niels 已提交
936
    // this [] operator only works for objects
N
Niels 已提交
937
    if (type_ != value_type::object)
N
Niels 已提交
938 939
    {
        throw std::domain_error("cannot add entry with key " +
N
Niels 已提交
940
                                std::string(key) + " to " + type_name());
N
Niels 已提交
941 942
    }

N
Niels 已提交
943
    // if the key does not exist, create it
N
Niels 已提交
944
    if (value_.object->find(key) == value_.object->end())
N
Niels 已提交
945
    {
N
Niels 已提交
946
        (*value_.object)[key] = json();
N
Niels 已提交
947 948
    }

N
Niels 已提交
949
    // return reference to element from array at given index
N
Niels 已提交
950
    return (*value_.object)[key];
N
Niels 已提交
951 952
}

N
Niels 已提交
953 954 955
/*!
This operator realizes read-only access to object elements given a string
key.
N
Niels 已提交
956

N
Niels 已提交
957 958
@param key  the key index of the element to return from the object
@return read-only reference to element for the given key
N
Niels 已提交
959

N
Niels 已提交
960 961 962 963
@pre Object is an object.
@exception std::domain_error if object is not an object
@exception std::out_of_range if key is not found in object
*/
N
Niels 已提交
964
const json& json::operator[](const std::string& key) const
N
Niels 已提交
965 966
{
    // this [] operator only works for objects
N
Niels 已提交
967
    if (type_ != value_type::object)
N
Niels 已提交
968
    {
N
Niels 已提交
969
        throw std::domain_error("cannot get entry with key " +
N
Niels 已提交
970
                                std::string(key) + " from " + type_name());
N
Niels 已提交
971 972
    }

N
Niels 已提交
973
    // search for the key
N
Niels 已提交
974
    const auto it = value_.object->find(key);
N
Niels 已提交
975 976

    // make sure the key exists in the object
N
Niels 已提交
977
    if (it == value_.object->end())
N
Niels 已提交
978 979
    {
        throw std::out_of_range("key " + key + " not found");
N
Niels 已提交
980 981
    }

N
Niels 已提交
982 983 984 985 986
    // return element from array at given key
    return it->second;
}

/*!
N
Niels 已提交
987
@copydoc json::at(const char* key)
N
Niels 已提交
988
*/
N
Niels 已提交
989
json& json::at(const std::string& key)
N
Niels 已提交
990 991
{
    return at(key.c_str());
N
Niels 已提交
992 993
}

N
Niels 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
/*!
This function realizes read/write access to object elements given a string
key.

@param key  the key index of the element to return from the object
@return reference to a JSON object for the given key (exception if key does not
        exist)

@pre  Object is an object.

@exception std::domain_error if object is not an object
@exception std::out_of_range if key was not found (via std::map::at)
*/
N
Niels 已提交
1007
json& json::at(const char* key)
N
Niels 已提交
1008
{
N
Niels 已提交
1009
    std::lock_guard<std::mutex> lg(token_);
N
Niels 已提交
1010 1011

    // this function operator only works for objects
N
Niels 已提交
1012
    if (type_ != value_type::object)
N
Niels 已提交
1013 1014
    {
        throw std::domain_error("cannot add entry with key " +
N
Niels 已提交
1015
                                std::string(key) + " to " + type_name());
N
Niels 已提交
1016 1017
    }

N
Niels 已提交
1018
    // return reference to element from array at given index
N
Niels 已提交
1019
    return value_.object->at(key);
N
Niels 已提交
1020 1021
}

N
Niels 已提交
1022
/*!
N
Niels 已提交
1023
@copydoc json::at(const char *key) const
N
Niels 已提交
1024
*/
N
Niels 已提交
1025
const json& json::at(const std::string& key) const
N
Niels 已提交
1026 1027 1028 1029
{
    return at(key.c_str());
}

N
Niels 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
/*!
This operator realizes read-only access to object elements given a string
key.

@param key  the key index of the element to return from the object
@return read-only reference to element for the given key

@pre Object is an object.
@exception std::domain_error if object is not an object
@exception std::out_of_range if key is not found (via std::map::at)
*/
N
Niels 已提交
1041
const json& json::at(const char* key) const
N
Niels 已提交
1042 1043
{
    // this [] operator only works for objects
N
Niels 已提交
1044
    if (type_ != value_type::object)
N
Niels 已提交
1045 1046
    {
        throw std::domain_error("cannot get entry with key " +
N
Niels 已提交
1047
                                std::string(key) + " from " + type_name());
N
Niels 已提交
1048
    }
N
Niels 已提交
1049 1050

    // return element from array at given key
N
Niels 已提交
1051
    return value_.object->at(key);
N
Niels 已提交
1052 1053
}

N
Niels 已提交
1054

N
Niels 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063
/*!
Returns the size of the JSON object.

@return the size of the JSON object; the size is the number of elements in
        compounds (array and object), 1 for value types (true, false, number,
        string), and 0 for null.

@invariant The size is reported as 0 if and only if empty() would return true.
*/
N
Niels 已提交
1064
size_t json::size() const noexcept
N
Niels 已提交
1065
{
N
Niels 已提交
1066
    switch (type_)
N
Niels 已提交
1067 1068 1069
    {
        case (value_type::array):
        {
N
Niels 已提交
1070
            return value_.array->size();
N
Niels 已提交
1071
        }
N
Niels 已提交
1072 1073
        case (value_type::object):
        {
N
Niels 已提交
1074
            return value_.object->size();
N
Niels 已提交
1075
        }
N
Niels 已提交
1076 1077
        case (value_type::null):
        {
N
Niels 已提交
1078
            return 0;
N
Niels 已提交
1079
        }
N
Niels 已提交
1080
        default:
N
Niels 已提交
1081
        {
N
Niels 已提交
1082
            return 1;
N
Niels 已提交
1083
        }
N
Niels 已提交
1084 1085 1086
    }
}

N
Niels 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095
/*!
Returns whether a JSON object is empty.

@return true for null objects and empty compounds (array and object); false
        for value types (true, false, number, string) and filled compounds
        (array and object).

@invariant Empty would report true if and only if size() would return 0.
*/
N
Niels 已提交
1096
bool json::empty() const noexcept
N
Niels 已提交
1097
{
N
Niels 已提交
1098
    switch (type_)
N
Niels 已提交
1099 1100 1101
    {
        case (value_type::array):
        {
N
Niels 已提交
1102
            return value_.array->empty();
N
Niels 已提交
1103
        }
N
Niels 已提交
1104 1105
        case (value_type::object):
        {
N
Niels 已提交
1106
            return value_.object->empty();
N
Niels 已提交
1107
        }
N
Niels 已提交
1108 1109
        case (value_type::null):
        {
N
Niels 已提交
1110
            return true;
N
Niels 已提交
1111
        }
N
Niels 已提交
1112
        default:
N
Niels 已提交
1113
        {
N
Niels 已提交
1114
            return false;
N
Niels 已提交
1115
        }
N
Niels 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    }
}

/*!
Removes all elements from compounds and resets values to default.

@invariant Clear will set any value type to its default value which is empty
           for compounds, false for booleans, 0 for integer numbers, and 0.0
           for floating numbers.
*/
N
Niels 已提交
1126
void json::clear() noexcept
N
Niels 已提交
1127
{
N
Niels 已提交
1128
    switch (type_)
N
Niels 已提交
1129 1130 1131
    {
        case (value_type::array):
        {
N
Niels 已提交
1132
            value_.array->clear();
N
Niels 已提交
1133
            break;
N
Niels 已提交
1134
        }
N
Niels 已提交
1135 1136
        case (value_type::object):
        {
N
Niels 已提交
1137
            value_.object->clear();
N
Niels 已提交
1138
            break;
N
Niels 已提交
1139
        }
N
Niels 已提交
1140 1141
        case (value_type::string):
        {
N
Niels 已提交
1142
            value_.string->clear();
N
Niels 已提交
1143 1144 1145 1146
            break;
        }
        case (value_type::boolean):
        {
N
Niels 已提交
1147
            value_.boolean = {};
N
Niels 已提交
1148 1149 1150 1151
            break;
        }
        case (value_type::number):
        {
N
Niels 已提交
1152
            value_.number = {};
N
Niels 已提交
1153 1154 1155 1156
            break;
        }
        case (value_type::number_float):
        {
N
Niels 已提交
1157
            value_.number_float = {};
N
Niels 已提交
1158 1159
            break;
        }
N
Niels 已提交
1160
        default:
N
Niels 已提交
1161 1162
        {
            break;
N
Niels 已提交
1163
        }
N
Niels 已提交
1164 1165 1166
    }
}

N
Niels 已提交
1167
json::value_type json::type() const noexcept
N
Niels 已提交
1168
{
N
Niels 已提交
1169
    return type_;
N
Niels 已提交
1170 1171
}

N
Niels 已提交
1172
json::iterator json::find(const std::string& key)
N
Niels 已提交
1173
{
N
Niels 已提交
1174 1175 1176
    return find(key.c_str());
}

N
Niels 已提交
1177
json::const_iterator json::find(const std::string& key) const
N
Niels 已提交
1178
{
N
Niels 已提交
1179 1180 1181
    return find(key.c_str());
}

N
Niels 已提交
1182
json::iterator json::find(const char* key)
N
Niels 已提交
1183
{
N
Niels 已提交
1184
    if (type_ != value_type::object)
N
Niels 已提交
1185
    {
N
Niels 已提交
1186
        return end();
N
Niels 已提交
1187 1188 1189
    }
    else
    {
N
Niels 已提交
1190 1191
        const object_t::iterator i = value_.object->find(key);
        if (i != value_.object->end())
N
Niels 已提交
1192
        {
N
Niels 已提交
1193
            json::iterator result(this);
N
Niels 已提交
1194 1195
            delete result.oi_;
            result.oi_ = new object_t::iterator(i);
N
Niels 已提交
1196
            return result;
N
Niels 已提交
1197 1198 1199
        }
        else
        {
N
Niels 已提交
1200 1201 1202 1203 1204
            return end();
        }
    }
}

N
Niels 已提交
1205
json::const_iterator json::find(const char* key) const
N
Niels 已提交
1206
{
N
Niels 已提交
1207
    if (type_ != value_type::object)
N
Niels 已提交
1208
    {
N
Niels 已提交
1209
        return end();
N
Niels 已提交
1210 1211 1212
    }
    else
    {
N
Niels 已提交
1213 1214
        const object_t::const_iterator i = value_.object->find(key);
        if (i != value_.object->end())
N
Niels 已提交
1215
        {
N
Niels 已提交
1216
            json::const_iterator result(this);
N
Niels 已提交
1217 1218
            delete result.oi_;
            result.oi_ = new object_t::const_iterator(i);
N
Niels 已提交
1219
            return result;
N
Niels 已提交
1220 1221 1222
        }
        else
        {
N
Niels 已提交
1223 1224 1225 1226 1227
            return end();
        }
    }
}

N
Niels 已提交
1228
bool json::operator==(const json& o) const noexcept
N
Niels 已提交
1229
{
N
Niels 已提交
1230
    switch (type_)
N
Niels 已提交
1231 1232 1233
    {
        case (value_type::array):
        {
N
Niels 已提交
1234
            if (o.type_ == value_type::array)
N
Niels 已提交
1235
            {
N
Niels 已提交
1236
                return *value_.array == *o.value_.array;
N
Niels 已提交
1237
            }
N
Niels 已提交
1238
            break;
N
Niels 已提交
1239
        }
N
Niels 已提交
1240 1241
        case (value_type::object):
        {
N
Niels 已提交
1242
            if (o.type_ == value_type::object)
N
Niels 已提交
1243
            {
N
Niels 已提交
1244
                return *value_.object == *o.value_.object;
N
Niels 已提交
1245
            }
N
Niels 已提交
1246
            break;
N
Niels 已提交
1247
        }
N
Niels 已提交
1248 1249
        case (value_type::null):
        {
N
Niels 已提交
1250
            if (o.type_ == value_type::null)
N
Niels 已提交
1251
            {
N
Niels 已提交
1252 1253
                return true;
            }
N
Niels 已提交
1254
            break;
N
Niels 已提交
1255
        }
N
Niels 已提交
1256 1257
        case (value_type::string):
        {
N
Niels 已提交
1258
            if (o.type_ == value_type::string)
N
Niels 已提交
1259
            {
N
Niels 已提交
1260
                return *value_.string == *o.value_.string;
N
Niels 已提交
1261
            }
N
Niels 已提交
1262
            break;
N
Niels 已提交
1263
        }
N
Niels 已提交
1264 1265
        case (value_type::boolean):
        {
N
Niels 已提交
1266
            if (o.type_ == value_type::boolean)
N
Niels 已提交
1267
            {
N
Niels 已提交
1268
                return value_.boolean == o.value_.boolean;
N
Niels 已提交
1269
            }
N
Niels 已提交
1270
            break;
N
Niels 已提交
1271
        }
N
Niels 已提交
1272 1273
        case (value_type::number):
        {
N
Niels 已提交
1274
            if (o.type_ == value_type::number)
N
Niels 已提交
1275
            {
N
Niels 已提交
1276
                return value_.number == o.value_.number;
1277
            }
N
Niels 已提交
1278
            if (o.type_ == value_type::number_float)
N
Niels 已提交
1279
            {
N
Niels 已提交
1280
                return value_.number == static_cast<number_t>(o.value_.number_float);
N
Niels 已提交
1281
            }
N
Niels 已提交
1282
            break;
N
Niels 已提交
1283
        }
N
Niels 已提交
1284 1285
        case (value_type::number_float):
        {
N
Niels 已提交
1286
            if (o.type_ == value_type::number)
N
Niels 已提交
1287
            {
N
Niels 已提交
1288
                return value_.number_float == static_cast<number_float_t>(o.value_.number);
1289
            }
N
Niels 已提交
1290
            if (o.type_ == value_type::number_float)
N
Niels 已提交
1291
            {
N
Niels 已提交
1292
                return value_.number_float == o.value_.number_float;
N
Niels 已提交
1293
            }
N
Niels 已提交
1294
            break;
N
Niels 已提交
1295 1296 1297 1298 1299 1300
        }
    }

    return false;
}

N
Niels 已提交
1301
bool json::operator!=(const json& o) const noexcept
N
Niels 已提交
1302
{
N
Niels 已提交
1303 1304
    return not operator==(o);
}
N
Niels 已提交
1305 1306


N
Niels 已提交
1307
json::iterator json::begin() noexcept
N
Niels 已提交
1308
{
N
Niels 已提交
1309
    return json::iterator(this);
N
Niels 已提交
1310 1311
}

N
Niels 已提交
1312
json::iterator json::end() noexcept
N
Niels 已提交
1313
{
N
Niels 已提交
1314
    return json::iterator();
N
Niels 已提交
1315 1316
}

N
Niels 已提交
1317
json::const_iterator json::begin() const noexcept
N
Niels 已提交
1318
{
N
Niels 已提交
1319
    return json::const_iterator(this);
N
Niels 已提交
1320 1321
}

N
Niels 已提交
1322
json::const_iterator json::end() const noexcept
N
Niels 已提交
1323
{
N
Niels 已提交
1324
    return json::const_iterator();
N
Niels 已提交
1325 1326
}

N
Niels 已提交
1327
json::const_iterator json::cbegin() const noexcept
N
Niels 已提交
1328
{
N
Niels 已提交
1329
    return json::const_iterator(this);
N
Niels 已提交
1330 1331
}

N
Niels 已提交
1332
json::const_iterator json::cend() const noexcept
N
Niels 已提交
1333
{
N
Niels 已提交
1334
    return json::const_iterator();
N
Niels 已提交
1335 1336 1337
}


N
Niels 已提交
1338
json::iterator::iterator(json* j) : object_(j)
N
Niels 已提交
1339
{
N
Niels 已提交
1340
    if (object_ != nullptr)
N
Niels 已提交
1341
    {
N
Niels 已提交
1342
        if (object_->type_ == value_type::array)
N
Niels 已提交
1343
        {
N
Niels 已提交
1344
            vi_ = new array_t::iterator(object_->value_.array->begin());
N
Niels 已提交
1345
        }
N
Niels 已提交
1346
        if (object_->type_ == value_type::object)
N
Niels 已提交
1347
        {
N
Niels 已提交
1348
            oi_ = new object_t::iterator(object_->value_.object->begin());
N
Niels 已提交
1349 1350
        }
    }
N
Niels 已提交
1351 1352
}

N
Niels 已提交
1353
json::iterator::iterator(const json::iterator& o) : object_(o.object_)
N
Niels 已提交
1354
{
N
Niels 已提交
1355
    if (object_ != nullptr)
N
Niels 已提交
1356
    {
N
Niels 已提交
1357
        if (object_->type_ == value_type::array)
N
Niels 已提交
1358
        {
N
Niels 已提交
1359
            vi_ = new array_t::iterator(*(o.vi_));
N
Niels 已提交
1360
        }
N
Niels 已提交
1361
        if (object_->type_ == value_type::object)
N
Niels 已提交
1362
        {
N
Niels 已提交
1363
            oi_ = new object_t::iterator(*(o.oi_));
N
Niels 已提交
1364
        }
N
Niels 已提交
1365
    }
N
Niels 已提交
1366 1367
}

N
Niels 已提交
1368
json::iterator::~iterator()
N
Niels 已提交
1369
{
N
Niels 已提交
1370 1371
    delete vi_;
    delete oi_;
N
Niels 已提交
1372 1373
}

N
Niels 已提交
1374
json::iterator& json::iterator::operator=(json::iterator o)
N
Niels 已提交
1375
{
N
Niels 已提交
1376 1377 1378
    std::swap(object_, o.object_);
    std::swap(vi_, o.vi_);
    std::swap(oi_, o.oi_);
N
Niels 已提交
1379 1380 1381
    return *this;
}

N
Niels 已提交
1382
bool json::iterator::operator==(const json::iterator& o) const
N
Niels 已提交
1383
{
N
Niels 已提交
1384
    if (object_ != o.object_)
N
Niels 已提交
1385 1386 1387 1388
    {
        return false;
    }

N
Niels 已提交
1389
    if (object_ != nullptr)
N
Niels 已提交
1390
    {
N
Niels 已提交
1391
        if (object_->type_ == value_type::array)
N
Niels 已提交
1392
        {
N
Niels 已提交
1393
            return (vi_ == o.vi_);
N
Niels 已提交
1394
        }
N
Niels 已提交
1395
        if (object_->type_ == value_type::object)
N
Niels 已提交
1396
        {
N
Niels 已提交
1397
            return (oi_ == o.oi_);
N
Niels 已提交
1398 1399
        }
    }
N
Niels 已提交
1400

N
Niels 已提交
1401
    return true;
N
Niels 已提交
1402 1403
}

N
Niels 已提交
1404
bool json::iterator::operator!=(const json::iterator& o) const
N
Niels 已提交
1405 1406
{
    return not operator==(o);
N
Niels 已提交
1407 1408
}

N
Niels 已提交
1409
json::iterator& json::iterator::operator++()
N
Niels 已提交
1410
{
N
Niels 已提交
1411
    // iterator cannot be incremented
N
Niels 已提交
1412
    if (object_ == nullptr)
N
Niels 已提交
1413
    {
N
Niels 已提交
1414 1415 1416
        return *this;
    }

N
Niels 已提交
1417
    switch (object_->type_)
N
Niels 已提交
1418 1419 1420
    {
        case (value_type::array):
        {
N
Niels 已提交
1421
            if (++(*vi_) == object_->value_.array->end())
N
Niels 已提交
1422
            {
N
Niels 已提交
1423
                object_ = nullptr;
N
Niels 已提交
1424 1425 1426
            }
            break;
        }
N
Niels 已提交
1427 1428
        case (value_type::object):
        {
N
Niels 已提交
1429
            if (++(*oi_) == object_->value_.object->end())
N
Niels 已提交
1430
            {
N
Niels 已提交
1431
                object_ = nullptr;
N
Niels 已提交
1432 1433 1434
            }
            break;
        }
N
Niels 已提交
1435
        default:
N
Niels 已提交
1436
        {
N
Niels 已提交
1437
            object_ = nullptr;
N
Niels 已提交
1438 1439 1440 1441 1442
        }
    }
    return *this;
}

N
Niels 已提交
1443
json& json::iterator::operator*() const
N
Niels 已提交
1444
{
N
Niels 已提交
1445
    // dereferencing end() is an error
N
Niels 已提交
1446
    if (object_ == nullptr)
N
Niels 已提交
1447
    {
N
Niels 已提交
1448 1449 1450
        throw std::runtime_error("cannot get value");
    }

N
Niels 已提交
1451
    switch (object_->type_)
N
Niels 已提交
1452 1453 1454
    {
        case (value_type::array):
        {
N
Niels 已提交
1455
            return **vi_;
N
Niels 已提交
1456
        }
N
Niels 已提交
1457 1458
        case (value_type::object):
        {
N
Niels 已提交
1459
            return (*oi_)->second;
N
Niels 已提交
1460
        }
N
Niels 已提交
1461
        default:
N
Niels 已提交
1462
        {
N
Niels 已提交
1463
            return *object_;
N
Niels 已提交
1464
        }
N
Niels 已提交
1465 1466 1467
    }
}

N
Niels 已提交
1468
json* json::iterator::operator->() const
N
Niels 已提交
1469
{
N
Niels 已提交
1470
    // dereferencing end() is an error
N
Niels 已提交
1471
    if (object_ == nullptr)
N
Niels 已提交
1472
    {
N
Niels 已提交
1473 1474 1475
        throw std::runtime_error("cannot get value");
    }

N
Niels 已提交
1476
    switch (object_->type_)
N
Niels 已提交
1477 1478 1479
    {
        case (value_type::array):
        {
N
Niels 已提交
1480
            return &(**vi_);
N
Niels 已提交
1481
        }
N
Niels 已提交
1482 1483
        case (value_type::object):
        {
N
Niels 已提交
1484
            return &((*oi_)->second);
N
Niels 已提交
1485
        }
N
Niels 已提交
1486
        default:
N
Niels 已提交
1487
        {
N
Niels 已提交
1488
            return object_;
N
Niels 已提交
1489
        }
N
Niels 已提交
1490 1491 1492
    }
}

N
Niels 已提交
1493
std::string json::iterator::key() const
N
Niels 已提交
1494
{
N
Niels 已提交
1495
    if (object_ != nullptr and object_->type_ == value_type::object)
N
Niels 已提交
1496
    {
N
Niels 已提交
1497
        return (*oi_)->first;
N
Niels 已提交
1498 1499 1500 1501
    }
    else
    {
        throw std::out_of_range("cannot get key");
N
Niels 已提交
1502 1503 1504
    }
}

N
Niels 已提交
1505
json& json::iterator::value() const
N
Niels 已提交
1506
{
N
Niels 已提交
1507
    // dereferencing end() is an error
N
Niels 已提交
1508
    if (object_ == nullptr)
N
Niels 已提交
1509 1510
    {
        throw std::out_of_range("cannot get value");
N
Niels 已提交
1511 1512
    }

N
Niels 已提交
1513
    switch (object_->type_)
N
Niels 已提交
1514 1515 1516
    {
        case (value_type::array):
        {
N
Niels 已提交
1517
            return **vi_;
N
Niels 已提交
1518
        }
N
Niels 已提交
1519 1520
        case (value_type::object):
        {
N
Niels 已提交
1521
            return (*oi_)->second;
N
Niels 已提交
1522
        }
N
Niels 已提交
1523
        default:
N
Niels 已提交
1524
        {
N
Niels 已提交
1525
            return *object_;
N
Niels 已提交
1526
        }
N
Niels 已提交
1527 1528 1529 1530
    }
}


N
Niels 已提交
1531
json::const_iterator::const_iterator(const json* j) : object_(j)
N
Niels 已提交
1532
{
N
Niels 已提交
1533
    if (object_ != nullptr)
N
Niels 已提交
1534
    {
N
Niels 已提交
1535
        if (object_->type_ == value_type::array)
N
Niels 已提交
1536
        {
N
Niels 已提交
1537
            vi_ = new array_t::const_iterator(object_->value_.array->begin());
N
Niels 已提交
1538
        }
N
Niels 已提交
1539
        if (object_->type_ == value_type::object)
N
Niels 已提交
1540
        {
N
Niels 已提交
1541
            oi_ = new object_t::const_iterator(object_->value_.object->begin());
N
Niels 已提交
1542 1543
        }
    }
N
Niels 已提交
1544 1545
}

N
Niels 已提交
1546
json::const_iterator::const_iterator(const json::const_iterator& o) : object_(o.object_)
N
Niels 已提交
1547
{
N
Niels 已提交
1548
    if (object_ != nullptr)
N
Niels 已提交
1549
    {
N
Niels 已提交
1550
        if (object_->type_ == value_type::array)
N
Niels 已提交
1551
        {
N
Niels 已提交
1552
            vi_ = new array_t::const_iterator(*(o.vi_));
N
Niels 已提交
1553
        }
N
Niels 已提交
1554
        if (object_->type_ == value_type::object)
N
Niels 已提交
1555
        {
N
Niels 已提交
1556
            oi_ = new object_t::const_iterator(*(o.oi_));
N
Niels 已提交
1557 1558
        }
    }
N
Niels 已提交
1559 1560
}

N
Niels 已提交
1561
json::const_iterator::const_iterator(const json::iterator& o) : object_(o.object_)
N
Niels 已提交
1562
{
N
Niels 已提交
1563
    if (object_ != nullptr)
N
Niels 已提交
1564
    {
N
Niels 已提交
1565
        if (object_->type_ == value_type::array)
N
Niels 已提交
1566
        {
N
Niels 已提交
1567
            vi_ = new array_t::const_iterator(*(o.vi_));
N
Niels 已提交
1568
        }
N
Niels 已提交
1569
        if (object_->type_ == value_type::object)
N
Niels 已提交
1570
        {
N
Niels 已提交
1571
            oi_ = new object_t::const_iterator(*(o.oi_));
N
Niels 已提交
1572 1573
        }
    }
N
Niels 已提交
1574 1575
}

N
Niels 已提交
1576
json::const_iterator::~const_iterator()
N
Niels 已提交
1577
{
N
Niels 已提交
1578 1579
    delete vi_;
    delete oi_;
N
Niels 已提交
1580 1581
}

N
Niels 已提交
1582
json::const_iterator& json::const_iterator::operator=(json::const_iterator o)
N
Niels 已提交
1583
{
N
Niels 已提交
1584 1585 1586
    std::swap(object_, o.object_);
    std::swap(vi_, o.vi_);
    std::swap(oi_, o.oi_);
N
Niels 已提交
1587 1588 1589
    return *this;
}

N
Niels 已提交
1590
bool json::const_iterator::operator==(const json::const_iterator& o) const
N
Niels 已提交
1591
{
N
Niels 已提交
1592
    if (object_ != o.object_)
N
Niels 已提交
1593 1594 1595 1596
    {
        return false;
    }

N
Niels 已提交
1597
    if (object_ != nullptr)
N
Niels 已提交
1598
    {
N
Niels 已提交
1599
        if (object_->type_ == value_type::array)
N
Niels 已提交
1600
        {
N
Niels 已提交
1601
            return (vi_ == o.vi_);
N
Niels 已提交
1602
        }
N
Niels 已提交
1603
        if (object_->type_ == value_type::object)
N
Niels 已提交
1604
        {
N
Niels 已提交
1605
            return (oi_ == o.oi_);
N
Niels 已提交
1606
        }
N
Niels 已提交
1607
    }
N
Niels 已提交
1608

N
Niels 已提交
1609
    return true;
N
Niels 已提交
1610 1611
}

N
Niels 已提交
1612
bool json::const_iterator::operator!=(const json::const_iterator& o) const
N
Niels 已提交
1613 1614
{
    return not operator==(o);
N
Niels 已提交
1615 1616
}

N
Niels 已提交
1617
json::const_iterator& json::const_iterator::operator++()
N
Niels 已提交
1618
{
N
Niels 已提交
1619
    // iterator cannot be incremented
N
Niels 已提交
1620
    if (object_ == nullptr)
N
Niels 已提交
1621
    {
N
Niels 已提交
1622 1623 1624
        return *this;
    }

N
Niels 已提交
1625
    switch (object_->type_)
N
Niels 已提交
1626 1627 1628
    {
        case (value_type::array):
        {
N
Niels 已提交
1629
            if (++(*vi_) == object_->value_.array->end())
N
Niels 已提交
1630
            {
N
Niels 已提交
1631
                object_ = nullptr;
N
Niels 已提交
1632 1633 1634
            }
            break;
        }
N
Niels 已提交
1635 1636
        case (value_type::object):
        {
N
Niels 已提交
1637
            if (++(*oi_) == object_->value_.object->end())
N
Niels 已提交
1638
            {
N
Niels 已提交
1639
                object_ = nullptr;
N
Niels 已提交
1640 1641 1642
            }
            break;
        }
N
Niels 已提交
1643
        default:
N
Niels 已提交
1644
        {
N
Niels 已提交
1645
            object_ = nullptr;
N
Niels 已提交
1646 1647 1648 1649 1650
        }
    }
    return *this;
}

N
Niels 已提交
1651
const json& json::const_iterator::operator*() const
N
Niels 已提交
1652
{
N
Niels 已提交
1653
    // dereferencing end() is an error
N
Niels 已提交
1654
    if (object_ == nullptr)
N
Niels 已提交
1655
    {
N
Niels 已提交
1656 1657 1658
        throw std::runtime_error("cannot get value");
    }

N
Niels 已提交
1659
    switch (object_->type_)
N
Niels 已提交
1660 1661 1662
    {
        case (value_type::array):
        {
N
Niels 已提交
1663
            return **vi_;
N
Niels 已提交
1664
        }
N
Niels 已提交
1665 1666
        case (value_type::object):
        {
N
Niels 已提交
1667
            return (*oi_)->second;
N
Niels 已提交
1668
        }
N
Niels 已提交
1669
        default:
N
Niels 已提交
1670
        {
N
Niels 已提交
1671
            return *object_;
N
Niels 已提交
1672
        }
N
Niels 已提交
1673 1674 1675
    }
}

N
Niels 已提交
1676
const json* json::const_iterator::operator->() const
N
Niels 已提交
1677
{
N
Niels 已提交
1678
    // dereferencing end() is an error
N
Niels 已提交
1679
    if (object_ == nullptr)
N
Niels 已提交
1680
    {
N
Niels 已提交
1681 1682 1683
        throw std::runtime_error("cannot get value");
    }

N
Niels 已提交
1684
    switch (object_->type_)
N
Niels 已提交
1685 1686 1687
    {
        case (value_type::array):
        {
N
Niels 已提交
1688
            return &(**vi_);
N
Niels 已提交
1689
        }
N
Niels 已提交
1690 1691
        case (value_type::object):
        {
N
Niels 已提交
1692
            return &((*oi_)->second);
N
Niels 已提交
1693
        }
N
Niels 已提交
1694
        default:
N
Niels 已提交
1695
        {
N
Niels 已提交
1696
            return object_;
N
Niels 已提交
1697
        }
N
Niels 已提交
1698 1699 1700
    }
}

N
Niels 已提交
1701
std::string json::const_iterator::key() const
N
Niels 已提交
1702
{
N
Niels 已提交
1703
    if (object_ != nullptr and object_->type_ == value_type::object)
N
Niels 已提交
1704
    {
N
Niels 已提交
1705
        return (*oi_)->first;
N
Niels 已提交
1706 1707 1708 1709
    }
    else
    {
        throw std::out_of_range("cannot get key");
N
Niels 已提交
1710 1711 1712
    }
}

N
Niels 已提交
1713
const json& json::const_iterator::value() const
N
Niels 已提交
1714
{
N
Niels 已提交
1715
    // dereferencing end() is an error
N
Niels 已提交
1716
    if (object_ == nullptr)
N
Niels 已提交
1717 1718
    {
        throw std::out_of_range("cannot get value");
N
Niels 已提交
1719 1720
    }

N
Niels 已提交
1721
    switch (object_->type_)
N
Niels 已提交
1722 1723 1724
    {
        case (value_type::array):
        {
N
Niels 已提交
1725
            return **vi_;
N
Niels 已提交
1726
        }
N
Niels 已提交
1727 1728
        case (value_type::object):
        {
N
Niels 已提交
1729
            return (*oi_)->second;
N
Niels 已提交
1730
        }
N
Niels 已提交
1731
        default:
N
Niels 已提交
1732
        {
N
Niels 已提交
1733
            return *object_;
N
Niels 已提交
1734
        }
N
Niels 已提交
1735 1736
    }
}
N
Niels 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745


/*!
Initialize the JSON parser given a string \p s.

@note After initialization, the function @ref parse has to be called manually.

@param s  string to parse

N
Niels 已提交
1746
@post \p s is copied to the buffer @ref buffer_ and the first character is
N
Niels 已提交
1747 1748
      read. Whitespace is skipped.
*/
N
Niels 已提交
1749
json::parser::parser(const char* s)
N
Niels 已提交
1750
    :  buffer_(s)
N
Niels 已提交
1751 1752 1753 1754 1755 1756
{
    // read first character
    next();
}

/*!
N
Niels 已提交
1757
@copydoc json::parser::parser(const char* s)
N
Niels 已提交
1758
*/
N
Niels 已提交
1759
json::parser::parser(const std::string& s)
N
Niels 已提交
1760
    : buffer_(s)
N
Niels 已提交
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
{
    // read first character
    next();
}

/*!
Initialize the JSON parser given an input stream \p _is.

@note After initialization, the function @ref parse has to be called manually.

\param _is input stream to parse

N
Niels 已提交
1773
@post \p _is is copied to the buffer @ref buffer_ and the firsr character is
N
Niels 已提交
1774 1775 1776
      read. Whitespace is skipped.

*/
N
Niels 已提交
1777
json::parser::parser(std::istream& _is)
N
Niels 已提交
1778
{
N
Niels 已提交
1779 1780
    while (_is)
    {
N
Niels 已提交
1781
        std::string input_line;
N
Niels 已提交
1782
        std::getline(_is, input_line);
N
Niels 已提交
1783
        buffer_ += input_line;
N
Niels 已提交
1784
    }
N
Niels 已提交
1785 1786 1787 1788 1789

    // read first character
    next();
}

N
Niels 已提交
1790
json json::parser::parse()
N
Niels 已提交
1791
{
N
Niels 已提交
1792
    switch (current_)
N
Niels 已提交
1793 1794 1795 1796
    {
        case ('{'):
        {
            // explicitly set result to object to cope with {}
N
Niels 已提交
1797
            json result(value_type::object);
N
Niels 已提交
1798 1799 1800 1801

            next();

            // process nonempty object
N
Niels 已提交
1802
            if (current_ != '}')
N
Niels 已提交
1803 1804 1805 1806
            {
                do
                {
                    // key
N
cleanup  
Niels 已提交
1807
                    auto key = parseString();
N
Niels 已提交
1808 1809 1810 1811 1812

                    // colon
                    expect(':');

                    // value
N
Niels 已提交
1813
                    result[std::move(key)] = parse();
N
cleanup  
Niels 已提交
1814
                    key.clear();
N
Niels 已提交
1815
                }
N
Niels 已提交
1816
                while (current_ == ',' and next());
N
Niels 已提交
1817 1818 1819 1820 1821
            }

            // closing brace
            expect('}');

N
Niels 已提交
1822
            return result;
N
Niels 已提交
1823 1824 1825 1826 1827
        }

        case ('['):
        {
            // explicitly set result to array to cope with []
N
Niels 已提交
1828
            json result(value_type::array);
N
Niels 已提交
1829 1830 1831 1832

            next();

            // process nonempty array
N
Niels 已提交
1833
            if (current_ != ']')
N
Niels 已提交
1834 1835 1836
            {
                do
                {
N
Niels 已提交
1837
                    result.push_back(parse());
N
Niels 已提交
1838
                }
N
Niels 已提交
1839
                while (current_ == ',' and next());
N
Niels 已提交
1840 1841 1842 1843 1844
            }

            // closing bracket
            expect(']');

N
Niels 已提交
1845
            return result;
N
Niels 已提交
1846 1847 1848 1849
        }

        case ('\"'):
        {
N
Niels 已提交
1850
            return json(parseString());
N
Niels 已提交
1851 1852 1853 1854 1855
        }

        case ('t'):
        {
            parseTrue();
N
Niels 已提交
1856
            return json(true);
N
Niels 已提交
1857 1858 1859 1860 1861
        }

        case ('f'):
        {
            parseFalse();
N
Niels 已提交
1862
            return json(false);
N
Niels 已提交
1863 1864 1865 1866 1867
        }

        case ('n'):
        {
            parseNull();
N
Niels 已提交
1868
            return json();
N
Niels 已提交
1869 1870
        }

N
cleanup  
Niels 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
        case ('-'):
        case ('0'):
        case ('1'):
        case ('2'):
        case ('3'):
        case ('4'):
        case ('5'):
        case ('6'):
        case ('7'):
        case ('8'):
        case ('9'):
N
Niels 已提交
1882
        {
N
cleanup  
Niels 已提交
1883
            // remember position of number's first character
N
Niels 已提交
1884
            const auto _firstpos_ = pos_ - 1;
N
cleanup  
Niels 已提交
1885

N
Niels 已提交
1886 1887 1888
            while (next() and (std::isdigit(current_) || current_ == '.'
                               || current_ == 'e' || current_ == 'E'
                               || current_ == '+' || current_ == '-'));
N
cleanup  
Niels 已提交
1889 1890

            try
N
Niels 已提交
1891
            {
N
Niels 已提交
1892
                const auto float_val = std::stod(buffer_.substr(_firstpos_, pos_ - _firstpos_));
N
cleanup  
Niels 已提交
1893
                const auto int_val = static_cast<int>(float_val);
N
Niels 已提交
1894

N
cleanup  
Niels 已提交
1895 1896
                // check if conversion loses precision
                if (float_val == int_val)
N
Niels 已提交
1897
                {
N
cleanup  
Niels 已提交
1898
                    // we would not lose precision -> int
N
Niels 已提交
1899
                    return json(int_val);
N
Niels 已提交
1900
                }
N
cleanup  
Niels 已提交
1901
                else
N
Niels 已提交
1902
                {
N
cleanup  
Niels 已提交
1903
                    // we would lose precision -> float
N
Niels 已提交
1904
                    return json(float_val);
N
Niels 已提交
1905 1906
                }
            }
N
cleanup  
Niels 已提交
1907
            catch (...)
N
Niels 已提交
1908
            {
N
cleanup  
Niels 已提交
1909
                error("error translating " +
N
Niels 已提交
1910
                      buffer_.substr(_firstpos_, pos_ - _firstpos_) + " to number");
N
Niels 已提交
1911 1912
            }
        }
N
cleanup  
Niels 已提交
1913 1914 1915 1916 1917

        default:
        {
            error("unexpected character");
        }
N
Niels 已提交
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
    }
}

/*!
This function reads the next character from the buffer while ignoring all
trailing whitespace. If another character could be read, the function returns
true. If the end of the buffer is reached, false is returned.

@return whether another non-whitespace character could be read

N
Niels 已提交
1928
@post current_ holds the next character
N
Niels 已提交
1929
*/
N
Niels 已提交
1930
bool json::parser::next()
N
Niels 已提交
1931
{
N
Niels 已提交
1932
    if (pos_ == buffer_.size())
N
Niels 已提交
1933 1934 1935 1936
    {
        return false;
    }

N
Niels 已提交
1937
    current_ = buffer_[pos_++];
N
Niels 已提交
1938 1939

    // skip trailing whitespace
N
Niels 已提交
1940
    while (std::isspace(current_))
N
Niels 已提交
1941
    {
N
Niels 已提交
1942
        if (pos_ == buffer_.size())
N
Niels 已提交
1943 1944 1945 1946
        {
            return false;
        }

N
Niels 已提交
1947
        current_ = buffer_[pos_++];
N
Niels 已提交
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    }

    return true;
}

/*!
This function encapsulates the error reporting functions of the parser class.
It throws a \p std::invalid_argument exception with a description where the
error occurred (given as the number of characters read), what went wrong (using
the error message \p msg), and the last read token.

@param msg  an error message
@return <em>This function does not return.</em>

@exception std::invalid_argument whenever the function is called
*/
N
Niels 已提交
1964
void json::parser::error(const std::string& msg)
N
Niels 已提交
1965 1966
{
    throw std::invalid_argument("parse error at position " +
N
Niels 已提交
1967 1968
                                std::to_string(pos_) + ": " + msg +
                                ", last read: '" + current_ + "'");
N
Niels 已提交
1969 1970 1971 1972 1973 1974 1975 1976
}

/*!
Parses a string after opening quotes (\p ") where read.

@return the parsed string

@pre  An opening quote \p " was read in the main parse function @ref parse.
N
Niels 已提交
1977
      pos_ is the position after the opening quote.
N
Niels 已提交
1978 1979

@post The character after the closing quote \p " is the current character @ref
N
Niels 已提交
1980
      current_. Whitespace is skipped.
N
Niels 已提交
1981
*/
N
Niels 已提交
1982
std::string json::parser::parseString()
N
Niels 已提交
1983 1984
{
    // get position of closing quotes
N
Niels 已提交
1985
    auto quotepos_ = buffer_.find_first_of("\"", pos_);
N
Niels 已提交
1986

N
Niels 已提交
1987 1988
    // if the closing quotes are escaped (character before the quotes is a
    // backslash), we continue looking for the final quotes
N
Niels 已提交
1989
    while (quotepos_ != std::string::npos and buffer_[quotepos_ - 1] == '\\')
N
Niels 已提交
1990
    {
N
Niels 已提交
1991
        quotepos_ = buffer_.find_first_of("\"", quotepos_ + 1);
N
Niels 已提交
1992 1993 1994
    }

    // check if closing quotes were found
N
Niels 已提交
1995
    if (quotepos_ == std::string::npos)
N
Niels 已提交
1996 1997 1998 1999
    {
        error("expected '\"'");
    }

N
Niels 已提交
2000
    // store the coordinates of the string for the later return value
N
Niels 已提交
2001 2002
    const auto stringBegin = pos_;
    const auto stringLength = quotepos_ - pos_;
N
Niels 已提交
2003

N
Niels 已提交
2004
    // set buffer position to the position behind (+1) the closing quote
N
Niels 已提交
2005
    pos_ = quotepos_ + 1;
N
Niels 已提交
2006 2007 2008 2009

    // read next character
    next();

N
Niels 已提交
2010
    // return the string value
N
Niels 已提交
2011
    return buffer_.substr(stringBegin, stringLength);
N
Niels 已提交
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
}

/*!
This function is called in case a \p "t" is read in the main parse function
@ref parse. In the standard, the \p "true" token is the only candidate, so the
next three characters are expected to be \p "rue". In case of a mismatch, an
error is raised via @ref error.

@pre  A \p "t" was read in the main parse function @ref parse.
@post The character after the \p "true" is the current character. Whitespace is
      skipped.
*/
N
Niels 已提交
2024
void json::parser::parseTrue()
N
Niels 已提交
2025
{
N
Niels 已提交
2026
    if (buffer_.substr(pos_, 3) != "rue")
N
Niels 已提交
2027 2028 2029 2030
    {
        error("expected true");
    }

N
Niels 已提交
2031
    pos_ += 3;
N
Niels 已提交
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046

    // read next character
    next();
}

/*!
This function is called in case an \p "f" is read in the main parse function
@ref parse. In the standard, the \p "false" token is the only candidate, so the
next four characters are expected to be \p "alse". In case of a mismatch, an
error is raised via @ref error.

@pre  An \p "f" was read in the main parse function.
@post The character after the \p "false" is the current character. Whitespace
      is skipped.
*/
N
Niels 已提交
2047
void json::parser::parseFalse()
N
Niels 已提交
2048
{
N
Niels 已提交
2049
    if (buffer_.substr(pos_, 4) != "alse")
N
Niels 已提交
2050 2051 2052 2053
    {
        error("expected false");
    }

N
Niels 已提交
2054
    pos_ += 4;
N
Niels 已提交
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069

    // read next character
    next();
}

/*!
This function is called in case an \p "n" is read in the main parse function
@ref parse. In the standard, the \p "null" token is the only candidate, so the
next three characters are expected to be \p "ull". In case of a mismatch, an
error is raised via @ref error.

@pre  An \p "n" was read in the main parse function.
@post The character after the \p "null" is the current character. Whitespace is
      skipped.
*/
N
Niels 已提交
2070
void json::parser::parseNull()
N
Niels 已提交
2071
{
N
Niels 已提交
2072
    if (buffer_.substr(pos_, 3) != "ull")
N
Niels 已提交
2073 2074 2075 2076
    {
        error("expected null");
    }

N
Niels 已提交
2077
    pos_ += 3;
N
Niels 已提交
2078 2079 2080 2081 2082 2083 2084

    // read next character
    next();
}

/*!
This function wraps functionality to check whether the current character @ref
N
Niels 已提交
2085 2086
current_ matches a given character \p c. In case of a match, the next character
of the buffer @ref buffer_ is read. In case of a mismatch, an error is raised
N
Niels 已提交
2087 2088 2089 2090 2091 2092
via @ref error.

@param c  character that is expected

@post The next chatacter is read. Whitespace is skipped.
*/
N
Niels 已提交
2093
void json::parser::expect(const char c)
N
Niels 已提交
2094
{
N
Niels 已提交
2095
    if (current_ != c)
N
Niels 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
    {
        std::string msg = "expected '";
        msg.append(1, c);
        msg += "'";
        error(msg);
    }
    else
    {
        next();
    }
}

/*!
This operator implements a user-defined string literal for JSON objects. It can
be used by adding \p "_json" to a string literal and returns a JSON object if
no parse error occurred.

@param s  a string representation of a JSON object
@return a JSON object
*/
N
Niels 已提交
2116
json operator "" _json(const char* s, size_t)
N
Niels 已提交
2117
{
N
Niels 已提交
2118
    return json::parse(s);
N
Niels 已提交
2119
}