json.hpp 132.9 KB
Newer Older
N
Niels 已提交
1 2 3 4 5 6 7 8 9
/*!
@file
@copyright The code is licensed under the MIT License
           <http://opensource.org/licenses/MIT>,
           Copyright (c) 2013-2015 Niels Lohmann.
@author Niels Lohmann <http://nlohmann.me>
@see https://github.com/nlohmann/json
*/

N
Niels 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*!
@defgroup container Container
@brief C++ Container concept

A Container is an object used to store other objects and taking care of the
management of the memory used by the objects it contains.

@see http://en.cppreference.com/w/cpp/concept/Container

@defgroup reversiblecontainer Reversible Container
@brief C++ Reversible Container concept
@ingroup container

A ReversibleContainer is a Container that has iterators that meet the
requirements of either BidirectionalIterator or RandomAccessIterator. Such
iterators allow a ReversibleContainer to be iterated over in reverse.

@see http://en.cppreference.com/w/cpp/concept/ReversibleContainer
*/

N
Niels 已提交
30 31 32 33
#ifndef _NLOHMANN_JSON
#define _NLOHMANN_JSON

#include <algorithm>
N
Niels 已提交
34
#include <cmath>
N
Niels 已提交
35
#include <cstdio>
N
Niels 已提交
36 37
#include <functional>
#include <initializer_list>
N
Niels 已提交
38
#include <iomanip>
N
Niels 已提交
39 40 41 42 43
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
N
Niels 已提交
44
#include <sstream>
N
Niels 已提交
45 46 47 48 49 50
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

/*!
N
Niels 已提交
51
@brief namespace for Niels Lohmann
N
Niels 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
@see https://github.com/nlohmann
*/
namespace nlohmann
{

/*!
@brief JSON

@tparam ObjectType         type for JSON objects
                           (@c std::map by default)
@tparam ArrayType          type for JSON arrays
                           (@c std::vector by default)
@tparam StringType         type for JSON strings and object keys
                           (@c std::string by default)
@tparam BooleanType        type for JSON booleans
                           (@c bool by default)
@tparam NumberIntegerType  type for JSON integer numbers
                           (@c int64_t by default)
N
Niels 已提交
70
@tparam NumberFloatType    type for JSON floating-point numbers
N
Niels 已提交
71
                           (@c double by default)
N
Niels 已提交
72 73
@tparam Allocator          type of the allocator to use
                           (@c std::allocator by default)
N
Niels 已提交
74

N
Niels 已提交
75 76
@note ObjectType trick from http://stackoverflow.com/a/9860911

N
Niels 已提交
77 78
@see RFC 7159 <http://rfc7159.net/rfc7159>
@see ECMA 404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>
N
Niels 已提交
79 80 81 82 83 84 85
*/
template <
    template<typename U, typename V, typename... Args> class ObjectType = std::map,
    template<typename U, typename... Args> class ArrayType = std::vector,
    class StringType = std::string,
    class BooleanType = bool,
    class NumberIntegerType = int64_t,
N
Niels 已提交
86 87
    class NumberFloatType = double,
    template<typename U> class Allocator = std::allocator
N
Niels 已提交
88 89 90 91 92 93 94 95
    >
class basic_json
{
  public:
    /////////////////////
    // container types //
    /////////////////////

N
Niels 已提交
96
    // forward declarations
N
Niels 已提交
97 98 99
    class iterator;
    class const_iterator;

N
Niels 已提交
100 101 102 103
    /*!
    @brief the type of elements in a basic_json container
    @ingroup container
    */
N
Niels 已提交
104
    using value_type = basic_json;
N
Niels 已提交
105 106 107 108 109

    /*!
    @brief the type of an element reference
    @ingroup container
    */
N
Niels 已提交
110
    using reference = basic_json&;
N
Niels 已提交
111 112 113 114 115

    /*!
    @brief the type of an element const reference
    @ingroup container
    */
N
Niels 已提交
116
    using const_reference = const basic_json&;
N
Niels 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    /*!
    @brief a type to represent differences between iterators
    @ingroup container
    */
    using difference_type = std::ptrdiff_t;

    /*!
    @brief a type to represent container sizes
    @ingroup container
    */
    using size_type = std::size_t;

    /// the allocator type
    using allocator_type = Allocator<basic_json>;

N
Niels 已提交
133 134 135 136
    /// the type of an element pointer
    using pointer = basic_json*;
    /// the type of an element const pointer
    using const_pointer = const basic_json*;
N
Niels 已提交
137 138 139 140 141

    /*!
    @brief an iterator for a basic_json container
    @ingroup container
    */
N
Niels 已提交
142
    using iterator = basic_json::iterator;
N
Niels 已提交
143 144 145 146 147

    /*!
    @brief a const iterator for a basic_json container
    @ingroup container
    */
N
Niels 已提交
148
    using const_iterator = basic_json::const_iterator;
N
Niels 已提交
149 150 151 152 153

    /*!
    @brief a reverse iterator for a basic_json container
    @ingroup reversiblecontainer
    */
N
Niels 已提交
154
    using reverse_iterator = std::reverse_iterator<iterator>;
N
Niels 已提交
155 156 157 158 159

    /*!
    @brief a const reverse iterator for a basic_json container
    @ingroup reversiblecontainer
    */
N
Niels 已提交
160
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
N
Niels 已提交
161

N
Niels 已提交
162

N
Niels 已提交
163 164 165 166 167 168 169
    /// returns the allocator associated with the container
    inline allocator_type get_allocator() const
    {
        return allocator_type();
    }


N
Niels 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183
    ///////////////////////////
    // JSON value data types //
    ///////////////////////////

    /// a type for an object
    using object_t = ObjectType<StringType, basic_json>;
    /// a type for an array
    using array_t = ArrayType<basic_json>;
    /// a type for a string
    using string_t = StringType;
    /// a type for a boolean
    using boolean_t = BooleanType;
    /// a type for a number (integer)
    using number_integer_t = NumberIntegerType;
N
Niels 已提交
184
    /// a type for a number (floating-point)
N
Niels 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    using number_float_t = NumberFloatType;
    /// a type for list initialization
    using list_init_t = std::initializer_list<basic_json>;


    ////////////////////////
    // JSON value storage //
    ////////////////////////

    /// a JSON value
    union json_value
    {
        /// object (stored with pointer to save storage)
        object_t* object;
        /// array (stored with pointer to save storage)
        array_t* array;
        /// string (stored with pointer to save storage)
        string_t* string;
        /// bolean
        boolean_t boolean;
        /// number (integer)
        number_integer_t number_integer;
N
Niels 已提交
207
        /// number (floating-point)
N
Niels 已提交
208 209 210 211 212 213 214 215
        number_float_t number_float;

        /// default constructor (for null values)
        json_value() = default;
        /// constructor for booleans
        json_value(boolean_t v) : boolean(v) {}
        /// constructor for numbers (integer)
        json_value(number_integer_t v) : number_integer(v) {}
N
Niels 已提交
216
        /// constructor for numbers (floating-point)
N
Niels 已提交
217 218 219 220 221 222 223 224 225 226 227
        json_value(number_float_t v) : number_float(v) {}
    };


    /////////////////////////////////
    // JSON value type enumeration //
    /////////////////////////////////

    /// JSON value type enumeration
    enum class value_t : uint8_t
    {
N
Niels 已提交
228 229 230 231 232 233 234
        null,           ///< null value
        object,         ///< object (unordered set of name/value pairs)
        array,          ///< array (ordered collection of values)
        string,         ///< string value
        boolean,        ///< boolean value
        number_integer, ///< number value (integer)
        number_float    ///< number value (floating-point)
N
Niels 已提交
235 236
    };

N
Niels 已提交
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
    /*!
    @brief comparison operator for JSON value types

    Returns an ordering that is similar to Python:
    - order: null < boolean < number < object < array < string
    - furthermore, each type is not smaller than itself
    */
    friend bool operator<(const value_t lhs, const value_t rhs)
    {
        // no type is smaller than itself
        if (lhs == rhs)
        {
            return false;
        }

        switch (lhs)
        {
            case (value_t::null):
            {
                // nulls are smaller than all other types
                return true;
            }

            case (value_t::boolean):
            {
                // only nulls are smaller than booleans
                return (rhs != value_t::null);
            }

            case (value_t::number_float):
            case (value_t::number_integer):
            {
                switch (rhs)
                {
                    // numbers are smaller than objects, arrays, and string
                    case (value_t::object):
                    case (value_t::array):
                    case (value_t::string):
                    {
                        return true;
                    }
N
Niels 已提交
278

N
Niels 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
                    default:
                    {
                        return false;
                    }
                }
            }

            case (value_t::object):
            {
                switch (rhs)
                {
                    // objects are smaller than arrays and string
                    case (value_t::array):
                    case (value_t::string):
                    {
                        return true;
                    }
N
Niels 已提交
296

N
Niels 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
                    default:
                    {
                        return false;
                    }
                }
            }

            case (value_t::array):
            {
                // arrays are smaller than strings
                return (rhs == value_t::string);
            }

            default:
            {
                // a string is not smaller than any other types
                return false;
            }
        }
    }

N
Niels 已提交
318 319 320 321 322

    //////////////////
    // constructors //
    //////////////////

N
Niels 已提交
323 324 325 326 327 328
    /*!
    @brief create an empty value with a given type
    @param value  the type to create an value of

    @exception std::bad_alloc  if allocation for object, array, or string fails.
    */
N
Niels 已提交
329 330 331 332 333 334 335 336 337 338 339 340
    inline basic_json(const value_t value)
        : m_type(value)
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                break;
            }

            case (value_t::object):
            {
N
Niels 已提交
341 342 343
                Allocator<object_t> alloc;
                m_value.object = alloc.allocate(1);
                alloc.construct(m_value.object);
N
Niels 已提交
344 345 346 347 348
                break;
            }

            case (value_t::array):
            {
N
Niels 已提交
349 350 351
                Allocator<array_t> alloc;
                m_value.array = alloc.allocate(1);
                alloc.construct(m_value.array);
N
Niels 已提交
352 353 354 355 356
                break;
            }

            case (value_t::string):
            {
N
Niels 已提交
357 358 359
                Allocator<string_t> alloc;
                m_value.string = alloc.allocate(1);
                alloc.construct(m_value.string, "");
N
Niels 已提交
360 361 362 363 364
                break;
            }

            case (value_t::boolean):
            {
N
Niels 已提交
365
                m_value.boolean = boolean_t(false);
N
Niels 已提交
366 367 368 369 370
                break;
            }

            case (value_t::number_integer):
            {
N
Niels 已提交
371
                m_value.number_integer = number_integer_t(0);
N
Niels 已提交
372 373 374 375 376
                break;
            }

            case (value_t::number_float):
            {
N
Niels 已提交
377
                m_value.number_float = number_float_t(0.0);
N
Niels 已提交
378 379 380 381 382
                break;
            }
        }
    }

N
Niels 已提交
383 384 385 386 387
    /*!
    @brief create a null object (implicitly)
    @ingroup container
    */
    inline basic_json() noexcept = default;
N
Niels 已提交
388 389 390 391 392 393 394 395

    /// create a null object (explicitly)
    inline basic_json(std::nullptr_t) noexcept
        : m_type(value_t::null)
    {}

    /// create an object (explicit)
    inline basic_json(const object_t& value)
N
Niels 已提交
396 397 398 399 400 401
        : m_type(value_t::object)
    {
        Allocator<object_t> alloc;
        m_value.object = alloc.allocate(1);
        alloc.construct(m_value.object, value);
    }
N
Niels 已提交
402 403 404 405 406 407 408 409

    /// create an object (implicit)
    template <class V, typename
              std::enable_if<
                  std::is_constructible<string_t, typename V::key_type>::value and
                  std::is_constructible<basic_json, typename V::mapped_type>::value, int>::type
              = 0>
    inline basic_json(const V& value)
N
Niels 已提交
410 411 412 413
        : m_type(value_t::object)
    {
        Allocator<object_t> alloc;
        m_value.object = alloc.allocate(1);
414 415 416
        using std::begin;
        using std::end;
        alloc.construct(m_value.object, begin(value), end(value));
N
Niels 已提交
417
    }
N
Niels 已提交
418 419 420

    /// create an array (explicit)
    inline basic_json(const array_t& value)
N
Niels 已提交
421 422 423 424 425 426
        : m_type(value_t::array)
    {
        Allocator<array_t> alloc;
        m_value.array = alloc.allocate(1);
        alloc.construct(m_value.array, value);
    }
N
Niels 已提交
427 428 429 430 431 432

    /// create an array (implicit)
    template <class V, typename
              std::enable_if<
                  not std::is_same<V, basic_json::iterator>::value and
                  not std::is_same<V, basic_json::const_iterator>::value and
N
Niels 已提交
433 434
                  not std::is_same<V, basic_json::reverse_iterator>::value and
                  not std::is_same<V, basic_json::const_reverse_iterator>::value and
N
Niels 已提交
435 436
                  not std::is_same<V, typename array_t::iterator>::value and
                  not std::is_same<V, typename array_t::const_iterator>::value and
N
Niels 已提交
437 438 439
                  std::is_constructible<basic_json, typename V::value_type>::value, int>::type
              = 0>
    inline basic_json(const V& value)
N
Niels 已提交
440 441 442 443
        : m_type(value_t::array)
    {
        Allocator<array_t> alloc;
        m_value.array = alloc.allocate(1);
444 445 446
        using std::begin;
        using std::end;
        alloc.construct(m_value.array, begin(value), end(value));
N
Niels 已提交
447
    }
N
Niels 已提交
448 449 450

    /// create a string (explicit)
    inline basic_json(const string_t& value)
N
Niels 已提交
451
        : m_type(value_t::string)
N
Niels 已提交
452 453 454 455 456
    {
        Allocator<string_t> alloc;
        m_value.string = alloc.allocate(1);
        alloc.construct(m_value.string, value);
    }
N
Niels 已提交
457 458 459

    /// create a string (explicit)
    inline basic_json(const typename string_t::value_type* value)
N
Niels 已提交
460
        : m_type(value_t::string)
N
Niels 已提交
461 462 463 464 465
    {
        Allocator<string_t> alloc;
        m_value.string = alloc.allocate(1);
        alloc.construct(m_value.string, value);
    }
N
Niels 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

    /// create a string (implicit)
    template <class V, typename
              std::enable_if<
                  std::is_constructible<string_t, V>::value, int>::type
              = 0>
    inline basic_json(const V& value)
        : basic_json(string_t(value))
    {}

    /// create a boolean (explicit)
    inline basic_json(boolean_t value)
        : m_type(value_t::boolean), m_value(value)
    {}

    /// create an integer number (explicit)
    inline basic_json(const number_integer_t& value)
        : m_type(value_t::number_integer), m_value(value)
    {}

    /// create an integer number (implicit)
    template<typename T, typename
             std::enable_if<
                 std::is_constructible<number_integer_t, T>::value and
                 std::numeric_limits<T>::is_integer, T>::type
             = 0>
    inline basic_json(const T value) noexcept
        : m_type(value_t::number_integer), m_value(number_integer_t(value))
    {}

N
Niels 已提交
496
    /// create a floating-point number (explicit)
N
Niels 已提交
497 498 499 500
    inline basic_json(const number_float_t& value)
        : m_type(value_t::number_float), m_value(value)
    {}

N
Niels 已提交
501
    /// create a floating-point number (implicit)
N
Niels 已提交
502 503 504 505 506 507 508 509 510
    template<typename T, typename = typename
             std::enable_if<
                 std::is_constructible<number_float_t, T>::value and
                 std::is_floating_point<T>::value>::type
             >
    inline basic_json(const T value) noexcept
        : m_type(value_t::number_float), m_value(number_float_t(value))
    {}

N
Niels 已提交
511
    /// create a container (array or object) from an initializer list
