unit-conversions.cpp 38.2 KB
Newer Older
1 2 3
/*
    __ _____ _____ _____
 __|  |   __|     |   | |  JSON for Modern C++ (test suite)
N
Niels Lohmann 已提交
4
|  |  |__   |  |  | | | |  version 2.1.1
5 6 7
|_____|_____|_____|_|___|  https://github.com/nlohmann/json

Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8
Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

Permission is hereby  granted, free of charge, to any  person obtaining a copy
of this software and associated  documentation files (the "Software"), to deal
in the Software  without restriction, including without  limitation the rights
to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"

#define private public
#include "json.hpp"
using nlohmann::json;

#include <deque>
#include <forward_list>
#include <list>
#include <unordered_map>
#include <unordered_set>
40
#include <valarray>
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

TEST_CASE("value conversion")
{
    SECTION("get an object (explicit)")
    {
        json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
        json j(o_reference);

        SECTION("json::object_t")
        {
            json::object_t o = j.get<json::object_t>();
            CHECK(json(o) == j);
        }

        SECTION("std::map<json::string_t, json>")
        {
            std::map<json::string_t, json> o = j.get<std::map<json::string_t, json>>();
            CHECK(json(o) == j);
        }

        SECTION("std::multimap<json::string_t, json>")
        {
            std::multimap<json::string_t, json> o = j.get<std::multimap<json::string_t, json>>();
            CHECK(json(o) == j);
        }

        SECTION("std::unordered_map<json::string_t, json>")
        {
            std::unordered_map<json::string_t, json> o = j.get<std::unordered_map<json::string_t, json>>();
            CHECK(json(o) == j);
        }

        SECTION("std::unordered_multimap<json::string_t, json>")
        {
            std::unordered_multimap<json::string_t, json> o =
                j.get<std::unordered_multimap<json::string_t, json>>();
            CHECK(json(o) == j);
        }

        SECTION("exception in case of a non-object type")
        {
82 83 84 85 86 87 88
            CHECK_THROWS_AS(json(json::value_t::null).get<json::object_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::array).get<json::object_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::string).get<json::object_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::boolean).get<json::object_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::object_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::object_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_float).get<json::object_t>(), json::type_error&);
89 90

            CHECK_THROWS_WITH(json(json::value_t::null).get<json::object_t>(),
91
                              "[json.exception.type_error.302] type must be object, but is null");
92
            CHECK_THROWS_WITH(json(json::value_t::array).get<json::object_t>(),
93
                              "[json.exception.type_error.302] type must be object, but is array");
94
            CHECK_THROWS_WITH(json(json::value_t::string).get<json::object_t>(),
95
                              "[json.exception.type_error.302] type must be object, but is string");
96
            CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::object_t>(),
97
                              "[json.exception.type_error.302] type must be object, but is boolean");
98
            CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::object_t>(),
99
                              "[json.exception.type_error.302] type must be object, but is number");
100
            CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::object_t>(),
101
                              "[json.exception.type_error.302] type must be object, but is number");
102
            CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::object_t>(),
103
                              "[json.exception.type_error.302] type must be object, but is number");
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        }
    }

    SECTION("get an object (implicit)")
    {
        json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
        json j(o_reference);

        SECTION("json::object_t")
        {
            json::object_t o = j;
            CHECK(json(o) == j);
        }

        SECTION("std::map<json::string_t, json>")
        {
            std::map<json::string_t, json> o = j;
            CHECK(json(o) == j);
        }

        SECTION("std::multimap<json::string_t, json>")
        {
            std::multimap<json::string_t, json> o = j;
            CHECK(json(o) == j);
        }

        SECTION("std::unordered_map<json::string_t, json>")
        {
            std::unordered_map<json::string_t, json> o = j;
            CHECK(json(o) == j);
        }

        SECTION("std::unordered_multimap<json::string_t, json>")
        {
            std::unordered_multimap<json::string_t, json> o = j;
            CHECK(json(o) == j);
        }
    }

    SECTION("get an array (explicit)")
    {
        json::array_t a_reference {json(1), json(1u), json(2.2), json(false), json("string"), json()};
        json j(a_reference);

        SECTION("json::array_t")
        {
            json::array_t a = j.get<json::array_t>();
            CHECK(json(a) == j);
        }

        SECTION("std::list<json>")
        {
            std::list<json> a = j.get<std::list<json>>();
            CHECK(json(a) == j);
        }

        SECTION("std::forward_list<json>")
        {
            std::forward_list<json> a = j.get<std::forward_list<json>>();
            CHECK(json(a) == j);
164

165
            CHECK_THROWS_AS(json(json::value_t::null).get<std::forward_list<json>>(), json::type_error&);
166
            CHECK_THROWS_WITH(json(json::value_t::null).get<std::forward_list<json>>(),
167
                              "[json.exception.type_error.302] type must be array, but is null");
168 169 170 171 172 173
        }

        SECTION("std::vector<json>")
        {
            std::vector<json> a = j.get<std::vector<json>>();
            CHECK(json(a) == j);
174

175
            CHECK_THROWS_AS(json(json::value_t::null).get<std::vector<json>>(), json::type_error&);
176
            CHECK_THROWS_WITH(json(json::value_t::null).get<std::vector<json>>(),
177
                              "[json.exception.type_error.302] type must be array, but is null");
178

179
#if not defined(JSON_NOEXCEPTION)
180 181
            SECTION("reserve is called on containers that supports it")
            {
T
Théo DELRIEU 已提交
182 183
                // making the call to from_json throw in order to check capacity
                std::vector<float> v;
184
                CHECK_THROWS_AS(nlohmann::from_json(j, v), json::type_error&);
T
Théo DELRIEU 已提交
185
                CHECK(v.capacity() == j.size());
186 187 188 189

                // make sure all values are properly copied
                std::vector<int> v2 = json({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
                CHECK(v2.size() == 10);
190
            }
191
#endif
192 193
        }

T
Théo DELRIEU 已提交
194 195
        SECTION("built-in arrays")
        {
N
Niels Lohmann 已提交
196 197
            const char str[] = "a string";
            const int nbs[] = {0, 1, 2};
T
Théo DELRIEU 已提交
198

N
Niels Lohmann 已提交
199 200
            json j2 = nbs;
            json j3 = str;
T
Théo DELRIEU 已提交
201

N
Niels Lohmann 已提交
202 203 204 205
            auto v = j2.get<std::vector<int>>();
            auto s = j3.get<std::string>();
            CHECK(std::equal(v.begin(), v.end(), std::begin(nbs)));
            CHECK(s == str);
T
Théo DELRIEU 已提交
206 207
        }

208 209 210 211 212 213 214 215
        SECTION("std::deque<json>")
        {
            std::deque<json> a = j.get<std::deque<json>>();
            CHECK(json(a) == j);
        }

        SECTION("exception in case of a non-array type")
        {
216 217 218 219 220 221 222
            CHECK_THROWS_AS(json(json::value_t::null).get<json::array_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::object).get<json::array_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::string).get<json::array_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::boolean).get<json::array_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::array_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::array_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_float).get<json::array_t>(), json::type_error&);
223

224
            CHECK_THROWS_WITH(json(json::value_t::object).get<std::vector<int>>(),
225
                              "[json.exception.type_error.302] type must be array, but is object");
226
            CHECK_THROWS_WITH(json(json::value_t::null).get<json::array_t>(),
227
                              "[json.exception.type_error.302] type must be array, but is null");
228
            CHECK_THROWS_WITH(json(json::value_t::object).get<json::array_t>(),
229
                              "[json.exception.type_error.302] type must be array, but is object");
230
            CHECK_THROWS_WITH(json(json::value_t::string).get<json::array_t>(),
231
                              "[json.exception.type_error.302] type must be array, but is string");
232
            CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::array_t>(),
233
                              "[json.exception.type_error.302] type must be array, but is boolean");
234
            CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::array_t>(),
235
                              "[json.exception.type_error.302] type must be array, but is number");
236
            CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::array_t>(),
237
                              "[json.exception.type_error.302] type must be array, but is number");
238
            CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::array_t>(),
239
                              "[json.exception.type_error.302] type must be array, but is number");
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        }
    }

    SECTION("get an array (implicit)")
    {
        json::array_t a_reference {json(1), json(1u), json(2.2), json(false), json("string"), json()};
        json j(a_reference);

        SECTION("json::array_t")
        {
            json::array_t a = j;
            CHECK(json(a) == j);
        }

        SECTION("std::list<json>")
        {
            std::list<json> a = j;
            CHECK(json(a) == j);
        }

        SECTION("std::forward_list<json>")
        {
            std::forward_list<json> a = j;
            CHECK(json(a) == j);
        }

        SECTION("std::vector<json>")
        {
            std::vector<json> a = j;
            CHECK(json(a) == j);
        }

        SECTION("std::deque<json>")
        {
            std::deque<json> a = j;
            CHECK(json(a) == j);
        }
    }

    SECTION("get a string (explicit)")
    {
        json::string_t s_reference {"Hello world"};
        json j(s_reference);

        SECTION("string_t")
        {
            json::string_t s = j.get<json::string_t>();
            CHECK(json(s) == j);
        }

        SECTION("std::string")
        {
            std::string s = j.get<std::string>();
            CHECK(json(s) == j);
        }

        SECTION("exception in case of a non-string type")
        {
298 299 300 301 302 303 304
            CHECK_THROWS_AS(json(json::value_t::null).get<json::string_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::object).get<json::string_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::array).get<json::string_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::boolean).get<json::string_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::string_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::string_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_float).get<json::string_t>(), json::type_error&);
305 306

            CHECK_THROWS_WITH(json(json::value_t::null).get<json::string_t>(),
307
                              "[json.exception.type_error.302] type must be string, but is null");
308
            CHECK_THROWS_WITH(json(json::value_t::object).get<json::string_t>(),
309
                              "[json.exception.type_error.302] type must be string, but is object");
310
            CHECK_THROWS_WITH(json(json::value_t::array).get<json::string_t>(),
311
                              "[json.exception.type_error.302] type must be string, but is array");
312
            CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::string_t>(),
313
                              "[json.exception.type_error.302] type must be string, but is boolean");
314
            CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::string_t>(),
315
                              "[json.exception.type_error.302] type must be string, but is number");
316
            CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::string_t>(),
317
                              "[json.exception.type_error.302] type must be string, but is number");
318
            CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::string_t>(),
319
                              "[json.exception.type_error.302] type must be string, but is number");
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        }
    }

    SECTION("get a string (implicit)")
    {
        json::string_t s_reference {"Hello world"};
        json j(s_reference);

        SECTION("string_t")
        {
            json::string_t s = j;
            CHECK(json(s) == j);
        }

        SECTION("std::string")
        {
            std::string s = j;
            CHECK(json(s) == j);
        }
    }

    SECTION("get a boolean (explicit)")
    {
        json::boolean_t b_reference {true};
        json j(b_reference);

        SECTION("boolean_t")
        {
            json::boolean_t b = j.get<json::boolean_t>();
            CHECK(json(b) == j);
        }

        SECTION("bool")
        {
            bool b = j.get<bool>();
            CHECK(json(b) == j);
        }

        SECTION("exception in case of a non-string type")
        {
360 361 362 363 364 365 366
            CHECK_THROWS_AS(json(json::value_t::null).get<json::boolean_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::object).get<json::boolean_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::array).get<json::boolean_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::string).get<json::boolean_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::boolean_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::boolean_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::number_float).get<json::boolean_t>(), json::type_error&);
367 368

            CHECK_THROWS_WITH(json(json::value_t::null).get<json::boolean_t>(),
369
                              "[json.exception.type_error.302] type must be boolean, but is null");
370
            CHECK_THROWS_WITH(json(json::value_t::object).get<json::boolean_t>(),
371
                              "[json.exception.type_error.302] type must be boolean, but is object");
372
            CHECK_THROWS_WITH(json(json::value_t::array).get<json::boolean_t>(),
373
                              "[json.exception.type_error.302] type must be boolean, but is array");
374
            CHECK_THROWS_WITH(json(json::value_t::string).get<json::boolean_t>(),
375
                              "[json.exception.type_error.302] type must be boolean, but is string");
376
            CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::boolean_t>(),
377
                              "[json.exception.type_error.302] type must be boolean, but is number");
378
            CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::boolean_t>(),
379
                              "[json.exception.type_error.302] type must be boolean, but is number");
380
            CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::boolean_t>(),
381
                              "[json.exception.type_error.302] type must be boolean, but is number");
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
        }
    }

    SECTION("get a boolean (implicit)")
    {
        json::boolean_t b_reference {true};
        json j(b_reference);

        SECTION("boolean_t")
        {
            json::boolean_t b = j;
            CHECK(json(b) == j);
        }

        SECTION("bool")
        {
            bool b = j;
            CHECK(json(b) == j);
        }
    }

    SECTION("get an integer number (explicit)")
    {
        json::number_integer_t n_reference {42};
        json j(n_reference);
        json::number_unsigned_t n_unsigned_reference {42u};
        json j_unsigned(n_unsigned_reference);

        SECTION("number_integer_t")
        {
            json::number_integer_t n = j.get<json::number_integer_t>();
            CHECK(json(n) == j);
        }

        SECTION("number_unsigned_t")
        {
            json::number_unsigned_t n = j_unsigned.get<json::number_unsigned_t>();
            CHECK(json(n) == j_unsigned);
        }

        SECTION("short")
        {
            short n = j.get<short>();
            CHECK(json(n) == j);
        }

        SECTION("unsigned short")
        {
            unsigned short n = j.get<unsigned short>();
            CHECK(json(n) == j);
        }

        SECTION("int")
        {
            int n = j.get<int>();
            CHECK(json(n) == j);
        }

        SECTION("unsigned int")
        {
            unsigned int n = j.get<unsigned int>();
            CHECK(json(n) == j);
        }

        SECTION("long")
        {
            long n = j.get<long>();
            CHECK(json(n) == j);
        }

        SECTION("unsigned long")
        {
            unsigned long n = j.get<unsigned long>();
            CHECK(json(n) == j);
        }

        SECTION("long long")
        {
            long long n = j.get<long long>();
            CHECK(json(n) == j);
        }

        SECTION("unsigned long long")
        {
            unsigned long long n = j.get<unsigned long long>();
            CHECK(json(n) == j);
        }

        SECTION("int8_t")
        {
            int8_t n = j.get<int8_t>();
            CHECK(json(n) == j);
        }

        SECTION("int16_t")
        {
            int16_t n = j.get<int16_t>();
            CHECK(json(n) == j);
        }

        SECTION("int32_t")
        {
            int32_t n = j.get<int32_t>();
            CHECK(json(n) == j);
        }

        SECTION("int64_t")
        {
            int64_t n = j.get<int64_t>();
            CHECK(json(n) == j);
        }

        SECTION("int8_fast_t")
        {
            int_fast8_t n = j.get<int_fast8_t>();
            CHECK(json(n) == j);
        }

        SECTION("int16_fast_t")
        {
            int_fast16_t n = j.get<int_fast16_t>();
            CHECK(json(n) == j);
        }

        SECTION("int32_fast_t")
        {
            int_fast32_t n = j.get<int_fast32_t>();
            CHECK(json(n) == j);
        }

        SECTION("int64_fast_t")
        {
            int_fast64_t n = j.get<int_fast64_t>();
            CHECK(json(n) == j);
        }

        SECTION("int8_least_t")
        {
            int_least8_t n = j.get<int_least8_t>();
            CHECK(json(n) == j);
        }

        SECTION("int16_least_t")
        {
            int_least16_t n = j.get<int_least16_t>();
            CHECK(json(n) == j);
        }

        SECTION("int32_least_t")
        {
            int_least32_t n = j.get<int_least32_t>();
            CHECK(json(n) == j);
        }

        SECTION("int64_least_t")
        {
            int_least64_t n = j.get<int_least64_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint8_t")
        {
            uint8_t n = j.get<uint8_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint16_t")
        {
            uint16_t n = j.get<uint16_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint32_t")
        {
            uint32_t n = j.get<uint32_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint64_t")
        {
            uint64_t n = j.get<uint64_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint8_fast_t")
        {
            uint_fast8_t n = j.get<uint_fast8_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint16_fast_t")
        {
            uint_fast16_t n = j.get<uint_fast16_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint32_fast_t")
        {
            uint_fast32_t n = j.get<uint_fast32_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint64_fast_t")
        {
            uint_fast64_t n = j.get<uint_fast64_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint8_least_t")
        {
            uint_least8_t n = j.get<uint_least8_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint16_least_t")
        {
            uint_least16_t n = j.get<uint_least16_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint32_least_t")
        {
            uint_least32_t n = j.get<uint_least32_t>();
            CHECK(json(n) == j);
        }

        SECTION("uint64_least_t")
        {
            uint_least64_t n = j.get<uint_least64_t>();
            CHECK(json(n) == j);
        }

        SECTION("exception in case of a non-number type")
        {
616 617 618 619 620
            CHECK_THROWS_AS(json(json::value_t::null).get<json::number_integer_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::object).get<json::number_integer_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::array).get<json::number_integer_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::string).get<json::number_integer_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_integer_t>(), json::type_error&);
621 622

            CHECK_THROWS_WITH(json(json::value_t::null).get<json::number_integer_t>(),
623
                              "[json.exception.type_error.302] type must be number, but is null");
624
            CHECK_THROWS_WITH(json(json::value_t::object).get<json::number_integer_t>(),
625
                              "[json.exception.type_error.302] type must be number, but is object");
626
            CHECK_THROWS_WITH(json(json::value_t::array).get<json::number_integer_t>(),
627
                              "[json.exception.type_error.302] type must be number, but is array");
628
            CHECK_THROWS_WITH(json(json::value_t::string).get<json::number_integer_t>(),
629
                              "[json.exception.type_error.302] type must be number, but is string");
630
            CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::number_integer_t>(),
631
                              "[json.exception.type_error.302] type must be number, but is boolean");
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 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 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

            CHECK_NOTHROW(json(json::value_t::number_float).get<json::number_integer_t>());
            CHECK_NOTHROW(json(json::value_t::number_float).get<json::number_unsigned_t>());
        }
    }

    SECTION("get an integer number (implicit)")
    {
        json::number_integer_t n_reference {42};
        json j(n_reference);
        json::number_unsigned_t n_unsigned_reference {42u};
        json j_unsigned(n_unsigned_reference);

        SECTION("number_integer_t")
        {
            json::number_integer_t n = j.get<json::number_integer_t>();
            CHECK(json(n) == j);
        }

        SECTION("number_unsigned_t")
        {
            json::number_unsigned_t n = j_unsigned.get<json::number_unsigned_t>();
            CHECK(json(n) == j_unsigned);
        }

        SECTION("short")
        {
            short n = j;
            CHECK(json(n) == j);
        }

        SECTION("unsigned short")
        {
            unsigned short n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("int")
        {
            int n = j;
            CHECK(json(n) == j);
        }

        SECTION("unsigned int")
        {
            unsigned int n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("long")
        {
            long n = j;
            CHECK(json(n) == j);
        }

        SECTION("unsigned long")
        {
            unsigned long n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("long long")
        {
            long long n = j;
            CHECK(json(n) == j);
        }

        SECTION("unsigned long long")
        {
            unsigned long long n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("int8_t")
        {
            int8_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int16_t")
        {
            int16_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int32_t")
        {
            int32_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int64_t")
        {
            int64_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int8_fast_t")
        {
            int_fast8_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int16_fast_t")
        {
            int_fast16_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int32_fast_t")
        {
            int_fast32_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int64_fast_t")
        {
            int_fast64_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int8_least_t")
        {
            int_least8_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int16_least_t")
        {
            int_least16_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int32_least_t")
        {
            int_least32_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("int64_least_t")
        {
            int_least64_t n = j;
            CHECK(json(n) == j);
        }

        SECTION("uint8_t")
        {
            uint8_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint16_t")
        {
            uint16_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint32_t")
        {
            uint32_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint64_t")
        {
            uint64_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint8_fast_t")
        {
            uint_fast8_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint16_fast_t")
        {
            uint_fast16_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint32_fast_t")
        {
            uint_fast32_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint64_fast_t")
        {
            uint_fast64_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint8_least_t")
        {
            uint_least8_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint16_least_t")
        {
            uint_least16_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint32_least_t")
        {
            uint_least32_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }

        SECTION("uint64_least_t")
        {
            uint_least64_t n = j_unsigned;
            CHECK(json(n) == j_unsigned);
        }
    }

    SECTION("get a floating-point number (explicit)")
    {
        json::number_float_t n_reference {42.23};
        json j(n_reference);

        SECTION("number_float_t")
        {
            json::number_float_t n = j.get<json::number_float_t>();
            CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
        }

        SECTION("float")
        {
            float n = j.get<float>();
            CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
        }

        SECTION("double")
        {
            double n = j.get<double>();
            CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
        }

        SECTION("exception in case of a non-string type")
        {
875 876 877 878 879
            CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::string).get<json::number_float_t>(), json::type_error&);
            CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(), json::type_error&);
880 881

            CHECK_THROWS_WITH(json(json::value_t::null).get<json::number_float_t>(),
882
                              "[json.exception.type_error.302] type must be number, but is null");
883
            CHECK_THROWS_WITH(json(json::value_t::object).get<json::number_float_t>(),
884
                              "[json.exception.type_error.302] type must be number, but is object");
885
            CHECK_THROWS_WITH(json(json::value_t::array).get<json::number_float_t>(),
886
                              "[json.exception.type_error.302] type must be number, but is array");
887
            CHECK_THROWS_WITH(json(json::value_t::string).get<json::number_float_t>(),
888
                              "[json.exception.type_error.302] type must be number, but is string");
889
            CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::number_float_t>(),
890
                              "[json.exception.type_error.302] type must be number, but is boolean");
891 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

            CHECK_NOTHROW(json(json::value_t::number_integer).get<json::number_float_t>());
            CHECK_NOTHROW(json(json::value_t::number_unsigned).get<json::number_float_t>());
        }
    }

    SECTION("get a floating-point number (implicit)")
    {
        json::number_float_t n_reference {42.23};
        json j(n_reference);

        SECTION("number_float_t")
        {
            json::number_float_t n = j;
            CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
        }

        SECTION("float")
        {
            float n = j;
            CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
        }

        SECTION("double")
        {
            double n = j;
            CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
        }
    }

921 922 923 924 925 926 927 928 929
    SECTION("get an enum")
    {
        enum c_enum { value_1, value_2 };
        enum class cpp_enum { value_1, value_2 };

        CHECK(json(value_1).get<c_enum>() == value_1);
        CHECK(json(cpp_enum::value_1).get<cpp_enum>() == cpp_enum::value_1);
    }

930 931 932 933 934 935 936 937 938 939 940 941
    SECTION("more involved conversions")
    {
        SECTION("object-like STL containers")
        {
            json j1 = {{"one", 1}, {"two", 2}, {"three", 3}};
            json j2 = {{"one", 1u}, {"two", 2u}, {"three", 3u}};
            json j3 = {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}};
            json j4 = {{"one", true}, {"two", false}, {"three", true}};
            json j5 = {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}};

            SECTION("std::map")
            {
942 943 944 945 946
                j1.get<std::map<std::string, int>>();
                j2.get<std::map<std::string, unsigned int>>();
                j3.get<std::map<std::string, double>>();
                j4.get<std::map<std::string, bool>>();
                j5.get<std::map<std::string, std::string>>();
947 948 949 950
            }

            SECTION("std::unordered_map")
            {
951 952 953 954 955
                j1.get<std::unordered_map<std::string, int>>();
                j2.get<std::unordered_map<std::string, unsigned int>>();
                j3.get<std::unordered_map<std::string, double>>();
                j4.get<std::unordered_map<std::string, bool>>();
                j5.get<std::unordered_map<std::string, std::string>>();
956 957 958 959 960
                //CHECK(m5["one"] == "eins");
            }

            SECTION("std::multimap")
            {
961 962 963 964 965
                j1.get<std::multimap<std::string, int>>();
                j2.get<std::multimap<std::string, unsigned int>>();
                j3.get<std::multimap<std::string, double>>();
                j4.get<std::multimap<std::string, bool>>();
                j5.get<std::multimap<std::string, std::string>>();
966 967 968 969 970
                //CHECK(m5["one"] == "eins");
            }

            SECTION("std::unordered_multimap")
            {
971 972 973 974 975
                j1.get<std::unordered_multimap<std::string, int>>();
                j2.get<std::unordered_multimap<std::string, unsigned int>>();
                j3.get<std::unordered_multimap<std::string, double>>();
                j4.get<std::unordered_multimap<std::string, bool>>();
                j5.get<std::unordered_multimap<std::string, std::string>>();
976 977 978 979 980
                //CHECK(m5["one"] == "eins");
            }

            SECTION("exception in case of a non-object type")
            {
981
                CHECK_THROWS_AS((json().get<std::map<std::string, int>>()), json::type_error&);
982
                CHECK_THROWS_WITH((json().get<std::map<std::string, int>>()), "[json.exception.type_error.302] type must be object, but is null");
983 984 985 986 987 988 989 990 991 992 993 994 995
            }
        }

        SECTION("array-like STL containers")
        {
            json j1 = {1, 2, 3, 4};
            json j2 = {1u, 2u, 3u, 4u};
            json j3 = {1.2, 2.3, 3.4, 4.5};
            json j4 = {true, false, true};
            json j5 = {"one", "two", "three"};

            SECTION("std::list")
            {
996 997 998 999 1000
                j1.get<std::list<int>>();
                j2.get<std::list<unsigned int>>();
                j3.get<std::list<double>>();
                j4.get<std::list<bool>>();
                j5.get<std::list<std::string>>();
1001 1002
            }

1003 1004
            SECTION("std::forward_list")
            {
1005 1006 1007 1008 1009
                j1.get<std::forward_list<int>>();
                j2.get<std::forward_list<unsigned int>>();
                j3.get<std::forward_list<double>>();
                j4.get<std::forward_list<bool>>();
                j5.get<std::forward_list<std::string>>();
1010
            }
1011

1012 1013
            SECTION("std::array")
            {
1014 1015 1016 1017 1018
                j1.get<std::array<int, 4>>();
                j2.get<std::array<unsigned int, 3>>();
                j3.get<std::array<double, 4>>();
                j4.get<std::array<bool, 3>>();
                j5.get<std::array<std::string, 3>>();
1019 1020 1021

                SECTION("std::array is larger than JSON")
                {
1022
                    std::array<int, 6> arr6 = {{1, 2, 3, 4, 5, 6}};
1023 1024 1025 1026 1027 1028
                    CHECK_THROWS_AS(arr6 = j1, json::out_of_range);
                    CHECK_THROWS_WITH(arr6 = j1, "[json.exception.out_of_range.401] array index 4 is out of range");
                }

                SECTION("std::array is smaller than JSON")
                {
1029
                    std::array<int, 2> arr2 = {{8, 9}};
1030 1031 1032 1033 1034 1035
                    arr2 = j1;
                    CHECK(arr2[0] == 1);
                    CHECK(arr2[1] == 2);
                }
            }

1036 1037
            SECTION("std::valarray")
            {
1038 1039 1040 1041 1042
                j1.get<std::valarray<int>>();
                j2.get<std::valarray<unsigned int>>();
                j3.get<std::valarray<double>>();
                j4.get<std::valarray<bool>>();
                j5.get<std::valarray<std::string>>();
1043 1044
            }

1045 1046
            SECTION("std::vector")
            {
1047 1048 1049 1050 1051
                j1.get<std::vector<int>>();
                j2.get<std::vector<unsigned int>>();
                j3.get<std::vector<double>>();
                j4.get<std::vector<bool>>();
                j5.get<std::vector<std::string>>();
1052 1053 1054 1055
            }

            SECTION("std::deque")
            {
1056 1057 1058 1059 1060
                j1.get<std::deque<int>>();
                j2.get<std::deque<unsigned int>>();
                j2.get<std::deque<double>>();
                j4.get<std::deque<bool>>();
                j5.get<std::deque<std::string>>();
1061 1062 1063 1064
            }

            SECTION("std::set")
            {
1065 1066 1067 1068 1069
                j1.get<std::set<int>>();
                j2.get<std::set<unsigned int>>();
                j3.get<std::set<double>>();
                j4.get<std::set<bool>>();
                j5.get<std::set<std::string>>();
1070 1071 1072 1073
            }

            SECTION("std::unordered_set")
            {
1074 1075 1076 1077 1078
                j1.get<std::unordered_set<int>>();
                j2.get<std::unordered_set<unsigned int>>();
                j3.get<std::unordered_set<double>>();
                j4.get<std::unordered_set<bool>>();
                j5.get<std::unordered_set<std::string>>();
1079 1080 1081 1082
            }

            SECTION("exception in case of a non-object type")
            {
1083 1084 1085 1086
                CHECK_THROWS_AS((json().get<std::list<int>>()), json::type_error&);
                CHECK_THROWS_AS((json().get<std::vector<int>>()), json::type_error&);
                CHECK_THROWS_AS((json().get<std::vector<json>>()), json::type_error&);
                CHECK_THROWS_AS((json().get<std::list<json>>()), json::type_error&);
1087
                CHECK_THROWS_AS((json().get<std::valarray<int>>()), json::type_error&);
1088

1089 1090
                // does type really must be an array? or it rather must not be null?
                // that's what I thought when other test like this one broke
1091 1092 1093 1094
                CHECK_THROWS_WITH((json().get<std::list<int>>()), "[json.exception.type_error.302] type must be array, but is null");
                CHECK_THROWS_WITH((json().get<std::vector<int>>()), "[json.exception.type_error.302] type must be array, but is null");
                CHECK_THROWS_WITH((json().get<std::vector<json>>()), "[json.exception.type_error.302] type must be array, but is null");
                CHECK_THROWS_WITH((json().get<std::list<json>>()), "[json.exception.type_error.302] type must be array, but is null");
1095
                CHECK_THROWS_WITH((json().get<std::valarray<int>>()), "[json.exception.type_error.302] type must be array, but is null");
1096 1097 1098 1099
            }
        }
    }
}