N
Niels 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    inline basic_json(list_init_t l, bool type_deduction = true, value_t manual_type = value_t::array)
    {
        // the initializer list could describe an object
        bool is_object = true;

        // check if each element is an array with two elements whose first element
        // is a string
        for (const auto& element : l)
        {
            if ((element.m_final and element.m_type == value_t::array)
                    or (element.m_type != value_t::array or element.size() != 2
                        or element[0].m_type != value_t::string))
            {
                // we found an element that makes it impossible to use the
                // initializer list as object
                is_object = false;
                break;
            }
        }

        // adjust type if type deduction is not wanted
        if (not type_deduction)
        {
            // mark this object's type as final
            m_final = true;

            // if array is wanted, do not create an object though possible
            if (manual_type == value_t::array)
            {
                is_object = false;
            }

            // if object is wanted but impossible, throw an exception
            if (manual_type == value_t::object and not is_object)
            {
                throw std::logic_error("cannot create JSON object from initializer list");
            }
        }

        if (is_object)
        {
            // the initializer list is a list of pairs -> create object
            m_type = value_t::object;
N
Niels 已提交
555 556 557 558
            Allocator<object_t> alloc;
            m_value.object = alloc.allocate(1);
            alloc.construct(m_value.object);

N
Niels 已提交
559 560 561 562 563 564 565 566 567
            for (auto& element : l)
            {
                m_value.object->emplace(std::move(*(element[0].m_value.string)), std::move(element[1]));
            }
        }
        else
        {
            // the initializer list describes an array -> create array
            m_type = value_t::array;
N
Niels 已提交
568 569 570
            Allocator<array_t> alloc;
            m_value.array = alloc.allocate(1);
            alloc.construct(m_value.array, std::move(l));
N
Niels 已提交
571 572 573
        }
    }

N
Niels 已提交
574
    /// explicitly create an array from an initializer list
N
Niels 已提交
575 576 577 578 579
    inline static basic_json array(list_init_t l = list_init_t())
    {
        return basic_json(l, false, value_t::array);
    }

N
Niels 已提交
580
    /// explicitly create an object from an initializer list
N
Niels 已提交
581 582 583 584 585
    inline static basic_json object(list_init_t l = list_init_t())
    {
        return basic_json(l, false, value_t::object);
    }

N
Niels 已提交
586

N
Niels 已提交
587 588 589 590
    ///////////////////////////////////////
    // other constructors and destructor //
    ///////////////////////////////////////

N
Niels 已提交
591 592
    /*!
    @brief copy constructor
N
Niels 已提交
593 594 595

    @exception std::bad_alloc  if allocation for object, array, or string fails.

N
Niels 已提交
596 597
    @ingroup container
    */
N
Niels 已提交
598 599 600 601 602 603 604 605 606
    inline basic_json(const basic_json& other)
        : m_type(other.m_type)
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                break;
            }
N
Niels 已提交
607

N
Niels 已提交
608 609
            case (value_t::object):
            {
N
Niels 已提交
610 611 612
                Allocator<object_t> alloc;
                m_value.object = alloc.allocate(1);
                alloc.construct(m_value.object, *other.m_value.object);
N
Niels 已提交
613 614
                break;
            }
N
Niels 已提交
615

N
Niels 已提交
616 617
            case (value_t::array):
            {
N
Niels 已提交
618 619 620
                Allocator<array_t> alloc;
                m_value.array = alloc.allocate(1);
                alloc.construct(m_value.array, *other.m_value.array);
N
Niels 已提交
621 622
                break;
            }
N
Niels 已提交
623

N
Niels 已提交
624 625
            case (value_t::string):
            {
N
Niels 已提交
626 627 628
                Allocator<string_t> alloc;
                m_value.string = alloc.allocate(1);
                alloc.construct(m_value.string, *other.m_value.string);
N
Niels 已提交
629 630
                break;
            }
N
Niels 已提交
631

N
Niels 已提交
632 633 634 635 636
            case (value_t::boolean):
            {
                m_value.boolean = other.m_value.boolean;
                break;
            }
N
Niels 已提交
637

N
Niels 已提交
638 639 640 641 642
            case (value_t::number_integer):
            {
                m_value.number_integer = other.m_value.number_integer;
                break;
            }
N
Niels 已提交
643

N
Niels 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656
            case (value_t::number_float):
            {
                m_value.number_float = other.m_value.number_float;
                break;
            }
        }
    }

    /// move constructor
    inline basic_json(basic_json&& other) noexcept
        : m_type(std::move(other.m_type)),
          m_value(std::move(other.m_value))
    {
N
Niels 已提交
657
        // invalidate payload
N
Niels 已提交
658 659 660 661
        other.m_type = value_t::null;
        other.m_value = {};
    }

N
Niels 已提交
662 663 664 665
    /*!
    @brief copy assignment
    @ingroup container
    */
N
Niels 已提交
666 667 668 669 670 671
    inline reference& operator=(basic_json other) noexcept (
        std::is_nothrow_move_constructible<value_t>::value and
        std::is_nothrow_move_assignable<value_t>::value and
        std::is_nothrow_move_constructible<json_value>::value and
        std::is_nothrow_move_assignable<json_value>::value
    )
N
Niels 已提交
672 673 674 675 676 677
    {
        std::swap(m_type, other.m_type);
        std::swap(m_value, other.m_value);
        return *this;
    }

N
Niels 已提交
678 679 680 681
    /*!
    @brief destructor
    @ingroup container
    */
N
Niels 已提交
682 683 684 685 686 687
    inline ~basic_json() noexcept
    {
        switch (m_type)
        {
            case (value_t::object):
            {
N
Niels 已提交
688 689 690
                Allocator<object_t> alloc;
                alloc.destroy(m_value.object);
                alloc.deallocate(m_value.object, 1);
N
Niels 已提交
691 692 693
                m_value.object = nullptr;
                break;
            }
N
Niels 已提交
694

N
Niels 已提交
695 696
            case (value_t::array):
            {
N
Niels 已提交
697 698 699
                Allocator<array_t> alloc;
                alloc.destroy(m_value.array);
                alloc.deallocate(m_value.array, 1);
N
Niels 已提交
700 701 702
                m_value.array = nullptr;
                break;
            }
N
Niels 已提交
703

N
Niels 已提交
704 705
            case (value_t::string):
            {
N
Niels 已提交
706
                Allocator<string_t> alloc;
N
Niels 已提交
707
                alloc.destroy(m_value.string);
N
Niels 已提交
708
                alloc.deallocate(m_value.string, 1);
N
Niels 已提交
709 710 711
                m_value.string = nullptr;
                break;
            }
N
Niels 已提交
712 713

            default:
N
Niels 已提交
714
            {
N
Niels 已提交
715
                // all other types need no specific destructor
N
Niels 已提交
716 717 718 719 720 721 722 723 724 725 726 727
                break;
            }
        }
    }


  public:
    ///////////////////////
    // object inspection //
    ///////////////////////

    /*!
N
Niels 已提交
728 729
    @brief serialization

N
Niels 已提交
730 731 732
    Serialization function for JSON objects. The function tries to mimick
    Python's @p json.dumps() function, and currently supports its @p indent
    parameter.
N
Niels 已提交
733

N
Niels 已提交
734 735 736 737
    @param indent  sif indent is nonnegative, then array elements and object
    members will be pretty-printed with that indent level. An indent level of 0
    will only insert newlines. -1 (the default) selects the most compact
    representation
N
Niels 已提交
738 739 740

    @see https://docs.python.org/2/library/json.html#json.dump
    */
N
Niels 已提交
741
    inline string_t dump(const int indent = -1) const noexcept
N
Niels 已提交
742 743 744 745 746 747 748 749 750 751 752
    {
        if (indent >= 0)
        {
            return dump(true, static_cast<unsigned int>(indent));
        }
        else
        {
            return dump(false, 0);
        }
    }

N
Niels 已提交
753
    /// return the type of the object (explicit)
N
Niels 已提交
754 755 756 757 758
    inline value_t type() const noexcept
    {
        return m_type;
    }

N
Niels 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    // return whether value is null
    inline bool is_null() const noexcept
    {
        return m_type == value_t::null;
    }

    // return whether value is boolean
    inline bool is_boolean() const noexcept
    {
        return m_type == value_t::boolean;
    }

    // return whether value is number
    inline bool is_number() const noexcept
    {
        return (m_type == value_t::number_integer) or (m_type == value_t::number_float);
    }

    // return whether value is object
    inline bool is_object() const noexcept
    {
        return m_type == value_t::object;
    }

    // return whether value is array
    inline bool is_array() const noexcept
    {
        return m_type == value_t::array;
    }

    // return whether value is string
    inline bool is_string() const noexcept
    {
        return m_type == value_t::string;
    }

N
Niels 已提交
795
    /// return the type of the object (implicit)
N
Niels 已提交
796
    inline operator value_t() const noexcept
N
Niels 已提交
797 798 799 800 801 802 803 804 805
    {
        return m_type;
    }


    //////////////////////
    // value conversion //
    //////////////////////

N
Niels 已提交
806
    /// get an object (explicit)
N
Niels 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    template <class T, typename
              std::enable_if<
                  std::is_constructible<string_t, typename T::key_type>::value and
                  std::is_constructible<basic_json, typename T::mapped_type>::value, int>::type
              = 0>
    inline T get() const
    {
        switch (m_type)
        {
            case (value_t::object):
                return T(m_value.object->begin(), m_value.object->end());
            default:
                throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
        }
    }

N
Niels 已提交
823
    /// get an array (explicit)
N
Niels 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    template <class T, typename
              std::enable_if<
                  not std::is_same<T, string_t>::value and
                  std::is_constructible<basic_json, typename T::value_type>::value, int>::type
              = 0>
    inline T get() const
    {
        switch (m_type)
        {
            case (value_t::array):
                return T(m_value.array->begin(), m_value.array->end());
            default:
                throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
        }
    }

N
Niels 已提交
840
    /// get a string (explicit)
N
Niels 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
    template <typename T, typename
              std::enable_if<
                  std::is_constructible<T, string_t>::value, int>::type
              = 0>
    inline T get() const
    {
        switch (m_type)
        {
            case (value_t::string):
                return *m_value.string;
            default:
                throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
        }
    }

N
Niels 已提交
856
    /// get a boolean (explicit)
N
Niels 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    template <typename T, typename
              std::enable_if<
                  std::is_same<boolean_t, T>::value, int>::type
              = 0>
    inline T get() const
    {
        switch (m_type)
        {
            case (value_t::boolean):
                return m_value.boolean;
            default:
                throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
        }
    }

N
Niels 已提交
872
    /// get a number (explicit)
N
Niels 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    template<typename T, typename
             std::enable_if<
                 not std::is_same<boolean_t, T>::value and
                 std::is_arithmetic<T>::value, int>::type
             = 0>
    inline T get() const
    {
        switch (m_type)
        {
            case (value_t::number_integer):
                return static_cast<T>(m_value.number_integer);
            case (value_t::number_float):
                return static_cast<T>(m_value.number_float);
            default:
                throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
        }
    }

N
Niels 已提交
891
    /// get a value (implicit)
N
Niels 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    template<typename T>
    inline operator T() const
    {
        return get<T>();
    }


    ////////////////////
    // element access //
    ////////////////////

    /// access specified element with bounds checking
    inline reference at(size_type pos)
    {
        // at only works for arrays
        if (m_type != value_t::array)
        {
            throw std::runtime_error("cannot use at with " + type_name());
        }

        return m_value.array->at(pos);
    }

    /// access specified element with bounds checking
    inline const_reference at(size_type pos) const
    {
        // at only works for arrays
        if (m_type != value_t::array)
        {
            throw std::runtime_error("cannot use at with " + type_name());
        }

        return m_value.array->at(pos);
    }

    /// access specified element
    inline reference operator[](size_type pos)
    {
N
Niels 已提交
930 931 932 933 934 935 936 937 938 939
        // implicitly convert null to object
        if (m_type == value_t::null)
        {
            m_type = value_t::array;
            Allocator<array_t> alloc;
            m_value.array = alloc.allocate(1);
            alloc.construct(m_value.array);
        }

        // [] only works for arrays
N
Niels 已提交
940 941 942 943 944
        if (m_type != value_t::array)
        {
            throw std::runtime_error("cannot use [] with " + type_name());
        }

N
Niels 已提交
945 946 947 948 949
        for (size_t i = m_value.array->size(); i <= pos; ++i)
        {
            m_value.array->push_back(basic_json());
        }

N
Niels 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
        return m_value.array->operator[](pos);
    }

    /// access specified element
    inline const_reference operator[](size_type pos) const
    {
        // at only works for arrays
        if (m_type != value_t::array)
        {
            throw std::runtime_error("cannot use [] with " + type_name());
        }

        return m_value.array->operator[](pos);
    }

    /// access specified element with bounds checking
    inline reference at(const typename object_t::key_type& key)
    {
        // at only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use at with " + type_name());
        }

        return m_value.object->at(key);
    }

    /// access specified element with bounds checking
    inline const_reference at(const typename object_t::key_type& key) const
    {
        // at only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use at with " + type_name());
        }

        return m_value.object->at(key);
    }

    /// access specified element
    inline reference operator[](const typename object_t::key_type& key)
    {
N
Niels 已提交
992 993 994 995 996 997 998 999 1000
        // implicitly convert null to object
        if (m_type == value_t::null)
        {
            m_type = value_t::object;
            Allocator<object_t> alloc;
            m_value.object = alloc.allocate(1);
            alloc.construct(m_value.object);
        }

N
Niels 已提交
1001
        // [] only works for objects
N
Niels 已提交
1002 1003 1004 1005 1006 1007 1008 1009
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use [] with " + type_name());
        }

        return m_value.object->operator[](key);
    }

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    /// access specified element
    inline const_reference operator[](const typename object_t::key_type& key) const
    {
        // at only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use [] with " + type_name());
        }

        return m_value.object->operator[](key);
    }

N
Niels 已提交
1022 1023 1024 1025
    /// access specified element (needed for clang)
    template<typename T, size_t n>
    inline reference operator[](const T (&key)[n])
    {
N
Niels 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034
        // implicitly convert null to object
        if (m_type == value_t::null)
        {
            m_type = value_t::object;
            Allocator<object_t> alloc;
            m_value.object = alloc.allocate(1);
            alloc.construct(m_value.object);
        }

N
Niels 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043
        // at only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use [] with " + type_name());
        }

        return m_value.object->operator[](key);
    }

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    /// access specified element (needed for clang)
    template<typename T, size_t n>
    inline const_reference operator[](const T (&key)[n]) const
    {
        // at only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use [] with " + type_name());
        }

        return m_value.object->operator[](key);
    }

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    /// remove element from an object given a key
    inline size_type erase(const typename object_t::key_type& key)
    {
        // at only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use at with " + type_name());
        }

        return m_value.object->erase(key);
    }

N
Niels 已提交
1069
    /// find an element in an object
N
Niels 已提交
1070
    inline iterator find(typename object_t::key_type key)
N
Niels 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    {
        auto result = end();

        if (m_type == value_t::object)
        {
            result.m_it.object_iterator = m_value.object->find(key);
        }

        return result;
    }

    /// find an element in an object
N
Niels 已提交
1083
    inline const_iterator find(typename object_t::key_type key) const
N
Niels 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    {
        auto result = cend();

        if (m_type == value_t::object)
        {
            result.m_it.object_iterator = m_value.object->find(key);
        }

        return result;
    }

1095 1096 1097 1098 1099 1100 1101
    /// returns the number of occurrences of a key in an object
    inline size_type count(typename object_t::key_type key) const
    {
        // return 0 for all nonobject types
        return (m_type == value_t::object) ? m_value.object->count(key) : 0;
    }

N
Niels 已提交
1102

N
Niels 已提交
1103 1104 1105 1106
    ///////////////
    // iterators //
    ///////////////

N
Niels 已提交
1107 1108 1109 1110
    /*!
    @brief returns an iterator to the first element
    @ingroup container
    */
N
Niels 已提交
1111 1112 1113 1114 1115 1116 1117
    inline iterator begin() noexcept
    {
        iterator result(this);
        result.set_begin();
        return result;
    }

N
Niels 已提交
1118 1119 1120 1121
    /*!
    @brief returns a const iterator to the first element
    @ingroup container
    */
N
Niels 已提交
1122 1123 1124 1125 1126 1127 1128
    inline const_iterator begin() const noexcept
    {
        const_iterator result(this);
        result.set_begin();
        return result;
    }

N
Niels 已提交
1129 1130 1131 1132
    /*!
    @brief returns a const iterator to the first element
    @ingroup container
    */
N
Niels 已提交
1133 1134 1135 1136 1137 1138 1139
    inline const_iterator cbegin() const noexcept
    {
        const_iterator result(this);
        result.set_begin();
        return result;
    }

N
Niels 已提交
1140 1141 1142 1143
    /*!
    @brief returns an iterator to one past the last element
    @ingroup container
    */
N
Niels 已提交
1144 1145 1146 1147 1148 1149 1150
    inline iterator end() noexcept
    {
        iterator result(this);
        result.set_end();
        return result;
    }

N
Niels 已提交
1151 1152 1153 1154
    /*!
    @brief returns a const iterator to one past the last element
    @ingroup container
    */
N
Niels 已提交
1155 1156 1157 1158 1159 1160 1161
    inline const_iterator end() const noexcept
    {
        const_iterator result(this);
        result.set_end();
        return result;
    }

N
Niels 已提交
1162 1163 1164 1165
    /*!
    @brief returns a const iterator to one past the last element
    @ingroup container
    */
N
Niels 已提交
1166 1167 1168 1169 1170 1171 1172
    inline const_iterator cend() const noexcept
    {
        const_iterator result(this);
        result.set_end();
        return result;
    }

N
Niels 已提交
1173 1174 1175 1176
    /*!
    @brief returns a reverse iterator to the first element
    @ingroup reversiblecontainer
    */
N
Niels 已提交
1177 1178 1179 1180 1181
    inline reverse_iterator rbegin() noexcept
    {
        return reverse_iterator(end());
    }

N
Niels 已提交
1182 1183 1184 1185
    /*!
    @brief returns a const reverse iterator to the first element
    @ingroup reversiblecontainer
    */
N
Niels 已提交
1186 1187 1188 1189 1190
    inline const_reverse_iterator rbegin() const noexcept
    {
        return const_reverse_iterator(end());
    }

N
Niels 已提交
1191 1192 1193 1194
    /*!
    @brief returns a reverse iterator to one past the last element
    @ingroup reversiblecontainer
    */
N
Niels 已提交
1195 1196 1197 1198 1199
    inline reverse_iterator rend() noexcept
    {
        return reverse_iterator(begin());
    }

N
Niels 已提交
1200 1201 1202 1203
    /*!
    @brief returns a const reverse iterator to one past the last element
    @ingroup reversiblecontainer
    */
N
Niels 已提交
1204 1205 1206 1207 1208
    inline const_reverse_iterator rend() const noexcept
    {
        return const_reverse_iterator(begin());
    }

N
Niels 已提交
1209 1210 1211 1212
    /*!
    @brief returns a const reverse iterator to the first element
    @ingroup reversiblecontainer
    */
N
Niels 已提交
1213 1214 1215 1216 1217
    inline const_reverse_iterator crbegin() const noexcept
    {
        return const_reverse_iterator(cend());
    }

N
Niels 已提交
1218 1219 1220 1221
    /*!
    @brief returns a const reverse iterator to one past the last element
    @ingroup reversiblecontainer
    */
N
Niels 已提交
1222 1223 1224 1225 1226
    inline const_reverse_iterator crend() const noexcept
    {
        return const_reverse_iterator(cbegin());
    }

N
Niels 已提交
1227 1228 1229 1230 1231

    //////////////
    // capacity //
    //////////////

N
Niels 已提交
1232 1233 1234 1235
    /*!
    @brief checks whether the container is empty
    @ingroup container
    */
N
Niels 已提交
1236 1237 1238 1239 1240 1241 1242 1243
    inline bool empty() const noexcept
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                return true;
            }
N
Niels 已提交
1244

N
Niels 已提交
1245 1246 1247 1248
            case (value_t::array):
            {
                return m_value.array->empty();
            }
N
Niels 已提交
1249

N
Niels 已提交
1250 1251 1252 1253
            case (value_t::object):
            {
                return m_value.object->empty();
            }
N
Niels 已提交
1254

N
Niels 已提交
1255 1256 1257 1258 1259 1260
            default:
            {
                // all other types are nonempty
                return false;
            }
        }
N
Niels 已提交
1261 1262
    }

N
Niels 已提交
1263 1264 1265 1266
    /*!
    @brief returns the number of elements
    @ingroup container
    */
N
Niels 已提交
1267 1268 1269 1270 1271 1272 1273 1274
    inline size_type size() const noexcept
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                return 0;
            }
N
Niels 已提交
1275

N
Niels 已提交
1276 1277 1278 1279
            case (value_t::array):
            {
                return m_value.array->size();
            }
N
Niels 已提交
1280

N
Niels 已提交
1281 1282 1283 1284
            case (value_t::object):
            {
                return m_value.object->size();
            }
N
Niels 已提交
1285

N
Niels 已提交
1286 1287 1288 1289 1290 1291
            default:
            {
                // all other types have size 1
                return 1;
            }
        }
N
Niels 已提交
1292 1293
    }

N
Niels 已提交
1294 1295 1296 1297
    /*!
    @brief returns the maximum possible number of elements
    @ingroup container
    */
N
Niels 已提交
1298 1299 1300 1301 1302 1303 1304 1305
    inline size_type max_size() const noexcept
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                return 0;
            }
N
Niels 已提交
1306

N
Niels 已提交
1307 1308 1309 1310
            case (value_t::array):
            {
                return m_value.array->max_size();
            }
N
Niels 已提交
1311

N
Niels 已提交
1312 1313 1314 1315
            case (value_t::object):
            {
                return m_value.object->max_size();
            }
N
Niels 已提交
1316

N
Niels 已提交
1317 1318 1319 1320 1321 1322
            default:
            {
                // all other types have max_size 1
                return 1;
            }
        }
N
Niels 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    }


    ///////////////
    // modifiers //
    ///////////////

    /// clears the contents
    inline void clear() noexcept
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                break;
            }
N
Niels 已提交
1339

N
Niels 已提交
1340 1341
            case (value_t::number_integer):
            {
N
Niels 已提交
1342
                m_value.number_integer = 0;
N
Niels 已提交
1343 1344
                break;
            }
N
Niels 已提交
1345

N
Niels 已提交
1346 1347
            case (value_t::number_float):
            {
N
Niels 已提交
1348
                m_value.number_float = 0.0;
N
Niels 已提交
1349 1350
                break;
            }
N
Niels 已提交
1351

N
Niels 已提交
1352 1353
            case (value_t::boolean):
            {
N
Niels 已提交
1354
                m_value.boolean = false;
N
Niels 已提交
1355 1356
                break;
            }
N
Niels 已提交
1357

N
Niels 已提交
1358 1359 1360 1361 1362
            case (value_t::string):
            {
                m_value.string->clear();
                break;
            }
N
Niels 已提交
1363

N
Niels 已提交
1364 1365 1366 1367 1368
            case (value_t::array):
            {
                m_value.array->clear();
                break;
            }
N
Niels 已提交
1369

N
Niels 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
            case (value_t::object):
            {
                m_value.object->clear();
                break;
            }
        }
    }

    /// add an object to an array
    inline void push_back(basic_json&& value)
    {
        // push_back only works for null objects or arrays
        if (not(m_type == value_t::null or m_type == value_t::array))
        {
            throw std::runtime_error("cannot add element to " + type_name());
        }

        // transform null object into an array
        if (m_type == value_t::null)
        {
            m_type = value_t::array;
N
Niels 已提交
1391 1392 1393
            Allocator<array_t> alloc;
            m_value.array = alloc.allocate(1);
            alloc.construct(m_value.array);
N
Niels 已提交
1394 1395 1396 1397 1398 1399 1400 1401
        }

        // add element to array (move semantics)
        m_value.array->push_back(std::move(value));
        // invalidate object
        value.m_type = value_t::null;
    }

N
Niels 已提交
1402 1403 1404 1405 1406 1407 1408
    /// add an object to an array
    inline reference operator+=(basic_json&& value)
    {
        push_back(std::move(value));
        return *this;
    }

N
Niels 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
    /// add an object to an array
    inline void push_back(const basic_json& value)
    {
        // push_back only works for null objects or arrays
        if (not(m_type == value_t::null or m_type == value_t::array))
        {
            throw std::runtime_error("cannot add element to " + type_name());
        }

        // transform null object into an array
        if (m_type == value_t::null)
        {
            m_type = value_t::array;
N
Niels 已提交
1422 1423 1424
            Allocator<array_t> alloc;
            m_value.array = alloc.allocate(1);
            alloc.construct(m_value.array);
N
Niels 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
        }

        // add element to array
        m_value.array->push_back(value);
    }

    /// add an object to an array
    inline reference operator+=(const basic_json& value)
    {
        push_back(value);
        return *this;
    }

    /// add an object to an object
    inline void push_back(const typename object_t::value_type& value)
    {
        // push_back only works for null objects or objects
        if (not(m_type == value_t::null or m_type == value_t::object))
        {
            throw std::runtime_error("cannot add element to " + type_name());
        }

        // transform null object into an object
        if (m_type == value_t::null)
        {
            m_type = value_t::object;
N
Niels 已提交
1451 1452 1453
            Allocator<object_t> alloc;
            m_value.object = alloc.allocate(1);
            alloc.construct(m_value.object);
N
Niels 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
        }

        // add element to array
        m_value.object->insert(value);
    }

    /// add an object to an object
    inline reference operator+=(const typename object_t::value_type& value)
    {
        push_back(value);
        return operator[](value.first);
    }

N
Niels 已提交
1467 1468 1469 1470
    /*!
    @brief exchanges the values
    @ingroup container
    */
N
Niels 已提交
1471 1472 1473 1474 1475 1476
    inline void swap(reference other) noexcept (
        std::is_nothrow_move_constructible<value_t>::value and
        std::is_nothrow_move_assignable<value_t>::value and
        std::is_nothrow_move_constructible<json_value>::value and
        std::is_nothrow_move_assignable<json_value>::value
    )
N
Niels 已提交
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
    {
        std::swap(m_type, other.m_type);
        std::swap(m_value, other.m_value);
    }

    /// swaps the contents
    inline void swap(array_t& other)
    {
        // swap only works for arrays
        if (m_type != value_t::array)
        {
            throw std::runtime_error("cannot use swap with " + type_name());
        }

        // swap arrays
        std::swap(*(m_value.array), other);
    }

    /// swaps the contents
    inline void swap(object_t& other)
    {
        // swap only works for objects
        if (m_type != value_t::object)
        {
            throw std::runtime_error("cannot use swap with " + type_name());
        }

        // swap arrays
        std::swap(*(m_value.object), other);
    }

    /// swaps the contents
    inline void swap(string_t& other)
    {
        // swap only works for strings
        if (m_type != value_t::string)
        {
            throw std::runtime_error("cannot use swap with " + type_name());
        }

        // swap arrays
        std::swap(*(m_value.string), other);
    }


    //////////////////////////////////////////
    // lexicographical comparison operators //
    //////////////////////////////////////////

N
Niels 已提交
1526 1527 1528 1529
    /*!
    @brief comparison: equal
    @ingroup container
    */
N
Niels 已提交
1530
    friend bool operator==(const_reference lhs, const_reference rhs) noexcept
N
Niels 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
    {
        switch (lhs.type())
        {
            case (value_t::array):
            {
                if (rhs.type() == value_t::array)
                {
                    return *lhs.m_value.array == *rhs.m_value.array;
                }
                break;
            }
            case (value_t::object):
            {
                if (rhs.type() == value_t::object)
                {
                    return *lhs.m_value.object == *rhs.m_value.object;
                }
                break;
            }
            case (value_t::null):
            {
                if (rhs.type() == value_t::null)
                {
                    return true;
                }
                break;
            }
            case (value_t::string):
            {
                if (rhs.type() == value_t::string)
                {
                    return *lhs.m_value.string == *rhs.m_value.string;
                }
                break;
            }
            case (value_t::boolean):
            {
                if (rhs.type() == value_t::boolean)
                {
                    return lhs.m_value.boolean == rhs.m_value.boolean;
                }
                break;
            }
            case (value_t::number_integer):
            {
                if (rhs.type() == value_t::number_integer)
                {
                    return lhs.m_value.number_integer == rhs.m_value.number_integer;
                }
                if (rhs.type() == value_t::number_float)
                {
                    return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_float);
                }
                break;
            }
            case (value_t::number_float):
            {
                if (rhs.type() == value_t::number_integer)
                {
1590
                    return approx(lhs.m_value.number_float, static_cast<number_float_t>(rhs.m_value.number_integer));
N
Niels 已提交
1591 1592 1593
                }
                if (rhs.type() == value_t::number_float)
                {
1594
                    return approx(lhs.m_value.number_float, rhs.m_value.number_float);
N
Niels 已提交
1595 1596 1597 1598 1599 1600 1601 1602
                }
                break;
            }
        }

        return false;
    }

N
Niels 已提交
1603 1604 1605 1606
    /*!
    @brief comparison: not equal
    @ingroup container
    */
N
Niels 已提交
1607
    friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
N
Niels 已提交
1608 1609 1610 1611 1612
    {
        return not (lhs == rhs);
    }

    /// comparison: less than
N
Niels 已提交
1613
    friend bool operator<(const_reference lhs, const_reference rhs) noexcept
N
Niels 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
    {
        switch (lhs.type())
        {
            case (value_t::array):
            {
                if (rhs.type() == value_t::array)
                {
                    return *lhs.m_value.array < *rhs.m_value.array;
                }
                break;
            }
            case (value_t::object):
            {
                if (rhs.type() == value_t::object)
                {
                    return *lhs.m_value.object < *rhs.m_value.object;
                }
                break;
            }
            case (value_t::null):
            {
                if (rhs.type() == value_t::null)
                {
                    return false;
                }
                break;
            }
            case (value_t::string):
            {
                if (rhs.type() == value_t::string)
                {
                    return *lhs.m_value.string < *rhs.m_value.string;
                }
                break;
            }
            case (value_t::boolean):
            {
                if (rhs.type() == value_t::boolean)
                {
                    return lhs.m_value.boolean < rhs.m_value.boolean;
                }
                break;
            }
            case (value_t::number_integer):
            {
                if (rhs.type() == value_t::number_integer)
                {
                    return lhs.m_value.number_integer < rhs.m_value.number_integer;
                }
                if (rhs.type() == value_t::number_float)
                {
                    return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_float);
                }
                break;
            }
            case (value_t::number_float):
            {
                if (rhs.type() == value_t::number_integer)
                {
                    return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
                }
                if (rhs.type() == value_t::number_float)
                {
                    return lhs.m_value.number_float < rhs.m_value.number_float;
                }
                break;
            }
        }

N
Niels 已提交
1683 1684 1685
        // We only reach this line if we cannot compare values. In that case,
        // we compare types.
        return lhs.type() < rhs.type();
N
Niels 已提交
1686 1687 1688
    }

    /// comparison: less than or equal
N
Niels 已提交
1689
    friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
N
Niels 已提交
1690 1691 1692 1693 1694
    {
        return not (rhs < lhs);
    }

    /// comparison: greater than
N
Niels 已提交
1695
    friend bool operator>(const_reference lhs, const_reference rhs) noexcept
N
Niels 已提交
1696 1697 1698 1699 1700
    {
        return not (lhs <= rhs);
    }

    /// comparison: greater than or equal
N
Niels 已提交
1701
    friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
N
Niels 已提交
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
    {
        return not (lhs < rhs);
    }


    ///////////////////
    // serialization //
    ///////////////////

    /// serialize to stream
    friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
    {
N
Niels 已提交
1714 1715 1716 1717
        // read width member and use it as indentation parameter if nonzero
        const int indentation = (o.width() == 0) ? -1 : o.width();

        o << j.dump(indentation);
N
Niels 已提交
1718 1719 1720 1721 1722 1723
        return o;
    }

    /// serialize to stream
    friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
    {
N
Niels 已提交
1724 1725 1726 1727
        // read width member and use it as indentation parameter if nonzero
        const int indentation = (o.width() == 0) ? -1 : o.width();

        o << j.dump(indentation);
N
Niels 已提交
1728 1729 1730 1731 1732 1733 1734 1735
        return o;
    }


    /////////////////////
    // deserialization //
    /////////////////////

N
Niels 已提交
1736
    /// deserialize from string
N
Niels 已提交
1737
    static basic_json parse(const string_t& s)
N
Niels 已提交
1738 1739 1740 1741
    {
        return parser(s).parse();
    }

N
Niels 已提交
1742 1743 1744 1745 1746 1747
    /// deserialize from stream
    static basic_json parse(std::istream& i)
    {
        return parser(i).parse();
    }

N
Niels 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
    /// deserialize from stream
    friend std::istream& operator>>(std::istream& i, basic_json& j)
    {
        j = parser(i).parse();
        return i;
    }

    /// deserialize from stream
    friend std::istream& operator<<(basic_json& j, std::istream& i)
    {
        j = parser(i).parse();
        return i;
    }


  private:
    ///////////////////////////
    // convenience functions //
    ///////////////////////////

    /// return the type as string
    inline string_t type_name() const noexcept
    {
        switch (m_type)
        {
            case (value_t::null):
            {
                return "null";
            }
N
Niels 已提交
1777

N
Niels 已提交
1778 1779 1780 1781
            case (value_t::object):
            {
                return "object";
            }
N
Niels 已提交
1782

N
Niels 已提交
1783 1784 1785 1786
            case (value_t::array):
            {
                return "array";
            }
N
Niels 已提交
1787

N
Niels 已提交
1788 1789 1790 1791
            case (value_t::string):
            {
                return "string";
            }
N
Niels 已提交
1792

N
Niels 已提交
1793 1794 1795 1796
            case (value_t::boolean):
            {
                return "boolean";
            }
N
Niels 已提交
1797 1798

            default:
N
Niels 已提交
1799 1800 1801 1802 1803 1804 1805 1806
            {
                return "number";
            }
        }
    }

    /*!
    @brief escape a string
N
Niels 已提交
1807

N
Niels 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
    Escape a string by replacing certain special characters by a sequence of an
    escape character (backslash) and another character and other control
    characters by a sequence of "\u" followed by a four-digit hex
    representation.

    @param s  the string to escape
    @return escaped string
    */
    static string_t escape_string(const string_t& s) noexcept
    {
        // create a result string of at least the size than s
        string_t result;
        result.reserve(s.size());

        for (const auto c : s)
        {
            switch (c)
            {
                // quotation mark (0x22)
                case '"':
                {
                    result += "\\\"";
                    break;
                }
N
Niels 已提交
1832

N
Niels 已提交
1833 1834 1835 1836 1837 1838
                // reverse solidus (0x5c)
                case '\\':
                {
                    result += "\\\\";
                    break;
                }
N
Niels 已提交
1839

N
Niels 已提交
1840 1841 1842 1843 1844 1845
                // backspace (0x08)
                case '\b':
                {
                    result += "\\b";
                    break;
                }
N
Niels 已提交
1846

N
Niels 已提交
1847 1848 1849 1850 1851 1852
                // formfeed (0x0c)
                case '\f':
                {
                    result += "\\f";
                    break;
                }
N
Niels 已提交
1853

N
Niels 已提交
1854 1855 1856 1857 1858 1859
                // newline (0x0a)
                case '\n':
                {
                    result += "\\n";
                    break;
                }
N
Niels 已提交
1860

N
Niels 已提交
1861 1862 1863 1864 1865 1866
                // carriage return (0x0d)
                case '\r':
                {
                    result += "\\r";
                    break;
                }
N
Niels 已提交
1867

N
Niels 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876
                // horizontal tab (0x09)
                case '\t':
                {
                    result += "\\t";
                    break;
                }

                default:
                {
N
Niels 已提交
1877
                    if (c >= 0 and c <= 0x1f)
N
Niels 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
                    {
                        // control characters (everything between 0x00 and 0x1f)
                        // -> create four-digit hex representation
                        std::stringstream ss;
                        ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c);
                        result += ss.str();
                    }
                    else
                    {
                        // all other characters are added as-is
                        result.append(1, c);
                    }
                    break;
                }
            }
        }

        return result;
    }


    /*!
    @brief internal implementation of the serialization function
N
Niels 已提交
1901

N
Niels 已提交
1902 1903 1904 1905
    This function is called by the public member function dump and organizes
    the serializaion internally. The indentation level is propagated as
    additional parameter. In case of arrays and objects, the function is called
    recursively. Note that
N
Niels 已提交
1906

N
Niels 已提交
1907
    - strings and object keys are escaped using escape_string()
N
Niels 已提交
1908 1909
    - integer numbers are converted to a string before output using
      std::to_string()
1910
    - floating-point numbers are converted to a string using "%g" format
N
Niels 已提交
1911 1912 1913 1914 1915 1916

    @param prettyPrint    whether the output shall be pretty-printed
    @param indentStep     the indent level
    @param currentIndent  the current indent level (only used internally)
    */
    inline string_t dump(const bool prettyPrint, const unsigned int indentStep,
N
Niels 已提交
1917
                         const unsigned int currentIndent = 0) const noexcept
N
Niels 已提交
1918
    {
N
Niels 已提交
1919 1920 1921
        // variable to hold indentation for recursive calls
        auto new_indent = currentIndent;

N
Niels 已提交
1922
        // helper function to return whitespace as indentation
N
Niels 已提交
1923
        const auto indent = [prettyPrint, &new_indent]()
N
Niels 已提交
1924
        {
N
Niels 已提交
1925
            return prettyPrint ? string_t(new_indent, ' ') : string_t();
N
Niels 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
        };

        switch (m_type)
        {
            case (value_t::object):
            {
                if (m_value.object->empty())
                {
                    return "{}";
                }

                string_t result = "{";

                // increase indentation
                if (prettyPrint)
                {
N
Niels 已提交
1942
                    new_indent += indentStep;
N
Niels 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
                    result += "\n";
                }

                for (auto i = m_value.object->cbegin(); i != m_value.object->cend(); ++i)
                {
                    if (i != m_value.object->cbegin())
                    {
                        result += prettyPrint ? ",\n" : ",";
                    }
                    result += indent() + "\"" + escape_string(i->first) + "\":" + (prettyPrint ? " " : "")
N
Niels 已提交
1953
                              + i->second.dump(prettyPrint, indentStep, new_indent);
N
Niels 已提交
1954 1955 1956 1957 1958
                }

                // decrease indentation
                if (prettyPrint)
                {
N
Niels 已提交
1959
                    new_indent -= indentStep;
N
Niels 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
                    result += "\n";
                }

                return result + indent() + "}";
            }

            case (value_t::array):
            {
                if (m_value.array->empty())
                {
                    return "[]";
                }

                string_t result = "[";

                // increase indentation
                if (prettyPrint)
                {
N
Niels 已提交
1978
                    new_indent += indentStep;
N
Niels 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987
                    result += "\n";
                }

                for (auto i = m_value.array->cbegin(); i != m_value.array->cend(); ++i)
                {
                    if (i != m_value.array->cbegin())
                    {
                        result += prettyPrint ? ",\n" : ",";
                    }
N
Niels 已提交
1988
                    result += indent() + i->dump(prettyPrint, indentStep, new_indent);
N
Niels 已提交
1989 1990 1991 1992 1993
                }

                // decrease indentation
                if (prettyPrint)
                {
N
Niels 已提交
1994
                    new_indent -= indentStep;
N
Niels 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
                    result += "\n";
                }

                return result + indent() + "]";
            }

            case (value_t::string):
            {
                return string_t("\"") + escape_string(*m_value.string) + "\"";
            }

            case (value_t::boolean):
            {
                return m_value.boolean ? "true" : "false";
            }

            case (value_t::number_integer):
            {
                return std::to_string(m_value.number_integer);
            }

            case (value_t::number_float):
            {
N
Niels 已提交
2018 2019 2020
                // 15 digits of precision allows round-trip IEEE 754
                // string->double->string
                const auto sz = static_cast<unsigned int>(std::snprintf(nullptr, 0, "%.15g", m_value.number_float));
2021 2022
                std::vector<char> buf(sz + 1);
                std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
N
Niels 已提交
2023
                return string_t(buf.data());
N
Niels 已提交
2024
            }
N
Niels 已提交
2025 2026 2027 2028 2029

            default:
            {
                return "null";
            }
N
Niels 已提交
2030 2031 2032
        }
    }

2033 2034 2035 2036 2037 2038 2039
    /// "equality" comparison for floating point numbers
    template<typename T>
    inline static bool approx(const T a, const T b)
    {
        return not (a > b or a < b);
    }

N
Niels 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054

  private:
    //////////////////////
    // member variables //
    //////////////////////

    /// the type of the current element
    value_t m_type = value_t::null;

    /// whether the type of JSON object may change later
    bool m_final = false;

    /// the value of the current element
    json_value m_value = {};

N
Niels 已提交
2055

N
Niels 已提交
2056
  private:
N
Niels 已提交
2057 2058 2059 2060
    ///////////////
    // iterators //
    ///////////////

N
Niels 已提交
2061
    /// an iterator value
N
Niels 已提交
2062
    template<typename array_iterator_t, typename object_iterator_t>
N
Niels 已提交
2063 2064 2065
    union internal_iterator
    {
        /// iterator for JSON objects
N
Niels 已提交
2066
        object_iterator_t object_iterator;
N
Niels 已提交
2067
        /// iterator for JSON arrays
N
Niels 已提交
2068
        array_iterator_t array_iterator;
N
Niels 已提交
2069
        /// generic iteraotr for all other value types
N
Niels 已提交
2070
        difference_type generic_iterator;
N
Niels 已提交
2071 2072

        /// default constructor
N
Niels 已提交
2073
        internal_iterator() : generic_iterator(-1) {}
N
Niels 已提交
2074 2075 2076
    };

  public:
N
Niels 已提交
2077 2078
    /// a random access iterator for the basic_json class
    class iterator : public std::iterator<std::random_access_iterator_tag, basic_json>
N
Niels 已提交
2079
    {
2080 2081 2082
        // allow basic_json class to access m_it
        friend class basic_json;

N
Niels 已提交
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
      public:
        /// the type of the values when the iterator is dereferenced
        using value_type = basic_json::value_type;
        /// a type to represent differences between iterators
        using difference_type = basic_json::difference_type;
        /// defines a pointer to the type iterated over (value_type)
        using pointer = basic_json::pointer;
        /// defines a reference to the type iterated over (value_type)
        using reference = basic_json::reference;
        /// the category of the iterator
        using iterator_category = std::bidirectional_iterator_tag;

2095 2096 2097
        /// default constructor
        inline iterator() = default;

N
Niels 已提交
2098
        /// constructor for a given JSON instance
N
Niels 已提交
2099
        inline iterator(pointer object) noexcept : m_object(object)
N
Niels 已提交
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = typename object_t::iterator();
                    break;
                }
                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = typename array_t::iterator();
                    break;
                }
                default:
                {
N
Niels 已提交
2115
                    m_it.generic_iterator = -1;
N
Niels 已提交
2116 2117 2118 2119 2120
                    break;
                }
            }
        }

N
Niels 已提交
2121 2122 2123 2124 2125
        /// copy constructor
        inline iterator(const iterator& other) noexcept
            : m_object(other.m_object), m_it(other.m_it)
        {}

N
Niels 已提交
2126
        /// copy assignment
N
Niels 已提交
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
        inline iterator& operator=(iterator other) noexcept (
            std::is_nothrow_move_constructible<pointer>::value and
            std::is_nothrow_move_assignable<pointer>::value and
            std::is_nothrow_move_constructible<internal_iterator<typename array_t::iterator, typename object_t::iterator>>::value
            and
            std::is_nothrow_move_assignable<internal_iterator<typename array_t::iterator, typename object_t::iterator>>::value
        )
        {
            std::swap(m_object, other.m_object);
            std::swap(m_it, other.m_it);
N
Niels 已提交
2137 2138 2139
            return *this;
        }

N
Niels 已提交
2140
      private:
N
Niels 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
        /// set the iterator to the first value
        inline void set_begin() noexcept
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = m_object->m_value.object->begin();
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = m_object->m_value.array->begin();
                    break;
                }

                case (basic_json::value_t::null):
                {
N
Niels 已提交
2160
                    // set to end so begin()==end() is true: null is empty
N
Niels 已提交
2161
                    m_it.generic_iterator = 1;
N
Niels 已提交
2162 2163 2164 2165 2166
                    break;
                }

                default:
                {
N
Niels 已提交
2167
                    m_it.generic_iterator = 0;
N
Niels 已提交
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
                    break;
                }
            }
        }

        /// set the iterator past the last value
        inline void set_end() noexcept
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = m_object->m_value.object->end();
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = m_object->m_value.array->end();
                    break;
                }

                default:
                {
N
Niels 已提交
2192
                    m_it.generic_iterator = 1;
N
Niels 已提交
2193 2194 2195 2196 2197
                    break;
                }
            }
        }

N
Niels 已提交
2198
      public:
N
Niels 已提交
2199
        /// return a reference to the value pointed to by the iterator
N
Niels 已提交
2200
        inline reference operator*()
N
Niels 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return m_it.object_iterator->second;
                }

                case (basic_json::value_t::array):
                {
                    return *m_it.array_iterator;
                }

                case (basic_json::value_t::null):
                {
                    throw std::out_of_range("cannot get value");
                }

                default:
                {
N
Niels 已提交
2221
                    if (m_it.generic_iterator == 0)
N
Niels 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
                    {
                        return *m_object;
                    }
                    else
                    {
                        throw std::out_of_range("cannot get value");
                    }
                }
            }
        }

        /// dereference the iterator
N
Niels 已提交
2234
        inline pointer operator->()
N
Niels 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return &(m_it.object_iterator->second);
                }

                case (basic_json::value_t::array):
                {
                    return &*m_it.array_iterator;
                }

N
Niels 已提交
2248 2249 2250 2251 2252
                case (basic_json::value_t::null):
                {
                    throw std::out_of_range("cannot get value");
                }

N
Niels 已提交
2253 2254
                default:
                {
N
Niels 已提交
2255
                    if (m_it.generic_iterator == 0)
N
Niels 已提交
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
                    {
                        return m_object;
                    }
                    else
                    {
                        throw std::out_of_range("cannot get value");
                    }
                }
            }
        }

        /// post-increment (it++)
        inline iterator operator++(int)
        {
N
Niels 已提交
2270
            auto result = *this;
N
Niels 已提交
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator++;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator++;
                    break;
                }

                default:
                {
N
Niels 已提交
2288
                    m_it.generic_iterator++;
N
Niels 已提交
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
                    break;
                }
            }

            return result;
        }

        /// pre-increment (++it)
        inline iterator& operator++()
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    ++m_it.object_iterator;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    ++m_it.array_iterator;
                    break;
                }

                default:
                {
N
Niels 已提交
2315
                    ++m_it.generic_iterator;
N
Niels 已提交
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
                    break;
                }
            }

            return *this;
        }

        /// post-decrement (it--)
        inline iterator operator--(int)
        {
N
Niels 已提交
2326
            auto result = *this;
N
Niels 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator--;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator--;
                    break;
                }

                default:
                {
N
Niels 已提交
2344
                    m_it.generic_iterator--;
N
Niels 已提交
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
                    break;
                }
            }

            return result;
        }

        /// pre-decrement (--it)
        inline iterator& operator--()
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    --m_it.object_iterator;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    --m_it.array_iterator;
                    break;
                }

                default:
                {
N
Niels 已提交
2371
                    --m_it.generic_iterator;
N
Niels 已提交
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
                    break;
                }
            }

            return *this;
        }

        /// comparison: equal
        inline bool operator==(const iterator& other) const
        {
N
Niels 已提交
2382 2383
            // if objects are not the same, the comparison is undefined
            if (m_object != other.m_object)
N
Niels 已提交
2384
            {
N
Niels 已提交
2385
                throw std::domain_error("cannot compare iterators of different containers");
N
Niels 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
            }

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return (m_it.object_iterator == other.m_it.object_iterator);
                }

                case (basic_json::value_t::array):
                {
                    return (m_it.array_iterator == other.m_it.array_iterator);
                }

                default:
                {
                    return (m_it.generic_iterator == other.m_it.generic_iterator);
                }
            }
        }

        /// comparison: not equal
        inline bool operator!=(const iterator& other) const
        {
            return not operator==(other);
        }

N
Niels 已提交
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
        /// comparison: smaller
        inline bool operator<(const iterator& other) const
        {
            // if objects are not the same, the comparison is undefined
            if (m_object != other.m_object)
            {
                throw std::domain_error("cannot compare iterators of different containers");
            }

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator< for object iterators");
                }

                case (basic_json::value_t::array):
                {
                    return (m_it.array_iterator < other.m_it.array_iterator);
                }

                default:
                {
                    return (m_it.generic_iterator < other.m_it.generic_iterator);
                }
            }
        }

        /// comparison: less than or equal
        inline bool operator<=(const iterator& other) const
        {
            return not other.operator < (*this);
        }

        /// comparison: greater than
        inline bool operator>(const iterator& other) const
        {
            return not operator<=(other);
        }

        /// comparison: greater than or equal
        inline bool operator>=(const iterator& other) const
        {
            return not operator<(other);
        }

        /// add to iterator
        inline iterator& operator+=(difference_type i)
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator+= for object iterators");
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator += i;
                    break;
                }

                default:
                {
                    m_it.generic_iterator += i;
                    break;
                }
            }

            return *this;
        }

        /// subtract from iterator
        inline iterator& operator-=(difference_type i)
        {
            return operator+=(-i);
        }

        /// add to iterator
        inline iterator operator+(difference_type i)
        {
            auto result = *this;
            result += i;
            return result;
        }

        /// subtract from iterator
        inline iterator operator-(difference_type i)
        {
            auto result = *this;
            result -= i;
            return result;
        }

        /// return difference
        inline difference_type operator-(const iterator& other) const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator- for object iterators");
                    return 0;
                }

                case (basic_json::value_t::array):
                {
                    return m_it.array_iterator - other.m_it.array_iterator;
                }

                default:
                {
                    return m_it.generic_iterator - other.m_it.generic_iterator;
                }
            }
        }

        /// access to successor
        inline reference operator[](difference_type n)
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator[] for object iterators");
                }

                case (basic_json::value_t::array):
                {
                    return *(m_it.array_iterator + n);
                }

                case (basic_json::value_t::null):
                {
                    throw std::out_of_range("cannot get value");
                }

                default:
                {
                    if (m_it.generic_iterator == -n)
                    {
                        return *m_object;
                    }
                    else
                    {
                        throw std::out_of_range("cannot get value");
                    }
                }
            }
        }

N
Niels 已提交
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
        inline typename object_t::key_type key() const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return m_it.object_iterator->first;
                }

                default:
                {
                    throw std::domain_error("cannot use key() for non-object iterators");
                }
            }
        }

        inline reference value()
        {
            return operator*();
        }

N
Niels 已提交
2585 2586 2587 2588
      private:
        /// associated JSON instance
        pointer m_object = nullptr;
        /// the actual iterator of the associated instance
N
Niels 已提交
2589
        internal_iterator<typename array_t::iterator, typename object_t::iterator> m_it;
N
Niels 已提交
2590 2591
    };

N
Niels 已提交
2592 2593
    /// a const random access iterator for the basic_json class
    class const_iterator : public std::iterator<std::random_access_iterator_tag, const basic_json>
N
Niels 已提交
2594
    {
2595 2596 2597
        // allow basic_json class to access m_it
        friend class basic_json;

N
Niels 已提交
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
      public:
        /// the type of the values when the iterator is dereferenced
        using value_type = basic_json::value_type;
        /// a type to represent differences between iterators
        using difference_type = basic_json::difference_type;
        /// defines a pointer to the type iterated over (value_type)
        using pointer = basic_json::const_pointer;
        /// defines a reference to the type iterated over (value_type)
        using reference = basic_json::const_reference;
        /// the category of the iterator
        using iterator_category = std::bidirectional_iterator_tag;

2610 2611 2612
        /// default constructor
        inline const_iterator() = default;

N
Niels 已提交
2613
        /// constructor for a given JSON instance
N
Niels 已提交
2614
        inline const_iterator(pointer object) noexcept : m_object(object)
N
Niels 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = typename object_t::const_iterator();
                    break;
                }
                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = typename array_t::const_iterator();
                    break;
                }
                default:
                {
N
Niels 已提交
2630
                    m_it.generic_iterator = -1;
N
Niels 已提交
2631 2632 2633 2634 2635
                    break;
                }
            }
        }

N
Niels 已提交
2636
        /// copy constructor given a nonconst iterator
N
Niels 已提交
2637
        inline const_iterator(const iterator& other) noexcept : m_object(other.m_object)
N
Niels 已提交
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = other.m_it.object_iterator;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = other.m_it.array_iterator;
                    break;
                }

                default:
                {
                    m_it.generic_iterator = other.m_it.generic_iterator;
                    break;
                }
            }
        }
N
Niels 已提交
2660

N
Niels 已提交
2661 2662 2663 2664 2665
        /// copy constructor
        inline const_iterator(const const_iterator& other) noexcept
            : m_object(other.m_object), m_it(other.m_it)
        {}

N
Niels 已提交
2666
        /// copy assignment
N
Niels 已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
        inline const_iterator& operator=(const_iterator other) noexcept(
            std::is_nothrow_move_constructible<pointer>::value and
            std::is_nothrow_move_assignable<pointer>::value and
            std::is_nothrow_move_constructible<internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator>>::value
            and
            std::is_nothrow_move_assignable<internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator>>::value
        )
        {
            std::swap(m_object, other.m_object);
            std::swap(m_it, other.m_it);
N
Niels 已提交
2677 2678 2679
            return *this;
        }

N
Niels 已提交
2680
      private:
N
Niels 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
        /// set the iterator to the first value
        inline void set_begin() noexcept
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = m_object->m_value.object->cbegin();
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = m_object->m_value.array->cbegin();
                    break;
                }

                case (basic_json::value_t::null):
                {
N
Niels 已提交
2700
                    // set to end so begin()==end() is true: null is empty
N
Niels 已提交
2701
                    m_it.generic_iterator = 1;
N
Niels 已提交
2702 2703 2704 2705 2706
                    break;
                }

                default:
                {
N
Niels 已提交
2707
                    m_it.generic_iterator = 0;
N
Niels 已提交
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
                    break;
                }
            }
        }

        /// set the iterator past the last value
        inline void set_end() noexcept
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator = m_object->m_value.object->cend();
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator = m_object->m_value.array->cend();
                    break;
                }

                default:
                {
N
Niels 已提交
2732
                    m_it.generic_iterator = 1;
N
Niels 已提交
2733 2734 2735 2736 2737
                    break;
                }
            }
        }

N
Niels 已提交
2738
      public:
N
Niels 已提交
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
        /// return a reference to the value pointed to by the iterator
        inline reference operator*() const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return m_it.object_iterator->second;
                }

                case (basic_json::value_t::array):
                {
                    return *m_it.array_iterator;
                }

                case (basic_json::value_t::null):
                {
                    throw std::out_of_range("cannot get value");
                }

                default:
                {
N
Niels 已提交
2761
                    if (m_it.generic_iterator == 0)
N
Niels 已提交
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
                    {
                        return *m_object;
                    }
                    else
                    {
                        throw std::out_of_range("cannot get value");
                    }
                }
            }
        }

        /// dereference the iterator
        inline pointer operator->() const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return &(m_it.object_iterator->second);
                }

                case (basic_json::value_t::array):
                {
                    return &*m_it.array_iterator;
                }

                default:
                {
N
Niels 已提交
2790
                    if (m_it.generic_iterator == 0)
N
Niels 已提交
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
                    {
                        return m_object;
                    }
                    else
                    {
                        throw std::out_of_range("cannot get value");
                    }
                }
            }
        }

        /// post-increment (it++)
        inline const_iterator operator++(int)
        {
N
Niels 已提交
2805
            auto result = *this;
N
Niels 已提交
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator++;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator++;
                    break;
                }

                default:
                {
N
Niels 已提交
2823
                    m_it.generic_iterator++;
N
Niels 已提交
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
                    break;
                }
            }

            return result;
        }

        /// pre-increment (++it)
        inline const_iterator& operator++()
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    ++m_it.object_iterator;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    ++m_it.array_iterator;
                    break;
                }

                default:
                {
N
Niels 已提交
2850
                    ++m_it.generic_iterator;
N
Niels 已提交
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
                    break;
                }
            }

            return *this;
        }

        /// post-decrement (it--)
        inline const_iterator operator--(int)
        {
N
Niels 已提交
2861
            auto result = *this;
N
Niels 已提交
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    m_it.object_iterator--;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator--;
                    break;
                }

                default:
                {
N
Niels 已提交
2879
                    m_it.generic_iterator--;
N
Niels 已提交
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
                    break;
                }
            }

            return result;
        }

        /// pre-decrement (--it)
        inline const_iterator& operator--()
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    --m_it.object_iterator;
                    break;
                }

                case (basic_json::value_t::array):
                {
                    --m_it.array_iterator;
                    break;
                }

                default:
                {
N
Niels 已提交
2906
                    --m_it.generic_iterator;
N
Niels 已提交
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
                    break;
                }
            }

            return *this;
        }

        /// comparison: equal
        inline bool operator==(const const_iterator& other) const
        {
N
Niels 已提交
2917 2918
            // if objects are not the same, the comparison is undefined
            if (m_object != other.m_object)
N
Niels 已提交
2919
            {
N
Niels 已提交
2920
                throw std::domain_error("cannot compare iterators of different containers");
N
Niels 已提交
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
            }

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return (m_it.object_iterator == other.m_it.object_iterator);
                }

                case (basic_json::value_t::array):
                {
                    return (m_it.array_iterator == other.m_it.array_iterator);
                }

                default:
                {
                    return (m_it.generic_iterator == other.m_it.generic_iterator);
                }
            }
        }

        /// comparison: not equal
        inline bool operator!=(const const_iterator& other) const
        {
            return not operator==(other);
        }

N
Niels 已提交
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
        /// comparison: smaller
        inline bool operator<(const const_iterator& other) const
        {
            // if objects are not the same, the comparison is undefined
            if (m_object != other.m_object)
            {
                throw std::domain_error("cannot compare iterators of different containers");
            }

            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator< for object iterators");
                }

                case (basic_json::value_t::array):
                {
                    return (m_it.array_iterator < other.m_it.array_iterator);
                }

                default:
                {
                    return (m_it.generic_iterator < other.m_it.generic_iterator);
                }
            }
        }

        /// comparison: less than or equal
        inline bool operator<=(const const_iterator& other) const
        {
            return not other.operator < (*this);
        }

        /// comparison: greater than
        inline bool operator>(const const_iterator& other) const
        {
            return not operator<=(other);
        }

        /// comparison: greater than or equal
        inline bool operator>=(const const_iterator& other) const
        {
            return not operator<(other);
        }

        /// add to iterator
        inline const_iterator& operator+=(difference_type i)
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator+= for object iterators");
                    break;
                }

                case (basic_json::value_t::array):
                {
                    m_it.array_iterator += i;
                    break;
                }

                default:
                {
                    m_it.generic_iterator += i;
                    break;
                }
            }

            return *this;
        }

        /// subtract from iterator
        inline const_iterator& operator-=(difference_type i)
        {
            return operator+=(-i);
        }

        /// add to iterator
        inline const_iterator operator+(difference_type i)
        {
            auto result = *this;
            result += i;
            return result;
        }

        /// subtract from iterator
        inline const_iterator operator-(difference_type i)
        {
            auto result = *this;
            result -= i;
            return result;
        }

        /// return difference
        inline difference_type operator-(const const_iterator& other) const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator- for object iterators");
                }

                case (basic_json::value_t::array):
                {
                    return m_it.array_iterator - other.m_it.array_iterator;
                }

                default:
                {
                    return m_it.generic_iterator - other.m_it.generic_iterator;
                }
            }
        }

        /// access to successor
        inline reference operator[](difference_type n) const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    throw std::domain_error("cannot use operator[] for object iterators");
                }

                case (basic_json::value_t::array):
                {
                    return *(m_it.array_iterator + n);
                }

                case (basic_json::value_t::null):
                {
                    throw std::out_of_range("cannot get value");
                }

                default:
                {
                    if (m_it.generic_iterator == -n)
                    {
                        return *m_object;
                    }
                    else
                    {
                        throw std::out_of_range("cannot get value");
                    }
                }
            }
        }

N
Niels 已提交
3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
        inline typename object_t::key_type key() const
        {
            switch (m_object->m_type)
            {
                case (basic_json::value_t::object):
                {
                    return m_it.object_iterator->first;
                }

                default:
                {
                    throw std::domain_error("cannot use key() for non-object iterators");
                }
            }
        }

        inline reference value() const
        {
            return operator*();
        }

N
Niels 已提交
3120 3121 3122 3123
      private:
        /// associated JSON instance
        pointer m_object = nullptr;
        /// the actual iterator of the associated instance
N
Niels 已提交
3124
        internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator> m_it;
N
Niels 已提交
3125 3126
    };

N
Niels 已提交
3127

N
Niels 已提交
3128
  private:
N
Niels 已提交
3129 3130 3131
    //////////////////////
    // lexer and parser //
    //////////////////////
N
Niels 已提交
3132

N
Niels 已提交
3133 3134 3135 3136 3137 3138 3139
    /*!
    @brief lexical analysis

    This class organizes the lexical analysis during JSON deserialization. The
    core of it is a scanner generated by re2c <http://re2c.org> that processes
    a buffer and recognizes tokens according to RFC 7159 and ECMA-404.
    */
N
Niels 已提交
3140
    class lexer
N
Niels 已提交
3141
    {
N
Niels 已提交
3142
      public:
N
Niels 已提交
3143 3144 3145
        /// token types for the parser
        enum class token_type
        {
N
Niels 已提交
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
            uninitialized,    ///< indicating the scanner is uninitialized
            literal_true,     ///< the "true" literal
            literal_false,    ///< the "false" literal
            literal_null,     ///< the "null" literal
            value_string,     ///< a string - use get_string() for actual value
            value_number,     ///< a number - use get_number() for actual value
            begin_array,      ///< the character for array begin "["
            begin_object,     ///< the character for object begin "{"
            end_array,        ///< the character for array end "]"
            end_object,       ///< the character for object end "}"
            name_separator,   ///< the name separator ":"
            value_separator,  ///< the value separator ","
            parse_error,      ///< indicating a parse error
            end_of_input      ///< indicating the end of the input buffer
N
Niels 已提交
3160 3161
        };

N
Niels 已提交
3162
        /// the char type to use in the lexer
N
Niels 已提交
3163
        using lexer_char_t = unsigned char;
N
Niels 已提交
3164

N
Niels 已提交
3165 3166
        /// constructor with a given buffer
        inline lexer(const string_t& s) noexcept
N
Niels 已提交
3167
            : m_stream(nullptr), m_buffer(s)
N
Niels 已提交
3168
        {
N
Niels 已提交
3169
            m_content = reinterpret_cast<const lexer_char_t*>(s.c_str());
N
Niels 已提交
3170
            m_start = m_cursor = m_content;
N
Niels 已提交
3171
            m_limit = m_content + s.size();
N
Niels 已提交
3172
        }
N
Niels 已提交
3173 3174 3175 3176 3177 3178 3179 3180
        inline lexer(std::istream* s) noexcept
            : m_stream(s)
        {
            getline(*m_stream, m_buffer);
            m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
            m_start = m_cursor = m_content;
            m_limit = m_content + m_buffer.size();
        }
N
Niels 已提交
3181

N
Niels 已提交
3182
        /// default constructor
N
Niels 已提交
3183 3184
        inline lexer() = default;

N
Niels 已提交
3185 3186 3187
        /*!
        @brief create a string from a Unicode code point

N
Niels 已提交
3188 3189
        @param codepoint1  the code point (can be high surrogate)
        @param codepoint2  the code point (can be low surrogate or 0)
N
Niels 已提交
3190
        @return string representation of the code point
N
Niels 已提交
3191 3192
        @exception std::out_of_range if code point is >0x10ffff
        @exception std::invalid_argument if the low surrogate is invalid
N
Niels 已提交
3193 3194 3195

        @see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
        */
N
Niels 已提交
3196 3197
        inline static string_t to_unicode(const size_t codepoint1,
                                          const size_t codepoint2 = 0)
N
Niels 已提交
3198
        {
N
Niels 已提交
3199
            string_t result;
N
Niels 已提交
3200

N
Niels 已提交
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
            // calculate the codepoint from the given code points
            size_t codepoint = codepoint1;
            if (codepoint1 >= 0xD800 and codepoint1 <= 0xDBFF)
            {
                if (codepoint2 >= 0xDC00 and codepoint2 <= 0xDFFF)
                {
                    codepoint =
                        // high surrogate occupies the most significant 22 bits
                        (codepoint1 << 10)
                        // low surrogate occupies the least significant 15 bits
                        + codepoint2
                        // there is still the 0xD800, 0xDC00 and 0x10000 noise
                        // in the result so we have to substract with:
                        // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
                        - 0x35FDC00;
                }
                else
                {
                    throw std::invalid_argument("missing or wrong low surrogate");
                }
            }

N
Niels 已提交
3223 3224
            if (codepoint <= 0x7f)
            {
N
Niels 已提交
3225
                // 1-byte characters: 0xxxxxxx (ASCII)
N
Niels 已提交
3226
                result.append(1, static_cast<typename string_t::value_type>(codepoint));
N
Niels 已提交
3227 3228 3229 3230
            }
            else if (codepoint <= 0x7ff)
            {
                // 2-byte characters: 110xxxxx 10xxxxxx
N
Niels 已提交
3231 3232
                result.append(1, static_cast<typename string_t::value_type>(0xC0 | ((codepoint >> 6) & 0x1F)));
                result.append(1, static_cast<typename string_t::value_type>(0x80 | (codepoint & 0x3F)));
N
Niels 已提交
3233 3234 3235 3236
            }
            else if (codepoint <= 0xffff)
            {
                // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
N
Niels 已提交
3237 3238 3239
                result.append(1, static_cast<typename string_t::value_type>(0xE0 | ((codepoint >> 12) & 0x0F)));
                result.append(1, static_cast<typename string_t::value_type>(0x80 | ((codepoint >> 6) & 0x3F)));
                result.append(1, static_cast<typename string_t::value_type>(0x80 | (codepoint & 0x3F)));
N
Niels 已提交
3240 3241 3242 3243
            }
            else if (codepoint <= 0x10ffff)
            {
                // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
N
Niels 已提交
3244 3245 3246 3247
                result.append(1, static_cast<typename string_t::value_type>(0xF0 | ((codepoint >> 18) & 0x07)));
                result.append(1, static_cast<typename string_t::value_type>(0x80 | ((codepoint >> 12) & 0x3F)));
                result.append(1, static_cast<typename string_t::value_type>(0x80 | ((codepoint >> 6) & 0x3F)));
                result.append(1, static_cast<typename string_t::value_type>(0x80 | (codepoint & 0x3F)));
N
Niels 已提交
3248 3249 3250
            }
            else
            {
N
Niels 已提交
3251
                throw std::out_of_range("code points above 0x10FFFF are invalid");
N
Niels 已提交
3252 3253 3254 3255 3256
            }

            return result;
        }

N
Niels 已提交
3257 3258
        /// return name of values of type token_type
        inline static std::string token_type_name(token_type t) noexcept
N
cleanup  
Niels 已提交
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
        {
            switch (t)
            {
                case (token_type::uninitialized):
                    return "<uninitialized>";
                case (token_type::literal_true):
                    return "true literal";
                case (token_type::literal_false):
                    return "false literal";
                case (token_type::literal_null):
                    return "null literal";
                case (token_type::value_string):
                    return "string literal";
                case (token_type::value_number):
                    return "number literal";
                case (token_type::begin_array):
                    return "[";
                case (token_type::begin_object):
                    return "{";
                case (token_type::end_array):
                    return "]";
                case (token_type::end_object):
                    return "}";
                case (token_type::name_separator):
                    return ":";
                case (token_type::value_separator):
                    return ",";
                case (token_type::end_of_input):
                    return "<end of input>";
N
Niels 已提交
3288 3289
                default:
                    return "<parse error>";
N
cleanup  
Niels 已提交
3290 3291 3292
            }
        }

N
fixes  
Niels 已提交
3293 3294 3295 3296
        /*!
        This function implements a scanner for JSON. It is specified using
        regular expressions that try to follow RFC 7159 and ECMA-404 as close
        as possible. These regular expressions are then translated into a
N
Niels 已提交
3297 3298 3299
        deterministic finite automaton (DFA) by the tool re2c
        <http://re2c.org>. As a result, the translated code for this function
        consists of a large block of code with goto jumps.
N
fixes  
Niels 已提交
3300 3301 3302

        @return the class of the next token read from the buffer
        */
N
Niels 已提交
3303
        inline token_type scan() noexcept
N
Niels 已提交
3304
        {
N
cleanup  
Niels 已提交
3305
            // pointer for backtracking information
N
Niels 已提交
3306
            m_marker = nullptr;
N
Niels 已提交
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350

            // remember the begin of the token
            m_start = m_cursor;


            {
                lexer_char_t yych;
                unsigned int yyaccept = 0;
                static const unsigned char yybm[] =
                {
                    0,  64,  64,  64,  64,  64,  64,  64,
                    64,  96,  96,  64,  64,  96,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    96,  64,   0,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    192, 192, 192, 192, 192, 192, 192, 192,
                    192, 192,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,   0,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                    64,  64,  64,  64,  64,  64,  64,  64,
                };

N
Niels 已提交
3351 3352 3353 3354
                if ((m_limit - m_cursor) < 5)
                {
                    yyfill();
                };
N
Niels 已提交
3355 3356 3357 3358 3359 3360
                yych = *m_cursor;
                if (yych <= '9')
                {
                    if (yych <= ' ')
                    {
                        if (yych <= '\n')
N
Niels 已提交
3361
                        {
N
Niels 已提交
3362
                            if (yych <= 0x00)
N
Niels 已提交
3363
                            {
N
Niels 已提交
3364
                                goto basic_json_parser_27;
N
Niels 已提交
3365
                            }
N
Niels 已提交
3366
                            if (yych <= 0x08)
N
Niels 已提交
3367
                            {
N
Niels 已提交
3368 3369 3370 3371 3372
                                goto basic_json_parser_29;
                            }
                            if (yych >= '\n')
                            {
                                goto basic_json_parser_4;
N
Niels 已提交
3373 3374 3375 3376
                            }
                        }
                        else
                        {
N
Niels 已提交
3377
                            if (yych == '\r')
N
Niels 已提交
3378
                            {
N
Niels 已提交
3379
                                goto basic_json_parser_2;
N
Niels 已提交
3380
                            }
N
Niels 已提交
3381
                            if (yych <= 0x1F)
N
Niels 已提交
3382
                            {
N
Niels 已提交
3383
                                goto basic_json_parser_29;
N
Niels 已提交
3384 3385 3386 3387 3388
                            }
                        }
                    }
                    else
                    {
N
Niels 已提交
3389
                        if (yych <= ',')
N
Niels 已提交
3390
                        {
N
Niels 已提交
3391
                            if (yych == '"')
N
Niels 已提交
3392
                            {
N
Niels 已提交
3393
                                goto basic_json_parser_26;
N
Niels 已提交
3394
                            }
N
Niels 已提交
3395
                            if (yych <= '+')
N
Niels 已提交
3396
                            {
N
Niels 已提交
3397
                                goto basic_json_parser_29;
N
Niels 已提交
3398
                            }
N
Niels 已提交
3399
                            goto basic_json_parser_14;
N
Niels 已提交
3400 3401 3402
                        }
                        else
                        {
N
Niels 已提交
3403
                            if (yych <= '-')
N
Niels 已提交
3404
                            {
N
Niels 已提交
3405
                                goto basic_json_parser_22;
N
Niels 已提交
3406
                            }
N
Niels 已提交
3407
                            if (yych <= '/')
N
Niels 已提交
3408
                            {
N
Niels 已提交
3409
                                goto basic_json_parser_29;
N
Niels 已提交
3410
                            }
N
Niels 已提交
3411 3412 3413 3414 3415
                            if (yych <= '0')
                            {
                                goto basic_json_parser_23;
                            }
                            goto basic_json_parser_25;
N
Niels 已提交
3416 3417
                        }
                    }
N
Niels 已提交
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
                }
                else
                {
                    if (yych <= 'm')
                    {
                        if (yych <= '\\')
                        {
                            if (yych <= ':')
                            {
                                goto basic_json_parser_16;
                            }
                            if (yych == '[')
                            {
                                goto basic_json_parser_6;
                            }
                            goto basic_json_parser_29;
                        }
                        else
                        {
                            if (yych <= ']')
                            {
                                goto basic_json_parser_8;
                            }
                            if (yych == 'f')
                            {
                                goto basic_json_parser_21;
                            }
                            goto basic_json_parser_29;
                        }
                    }
                    else
N
Niels 已提交
3449
                    {
N
Niels 已提交
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
                        if (yych <= 'z')
                        {
                            if (yych <= 'n')
                            {
                                goto basic_json_parser_18;
                            }
                            if (yych == 't')
                            {
                                goto basic_json_parser_20;
                            }
                            goto basic_json_parser_29;
                        }
                        else
                        {
                            if (yych <= '{')
                            {
                                goto basic_json_parser_10;
                            }
                            if (yych == '}')
                            {
                                goto basic_json_parser_12;
                            }
                            goto basic_json_parser_29;
                        }
N
Niels 已提交
3474
                    }
N
Niels 已提交
3475 3476 3477 3478 3479 3480 3481 3482 3483
                }
basic_json_parser_2:
                ++m_cursor;
                yych = *m_cursor;
                goto basic_json_parser_5;
basic_json_parser_3:
                {
                    return scan();
                }
N
Niels 已提交
3484
basic_json_parser_4:
N
Niels 已提交
3485
                ++m_cursor;
N
Niels 已提交
3486 3487 3488 3489
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3490
                yych = *m_cursor;
N
Niels 已提交
3491
basic_json_parser_5:
N
Niels 已提交
3492 3493 3494 3495 3496
                if (yybm[0 + yych] & 32)
                {
                    goto basic_json_parser_4;
                }
                goto basic_json_parser_3;
N
Niels 已提交
3497
basic_json_parser_6:
N
Niels 已提交
3498 3499 3500 3501
                ++m_cursor;
                {
                    return token_type::begin_array;
                }
N
Niels 已提交
3502
basic_json_parser_8:
N
Niels 已提交
3503 3504 3505 3506
                ++m_cursor;
                {
                    return token_type::end_array;
                }
N
Niels 已提交
3507
basic_json_parser_10:
N
Niels 已提交
3508 3509 3510 3511
                ++m_cursor;
                {
                    return token_type::begin_object;
                }
N
Niels 已提交
3512
basic_json_parser_12:
N
Niels 已提交
3513 3514 3515 3516
                ++m_cursor;
                {
                    return token_type::end_object;
                }
N
Niels 已提交
3517
basic_json_parser_14:
N
Niels 已提交
3518 3519 3520 3521
                ++m_cursor;
                {
                    return token_type::value_separator;
                }
N
Niels 已提交
3522
basic_json_parser_16:
N
Niels 已提交
3523 3524 3525 3526
                ++m_cursor;
                {
                    return token_type::name_separator;
                }
N
Niels 已提交
3527
basic_json_parser_18:
N
Niels 已提交
3528 3529 3530 3531 3532 3533
                yyaccept = 0;
                yych = *(m_marker = ++m_cursor);
                if (yych == 'u')
                {
                    goto basic_json_parser_59;
                }
N
Niels 已提交
3534
basic_json_parser_19:
N
Niels 已提交
3535 3536 3537
                {
                    return token_type::parse_error;
                }
N
Niels 已提交
3538
basic_json_parser_20:
N
Niels 已提交
3539 3540 3541 3542 3543 3544 3545
                yyaccept = 0;
                yych = *(m_marker = ++m_cursor);
                if (yych == 'r')
                {
                    goto basic_json_parser_55;
                }
                goto basic_json_parser_19;
N
Niels 已提交
3546
basic_json_parser_21:
N
Niels 已提交
3547 3548 3549 3550 3551 3552 3553
                yyaccept = 0;
                yych = *(m_marker = ++m_cursor);
                if (yych == 'a')
                {
                    goto basic_json_parser_50;
                }
                goto basic_json_parser_19;
N
Niels 已提交
3554
basic_json_parser_22:
N
Niels 已提交
3555 3556 3557
                yych = *++m_cursor;
                if (yych <= '/')
                {
N
Niels 已提交
3558
                    goto basic_json_parser_19;
N
Niels 已提交
3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
                }
                if (yych <= '0')
                {
                    goto basic_json_parser_49;
                }
                if (yych <= '9')
                {
                    goto basic_json_parser_40;
                }
                goto basic_json_parser_19;
N
Niels 已提交
3569
basic_json_parser_23:
N
Niels 已提交
3570 3571 3572 3573 3574
                yyaccept = 1;
                yych = *(m_marker = ++m_cursor);
                if (yych <= 'D')
                {
                    if (yych == '.')
N
Niels 已提交
3575
                    {
N
Niels 已提交
3576
                        goto basic_json_parser_42;
N
Niels 已提交
3577
                    }
N
Niels 已提交
3578 3579 3580 3581
                }
                else
                {
                    if (yych <= 'E')
N
Niels 已提交
3582
                    {
N
Niels 已提交
3583
                        goto basic_json_parser_43;
N
Niels 已提交
3584
                    }
N
Niels 已提交
3585
                    if (yych == 'e')
N
Niels 已提交
3586
                    {
N
Niels 已提交
3587
                        goto basic_json_parser_43;
N
Niels 已提交
3588
                    }
N
Niels 已提交
3589 3590 3591 3592 3593
                }
basic_json_parser_24:
                {
                    return token_type::value_number;
                }
N
Niels 已提交
3594
basic_json_parser_25:
N
Niels 已提交
3595 3596 3597
                yyaccept = 1;
                yych = *(m_marker = ++m_cursor);
                goto basic_json_parser_41;
N
Niels 已提交
3598
basic_json_parser_26:
N
Niels 已提交
3599 3600 3601 3602 3603 3604 3605
                yyaccept = 0;
                yych = *(m_marker = ++m_cursor);
                if (yych <= 0x00)
                {
                    goto basic_json_parser_19;
                }
                goto basic_json_parser_31;
N
Niels 已提交
3606
basic_json_parser_27:
N
Niels 已提交
3607 3608 3609 3610
                ++m_cursor;
                {
                    return token_type::end_of_input;
                }
N
Niels 已提交
3611
basic_json_parser_29:
N
Niels 已提交
3612 3613
                yych = *++m_cursor;
                goto basic_json_parser_19;
N
Niels 已提交
3614
basic_json_parser_30:
N
Niels 已提交
3615
                ++m_cursor;
N
Niels 已提交
3616 3617 3618 3619
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3620
                yych = *m_cursor;
N
Niels 已提交
3621
basic_json_parser_31:
N
Niels 已提交
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
                if (yybm[0 + yych] & 64)
                {
                    goto basic_json_parser_30;
                }
                if (yych <= 0x00)
                {
                    goto basic_json_parser_32;
                }
                if (yych <= '"')
                {
                    goto basic_json_parser_34;
                }
                goto basic_json_parser_33;
N
Niels 已提交
3635
basic_json_parser_32:
N
Niels 已提交
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
                m_cursor = m_marker;
                if (yyaccept == 0)
                {
                    goto basic_json_parser_19;
                }
                else
                {
                    goto basic_json_parser_24;
                }
basic_json_parser_33:
                ++m_cursor;
N
Niels 已提交
3647 3648 3649 3650
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3651 3652 3653 3654
                yych = *m_cursor;
                if (yych <= 'e')
                {
                    if (yych <= '/')
N
Niels 已提交
3655
                    {
N
Niels 已提交
3656 3657 3658 3659 3660 3661 3662 3663 3664
                        if (yych == '"')
                        {
                            goto basic_json_parser_30;
                        }
                        if (yych <= '.')
                        {
                            goto basic_json_parser_32;
                        }
                        goto basic_json_parser_30;
N
Niels 已提交
3665 3666 3667
                    }
                    else
                    {
N
Niels 已提交
3668
                        if (yych <= '\\')
N
Niels 已提交
3669
                        {
N
Niels 已提交
3670
                            if (yych <= '[')
N
Niels 已提交
3671
                            {
N
Niels 已提交
3672
                                goto basic_json_parser_32;
N
Niels 已提交
3673
                            }
N
Niels 已提交
3674
                            goto basic_json_parser_30;
N
Niels 已提交
3675 3676 3677
                        }
                        else
                        {
N
Niels 已提交
3678
                            if (yych == 'b')
N
Niels 已提交
3679
                            {
N
Niels 已提交
3680
                                goto basic_json_parser_30;
N
Niels 已提交
3681
                            }
N
Niels 已提交
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
                            goto basic_json_parser_32;
                        }
                    }
                }
                else
                {
                    if (yych <= 'q')
                    {
                        if (yych <= 'f')
                        {
                            goto basic_json_parser_30;
                        }
                        if (yych == 'n')
                        {
                            goto basic_json_parser_30;
N
Niels 已提交
3697
                        }
N
Niels 已提交
3698
                        goto basic_json_parser_32;
N
Niels 已提交
3699 3700 3701
                    }
                    else
                    {
N
Niels 已提交
3702
                        if (yych <= 's')
N
Niels 已提交
3703
                        {
N
Niels 已提交
3704
                            if (yych <= 'r')
N
Niels 已提交
3705
                            {
N
Niels 已提交
3706
                                goto basic_json_parser_30;
N
Niels 已提交
3707
                            }
N
Niels 已提交
3708
                            goto basic_json_parser_32;
N
Niels 已提交
3709 3710 3711
                        }
                        else
                        {
N
Niels 已提交
3712
                            if (yych <= 't')
N
Niels 已提交
3713
                            {
N
Niels 已提交
3714
                                goto basic_json_parser_30;
N
Niels 已提交
3715
                            }
N
Niels 已提交
3716
                            if (yych <= 'u')
N
Niels 已提交
3717
                            {
N
Niels 已提交
3718
                                goto basic_json_parser_36;
N
Niels 已提交
3719
                            }
N
Niels 已提交
3720
                            goto basic_json_parser_32;
N
Niels 已提交
3721 3722
                        }
                    }
N
Niels 已提交
3723
                }
N
Niels 已提交
3724
basic_json_parser_34:
N
Niels 已提交
3725 3726 3727 3728
                ++m_cursor;
                {
                    return token_type::value_string;
                }
N
Niels 已提交
3729
basic_json_parser_36:
N
Niels 已提交
3730
                ++m_cursor;
N
Niels 已提交
3731 3732 3733 3734
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3735 3736 3737 3738
                yych = *m_cursor;
                if (yych <= '@')
                {
                    if (yych <= '/')
N
Niels 已提交
3739
                    {
N
Niels 已提交
3740
                        goto basic_json_parser_32;
N
Niels 已提交
3741
                    }
N
Niels 已提交
3742
                    if (yych >= ':')
N
Niels 已提交
3743
                    {
N
Niels 已提交
3744
                        goto basic_json_parser_32;
N
Niels 已提交
3745
                    }
N
Niels 已提交
3746 3747 3748 3749
                }
                else
                {
                    if (yych <= 'F')
N
Niels 已提交
3750
                    {
N
Niels 已提交
3751
                        goto basic_json_parser_37;
N
Niels 已提交
3752
                    }
N
Niels 已提交
3753
                    if (yych <= '`')
N
Niels 已提交
3754
                    {
N
Niels 已提交
3755
                        goto basic_json_parser_32;
N
Niels 已提交
3756
                    }
N
Niels 已提交
3757
                    if (yych >= 'g')
N
Niels 已提交
3758
                    {
N
Niels 已提交
3759
                        goto basic_json_parser_32;
N
Niels 已提交
3760
                    }
N
Niels 已提交
3761 3762 3763
                }
basic_json_parser_37:
                ++m_cursor;
N
Niels 已提交
3764 3765 3766 3767
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3768 3769 3770 3771
                yych = *m_cursor;
                if (yych <= '@')
                {
                    if (yych <= '/')
N
Niels 已提交
3772
                    {
N
Niels 已提交
3773
                        goto basic_json_parser_32;
N
Niels 已提交
3774
                    }
N
Niels 已提交
3775
                    if (yych >= ':')
N
Niels 已提交
3776
                    {
N
Niels 已提交
3777
                        goto basic_json_parser_32;
N
Niels 已提交
3778
                    }
N
Niels 已提交
3779 3780 3781 3782
                }
                else
                {
                    if (yych <= 'F')
N
Niels 已提交
3783
                    {
N
Niels 已提交
3784
                        goto basic_json_parser_38;
N
Niels 已提交
3785
                    }
N
Niels 已提交
3786
                    if (yych <= '`')
N
Niels 已提交
3787
                    {
N
Niels 已提交
3788
                        goto basic_json_parser_32;
N
Niels 已提交
3789
                    }
N
Niels 已提交
3790
                    if (yych >= 'g')
N
Niels 已提交
3791
                    {
N
Niels 已提交
3792
                        goto basic_json_parser_32;
N
Niels 已提交
3793
                    }
N
Niels 已提交
3794 3795 3796
                }
basic_json_parser_38:
                ++m_cursor;
N
Niels 已提交
3797 3798 3799 3800
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3801 3802 3803
                yych = *m_cursor;
                if (yych <= '@')
                {
N
Niels 已提交
3804 3805
                    if (yych <= '/')
                    {
N
Niels 已提交
3806
                        goto basic_json_parser_32;
N
Niels 已提交
3807
                    }
N
Niels 已提交
3808
                    if (yych >= ':')
N
Niels 已提交
3809
                    {
N
Niels 已提交
3810
                        goto basic_json_parser_32;
N
Niels 已提交
3811
                    }
N
Niels 已提交
3812 3813 3814 3815
                }
                else
                {
                    if (yych <= 'F')
N
Niels 已提交
3816
                    {
N
Niels 已提交
3817
                        goto basic_json_parser_39;
N
Niels 已提交
3818
                    }
N
Niels 已提交
3819
                    if (yych <= '`')
N
Niels 已提交
3820
                    {
N
Niels 已提交
3821
                        goto basic_json_parser_32;
N
Niels 已提交
3822
                    }
N
Niels 已提交
3823
                    if (yych >= 'g')
N
Niels 已提交
3824
                    {
N
Niels 已提交
3825
                        goto basic_json_parser_32;
N
Niels 已提交
3826
                    }
N
Niels 已提交
3827 3828 3829
                }
basic_json_parser_39:
                ++m_cursor;
N
Niels 已提交
3830 3831 3832 3833
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3834 3835 3836 3837
                yych = *m_cursor;
                if (yych <= '@')
                {
                    if (yych <= '/')
N
Niels 已提交
3838
                    {
N
Niels 已提交
3839
                        goto basic_json_parser_32;
N
Niels 已提交
3840
                    }
N
Niels 已提交
3841
                    if (yych <= '9')
N
Niels 已提交
3842
                    {
N
Niels 已提交
3843
                        goto basic_json_parser_30;
N
Niels 已提交
3844
                    }
N
Niels 已提交
3845 3846 3847 3848 3849
                    goto basic_json_parser_32;
                }
                else
                {
                    if (yych <= 'F')
N
Niels 已提交
3850
                    {
N
Niels 已提交
3851
                        goto basic_json_parser_30;
N
Niels 已提交
3852
                    }
N
Niels 已提交
3853
                    if (yych <= '`')
N
Niels 已提交
3854
                    {
N
Niels 已提交
3855
                        goto basic_json_parser_32;
N
Niels 已提交
3856
                    }
N
Niels 已提交
3857
                    if (yych <= 'f')
N
Niels 已提交
3858
                    {
N
Niels 已提交
3859
                        goto basic_json_parser_30;
N
Niels 已提交
3860
                    }
N
Niels 已提交
3861 3862 3863 3864 3865
                    goto basic_json_parser_32;
                }
basic_json_parser_40:
                yyaccept = 1;
                m_marker = ++m_cursor;
N
Niels 已提交
3866 3867 3868 3869
                if ((m_limit - m_cursor) < 3)
                {
                    yyfill();
                };
N
Niels 已提交
3870 3871 3872 3873 3874 3875 3876 3877 3878
                yych = *m_cursor;
basic_json_parser_41:
                if (yybm[0 + yych] & 128)
                {
                    goto basic_json_parser_40;
                }
                if (yych <= 'D')
                {
                    if (yych != '.')
N
Niels 已提交
3879
                    {
N
Niels 已提交
3880
                        goto basic_json_parser_24;
N
Niels 已提交
3881
                    }
N
Niels 已提交
3882 3883 3884 3885
                }
                else
                {
                    if (yych <= 'E')
N
Niels 已提交
3886
                    {
N
Niels 已提交
3887
                        goto basic_json_parser_43;
N
Niels 已提交
3888
                    }
N
Niels 已提交
3889
                    if (yych == 'e')
N
Niels 已提交
3890
                    {
N
Niels 已提交
3891
                        goto basic_json_parser_43;
N
Niels 已提交
3892
                    }
N
Niels 已提交
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910
                    goto basic_json_parser_24;
                }
basic_json_parser_42:
                yych = *++m_cursor;
                if (yych <= '/')
                {
                    goto basic_json_parser_32;
                }
                if (yych <= '9')
                {
                    goto basic_json_parser_47;
                }
                goto basic_json_parser_32;
basic_json_parser_43:
                yych = *++m_cursor;
                if (yych <= ',')
                {
                    if (yych != '+')
N
Niels 已提交
3911
                    {
N
Niels 已提交
3912
                        goto basic_json_parser_32;
N
Niels 已提交
3913
                    }
N
Niels 已提交
3914 3915 3916 3917 3918 3919 3920 3921
                }
                else
                {
                    if (yych <= '-')
                    {
                        goto basic_json_parser_44;
                    }
                    if (yych <= '/')
N
Niels 已提交
3922
                    {
N
Niels 已提交
3923
                        goto basic_json_parser_32;
N
Niels 已提交
3924
                    }
N
Niels 已提交
3925
                    if (yych <= '9')
N
Niels 已提交
3926
                    {
N
Niels 已提交
3927
                        goto basic_json_parser_45;
N
Niels 已提交
3928
                    }
N
Niels 已提交
3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942
                    goto basic_json_parser_32;
                }
basic_json_parser_44:
                yych = *++m_cursor;
                if (yych <= '/')
                {
                    goto basic_json_parser_32;
                }
                if (yych >= ':')
                {
                    goto basic_json_parser_32;
                }
basic_json_parser_45:
                ++m_cursor;
N
Niels 已提交
3943 3944 3945 3946
                if (m_limit <= m_cursor)
                {
                    yyfill();
                };
N
Niels 已提交
3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
                yych = *m_cursor;
                if (yych <= '/')
                {
                    goto basic_json_parser_24;
                }
                if (yych <= '9')
                {
                    goto basic_json_parser_45;
                }
                goto basic_json_parser_24;
basic_json_parser_47:
                yyaccept = 1;
                m_marker = ++m_cursor;
N
Niels 已提交
3960 3961 3962 3963
                if ((m_limit - m_cursor) < 3)
                {
                    yyfill();
                };
N
Niels 已提交
3964 3965 3966 3967
                yych = *m_cursor;
                if (yych <= 'D')
                {
                    if (yych <= '/')
N
Niels 已提交
3968
                    {
N
Niels 已提交
3969
                        goto basic_json_parser_24;
N
Niels 已提交
3970
                    }
N
Niels 已提交
3971
                    if (yych <= '9')
N
Niels 已提交
3972
                    {
N
Niels 已提交
3973
                        goto basic_json_parser_47;
N
Niels 已提交
3974
                    }
N
Niels 已提交
3975 3976 3977 3978 3979
                    goto basic_json_parser_24;
                }
                else
                {
                    if (yych <= 'E')
N
Niels 已提交
3980
                    {
N
Niels 已提交
3981
                        goto basic_json_parser_43;
N
Niels 已提交
3982
                    }
N
Niels 已提交
3983
                    if (yych == 'e')
N
Niels 已提交
3984
                    {
N
Niels 已提交
3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996
                        goto basic_json_parser_43;
                    }
                    goto basic_json_parser_24;
                }
basic_json_parser_49:
                yyaccept = 1;
                yych = *(m_marker = ++m_cursor);
                if (yych <= 'D')
                {
                    if (yych == '.')
                    {
                        goto basic_json_parser_42;
N
Niels 已提交
3997
                    }
N
Niels 已提交
3998 3999 4000 4001 4002
                    goto basic_json_parser_24;
                }
                else
                {
                    if (yych <= 'E')
N
Niels 已提交
4003
                    {
N
Niels 已提交
4004
                        goto basic_json_parser_43;
N
Niels 已提交
4005
                    }
N
Niels 已提交
4006
                    if (yych == 'e')
N
Niels 已提交
4007
                    {
N
Niels 已提交
4008
                        goto basic_json_parser_43;
N
Niels 已提交
4009
                    }
N
Niels 已提交
4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060
                    goto basic_json_parser_24;
                }
basic_json_parser_50:
                yych = *++m_cursor;
                if (yych != 'l')
                {
                    goto basic_json_parser_32;
                }
                yych = *++m_cursor;
                if (yych != 's')
                {
                    goto basic_json_parser_32;
                }
                yych = *++m_cursor;
                if (yych != 'e')
                {
                    goto basic_json_parser_32;
                }
                ++m_cursor;
                {
                    return token_type::literal_false;
                }
basic_json_parser_55:
                yych = *++m_cursor;
                if (yych != 'u')
                {
                    goto basic_json_parser_32;
                }
                yych = *++m_cursor;
                if (yych != 'e')
                {
                    goto basic_json_parser_32;
                }
                ++m_cursor;
                {
                    return token_type::literal_true;
                }
basic_json_parser_59:
                yych = *++m_cursor;
                if (yych != 'l')
                {
                    goto basic_json_parser_32;
                }
                yych = *++m_cursor;
                if (yych != 'l')
                {
                    goto basic_json_parser_32;
                }
                ++m_cursor;
                {
                    return token_type::literal_null;
N
Niels 已提交
4061
                }
N
Niels 已提交
4062
            }
N
Niels 已提交
4063

N
Niels 已提交
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088

        }

        /// append data from the stream to the internal buffer
        inline void yyfill() noexcept
        {
            if (not m_stream or not * m_stream)
            {
                return;
            }

            const ssize_t offset_start = m_start - m_content;
            const ssize_t offset_marker = m_marker - m_start;
            const ssize_t offset_cursor = m_cursor - m_start;

            m_buffer.erase(0, static_cast<size_t>(offset_start));
            std::string line;
            std::getline(*m_stream, line);
            m_buffer += line;

            m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
            m_start  = m_content;
            m_marker = m_start + offset_marker;
            m_cursor = m_start + offset_cursor;
            m_limit  = m_start + m_buffer.size() - 1;
N
Niels 已提交
4089 4090
        }

N
Niels 已提交
4091 4092
        /// return string representation of last read token
        inline string_t get_token() const noexcept
N
Niels 已提交
4093
        {
N
Niels 已提交
4094 4095
            return string_t(reinterpret_cast<typename string_t::const_pointer>(m_start),
                            static_cast<size_t>(m_cursor - m_start));
N
Niels 已提交
4096 4097 4098
        }

        /*!
N
Niels 已提交
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
        @brief return string value for string tokens

        The function iterates the characters between the opening and closing
        quotes of the string value. The complete string is the range
        [m_start,m_cursor). Consequently, we iterate from m_start+1 to
        m_cursor-1.

        We differentiate two cases:

        1. Escaped characters. In this case, a new character is constructed
           according to the nature of the escape. Some escapes create new
           characters (e.g., @c "\\n" is replaced by @c "\n"), some are copied
           as is (e.g., @c "\\\\"). Furthermore, Unicode escapes of the shape
           @c "\\uxxxx" need special care. In this case, to_unicode takes care
           of the construction of the values.
        2. Unescaped characters are copied as is.
N
Niels 已提交
4115 4116

        @return string value of current token without opening and closing quotes
N
Niels 已提交
4117
        @exception std::out_of_range if to_unicode fails
N
Niels 已提交
4118
        */
N
Niels 已提交
4119
        inline string_t get_string() const
N
Niels 已提交
4120
        {
N
Niels 已提交
4121
            string_t result;
N
Niels 已提交
4122 4123 4124
            result.reserve(static_cast<size_t>(m_cursor - m_start - 2));

            // iterate the result between the quotes
N
Niels 已提交
4125
            for (const lexer_char_t* i = m_start + 1; i < m_cursor - 1; ++i)
N
Niels 已提交
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181
            {
                // process escaped characters
                if (*i == '\\')
                {
                    // read next character
                    ++i;

                    switch (*i)
                    {
                        // the default escapes
                        case 't':
                        {
                            result += "\t";
                            break;
                        }
                        case 'b':
                        {
                            result += "\b";
                            break;
                        }
                        case 'f':
                        {
                            result += "\f";
                            break;
                        }
                        case 'n':
                        {
                            result += "\n";
                            break;
                        }
                        case 'r':
                        {
                            result += "\r";
                            break;
                        }

                        // characters that are not "un"escsaped
                        case '\\':
                        {
                            result += "\\\\";
                            break;
                        }
                        case '/':
                        {
                            result += "\\/";
                            break;
                        }
                        case '"':
                        {
                            result += "\\\"";
                            break;
                        }

                        // unicode
                        case 'u':
                        {
N
Niels 已提交
4182
                            // get code xxxx from uxxxx
N
Niels 已提交
4183 4184
                            auto codepoint = std::strtoul(std::string(reinterpret_cast<typename string_t::const_pointer>(i + 1),
                                                          4).c_str(), nullptr, 16);
N
Niels 已提交
4185 4186 4187

                            if (codepoint >= 0xD800 and codepoint <= 0xDBFF)
                            {
N
Niels 已提交
4188
                                // make sure there is a subsequent unicode
N
Niels 已提交
4189
                                if ((i + 6 >= m_limit) or * (i + 5) != '\\' or * (i + 6) != 'u')
N
Niels 已提交
4190 4191 4192 4193
                                {
                                    throw std::invalid_argument("missing low surrogate");
                                }

N
Niels 已提交
4194
                                // get code yyyy from uxxxx\uyyyy
N
Niels 已提交
4195 4196
                                auto codepoint2 = std::strtoul(std::string(reinterpret_cast<typename string_t::const_pointer>
                                                               (i + 7), 4).c_str(), nullptr, 16);
N
Niels 已提交
4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
                                result += to_unicode(codepoint, codepoint2);
                                // skip the next 11 characters (xxxx\uyyyy)
                                i += 11;
                            }
                            else
                            {
                                // add unicode character(s)
                                result += to_unicode(codepoint);
                                // skip the next four characters (xxxx)
                                i += 4;
                            }
N
Niels 已提交
4208 4209 4210 4211 4212 4213 4214 4215
                            break;
                        }
                    }
                }
                else
                {
                    // all other characters are just copied to the end of the
                    // string
N
Niels 已提交
4216
                    result.append(1, static_cast<typename string_t::value_type>(*i));
N
Niels 已提交
4217 4218 4219 4220
                }
            }

            return result;
N
Niels 已提交
4221 4222
        }

N
Niels 已提交
4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239
        /*!
        @brief return number value for number tokens

        This function translates the last token into a floating point number.
        The pointer m_begin points to the beginning of the parsed number. We
        pass this pointer to std::strtod which sets endptr to the first
        character past the converted number. If this pointer is not the same as
        m_cursor, then either more or less characters have been used during the
        comparison. This can happen for inputs like "01" which will be treated
        like number 0 followed by number 1.

        @return the result of the number conversion or NAN if the conversion
        read past the current token. The latter case needs to be treated by the
        caller function.

        @exception std::range_error if passed value is out of range
        */
N
Niels 已提交
4240 4241 4242
        inline number_float_t get_number() const
        {
            // conversion
N
Niels 已提交
4243 4244
            typename string_t::value_type* endptr;
            const auto float_val = std::strtod(reinterpret_cast<typename string_t::const_pointer>(m_start),
N
Niels 已提交
4245
                                               &endptr);
N
Niels 已提交
4246

N
Niels 已提交
4247 4248
            // return float_val if the whole number was translated and NAN
            // otherwise
N
Niels 已提交
4249
            return (reinterpret_cast<lexer_char_t*>(endptr) == m_cursor) ? float_val : NAN;
N
Niels 已提交
4250 4251 4252
        }

      private:
N
Niels 已提交
4253 4254
        /// optional input stream
        std::istream* m_stream;
N
fixes  
Niels 已提交
4255
        /// the buffer
N
Niels 已提交
4256 4257
        string_t m_buffer;
        /// the buffer pointer
N
Niels 已提交
4258
        const lexer_char_t* m_content = nullptr;
N
Niels 已提交
4259
        /// pointer to the beginning of the current symbol
N
Niels 已提交
4260
        const lexer_char_t* m_start = nullptr;
N
Niels 已提交
4261 4262
        /// pointer for backtracking information
        const lexer_char_t* m_marker = nullptr;
N
fixes  
Niels 已提交
4263
        /// pointer to the current symbol
N
Niels 已提交
4264
        const lexer_char_t* m_cursor = nullptr;
N
fixes  
Niels 已提交
4265
        /// pointer to the end of the buffer
N
Niels 已提交
4266
        const lexer_char_t* m_limit = nullptr;
N
Niels 已提交
4267 4268
    };

N
Niels 已提交
4269 4270 4271
    /*!
    @brief syntax analysis
    */
N
Niels 已提交
4272 4273 4274 4275
    class parser
    {
      public:
        /// constructor for strings
N
Niels 已提交
4276
        inline parser(const string_t& s) : m_lexer(s)
N
Niels 已提交
4277 4278 4279 4280 4281 4282
        {
            // read first token
            get_token();
        }

        /// a parser reading from an input stream
N
Niels 已提交
4283
        inline parser(std::istream& _is) : m_lexer(&_is)
N
Niels 已提交
4284 4285 4286 4287 4288
        {
            // read first token
            get_token();
        }

N
Niels 已提交
4289
        /// public parser interface
N
Niels 已提交
4290
        inline basic_json parse()
N
Niels 已提交
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301
        {
            basic_json result = parse_internal();

            expect(lexer::token_type::end_of_input);

            return result;
        }

      private:
        /// the actual parser
        inline basic_json parse_internal()
N
Niels 已提交
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315
        {
            switch (last_token)
            {
                case (lexer::token_type::begin_object):
                {
                    // explicitly set result to object to cope with {}
                    basic_json result(value_t::object);

                    // read next token
                    get_token();

                    // closing } -> we are done
                    if (last_token == lexer::token_type::end_object)
                    {
N
Niels 已提交
4316
                        get_token();
N
Niels 已提交
4317 4318 4319 4320 4321 4322
                        return result;
                    }

                    // otherwise: parse key-value pairs
                    do
                    {
N
Niels 已提交
4323 4324 4325 4326 4327 4328
                        // ugly, but could be fixed with loop reorganization
                        if (last_token == lexer::token_type::value_separator)
                        {
                            get_token();
                        }

N
Niels 已提交
4329 4330 4331 4332 4333 4334 4335 4336 4337 4338
                        // store key
                        expect(lexer::token_type::value_string);
                        const auto key = m_lexer.get_string();

                        // parse separator (:)
                        get_token();
                        expect(lexer::token_type::name_separator);

                        // parse value
                        get_token();
N
Niels 已提交
4339
                        result[key] = parse_internal();
N
Niels 已提交
4340
                    }
N
Niels 已提交
4341
                    while (last_token == lexer::token_type::value_separator);
N
Niels 已提交
4342 4343 4344

                    // closing }
                    expect(lexer::token_type::end_object);
N
Niels 已提交
4345
                    get_token();
N
Niels 已提交
4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360

                    return result;
                }

                case (lexer::token_type::begin_array):
                {
                    // explicitly set result to object to cope with []
                    basic_json result(value_t::array);

                    // read next token
                    get_token();

                    // closing ] -> we are done
                    if (last_token == lexer::token_type::end_array)
                    {
N
Niels 已提交
4361
                        get_token();
N
Niels 已提交
4362 4363 4364 4365 4366 4367
                        return result;
                    }

                    // otherwise: parse values
                    do
                    {
N
Niels 已提交
4368 4369 4370 4371 4372
                        // ugly, but could be fixed with loop reorganization
                        if (last_token == lexer::token_type::value_separator)
                        {
                            get_token();
                        }
N
Niels 已提交
4373

N
Niels 已提交
4374 4375
                        // parse value
                        result.push_back(parse_internal());
N
Niels 已提交
4376
                    }
N
Niels 已提交
4377
                    while (last_token == lexer::token_type::value_separator);
N
Niels 已提交
4378 4379 4380

                    // closing ]
                    expect(lexer::token_type::end_array);
N
Niels 已提交
4381
                    get_token();
N
Niels 已提交
4382 4383 4384 4385 4386 4387

                    return result;
                }

                case (lexer::token_type::literal_null):
                {
N
Niels 已提交
4388
                    get_token();
N
Niels 已提交
4389 4390 4391 4392 4393
                    return basic_json(nullptr);
                }

                case (lexer::token_type::value_string):
                {
N
Niels 已提交
4394 4395 4396
                    const auto s = m_lexer.get_string();
                    get_token();
                    return basic_json(s);
N
Niels 已提交
4397 4398 4399 4400
                }

                case (lexer::token_type::literal_true):
                {
N
Niels 已提交
4401
                    get_token();
N
Niels 已提交
4402 4403 4404 4405 4406
                    return basic_json(true);
                }

                case (lexer::token_type::literal_false):
                {
N
Niels 已提交
4407
                    get_token();
N
Niels 已提交
4408 4409 4410 4411 4412 4413 4414
                    return basic_json(false);
                }

                case (lexer::token_type::value_number):
                {
                    auto float_val = m_lexer.get_number();

N
Niels 已提交
4415 4416
                    // NAN is returned if token could not be translated
                    // completely
N
Niels 已提交
4417 4418 4419
                    if (std::isnan(float_val))
                    {
                        throw std::invalid_argument(std::string("parse error - ") +
N
Niels 已提交
4420
                                                    m_lexer.get_token() + " is not a number");
N
Niels 已提交
4421 4422
                    }

N
Niels 已提交
4423 4424
                    get_token();

N
Niels 已提交
4425 4426
                    // check if conversion loses precision
                    const auto int_val = static_cast<number_integer_t>(float_val);
4427
                    if (approx(float_val, static_cast<number_float_t>(int_val)))
N
Niels 已提交
4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441
                    {
                        // we basic_json not lose precision -> return int
                        return basic_json(int_val);
                    }
                    else
                    {
                        // we would lose precision -> returnfloat
                        return basic_json(float_val);
                    }
                }

                default:
                {
                    std::string error_msg = "parse error - unexpected \'";
N
Niels 已提交
4442
                    error_msg += m_lexer.get_token();
N
Niels 已提交
4443
                    error_msg += "\' (";
N
cleanup  
Niels 已提交
4444
                    error_msg += lexer::token_type_name(last_token) + ")";
N
Niels 已提交
4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457
                    throw std::invalid_argument(error_msg);
                }
            }
        }

        /// get next token from lexer
        inline typename lexer::token_type get_token()
        {
            last_token = m_lexer.scan();
            return last_token;
        }

        inline void expect(typename lexer::token_type t) const
N
Niels 已提交
4458 4459 4460 4461
        {
            if (t != last_token)
            {
                std::string error_msg = "parse error - unexpected \'";
N
Niels 已提交
4462
                error_msg += m_lexer.get_token();
N
cleanup  
Niels 已提交
4463 4464
                error_msg += "\' (" + lexer::token_type_name(last_token);
                error_msg += "); expected " + lexer::token_type_name(t);
N
Niels 已提交
4465 4466 4467 4468
                throw std::invalid_argument(error_msg);
            }
        }

N
Niels 已提交
4469
      private:
N
Niels 已提交
4470
        /// the type of the last read token
N
Niels 已提交
4471
        typename lexer::token_type last_token = lexer::token_type::uninitialized;
N
Niels 已提交
4472
        /// the lexer
N
Niels 已提交
4473
        lexer m_lexer;
N
Niels 已提交
4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493
    };
};


/////////////
// presets //
/////////////

/// default JSON class
using json = basic_json<>;
}


/////////////////////////
// nonmember functions //
/////////////////////////

// specialization of std::swap, and std::hash
namespace std
{
N
Niels 已提交
4494 4495 4496 4497
/*!
@brief exchanges the values of two JSON objects
@ingroup container
*/
N
Niels 已提交
4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511
template <>
inline void swap(nlohmann::json& j1,
                 nlohmann::json& j2) noexcept(
                     is_nothrow_move_constructible<nlohmann::json>::value and
                     is_nothrow_move_assignable<nlohmann::json>::value
                 )
{
    j1.swap(j2);
}

/// hash value for JSON objects
template <>
struct hash<nlohmann::json>
{
N
Niels 已提交
4512
    /// return a hash value for a JSON object
N
Niels 已提交
4513
    inline size_t operator()(const nlohmann::json& j) const
N
Niels 已提交
4514 4515
    {
        // a naive hashing via the string representation
N
Niels 已提交
4516 4517
        const auto& h = hash<nlohmann::json::string_t>();
        return h(j.dump());
N
Niels 已提交
4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
    }
};
}

/*!
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 已提交
4530
inline nlohmann::json operator "" _json(const char* s, std::size_t)
N
Niels 已提交
4531
{
N
Niels 已提交
4532 4533
    return nlohmann::json::parse(reinterpret_cast<nlohmann::json::string_t::value_type*>
                                 (const_cast<char*>(s)));
N
Niels 已提交
4534 4535 4536
}

#endif