30 #ifndef INCLUDE_NLOHMANN_JSON_HPP_ 31 #define INCLUDE_NLOHMANN_JSON_HPP_ 33 #define NLOHMANN_JSON_VERSION_MAJOR 3
34 #define NLOHMANN_JSON_VERSION_MINOR 7
35 #define NLOHMANN_JSON_VERSION_PATCH 0
42 #include <initializer_list> 62 #include <forward_list> 67 #include <type_traits> 68 #include <unordered_map> 92 std::size_t chars_read_total = 0;
94 std::size_t chars_read_current_line = 0;
96 std::size_t lines_read = 0;
99 constexpr operator size_t()
const 101 return chars_read_total;
125 #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 9
) 126 #if defined(JSON_HEDLEY_VERSION) 127 #undef JSON_HEDLEY_VERSION 129 #define JSON_HEDLEY_VERSION 9
131 #if defined(JSON_HEDLEY_STRINGIFY_EX) 132 #undef JSON_HEDLEY_STRINGIFY_EX 134 #define JSON_HEDLEY_STRINGIFY_EX(x) #x 136 #if defined(JSON_HEDLEY_STRINGIFY) 137 #undef JSON_HEDLEY_STRINGIFY 139 #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x) 141 #if defined(JSON_HEDLEY_CONCAT_EX) 142 #undef JSON_HEDLEY_CONCAT_EX 144 #define JSON_HEDLEY_CONCAT_EX(a,b) a##b 146 #if defined(JSON_HEDLEY_CONCAT) 147 #undef JSON_HEDLEY_CONCAT 149 #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b) 151 #if defined(JSON_HEDLEY_VERSION_ENCODE) 152 #undef JSON_HEDLEY_VERSION_ENCODE 154 #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000
) + ((minor) * 1000
) + (revision)) 156 #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR) 157 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR 159 #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000
) 161 #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR) 162 #undef JSON_HEDLEY_VERSION_DECODE_MINOR 164 #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000
) / 1000
) 166 #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION) 167 #undef JSON_HEDLEY_VERSION_DECODE_REVISION 169 #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000
) 171 #if defined(JSON_HEDLEY_GNUC_VERSION) 172 #undef JSON_HEDLEY_GNUC_VERSION 174 #if defined(__GNUC__
) && defined(__GNUC_PATCHLEVEL__
) 175 #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
) 176 #elif defined(__GNUC__) 177 #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0
) 180 #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK) 181 #undef JSON_HEDLEY_GNUC_VERSION_CHECK 183 #if defined(JSON_HEDLEY_GNUC_VERSION
) 184 #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 186 #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0
) 189 #if defined(JSON_HEDLEY_MSVC_VERSION) 190 #undef JSON_HEDLEY_MSVC_VERSION 192 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000
) 193 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000
, (_MSC_FULL_VER % 10000000
) / 100000
, (_MSC_FULL_VER % 100000
) / 100
) 194 #elif defined(_MSC_FULL_VER) 195 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000
, (_MSC_FULL_VER % 1000000
) / 10000
, (_MSC_FULL_VER % 10000
) / 10
) 196 #elif defined(_MSC_VER) 197 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100
, _MSC_VER % 100
, 0
) 200 #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK) 201 #undef JSON_HEDLEY_MSVC_VERSION_CHECK 203 #if !defined(_MSC_VER) 204 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0
) 205 #elif defined(_MSC_VER) && (_MSC_VER >= 1400
) 206 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000
) + (minor * 100000
) + (patch))) 207 #elif defined(_MSC_VER) && (_MSC_VER >= 1200
) 208 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000
) + (minor * 10000
) + (patch))) 210 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100
) + (minor))) 213 #if defined(JSON_HEDLEY_INTEL_VERSION) 214 #undef JSON_HEDLEY_INTEL_VERSION 216 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) 217 #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100
, __INTEL_COMPILER % 100
, __INTEL_COMPILER_UPDATE) 218 #elif defined(__INTEL_COMPILER) 219 #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100
, __INTEL_COMPILER % 100
, 0
) 222 #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK) 223 #undef JSON_HEDLEY_INTEL_VERSION_CHECK 225 #if defined(JSON_HEDLEY_INTEL_VERSION) 226 #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 228 #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0
) 231 #if defined(JSON_HEDLEY_PGI_VERSION) 232 #undef JSON_HEDLEY_PGI_VERSION 234 #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__) 235 #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) 238 #if defined(JSON_HEDLEY_PGI_VERSION_CHECK) 239 #undef JSON_HEDLEY_PGI_VERSION_CHECK 241 #if defined(JSON_HEDLEY_PGI_VERSION) 242 #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 244 #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0
) 247 #if defined(JSON_HEDLEY_SUNPRO_VERSION) 248 #undef JSON_HEDLEY_SUNPRO_VERSION 250 #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000
) 251 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16
) & 0xf
) * 10
) + ((__SUNPRO_C >> 12
) & 0xf
), (((__SUNPRO_C >> 8
) & 0xf
) * 10
) + ((__SUNPRO_C >> 4
) & 0xf
), (__SUNPRO_C & 0xf
) * 10
) 252 #elif defined(__SUNPRO_C) 253 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8
) & 0xf
, (__SUNPRO_C >> 4
) & 0xf
, (__SUNPRO_C) & 0xf
) 254 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000
) 255 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16
) & 0xf
) * 10
) + ((__SUNPRO_CC >> 12
) & 0xf
), (((__SUNPRO_CC >> 8
) & 0xf
) * 10
) + ((__SUNPRO_CC >> 4
) & 0xf
), (__SUNPRO_CC & 0xf
) * 10
) 256 #elif defined(__SUNPRO_CC) 257 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8
) & 0xf
, (__SUNPRO_CC >> 4
) & 0xf
, (__SUNPRO_CC) & 0xf
) 260 #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK) 261 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK 263 #if defined(JSON_HEDLEY_SUNPRO_VERSION) 264 #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 266 #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0
) 269 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) 270 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION 272 #if defined(__EMSCRIPTEN__) 273 #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__) 276 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK) 277 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK 279 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) 280 #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 282 #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0
) 285 #if defined(JSON_HEDLEY_ARM_VERSION) 286 #undef JSON_HEDLEY_ARM_VERSION 288 #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION) 289 #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000
, (__ARMCOMPILER_VERSION % 1000000
) / 10000
, (__ARMCOMPILER_VERSION % 10000
) / 100
) 290 #elif defined(__CC_ARM) && defined(__ARMCC_VERSION) 291 #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000
, (__ARMCC_VERSION % 1000000
) / 10000
, (__ARMCC_VERSION % 10000
) / 100
) 294 #if defined(JSON_HEDLEY_ARM_VERSION_CHECK) 295 #undef JSON_HEDLEY_ARM_VERSION_CHECK 297 #if defined(JSON_HEDLEY_ARM_VERSION) 298 #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 300 #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0
) 303 #if defined(JSON_HEDLEY_IBM_VERSION) 304 #undef JSON_HEDLEY_IBM_VERSION 306 #if defined(__ibmxl__) 307 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__) 308 #elif defined(__xlC__) && defined(__xlC_ver__) 309 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8
, __xlC__ & 0xff
, (__xlC_ver__ >> 8
) & 0xff
) 310 #elif defined(__xlC__) 311 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8
, __xlC__ & 0xff
, 0
) 314 #if defined(JSON_HEDLEY_IBM_VERSION_CHECK) 315 #undef JSON_HEDLEY_IBM_VERSION_CHECK 317 #if defined(JSON_HEDLEY_IBM_VERSION) 318 #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 320 #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0
) 323 #if defined(JSON_HEDLEY_TI_VERSION) 324 #undef JSON_HEDLEY_TI_VERSION 326 #if defined(__TI_COMPILER_VERSION__) 327 #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000
, (__TI_COMPILER_VERSION__ % 1000000
) / 1000
, (__TI_COMPILER_VERSION__ % 1000
)) 330 #if defined(JSON_HEDLEY_TI_VERSION_CHECK) 331 #undef JSON_HEDLEY_TI_VERSION_CHECK 333 #if defined(JSON_HEDLEY_TI_VERSION) 334 #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 336 #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0
) 339 #if defined(JSON_HEDLEY_CRAY_VERSION) 340 #undef JSON_HEDLEY_CRAY_VERSION 343 #if defined(_RELEASE_PATCHLEVEL) 344 #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL) 346 #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0
) 350 #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK) 351 #undef JSON_HEDLEY_CRAY_VERSION_CHECK 353 #if defined(JSON_HEDLEY_CRAY_VERSION) 354 #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 356 #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0
) 359 #if defined(JSON_HEDLEY_IAR_VERSION) 360 #undef JSON_HEDLEY_IAR_VERSION 362 #if defined(__IAR_SYSTEMS_ICC__) 364 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000
), ((__VER__ / 1000
) % 1000
), (__VER__ % 1000
)) 366 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(VER / 100
, __VER__ % 100
, 0
) 370 #if defined(JSON_HEDLEY_IAR_VERSION_CHECK) 371 #undef JSON_HEDLEY_IAR_VERSION_CHECK 373 #if defined(JSON_HEDLEY_IAR_VERSION) 374 #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 376 #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0
) 379 #if defined(JSON_HEDLEY_TINYC_VERSION) 380 #undef JSON_HEDLEY_TINYC_VERSION 382 #if defined(__TINYC__) 383 #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000
, (__TINYC__ / 100
) % 10
, __TINYC__ % 100
) 386 #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK) 387 #undef JSON_HEDLEY_TINYC_VERSION_CHECK 389 #if defined(JSON_HEDLEY_TINYC_VERSION) 390 #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 392 #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0
) 395 #if defined(JSON_HEDLEY_DMC_VERSION) 396 #undef JSON_HEDLEY_DMC_VERSION 399 #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8
, (__DMC__ >> 4
) & 0xf
, __DMC__ & 0xf
) 402 #if defined(JSON_HEDLEY_DMC_VERSION_CHECK) 403 #undef JSON_HEDLEY_DMC_VERSION_CHECK 405 #if defined(JSON_HEDLEY_DMC_VERSION) 406 #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 408 #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0
) 411 #if defined(JSON_HEDLEY_COMPCERT_VERSION) 412 #undef JSON_HEDLEY_COMPCERT_VERSION 414 #if defined(__COMPCERT_VERSION__) 415 #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000
, (__COMPCERT_VERSION__ / 100
) % 100
, __COMPCERT_VERSION__ % 100
) 418 #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK) 419 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK 421 #if defined(JSON_HEDLEY_COMPCERT_VERSION) 422 #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 424 #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0
) 427 #if defined(JSON_HEDLEY_PELLES_VERSION) 428 #undef JSON_HEDLEY_PELLES_VERSION 430 #if defined(__POCC__) 431 #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100
, __POCC__ % 100
, 0
) 434 #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK) 435 #undef JSON_HEDLEY_PELLES_VERSION_CHECK 437 #if defined(JSON_HEDLEY_PELLES_VERSION) 438 #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 440 #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0
) 443 #if defined(JSON_HEDLEY_GCC_VERSION) 444 #undef JSON_HEDLEY_GCC_VERSION 447 defined(JSON_HEDLEY_GNUC_VERSION
) && 448 !defined(__clang__
) && 449 !defined(JSON_HEDLEY_INTEL_VERSION) && 450 !defined(JSON_HEDLEY_PGI_VERSION) && 451 !defined(JSON_HEDLEY_ARM_VERSION) && 452 !defined(JSON_HEDLEY_TI_VERSION) && 453 !defined(__COMPCERT__) 454 #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION 457 #if defined(JSON_HEDLEY_GCC_VERSION_CHECK) 458 #undef JSON_HEDLEY_GCC_VERSION_CHECK 460 #if defined(JSON_HEDLEY_GCC_VERSION) 461 #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) 463 #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0
) 466 #if defined(JSON_HEDLEY_HAS_ATTRIBUTE) 467 #undef JSON_HEDLEY_HAS_ATTRIBUTE 469 #if defined(__has_attribute
) 470 #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) 472 #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0
) 475 #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE) 476 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE 478 #if defined(__has_attribute
) 479 #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute) 481 #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 484 #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE) 485 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE 487 #if defined(__has_attribute
) 488 #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute) 490 #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 493 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE) 494 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE 496 #if defined(__has_cpp_attribute
) && defined(__cplusplus
) 497 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute) 499 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0
) 502 #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE) 503 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE 505 #if defined(__has_cpp_attribute
) && defined(__cplusplus
) 506 #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) 508 #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 511 #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE) 512 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE 514 #if defined(__has_cpp_attribute
) && defined(__cplusplus
) 515 #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) 517 #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 520 #if defined(JSON_HEDLEY_HAS_BUILTIN) 521 #undef JSON_HEDLEY_HAS_BUILTIN 523 #if defined(__has_builtin
) 524 #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin) 526 #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0
) 529 #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN) 530 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN 532 #if defined(__has_builtin
) 533 #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) 535 #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 538 #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN) 539 #undef JSON_HEDLEY_GCC_HAS_BUILTIN 541 #if defined(__has_builtin
) 542 #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) 544 #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 547 #if defined(JSON_HEDLEY_HAS_FEATURE) 548 #undef JSON_HEDLEY_HAS_FEATURE 550 #if defined(__has_feature
) 551 #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature) 553 #define JSON_HEDLEY_HAS_FEATURE(feature) (0
) 556 #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE) 557 #undef JSON_HEDLEY_GNUC_HAS_FEATURE 559 #if defined(__has_feature
) 560 #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) 562 #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 565 #if defined(JSON_HEDLEY_GCC_HAS_FEATURE) 566 #undef JSON_HEDLEY_GCC_HAS_FEATURE 568 #if defined(__has_feature
) 569 #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) 571 #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 574 #if defined(JSON_HEDLEY_HAS_EXTENSION) 575 #undef JSON_HEDLEY_HAS_EXTENSION 577 #if defined(__has_extension
) 578 #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension) 580 #define JSON_HEDLEY_HAS_EXTENSION(extension) (0
) 583 #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION) 584 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION 586 #if defined(__has_extension
) 587 #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) 589 #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 592 #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION) 593 #undef JSON_HEDLEY_GCC_HAS_EXTENSION 595 #if defined(__has_extension
) 596 #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) 598 #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 601 #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE) 602 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE 604 #if defined(__has_declspec_attribute
) 605 #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute) 607 #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0
) 610 #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE) 611 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE 613 #if defined(__has_declspec_attribute
) 614 #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) 616 #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 619 #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE) 620 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE 622 #if defined(__has_declspec_attribute
) 623 #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) 625 #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 628 #if defined(JSON_HEDLEY_HAS_WARNING) 629 #undef JSON_HEDLEY_HAS_WARNING 631 #if defined(__has_warning
) 632 #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning) 634 #define JSON_HEDLEY_HAS_WARNING(warning) (0
) 637 #if defined(JSON_HEDLEY_GNUC_HAS_WARNING) 638 #undef JSON_HEDLEY_GNUC_HAS_WARNING 640 #if defined(__has_warning
) 641 #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) 643 #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) 646 #if defined(JSON_HEDLEY_GCC_HAS_WARNING) 647 #undef JSON_HEDLEY_GCC_HAS_WARNING 649 #if defined(__has_warning
) 650 #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) 652 #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 656 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L
)) || 657 defined(__clang__
) || 668 #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) 669 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15
,0
,0
) 670 #define JSON_HEDLEY_PRAGMA(value) __pragma(value) 672 #define JSON_HEDLEY_PRAGMA(value) 675 #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) 676 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH 678 #if defined(JSON_HEDLEY_DIAGNOSTIC_POP) 679 #undef JSON_HEDLEY_DIAGNOSTIC_POP 681 #if defined(__clang__
) 682 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") 683 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") 684 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) 685 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") 686 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") 687 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4
,6
,0
) 688 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") 689 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") 690 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15
,0
,0
) 691 #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) 692 #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) 693 #elif JSON_HEDLEY_ARM_VERSION_CHECK(5
,6
,0
) 694 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") 695 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") 696 #elif JSON_HEDLEY_TI_VERSION_CHECK(8
,1
,0
) 697 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") 698 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") 699 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2
,90
,0
) 700 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") 701 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") 703 #define JSON_HEDLEY_DIAGNOSTIC_PUSH 704 #define JSON_HEDLEY_DIAGNOSTIC_POP 707 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED) 708 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED 711 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") 712 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) 713 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)") 714 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17
,10
,0
) 715 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") 716 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4
,3
,0
) 717 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") 718 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15
,0
,0
) 719 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996
)) 720 #elif JSON_HEDLEY_TI_VERSION_CHECK(8
,0
,0
) 721 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718") 722 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5
,13
,0
) && !defined(__cplusplus) 723 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)") 724 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5
,13
,0
) && defined(__cplusplus) 725 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)") 726 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8
,0
,0
) 727 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215") 728 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2
,90
,0
) 729 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)") 731 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED 734 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS) 735 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS 738 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") 739 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) 740 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)") 741 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17
,10
,0
) 742 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675") 743 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4
,3
,0
) 744 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"") 745 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15
,0
,0
) 746 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068
)) 747 #elif JSON_HEDLEY_TI_VERSION_CHECK(8
,0
,0
) 748 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") 749 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8
,0
,0
) 750 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161") 752 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS 755 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL) 756 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL 759 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"") 760 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) 761 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)") 762 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3
,0
,0
) 763 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") 765 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL 768 #if defined(JSON_HEDLEY_DEPRECATED) 769 #undef JSON_HEDLEY_DEPRECATED 771 #if defined(JSON_HEDLEY_DEPRECATED_FOR) 772 #undef JSON_HEDLEY_DEPRECATED_FOR 774 #if defined(__cplusplus
) && (__cplusplus
>= 201402L
) 775 #define JSON_HEDLEY_DEPRECATED(since) [[deprecated("Since " #since)]] 776 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]] 785 #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) 786 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) 788 JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || 789 JSON_HEDLEY_GCC_VERSION_CHECK(3
,1
,0
) || 790 JSON_HEDLEY_ARM_VERSION_CHECK(4
,1
,0
) || 791 JSON_HEDLEY_TI_VERSION_CHECK(8
,0
,0
) || 792 (JSON_HEDLEY_TI_VERSION_CHECK(7
,3
,0
) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) 793 #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__)) 794 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) 795 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(14
,0
,0
) 796 #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) 797 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) 799 JSON_HEDLEY_MSVC_VERSION_CHECK(13
,10
,0
) || 800 JSON_HEDLEY_PELLES_VERSION_CHECK(6
,50
,0
) 801 #define JSON_HEDLEY_DEPRECATED(since) _declspec(deprecated) 802 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated) 803 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8
,0
,0
) 804 #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated") 805 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated") 807 #define JSON_HEDLEY_DEPRECATED(since) 808 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) 811 #if defined(JSON_HEDLEY_UNAVAILABLE) 812 #undef JSON_HEDLEY_UNAVAILABLE 818 #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since))) 820 #define JSON_HEDLEY_UNAVAILABLE(available_since) 826 #if defined(__cplusplus
) && (__cplusplus
>= 201703L
) 827 #define [[nodiscard]] 836 #define __attribute__((__warn_unused_result__)) 837 #elif defined(_Check_return_) 838 #define _Check_return_ 843 #if defined(JSON_HEDLEY_SENTINEL) 844 #undef JSON_HEDLEY_SENTINEL 851 #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position))) 853 #define JSON_HEDLEY_SENTINEL(position) 856 #if defined(JSON_HEDLEY_NO_RETURN) 857 #undef JSON_HEDLEY_NO_RETURN 860 #define JSON_HEDLEY_NO_RETURN __noreturn 862 #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) 863 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
864 #define JSON_HEDLEY_NO_RETURN _Noreturn 865 #elif defined(__cplusplus
) && (__cplusplus
>= 201103L
) 866 #define JSON_HEDLEY_NO_RETURN [[noreturn]] 868 JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || 869 JSON_HEDLEY_GCC_VERSION_CHECK(3
,2
,0
) || 870 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5
,11
,0
) || 871 JSON_HEDLEY_ARM_VERSION_CHECK(4
,1
,0
) || 872 JSON_HEDLEY_IBM_VERSION_CHECK(10
,1
,0
) || 873 JSON_HEDLEY_TI_VERSION_CHECK(18
,0
,0
) || 874 (JSON_HEDLEY_TI_VERSION_CHECK(17
,3
,0
) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) 875 #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) 876 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(13
,10
,0
) 877 #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) 878 #elif JSON_HEDLEY_TI_VERSION_CHECK(6
,0
,0
) && defined(__cplusplus) 879 #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;") 880 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3
,2
,0
) 881 #define JSON_HEDLEY_NO_RETURN __attribute((noreturn)) 882 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9
,0
,0
) 883 #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) 885 #define JSON_HEDLEY_NO_RETURN 888 #if defined(JSON_HEDLEY_UNREACHABLE) 889 #undef JSON_HEDLEY_UNREACHABLE 891 #if defined(JSON_HEDLEY_UNREACHABLE_RETURN) 892 #undef JSON_HEDLEY_UNREACHABLE_RETURN 899 #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable() 900 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(13
,10
,0
) 901 #define JSON_HEDLEY_UNREACHABLE() __assume(0
) 902 #elif JSON_HEDLEY_TI_VERSION_CHECK(6
,0
,0
) 903 #if defined(__cplusplus) 904 #define JSON_HEDLEY_UNREACHABLE() std::_nassert(0
) 906 #define JSON_HEDLEY_UNREACHABLE() _nassert(0
) 908 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return value 909 #elif defined(EXIT_FAILURE) 910 #define JSON_HEDLEY_UNREACHABLE() abort() 912 #define JSON_HEDLEY_UNREACHABLE() 913 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return value 915 #if !defined(JSON_HEDLEY_UNREACHABLE_RETURN) 916 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE() 919 #if defined(JSON_HEDLEY_ASSUME) 920 #undef JSON_HEDLEY_ASSUME 925 #define JSON_HEDLEY_ASSUME(expr) __assume(expr) 927 #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr) 928 #elif JSON_HEDLEY_TI_VERSION_CHECK(6
,0
,0
) 929 #if defined(__cplusplus) 930 #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr) 932 #define JSON_HEDLEY_ASSUME(expr) _nassert(expr) 935 (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && !defined(JSON_HEDLEY_ARM_VERSION)) || 936 JSON_HEDLEY_GCC_VERSION_CHECK(4
,5
,0
) || 937 JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) || 938 JSON_HEDLEY_IBM_VERSION_CHECK(13
,1
,5
) 939 #define JSON_HEDLEY_ASSUME(expr) ((void) ((expr) ? 1
: (__builtin_unreachable(), 1
))) 941 #define JSON_HEDLEY_ASSUME(expr) ((void) (expr)) 949 #if defined(__clang__
) 950 #pragma clang diagnostic ignored "-Wvariadic-macros" 951 #elif defined(JSON_HEDLEY_GCC_VERSION) 952 #pragma GCC diagnostic ignored "-Wvariadic-macros" 955 #if defined(JSON_HEDLEY_NON_NULL) 956 #undef JSON_HEDLEY_NON_NULL 963 #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__))) 965 #define JSON_HEDLEY_NON_NULL(...) 969 #if defined(JSON_HEDLEY_PRINTF_FORMAT) 970 #undef JSON_HEDLEY_PRINTF_FORMAT 973 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check))) 975 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check))) 984 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check))) 985 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6
,0
,0
) 986 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check)) 988 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) 991 #if defined(JSON_HEDLEY_CONSTEXPR) 992 #undef JSON_HEDLEY_CONSTEXPR 994 #if defined(__cplusplus
) 995 #if __cplusplus
>= 201103L
996 #define JSON_HEDLEY_CONSTEXPR constexpr 1000 #define JSON_HEDLEY_CONSTEXPR 1003 #if defined(JSON_HEDLEY_PREDICT) 1004 #undef JSON_HEDLEY_PREDICT 1006 #if defined(JSON_HEDLEY_LIKELY) 1007 #undef JSON_HEDLEY_LIKELY 1009 #if defined(JSON_HEDLEY_UNLIKELY) 1010 #undef JSON_HEDLEY_UNLIKELY 1012 #if defined(JSON_HEDLEY_UNPREDICTABLE) 1013 #undef JSON_HEDLEY_UNPREDICTABLE 1016 #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable(!!(expr)) 1021 # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability(expr, value, probability) 1022 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1
, probability) 1023 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0
, probability) 1024 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1
) 1025 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0
) 1026 #if !defined(JSON_HEDLEY_BUILTIN_UNPREDICTABLE) 1027 #define JSON_HEDLEY_BUILTIN_UNPREDICTABLE(expr) __builtin_expect_with_probability(!!(expr), 1
, 0.5
) 1038 # define JSON_HEDLEY_PREDICT(expr, expected, probability) 1039 (((probability) >= 0.9
) ? __builtin_expect(!!(expr), (expected)) : (((void) (expected)), !!(expr))) 1040 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) 1042 JSON_HEDLEY_CONSTEXPR double hedley_probability_ = (probability); 1043 ((hedley_probability_ >= 0.9
) ? __builtin_expect(!!(expr), 1
) : ((hedley_probability_ <= 0.1
) ? __builtin_expect(!!(expr), 0
) : !!(expr))); 1045 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) 1047 JSON_HEDLEY_CONSTEXPR double hedley_probability_ = (probability); 1048 ((hedley_probability_ >= 0.9
) ? __builtin_expect(!!(expr), 0
) : ((hedley_probability_ <= 0.1
) ? __builtin_expect(!!(expr), 1
) : !!(expr))); 1050 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1
) 1051 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0
) 1053 # define JSON_HEDLEY_PREDICT(expr, expected, probability) (((void) (expected)), !!(expr)) 1054 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr)) 1055 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr)) 1056 # define JSON_HEDLEY_LIKELY(expr) (!!(expr)) 1057 # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr)) 1060 #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1
, 0.5
) 1063 #if defined(JSON_HEDLEY_MALLOC) 1064 #undef JSON_HEDLEY_MALLOC 1075 #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) 1076 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(14
, 0
, 0
) 1077 #define JSON_HEDLEY_MALLOC __declspec(restrict) 1079 #define JSON_HEDLEY_MALLOC 1082 #if defined(JSON_HEDLEY_PURE) 1083 #undef JSON_HEDLEY_PURE 1095 #define JSON_HEDLEY_PURE __attribute__((__pure__)) 1096 #elif JSON_HEDLEY_TI_VERSION_CHECK(6
,0
,0
) && defined(__cplusplus) 1097 #define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;") 1099 #define JSON_HEDLEY_PURE 1102 #if defined(JSON_HEDLEY_CONST) 1103 #undef JSON_HEDLEY_CONST 1115 #define JSON_HEDLEY_CONST __attribute__((__const__)) 1117 #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE 1120 #if defined(JSON_HEDLEY_RESTRICT) 1121 #undef JSON_HEDLEY_RESTRICT 1123 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L
) && !defined(__cplusplus
) 1124 #define JSON_HEDLEY_RESTRICT restrict 1136 #define JSON_HEDLEY_RESTRICT __restrict 1137 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5
,3
,0
) && !defined(__cplusplus) 1138 #define JSON_HEDLEY_RESTRICT _Restrict 1140 #define JSON_HEDLEY_RESTRICT 1143 #if defined(JSON_HEDLEY_INLINE) 1144 #undef JSON_HEDLEY_INLINE 1147 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L
)) || 1148 (defined(__cplusplus
) && (__cplusplus
>= 199711L
)) 1149 #define JSON_HEDLEY_INLINE inline 1151 defined(JSON_HEDLEY_GCC_VERSION) || 1152 JSON_HEDLEY_ARM_VERSION_CHECK(6
,2
,0
) 1153 #define JSON_HEDLEY_INLINE __inline__ 1155 JSON_HEDLEY_MSVC_VERSION_CHECK(12
,0
,0
) || 1156 JSON_HEDLEY_ARM_VERSION_CHECK(4
,1
,0
) || 1157 JSON_HEDLEY_TI_VERSION_CHECK(8
,0
,0
) 1158 #define JSON_HEDLEY_INLINE __inline 1160 #define JSON_HEDLEY_INLINE 1163 #if defined(JSON_HEDLEY_ALWAYS_INLINE) 1164 #undef JSON_HEDLEY_ALWAYS_INLINE 1175 #define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE 1176 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(12
,0
,0
) 1177 #define JSON_HEDLEY_ALWAYS_INLINE __forceinline 1178 #elif JSON_HEDLEY_TI_VERSION_CHECK(7
,0
,0
) && defined(__cplusplus) 1179 #define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;") 1180 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8
,0
,0
) 1181 #define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced") 1183 #define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE 1186 #if defined(JSON_HEDLEY_NEVER_INLINE) 1187 #undef JSON_HEDLEY_NEVER_INLINE 1198 #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) 1199 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(13
,10
,0
) 1200 #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) 1201 #elif JSON_HEDLEY_PGI_VERSION_CHECK(10
,2
,0
) 1202 #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline") 1203 #elif JSON_HEDLEY_TI_VERSION_CHECK(6
,0
,0
) && defined(__cplusplus) 1204 #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;") 1205 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8
,0
,0
) 1206 #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never") 1207 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3
,2
,0
) 1208 #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline)) 1209 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9
,0
,0
) 1210 #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) 1212 #define JSON_HEDLEY_NEVER_INLINE 1215 #if defined(JSON_HEDLEY_PRIVATE) 1216 #undef JSON_HEDLEY_PRIVATE 1218 #if defined(JSON_HEDLEY_PUBLIC) 1219 #undef JSON_HEDLEY_PUBLIC 1221 #if defined(JSON_HEDLEY_IMPORT) 1222 #undef JSON_HEDLEY_IMPORT 1224 #if defined(_WIN32) || defined(__CYGWIN__) 1225 #define JSON_HEDLEY_PRIVATE 1226 #define JSON_HEDLEY_PUBLIC __declspec(dllexport) 1227 #define JSON_HEDLEY_IMPORT __declspec(dllimport) 1238 #define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden"))) 1239 #define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default"))) 1241 #define JSON_HEDLEY_PRIVATE 1242 #define JSON_HEDLEY_PUBLIC 1244 #define JSON_HEDLEY_IMPORT extern 1247 #if defined(JSON_HEDLEY_NO_THROW) 1248 #undef JSON_HEDLEY_NO_THROW 1254 #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) 1256 JSON_HEDLEY_MSVC_VERSION_CHECK(13
,1
,0
) || 1257 JSON_HEDLEY_ARM_VERSION_CHECK(4
,1
,0
) 1258 #define JSON_HEDLEY_NO_THROW __declspec(nothrow) 1260 #define JSON_HEDLEY_NO_THROW 1263 #if defined(JSON_HEDLEY_FALL_THROUGH) 1264 #undef JSON_HEDLEY_FALL_THROUGH 1267 defined(__cplusplus
) && 1269 !defined(JSON_HEDLEY_PGI_VERSION) 1271 (__cplusplus
>= 201703L
) || 1273 #define JSON_HEDLEY_FALL_THROUGH [[fallthrough]] 1274 #elif (__cplusplus >= 201103L
) && JSON_HEDLEY_HAS_CPP_ATTRIBUTE(clang::fallthrough) 1275 #define JSON_HEDLEY_FALL_THROUGH [[clang::fallthrough]] 1276 #elif (__cplusplus >= 201103L
) && JSON_HEDLEY_GCC_VERSION_CHECK(7
,0
,0
) 1277 #define JSON_HEDLEY_FALL_THROUGH [[gnu::fallthrough]] 1281 #if JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(fallthrough,7
,0
,0
) && !defined(JSON_HEDLEY_PGI_VERSION) 1282 #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__)) 1283 #elif defined(__fallthrough) 1284 #define JSON_HEDLEY_FALL_THROUGH __fallthrough 1286 #define JSON_HEDLEY_FALL_THROUGH 1296 #define __attribute__((__returns_nonnull__)) 1297 #elif defined(_Ret_notnull_) 1298 #define _Ret_notnull_ 1303 #if defined(JSON_HEDLEY_ARRAY_PARAM) 1304 #undef JSON_HEDLEY_ARRAY_PARAM 1307 defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L
) && 1308 !defined(__STDC_NO_VLA__) && 1309 !defined(__cplusplus
) && 1310 !defined(JSON_HEDLEY_PGI_VERSION) && 1311 !defined(JSON_HEDLEY_TINYC_VERSION) 1312 #define JSON_HEDLEY_ARRAY_PARAM(name) (name) 1314 #define JSON_HEDLEY_ARRAY_PARAM(name) 1317 #if defined(JSON_HEDLEY_IS_CONSTANT) 1318 #undef JSON_HEDLEY_IS_CONSTANT 1320 #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR) 1321 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR 1325 #if defined(JSON_HEDLEY__IS_CONSTEXPR) 1326 #undef JSON_HEDLEY__IS_CONSTEXPR 1339 #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr) 1341 #if !defined(__cplusplus
) 1343 JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || 1344 JSON_HEDLEY_GCC_VERSION_CHECK(3
,4
,0
) || 1345 JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) || 1346 JSON_HEDLEY_IBM_VERSION_CHECK(13
,1
,0
) || 1347 JSON_HEDLEY_CRAY_VERSION_CHECK(8
,1
,0
) || 1348 JSON_HEDLEY_ARM_VERSION_CHECK(5
,4
,0
) || 1349 JSON_HEDLEY_TINYC_VERSION_CHECK(0
,9
,24
) 1350 #if defined(__INTPTR_TYPE__) 1351 #define JSON_HEDLEY__IS_CONSTEXPR(expr) __builtin_types_compatible_p(__typeof__((1
? (void*) ((__INTPTR_TYPE__) ((expr) * 0
)) : (int*) 0
)), int*) 1354 #define JSON_HEDLEY__IS_CONSTEXPR(expr) __builtin_types_compatible_p(__typeof__((1
? (void*) ((intptr_t) ((expr) * 0
)) : (int*) 0
)), int*) 1357 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L
) && !defined(JSON_HEDLEY_SUNPRO_VERSION) && !defined(JSON_HEDLEY_PGI_VERSION)) || 1358 JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) || 1359 JSON_HEDLEY_GCC_VERSION_CHECK(4
,9
,0
) || 1360 JSON_HEDLEY_INTEL_VERSION_CHECK(17
,0
,0
) || 1361 JSON_HEDLEY_IBM_VERSION_CHECK(12
,1
,0
) || 1362 JSON_HEDLEY_ARM_VERSION_CHECK(5
,3
,0
) 1363 #if defined(__INTPTR_TYPE__) 1364 #define JSON_HEDLEY__IS_CONSTEXPR(expr) _Generic((1
? (void*) ((__INTPTR_TYPE__) ((expr) * 0
)) : (int*) 0
), int*: 1
, void*: 0
) 1367 #define JSON_HEDLEY__IS_CONSTEXPR(expr) _Generic((1
? (void*) ((intptr_t) * 0
) : (int*) 0
), int*: 1
, void*: 0
) 1370 defined(JSON_HEDLEY_GCC_VERSION) || 1371 defined(JSON_HEDLEY_INTEL_VERSION) || 1372 defined(JSON_HEDLEY_TINYC_VERSION) || 1373 defined(JSON_HEDLEY_TI_VERSION) || 1375 # define JSON_HEDLEY__IS_CONSTEXPR(expr) ( 1379 ((void*) ((expr) * 0L
) ) : \ 1380 ((struct{ char v[sizeof(void) * 2
]; } *) 1
) 1386 #if defined(JSON_HEDLEY__IS_CONSTEXPR) 1387 #if !defined(JSON_HEDLEY_IS_CONSTANT) 1388 #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY__IS_CONSTEXPR(expr) 1390 #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY__IS_CONSTEXPR(expr) ? (expr) : (-1
)) 1393 #define JSON_HEDLEY_IS_CONSTANT(expr) (0
) 1395 #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr) 1398 #if defined(JSON_HEDLEY_BEGIN_C_DECLS) 1399 #undef JSON_HEDLEY_BEGIN_C_DECLS 1401 #if defined(JSON_HEDLEY_END_C_DECLS) 1402 #undef JSON_HEDLEY_END_C_DECLS 1404 #if defined(JSON_HEDLEY_C_DECL) 1405 #undef JSON_HEDLEY_C_DECL 1407 #if defined(__cplusplus
) 1408 #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" { 1409 #define JSON_HEDLEY_END_C_DECLS } 1410 #define JSON_HEDLEY_C_DECL extern "C" 1412 #define JSON_HEDLEY_BEGIN_C_DECLS 1413 #define JSON_HEDLEY_END_C_DECLS 1414 #define JSON_HEDLEY_C_DECL 1417 #if defined(JSON_HEDLEY_STATIC_ASSERT) 1418 #undef JSON_HEDLEY_STATIC_ASSERT 1421 !defined(__cplusplus
) && ( 1422 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L
)) || 1426 defined(_Static_assert) 1428 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message) 1430 (defined(__cplusplus
) && (__cplusplus
>= 201703L
)) || 1433 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message) 1434 #elif defined(__cplusplus
) && (__cplusplus
>= 201103L
) 1435 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr) 1437 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) 1440 #if defined(JSON_HEDLEY_CONST_CAST) 1441 #undef JSON_HEDLEY_CONST_CAST 1443 #if defined(__cplusplus
) 1444 # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr)) 1446 JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || 1447 JSON_HEDLEY_GCC_VERSION_CHECK(4
,6
,0
) || 1448 JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) 1449 # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ 1450 JSON_HEDLEY_DIAGNOSTIC_PUSH 1451 JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL 1453 JSON_HEDLEY_DIAGNOSTIC_POP 1456 # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr)) 1459 #if defined(JSON_HEDLEY_REINTERPRET_CAST) 1460 #undef JSON_HEDLEY_REINTERPRET_CAST 1462 #if defined(__cplusplus
) 1463 #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr)) 1465 #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (*((T*) &(expr))) 1468 #if defined(JSON_HEDLEY_STATIC_CAST) 1469 #undef JSON_HEDLEY_STATIC_CAST 1471 #if defined(__cplusplus
) 1472 #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr)) 1474 #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr)) 1477 #if defined(JSON_HEDLEY_CPP_CAST) 1478 #undef JSON_HEDLEY_CPP_CAST 1480 #if defined(__cplusplus
) 1481 #define JSON_HEDLEY_CPP_CAST(T, expr) static_cast<T>(expr) 1483 #define JSON_HEDLEY_CPP_CAST(T, expr) (expr) 1486 #if defined(JSON_HEDLEY_MESSAGE) 1487 #undef JSON_HEDLEY_MESSAGE 1490 # define JSON_HEDLEY_MESSAGE(msg) 1491 JSON_HEDLEY_DIAGNOSTIC_PUSH 1492 JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS 1494 JSON_HEDLEY_DIAGNOSTIC_POP 1496 JSON_HEDLEY_GCC_VERSION_CHECK(4
,4
,0
) || 1497 JSON_HEDLEY_INTEL_VERSION_CHECK(13
,0
,0
) 1498 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg) 1499 #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5
,0
,0
) 1500 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg) 1501 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8
,0
,0
) 1502 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) 1503 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2
,0
,0
) 1504 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) 1506 # define JSON_HEDLEY_MESSAGE(msg) 1509 #if defined(JSON_HEDLEY_WARNING) 1510 #undef JSON_HEDLEY_WARNING 1513 # define JSON_HEDLEY_WARNING(msg) 1514 JSON_HEDLEY_DIAGNOSTIC_PUSH 1515 JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS 1517 JSON_HEDLEY_DIAGNOSTIC_POP 1519 JSON_HEDLEY_GCC_VERSION_CHECK(4
,8
,0
) || 1520 JSON_HEDLEY_PGI_VERSION_CHECK(18
,4
,0
) 1521 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg) 1522 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15
,0
,0
) 1523 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg)) 1525 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg) 1528 #if defined(JSON_HEDLEY_REQUIRE_MSG) 1529 #undef JSON_HEDLEY_REQUIRE_MSG 1533 # define JSON_HEDLEY_REQUIRE_MSG(expr, msg) 1534 JSON_HEDLEY_DIAGNOSTIC_PUSH 1535 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") 1536 __attribute__((__diagnose_if__(!(expr), msg, "error"))) 1537 JSON_HEDLEY_DIAGNOSTIC_POP 1539 # define JSON_HEDLEY_REQUIRE_MSG(expr, msg) __attribute__((__diagnose_if__(!(expr), msg, "error"))) 1542 # define JSON_HEDLEY_REQUIRE_MSG(expr, msg) 1545 #if defined(JSON_HEDLEY_REQUIRE) 1546 #undef JSON_HEDLEY_REQUIRE 1548 #define JSON_HEDLEY_REQUIRE(expr) JSON_HEDLEY_REQUIRE_MSG(expr, #expr) 1550 #if defined(JSON_HEDLEY_FLAGS) 1551 #undef JSON_HEDLEY_FLAGS 1554 #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) 1557 #if defined(JSON_HEDLEY_FLAGS_CAST) 1558 #undef JSON_HEDLEY_FLAGS_CAST 1561 # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ 1562 JSON_HEDLEY_DIAGNOSTIC_PUSH 1563 _Pragma("warning(disable:188)") 1565 JSON_HEDLEY_DIAGNOSTIC_POP 1568 # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr) 1573 #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK) 1574 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK 1576 #if defined(__clang__
) 1577 #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0
) 1579 #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) 1582 #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE) 1583 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE 1585 #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) 1587 #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE) 1588 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE 1590 #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) 1592 #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN) 1593 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN 1595 #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin) 1597 #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE) 1598 #undef JSON_HEDLEY_CLANG_HAS_FEATURE 1600 #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature) 1602 #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION) 1603 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION 1605 #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension) 1607 #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE) 1608 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE 1610 #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) 1612 #if defined(JSON_HEDLEY_CLANG_HAS_WARNING) 1613 #undef JSON_HEDLEY_CLANG_HAS_WARNING 1615 #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning) 1624 #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) 1625 #if defined(__clang__
) 1626 #if (__clang_major__
* 10000
+ __clang_minor__
* 100
+ __clang_patchlevel__
) < 30400
1627 #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" 1629 #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) 1630 #if (__GNUC__ * 10000
+ __GNUC_MINOR__ * 100
+ __GNUC_PATCHLEVEL__) < 40800
1631 #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" 1637 #if (defined(__cplusplus
) && __cplusplus
>= 201703L
) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1
) 1638 #define JSON_HAS_CPP_17 1639 #define JSON_HAS_CPP_14 1640 #elif (defined(__cplusplus
) && __cplusplus
>= 201402L
) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1
) 1641 #define JSON_HAS_CPP_14 1645 #if defined(__clang__
) || defined(__GNUC__
) || defined(__GNUG__
) 1646 #pragma GCC diagnostic push 1647 #pragma GCC diagnostic ignored "-Wfloat-equal" 1651 #if defined(__clang__
) 1652 #pragma GCC diagnostic push 1653 #pragma GCC diagnostic ignored "-Wdocumentation" 1657 #if (defined(__cpp_exceptions
) || defined(__EXCEPTIONS
) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) 1658 #define JSON_THROW(exception) throw exception 1659 #define JSON_TRY try 1660 #define JSON_CATCH(exception) catch(exception) 1661 #define JSON_INTERNAL_CATCH(exception) catch(exception) 1664 #define JSON_THROW(exception) std::abort() 1665 #define JSON_TRY if(true) 1666 #define JSON_CATCH(exception) if(false) 1667 #define JSON_INTERNAL_CATCH(exception) if(false) 1671 #if defined(JSON_THROW_USER) 1673 #define JSON_THROW JSON_THROW_USER 1675 #if defined(JSON_TRY_USER) 1677 #define JSON_TRY JSON_TRY_USER 1679 #if defined(JSON_CATCH_USER) 1681 #define JSON_CATCH JSON_CATCH_USER 1682 #undef JSON_INTERNAL_CATCH 1683 #define JSON_INTERNAL_CATCH JSON_CATCH_USER 1685 #if defined(JSON_INTERNAL_CATCH_USER) 1686 #undef JSON_INTERNAL_CATCH 1687 #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER 1695 #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) 1696 template<typename BasicJsonType> 1697 inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) 1699 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); 1700 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; 1701 auto it = std::find_if(std::begin(m), std::end(m), 1702 [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool 1704 return ej_pair.first == e; 1706 j = ((it != std::end(m)) ? it : std::begin(m))->second; 1708 template<typename BasicJsonType> 1709 inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) 1711 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); 1712 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; 1713 auto it = std::find_if(std::begin(m), std::end(m), 1714 [j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool 1716 return ej_pair.second == j; 1718 e = ((it != std::end(m)) ? it : std::begin(m))->first; 1724 #define NLOHMANN_BASIC_JSON_TPL_DECLARATION 1725 template<template<typename, typename, typename...> class ObjectType, 1726 template<typename, typename...> class ArrayType, 1727 class StringType, class BooleanType, class NumberIntegerType, 1728 class NumberUnsignedType, class NumberFloatType, 1729 template<typename> class AllocatorType, 1730 template<typename, typename = void> class JSONSerializer> 1732 #define NLOHMANN_BASIC_JSON_TPL 1733 basic_json<ObjectType, ArrayType, StringType, BooleanType, 1734 NumberIntegerType, NumberUnsignedType, NumberFloatType, 1735 AllocatorType, JSONSerializer> 1774 class exception :
public std::exception
1779 const char* what()
const noexcept override
1789 exception(
int id_,
const char* what_arg) : id(id_), m(what_arg) {}
1791 static std::string name(
const std::string& ename,
int id_)
1793 return "[json.exception." + ename +
"." + std::to_string(id_) +
"] ";
1798 std::runtime_error m;
1845 class parse_error :
public exception
1857 static parse_error create(
int id_,
const position_t& pos,
const std::string& what_arg)
1859 std::string w = exception::name(
"parse_error", id_) +
"parse error" +
1860 position_string(pos) +
": " + what_arg;
1861 return parse_error(id_, pos.chars_read_total, w.c_str());
1864 static parse_error create(
int id_, std::size_t byte_,
const std::string& what_arg)
1866 std::string w = exception::name(
"parse_error", id_) +
"parse error" +
1867 (byte_ != 0 ? (
" at byte " + std::to_string(byte_)) :
"") +
1869 return parse_error(id_, byte_, w.c_str());
1881 const std::size_t byte;
1884 parse_error(
int id_, std::size_t byte_,
const char* what_arg)
1885 : exception(id_, what_arg), byte(byte_) {}
1887 static std::string position_string(
const position_t& pos)
1889 return " at line " + std::to_string(pos.lines_read + 1) +
1890 ", column " + std::to_string(pos.chars_read_current_line);
1931 class invalid_iterator :
public exception
1934 static invalid_iterator create(
int id_,
const std::string& what_arg)
1936 std::string w = exception::name(
"invalid_iterator", id_) + what_arg;
1937 return invalid_iterator(id_, w.c_str());
1942 invalid_iterator(
int id_,
const char* what_arg)
1943 : exception(id_, what_arg) {}
1985 class type_error :
public exception
1988 static type_error create(
int id_,
const std::string& what_arg)
1990 std::string w = exception::name(
"type_error", id_) + what_arg;
1991 return type_error(id_, w.c_str());
1996 type_error(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
2032 class out_of_range :
public exception
2035 static out_of_range create(
int id_,
const std::string& what_arg)
2037 std::string w = exception::name(
"out_of_range", id_) + what_arg;
2038 return out_of_range(id_, w.c_str());
2043 out_of_range(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
2070 class other_error :
public exception
2073 static other_error create(
int id_,
const std::string& what_arg)
2075 std::string w = exception::name(
"other_error", id_) + what_arg;
2076 return other_error(id_, w.c_str());
2081 other_error(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
2093 #include <type_traits> 2100 template<
bool B,
typename T =
void>
2101 using enable_if_t =
typename std::enable_if<B, T>::type;
2103 template<
typename T>
2104 using uncvref_t =
typename std::remove_cv<
typename std::remove_reference<T>::type>::type;
2108 template<std::size_t... Ints>
2109 struct index_sequence
2111 using type = index_sequence;
2112 using value_type = std::size_t;
2113 static constexpr std::size_t size()
noexcept 2115 return sizeof...(Ints);
2119 template<
class Sequence1,
class Sequence2>
2120 struct merge_and_renumber;
2122 template<std::size_t... I1, std::size_t... I2>
2123 struct merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>>
2124 : index_sequence < I1..., (
sizeof...(I1) + I2)... > {};
2126 template<std::size_t N>
2127 struct make_index_sequence
2128 : merge_and_renumber <
typename make_index_sequence < N / 2 >::type,
2129 typename make_index_sequence < N - N / 2 >::type > {};
2131 template<>
struct make_index_sequence<0> : index_sequence<> {};
2132 template<>
struct make_index_sequence<1> : index_sequence<0> {};
2134 template<
typename... Ts>
2135 using index_sequence_for = make_index_sequence<
sizeof...(Ts)>;
2138 template<
unsigned N>
struct priority_tag : priority_tag < N - 1 > {};
2139 template<>
struct priority_tag<0> {};
2142 template<
typename T>
2145 static constexpr T value{};
2148 template<
typename T>
2149 constexpr T static_const<T>::value;
2158 #include <type_traits> 2173 template <
typename ...Ts>
struct make_void
2177 template <
typename ...Ts>
using void_t =
typename make_void<Ts...>::type;
2188 template <
typename It,
typename =
void>
2189 struct iterator_types {};
2191 template <
typename It>
2192 struct iterator_types <
2194 void_t<
typename It::difference_type,
typename It::value_type,
typename It::pointer,
2195 typename It::reference,
typename It::iterator_category >>
2197 using difference_type =
typename It::difference_type;
2198 using value_type =
typename It::value_type;
2199 using pointer =
typename It::pointer;
2200 using reference =
typename It::reference;
2201 using iterator_category =
typename It::iterator_category;
2206 template <
typename T,
typename =
void>
2207 struct iterator_traits
2211 template <
typename T>
2212 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
2217 template <
typename T>
2218 struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
2220 using iterator_category = std::random_access_iterator_tag;
2221 using value_type = T;
2222 using difference_type = ptrdiff_t;
2224 using reference = T&;
2236 #include <type_traits> 2248 nonesuch() =
delete;
2249 ~nonesuch() =
delete;
2250 nonesuch(nonesuch
const&) =
delete;
2251 nonesuch(nonesuch
const&&) =
delete;
2252 void operator=(nonesuch
const&) =
delete;
2253 void operator=(nonesuch&&) =
delete;
2256 template <
class Default,
2258 template <
class...>
class Op,
2262 using value_t = std::false_type;
2263 using type = Default;
2266 template <
class Default,
template <
class...>
class Op,
class... Args>
2267 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
2269 using value_t = std::true_type;
2270 using type = Op<Args...>;
2273 template <
template <
class...>
class Op,
class... Args>
2274 using is_detected =
typename detector<nonesuch,
void, Op, Args...>::value_t;
2276 template <
template <
class...>
class Op,
class... Args>
2277 using detected_t =
typename detector<nonesuch,
void, Op, Args...>::type;
2279 template <
class Default,
template <
class...>
class Op,
class... Args>
2280 using detected_or = detector<Default,
void, Op, Args...>;
2282 template <
class Default,
template <
class...>
class Op,
class... Args>
2283 using detected_or_t =
typename detected_or<Default, Op, Args...>::type;
2285 template <
class Expected,
template <
class...>
class Op,
class... Args>
2286 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
2288 template <
class To,
template <
class...>
class Op,
class... Args>
2289 using is_detected_convertible =
2290 std::is_convertible<detected_t<Op, Args...>, To>;
2295 #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_ 2296 #define INCLUDE_NLOHMANN_JSON_FWD_HPP_ 2318 template<
typename T =
void,
typename SFINAE =
void>
2319 struct adl_serializer;
2321 template<
template<
typename U,
typename V,
typename... Args>
class ObjectType =
2323 template<
typename U,
typename... Args>
class ArrayType =
std::
vector,
2324 class StringType =
std::
string,
class BooleanType =
bool,
2327 class NumberFloatType =
double,
2328 template<
typename U>
class AllocatorType =
std::
allocator,
2329 template<
typename T,
typename SFINAE =
void>
class JSONSerializer =
2344 template<
typename BasicJsonType>
2386 template<
typename>
struct is_basic_json : std::false_type {};
2395 template <
typename T>
2396 using mapped_type_t =
typename T::mapped_type;
2398 template <
typename T>
2399 using key_type_t =
typename T::key_type;
2401 template <
typename T>
2402 using value_type_t =
typename T::value_type;
2404 template <
typename T>
2405 using difference_type_t =
typename T::difference_type;
2407 template <
typename T>
2408 using pointer_t =
typename T::pointer;
2410 template <
typename T>
2411 using reference_t =
typename T::reference;
2413 template <
typename T>
2414 using iterator_category_t =
typename T::iterator_category;
2416 template <
typename T>
2417 using iterator_t =
typename T::iterator;
2419 template <
typename T,
typename... Args>
2420 using to_json_function =
decltype(T::to_json(std::declval<Args>()...));
2422 template <
typename T,
typename... Args>
2423 using from_json_function =
decltype(T::from_json(std::declval<Args>()...));
2425 template <
typename T,
typename U>
2426 using get_template_function =
decltype(std::declval<T>().
template get<U>());
2429 template <
typename BasicJsonType,
typename T,
typename =
void>
2430 struct has_from_json : std::false_type {};
2432 template <
typename BasicJsonType,
typename T>
2433 struct has_from_json<BasicJsonType, T,
2434 enable_if_t<
not is_basic_json<T>::value>>
2436 using serializer =
typename BasicJsonType::
template json_serializer<T,
void>;
2438 static constexpr bool value =
2439 is_detected_exact<
void, from_json_function, serializer,
2440 const BasicJsonType&, T&>::value;
2445 template <
typename BasicJsonType,
typename T,
typename =
void>
2446 struct has_non_default_from_json : std::false_type {};
2448 template<
typename BasicJsonType,
typename T>
2449 struct has_non_default_from_json<BasicJsonType, T, enable_if_t<
not is_basic_json<T>::value>>
2451 using serializer =
typename BasicJsonType::
template json_serializer<T,
void>;
2453 static constexpr bool value =
2454 is_detected_exact<T, from_json_function, serializer,
2455 const BasicJsonType&>::value;
2460 template <
typename BasicJsonType,
typename T,
typename =
void>
2461 struct has_to_json : std::false_type {};
2463 template <
typename BasicJsonType,
typename T>
2464 struct has_to_json<BasicJsonType, T, enable_if_t<
not is_basic_json<T>::value>>
2466 using serializer =
typename BasicJsonType::
template json_serializer<T,
void>;
2468 static constexpr bool value =
2469 is_detected_exact<
void, to_json_function, serializer, BasicJsonType&,
2478 template <
typename T,
typename =
void>
2479 struct is_iterator_traits : std::false_type {};
2481 template <
typename T>
2482 struct is_iterator_traits<iterator_traits<T>>
2485 using traits = iterator_traits<T>;
2488 static constexpr auto value =
2489 is_detected<value_type_t, traits>::value &&
2490 is_detected<difference_type_t, traits>::value &&
2491 is_detected<pointer_t, traits>::value &&
2492 is_detected<iterator_category_t, traits>::value &&
2493 is_detected<reference_t, traits>::value;
2498 template <
typename T,
typename =
void>
2499 struct is_complete_type : std::false_type {};
2501 template <
typename T>
2502 struct is_complete_type<T,
decltype(
void(
sizeof(T)))> : std::true_type {};
2504 template <
typename BasicJsonType,
typename CompatibleObjectType,
2506 struct is_compatible_object_type_impl : std::false_type {};
2508 template <
typename BasicJsonType,
typename CompatibleObjectType>
2509 struct is_compatible_object_type_impl <
2510 BasicJsonType, CompatibleObjectType,
2511 enable_if_t<is_detected<mapped_type_t, CompatibleObjectType>::value
and 2512 is_detected<key_type_t, CompatibleObjectType>::value >>
2515 using object_t =
typename BasicJsonType::object_t;
2518 static constexpr bool value =
2519 std::is_constructible<
typename object_t::key_type,
2520 typename CompatibleObjectType::key_type>::value
and 2521 std::is_constructible<
typename object_t::mapped_type,
2522 typename CompatibleObjectType::mapped_type>::value;
2525 template <
typename BasicJsonType,
typename CompatibleObjectType>
2526 struct is_compatible_object_type
2527 : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
2529 template <
typename BasicJsonType,
typename ConstructibleObjectType,
2531 struct is_constructible_object_type_impl : std::false_type {};
2533 template <
typename BasicJsonType,
typename ConstructibleObjectType>
2534 struct is_constructible_object_type_impl <
2535 BasicJsonType, ConstructibleObjectType,
2536 enable_if_t<is_detected<mapped_type_t, ConstructibleObjectType>::value
and 2537 is_detected<key_type_t, ConstructibleObjectType>::value >>
2539 using object_t =
typename BasicJsonType::object_t;
2541 static constexpr bool value =
2542 (std::is_default_constructible<ConstructibleObjectType>::value
and 2543 (std::is_move_assignable<ConstructibleObjectType>::value
or 2544 std::is_copy_assignable<ConstructibleObjectType>::value)
and 2545 (std::is_constructible<
typename ConstructibleObjectType::key_type,
2546 typename object_t::key_type>::value
and 2548 typename object_t::mapped_type,
2549 typename ConstructibleObjectType::mapped_type >::value))
or 2550 (has_from_json<BasicJsonType,
2551 typename ConstructibleObjectType::mapped_type>::value
or 2552 has_non_default_from_json <
2554 typename ConstructibleObjectType::mapped_type >::value);
2557 template <
typename BasicJsonType,
typename ConstructibleObjectType>
2558 struct is_constructible_object_type
2559 : is_constructible_object_type_impl<BasicJsonType,
2560 ConstructibleObjectType> {};
2562 template <
typename BasicJsonType,
typename CompatibleStringType,
2564 struct is_compatible_string_type_impl : std::false_type {};
2566 template <
typename BasicJsonType,
typename CompatibleStringType>
2567 struct is_compatible_string_type_impl <
2568 BasicJsonType, CompatibleStringType,
2569 enable_if_t<is_detected_exact<
typename BasicJsonType::string_t::value_type,
2570 value_type_t, CompatibleStringType>::value >>
2572 static constexpr auto value =
2573 std::is_constructible<
typename BasicJsonType::string_t, CompatibleStringType>::value;
2576 template <
typename BasicJsonType,
typename ConstructibleStringType>
2577 struct is_compatible_string_type
2578 : is_compatible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
2580 template <
typename BasicJsonType,
typename ConstructibleStringType,
2582 struct is_constructible_string_type_impl : std::false_type {};
2584 template <
typename BasicJsonType,
typename ConstructibleStringType>
2585 struct is_constructible_string_type_impl <
2586 BasicJsonType, ConstructibleStringType,
2587 enable_if_t<is_detected_exact<
typename BasicJsonType::string_t::value_type,
2588 value_type_t, ConstructibleStringType>::value >>
2590 static constexpr auto value =
2591 std::is_constructible<ConstructibleStringType,
2592 typename BasicJsonType::string_t>::value;
2595 template <
typename BasicJsonType,
typename ConstructibleStringType>
2596 struct is_constructible_string_type
2597 : is_constructible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
2599 template <
typename BasicJsonType,
typename CompatibleArrayType,
typename =
void>
2600 struct is_compatible_array_type_impl : std::false_type {};
2602 template <
typename BasicJsonType,
typename CompatibleArrayType>
2603 struct is_compatible_array_type_impl <
2604 BasicJsonType, CompatibleArrayType,
2605 enable_if_t<is_detected<value_type_t, CompatibleArrayType>::value
and 2606 is_detected<iterator_t, CompatibleArrayType>::value
and 2610 not is_iterator_traits<
2611 iterator_traits<CompatibleArrayType>>::value >>
2613 static constexpr bool value =
2614 std::is_constructible<BasicJsonType,
2615 typename CompatibleArrayType::value_type>::value;
2618 template <
typename BasicJsonType,
typename CompatibleArrayType>
2619 struct is_compatible_array_type
2620 : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
2622 template <
typename BasicJsonType,
typename ConstructibleArrayType,
typename =
void>
2623 struct is_constructible_array_type_impl : std::false_type {};
2625 template <
typename BasicJsonType,
typename ConstructibleArrayType>
2626 struct is_constructible_array_type_impl <
2627 BasicJsonType, ConstructibleArrayType,
2628 enable_if_t<std::is_same<ConstructibleArrayType,
2629 typename BasicJsonType::value_type>::value >>
2630 : std::true_type {};
2632 template <
typename BasicJsonType,
typename ConstructibleArrayType>
2633 struct is_constructible_array_type_impl <
2634 BasicJsonType, ConstructibleArrayType,
2635 enable_if_t<
not std::is_same<ConstructibleArrayType,
2636 typename BasicJsonType::value_type>::value
and 2637 std::is_default_constructible<ConstructibleArrayType>::value
and 2638 (std::is_move_assignable<ConstructibleArrayType>::value
or 2639 std::is_copy_assignable<ConstructibleArrayType>::value)
and 2640 is_detected<value_type_t, ConstructibleArrayType>::value
and 2641 is_detected<iterator_t, ConstructibleArrayType>::value
and 2643 detected_t<value_type_t, ConstructibleArrayType>>::value >>
2645 static constexpr bool value =
2651 not is_iterator_traits<iterator_traits<ConstructibleArrayType>>::value
and 2653 (std::is_same<
typename ConstructibleArrayType::value_type,
2654 typename BasicJsonType::array_t::value_type>::value
or 2655 has_from_json<BasicJsonType,
2656 typename ConstructibleArrayType::value_type>::value
or 2657 has_non_default_from_json <
2658 BasicJsonType,
typename ConstructibleArrayType::value_type >::value);
2661 template <
typename BasicJsonType,
typename ConstructibleArrayType>
2662 struct is_constructible_array_type
2663 : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
2665 template <
typename RealIntegerType,
typename CompatibleNumberIntegerType,
2667 struct is_compatible_integer_type_impl : std::false_type {};
2669 template <
typename RealIntegerType,
typename CompatibleNumberIntegerType>
2670 struct is_compatible_integer_type_impl <
2671 RealIntegerType, CompatibleNumberIntegerType,
2672 enable_if_t<std::is_integral<RealIntegerType>::value
and 2673 std::is_integral<CompatibleNumberIntegerType>::value
and 2674 not std::is_same<
bool, CompatibleNumberIntegerType>::value >>
2677 using RealLimits = std::numeric_limits<RealIntegerType>;
2678 using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
2680 static constexpr auto value =
2681 std::is_constructible<RealIntegerType,
2682 CompatibleNumberIntegerType>::value
and 2683 CompatibleLimits::is_integer
and 2684 RealLimits::is_signed == CompatibleLimits::is_signed;
2687 template <
typename RealIntegerType,
typename CompatibleNumberIntegerType>
2688 struct is_compatible_integer_type
2689 : is_compatible_integer_type_impl<RealIntegerType,
2690 CompatibleNumberIntegerType> {};
2692 template <
typename BasicJsonType,
typename CompatibleType,
typename =
void>
2693 struct is_compatible_type_impl: std::false_type {};
2695 template <
typename BasicJsonType,
typename CompatibleType>
2696 struct is_compatible_type_impl <
2697 BasicJsonType, CompatibleType,
2698 enable_if_t<is_complete_type<CompatibleType>::value >>
2700 static constexpr bool value =
2701 has_to_json<BasicJsonType, CompatibleType>::value;
2704 template <
typename BasicJsonType,
typename CompatibleType>
2705 struct is_compatible_type
2706 : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
2751 enum class value_t : std::uint8_t
2774 inline bool operator<(
const value_t lhs,
const value_t rhs)
noexcept 2776 static constexpr std::array<std::uint8_t, 8> order = {{
2782 const auto l_index =
static_cast<std::size_t>(lhs);
2783 const auto r_index =
static_cast<std::size_t>(rhs);
2784 return l_index < order.size()
and r_index < order.size()
and order[l_index] < order[r_index];
2794 template<
typename BasicJsonType>
2795 void from_json(
const BasicJsonType& j,
typename std::nullptr_t& n)
2799 JSON_THROW(type_error::create(302,
"type must be null, but is " + std::string(j.type_name())));
2805 template<
typename BasicJsonType,
typename ArithmeticType,
2806 enable_if_t<std::is_arithmetic<ArithmeticType>::value
and 2807 not std::is_same<ArithmeticType,
typename BasicJsonType::boolean_t>::value,
2809 void get_arithmetic_value(
const BasicJsonType& j, ArithmeticType& val)
2811 switch (
static_cast<value_t>(j))
2813 case value_t::number_unsigned:
2815 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::number_unsigned_t*>());
2818 case value_t::number_integer:
2820 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::number_integer_t*>());
2823 case value_t::number_float:
2825 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::number_float_t*>());
2830 JSON_THROW(type_error::create(302,
"type must be number, but is " + std::string(j.type_name())));
2834 template<
typename BasicJsonType>
2835 void from_json(
const BasicJsonType& j,
typename BasicJsonType::boolean_t& b)
2839 JSON_THROW(type_error::create(302,
"type must be boolean, but is " + std::string(j.type_name())));
2841 b = *j.
template get_ptr<
const typename BasicJsonType::boolean_t*>();
2844 template<
typename BasicJsonType>
2845 void from_json(
const BasicJsonType& j,
typename BasicJsonType::string_t& s)
2849 JSON_THROW(type_error::create(302,
"type must be string, but is " + std::string(j.type_name())));
2851 s = *j.
template get_ptr<
const typename BasicJsonType::string_t*>();
2855 typename BasicJsonType,
typename ConstructibleStringType,
2857 is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value
and 2858 not std::is_same<
typename BasicJsonType::string_t,
2859 ConstructibleStringType>::value,
2861 void from_json(
const BasicJsonType& j, ConstructibleStringType& s)
2865 JSON_THROW(type_error::create(302,
"type must be string, but is " + std::string(j.type_name())));
2868 s = *j.
template get_ptr<
const typename BasicJsonType::string_t*>();
2871 template<
typename BasicJsonType>
2872 void from_json(
const BasicJsonType& j,
typename BasicJsonType::number_float_t& val)
2874 get_arithmetic_value(j, val);
2877 template<
typename BasicJsonType>
2878 void from_json(
const BasicJsonType& j,
typename BasicJsonType::number_unsigned_t& val)
2880 get_arithmetic_value(j, val);
2883 template<
typename BasicJsonType>
2884 void from_json(
const BasicJsonType& j,
typename BasicJsonType::number_integer_t& val)
2886 get_arithmetic_value(j, val);
2889 template<
typename BasicJsonType,
typename EnumType,
2890 enable_if_t<std::is_enum<EnumType>::value,
int> = 0>
2891 void from_json(
const BasicJsonType& j, EnumType& e)
2893 typename std::underlying_type<EnumType>::type val;
2894 get_arithmetic_value(j, val);
2895 e =
static_cast<EnumType>(val);
2899 template<
typename BasicJsonType,
typename T,
typename Allocator,
2900 enable_if_t<std::is_convertible<BasicJsonType, T>::value,
int> = 0>
2901 void from_json(
const BasicJsonType& j, std::forward_list<T, Allocator>& l)
2905 JSON_THROW(type_error::create(302,
"type must be array, but is " + std::string(j.type_name())));
2908 std::transform(j.rbegin(), j.rend(),
2909 std::front_inserter(l), [](
const BasicJsonType & i)
2911 return i.
template get<T>();
2916 template<
typename BasicJsonType,
typename T,
2917 enable_if_t<std::is_convertible<BasicJsonType, T>::value,
int> = 0>
2918 void from_json(
const BasicJsonType& j, std::valarray<T>& l)
2922 JSON_THROW(type_error::create(302,
"type must be array, but is " + std::string(j.type_name())));
2925 std::copy(j.m_value.array->begin(), j.m_value.array->end(), std::begin(l));
2928 template <
typename BasicJsonType,
typename T, std::size_t N>
2929 auto from_json(
const BasicJsonType& j, T (&arr)[N])
2930 ->
decltype(j.
template get<T>(),
void())
2932 for (std::size_t i = 0; i < N; ++i)
2934 arr[i] = j.at(i).
template get<T>();
2938 template<
typename BasicJsonType>
2939 void from_json_array_impl(
const BasicJsonType& j,
typename BasicJsonType::array_t& arr, priority_tag<3> )
2941 arr = *j.
template get_ptr<
const typename BasicJsonType::array_t*>();
2944 template <
typename BasicJsonType,
typename T, std::size_t N>
2945 auto from_json_array_impl(
const BasicJsonType& j, std::array<T, N>& arr,
2947 ->
decltype(j.
template get<T>(),
void())
2949 for (std::size_t i = 0; i < N; ++i)
2951 arr[i] = j.at(i).
template get<T>();
2955 template<
typename BasicJsonType,
typename ConstructibleArrayType>
2956 auto from_json_array_impl(
const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> )
2958 arr.reserve(std::declval<
typename ConstructibleArrayType::size_type>()),
2959 j.
template get<
typename ConstructibleArrayType::value_type>(),
2964 ConstructibleArrayType ret;
2965 ret.reserve(j.size());
2966 std::transform(j.begin(), j.end(),
2967 std::inserter(ret, end(ret)), [](
const BasicJsonType & i)
2971 return i.
template get<
typename ConstructibleArrayType::value_type>();
2973 arr = std::move(ret);
2976 template <
typename BasicJsonType,
typename ConstructibleArrayType>
2977 void from_json_array_impl(
const BasicJsonType& j, ConstructibleArrayType& arr,
2982 ConstructibleArrayType ret;
2984 j.begin(), j.end(), std::inserter(ret, end(ret)),
2985 [](
const BasicJsonType & i)
2989 return i.
template get<
typename ConstructibleArrayType::value_type>();
2991 arr = std::move(ret);
2994 template <
typename BasicJsonType,
typename ConstructibleArrayType,
2996 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value
and 2997 not is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value
and 2998 not is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value
and 2999 not is_basic_json<ConstructibleArrayType>::value,
3002 auto from_json(
const BasicJsonType& j, ConstructibleArrayType& arr)
3003 ->
decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
3004 j.
template get<
typename ConstructibleArrayType::value_type>(),
3009 JSON_THROW(type_error::create(302,
"type must be array, but is " +
3010 std::string(j.type_name())));
3013 from_json_array_impl(j, arr, priority_tag<3> {});
3016 template<
typename BasicJsonType,
typename ConstructibleObjectType,
3017 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value,
int> = 0>
3018 void from_json(
const BasicJsonType& j, ConstructibleObjectType& obj)
3022 JSON_THROW(type_error::create(302,
"type must be object, but is " + std::string(j.type_name())));
3025 ConstructibleObjectType ret;
3026 auto inner_object = j.
template get_ptr<
const typename BasicJsonType::object_t*>();
3027 using value_type =
typename ConstructibleObjectType::value_type;
3029 inner_object->begin(), inner_object->end(),
3030 std::inserter(ret, ret.begin()),
3031 [](
typename BasicJsonType::object_t::value_type
const & p)
3033 return value_type(p.first, p.second.
template get<
typename ConstructibleObjectType::mapped_type>());
3035 obj = std::move(ret);
3042 template<
typename BasicJsonType,
typename ArithmeticType,
3044 std::is_arithmetic<ArithmeticType>::value
and 3045 not std::is_same<ArithmeticType,
typename BasicJsonType::number_unsigned_t>::value
and 3046 not std::is_same<ArithmeticType,
typename BasicJsonType::number_integer_t>::value
and 3047 not std::is_same<ArithmeticType,
typename BasicJsonType::number_float_t>::value
and 3048 not std::is_same<ArithmeticType,
typename BasicJsonType::boolean_t>::value,
3050 void from_json(
const BasicJsonType& j, ArithmeticType& val)
3052 switch (
static_cast<value_t>(j))
3054 case value_t::number_unsigned:
3056 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::number_unsigned_t*>());
3059 case value_t::number_integer:
3061 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::number_integer_t*>());
3064 case value_t::number_float:
3066 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::number_float_t*>());
3069 case value_t::boolean:
3071 val =
static_cast<ArithmeticType>(*j.
template get_ptr<
const typename BasicJsonType::boolean_t*>());
3076 JSON_THROW(type_error::create(302,
"type must be number, but is " + std::string(j.type_name())));
3080 template<
typename BasicJsonType,
typename A1,
typename A2>
3081 void from_json(
const BasicJsonType& j, std::pair<A1, A2>& p)
3083 p = {j.at(0).
template get<A1>(), j.at(1).
template get<A2>()};
3086 template<
typename BasicJsonType,
typename Tuple, std::size_t... Idx>
3087 void from_json_tuple_impl(
const BasicJsonType& j, Tuple& t, index_sequence<Idx...> )
3089 t = std::make_tuple(j.at(Idx).
template get<
typename std::tuple_element<Idx, Tuple>::type>()...);
3092 template<
typename BasicJsonType,
typename... Args>
3093 void from_json(
const BasicJsonType& j, std::tuple<Args...>& t)
3095 from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
3098 template <
typename BasicJsonType,
typename Key,
typename Value,
typename Compare,
typename Allocator,
3099 typename = enable_if_t<
not std::is_constructible<
3100 typename BasicJsonType::string_t, Key>::value>>
3101 void from_json(
const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
3105 JSON_THROW(type_error::create(302,
"type must be array, but is " + std::string(j.type_name())));
3108 for (
const auto& p : j)
3112 JSON_THROW(type_error::create(302,
"type must be array, but is " + std::string(p.type_name())));
3114 m.emplace(p.at(0).
template get<Key>(), p.at(1).
template get<Value>());
3118 template <
typename BasicJsonType,
typename Key,
typename Value,
typename Hash,
typename KeyEqual,
typename Allocator,
3119 typename = enable_if_t<
not std::is_constructible<
3120 typename BasicJsonType::string_t, Key>::value>>
3121 void from_json(
const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
3125 JSON_THROW(type_error::create(302,
"type must be array, but is " + std::string(j.type_name())));
3128 for (
const auto& p : j)
3132 JSON_THROW(type_error::create(302,
"type must be array, but is " + std::string(p.type_name())));
3134 m.emplace(p.at(0).
template get<Key>(), p.at(1).
template get<Value>());
3140 template<
typename BasicJsonType,
typename T>
3141 auto operator()(
const BasicJsonType& j, T& val)
const 3142 noexcept(
noexcept(from_json(j, val)))
3143 ->
decltype(from_json(j, val),
void())
3145 return from_json(j, val);
3155 constexpr const auto&
from_json = detail::static_const<detail::from_json_fn>::value;
3162 #include <algorithm> 3167 #include <type_traits> 3189 template <
typename IteratorType>
class iteration_proxy_value
3192 using difference_type = std::ptrdiff_t;
3193 using value_type = iteration_proxy_value;
3194 using pointer = value_type * ;
3195 using reference = value_type & ;
3196 using iterator_category = std::input_iterator_tag;
3200 IteratorType anchor;
3202 std::size_t array_index = 0;
3204 mutable std::size_t array_index_last = 0;
3206 mutable std::string array_index_str =
"0";
3208 const std::string empty_str =
"";
3211 explicit iteration_proxy_value(IteratorType it)
noexcept : anchor(it) {}
3214 iteration_proxy_value& operator*()
3220 iteration_proxy_value& operator++()
3229 bool operator==(
const iteration_proxy_value& o)
const 3231 return anchor == o.anchor;
3235 bool operator!=(
const iteration_proxy_value& o)
const 3237 return anchor != o.anchor;
3241 const std::string& key()
const 3243 assert(anchor.m_object !=
nullptr);
3245 switch (anchor.m_object->type())
3248 case value_t::array:
3250 if (array_index != array_index_last)
3252 array_index_str = std::to_string(array_index);
3253 array_index_last = array_index;
3255 return array_index_str;
3259 case value_t::object:
3260 return anchor.key();
3269 typename IteratorType::reference value()
const 3271 return anchor.value();
3276 template<
typename IteratorType>
class iteration_proxy
3280 typename IteratorType::reference container;
3284 explicit iteration_proxy(
typename IteratorType::reference cont)
noexcept 3285 : container(cont) {}
3288 iteration_proxy_value<IteratorType> begin()
noexcept 3290 return iteration_proxy_value<IteratorType>(container.begin());
3294 iteration_proxy_value<IteratorType> end()
noexcept 3296 return iteration_proxy_value<IteratorType>(container.end());
3302 template <std::size_t N,
typename IteratorType, enable_if_t<N == 0,
int> = 0>
3303 auto get(
const nlohmann::detail::iteration_proxy_value<IteratorType>& i) ->
decltype(i.key())
3310 template <std::size_t N,
typename IteratorType, enable_if_t<N == 1,
int> = 0>
3311 auto get(
const nlohmann::detail::iteration_proxy_value<IteratorType>& i) ->
decltype(i.value())
3324 #if defined(__clang__
) 3326 #pragma clang diagnostic push 3327 #pragma clang diagnostic ignored "-Wmismatched-tags" 3329 template <
typename IteratorType>
3330 class tuple_size<::
nlohmann::detail::iteration_proxy_value<IteratorType>>
3331 :
public std::integral_constant<std::size_t, 2> {};
3333 template <std::size_t N,
typename IteratorType>
3334 class tuple_element<N, ::
nlohmann::detail::iteration_proxy_value<IteratorType >>
3337 using type =
decltype(
3338 get<N>(std::declval <
3339 ::
nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
3341 #if defined(__clang__
) 3342 #pragma clang diagnostic pop 3366 template<
typename BasicJsonType>
3367 static void construct(BasicJsonType& j,
typename BasicJsonType::boolean_t b)
noexcept 3369 j.m_type = value_t::boolean;
3371 j.assert_invariant();
3378 template<
typename BasicJsonType>
3379 static void construct(BasicJsonType& j,
const typename BasicJsonType::string_t& s)
3381 j.m_type = value_t::string;
3383 j.assert_invariant();
3386 template<
typename BasicJsonType>
3387 static void construct(BasicJsonType& j,
typename BasicJsonType::string_t&& s)
3389 j.m_type = value_t::string;
3390 j.m_value = std::move(s);
3391 j.assert_invariant();
3394 template<
typename BasicJsonType,
typename CompatibleStringType,
3395 enable_if_t<
not std::is_same<CompatibleStringType,
typename BasicJsonType::string_t>::value,
3397 static void construct(BasicJsonType& j,
const CompatibleStringType& str)
3399 j.m_type = value_t::string;
3400 j.m_value.string = j.
template create<
typename BasicJsonType::string_t>(str);
3401 j.assert_invariant();
3408 template<
typename BasicJsonType>
3409 static void construct(BasicJsonType& j,
typename BasicJsonType::number_float_t val)
noexcept 3411 j.m_type = value_t::number_float;
3413 j.assert_invariant();
3420 template<
typename BasicJsonType>
3421 static void construct(BasicJsonType& j,
typename BasicJsonType::number_unsigned_t val)
noexcept 3423 j.m_type = value_t::number_unsigned;
3425 j.assert_invariant();
3432 template<
typename BasicJsonType>
3433 static void construct(BasicJsonType& j,
typename BasicJsonType::number_integer_t val)
noexcept 3435 j.m_type = value_t::number_integer;
3437 j.assert_invariant();
3444 template<
typename BasicJsonType>
3445 static void construct(BasicJsonType& j,
const typename BasicJsonType::array_t& arr)
3447 j.m_type = value_t::array;
3449 j.assert_invariant();
3452 template<
typename BasicJsonType>
3453 static void construct(BasicJsonType& j,
typename BasicJsonType::array_t&& arr)
3455 j.m_type = value_t::array;
3456 j.m_value = std::move(arr);
3457 j.assert_invariant();
3460 template<
typename BasicJsonType,
typename CompatibleArrayType,
3461 enable_if_t<
not std::is_same<CompatibleArrayType,
typename BasicJsonType::array_t>::value,
3463 static void construct(BasicJsonType& j,
const CompatibleArrayType& arr)
3467 j.m_type = value_t::array;
3468 j.m_value.array = j.
template create<
typename BasicJsonType::array_t>(begin(arr), end(arr));
3469 j.assert_invariant();
3472 template<
typename BasicJsonType>
3473 static void construct(BasicJsonType& j,
const std::vector<
bool>& arr)
3475 j.m_type = value_t::array;
3476 j.m_value = value_t::array;
3477 j.m_value.array->reserve(arr.size());
3478 for (
const bool x : arr)
3480 j.m_value.array->push_back(x);
3482 j.assert_invariant();
3485 template<
typename BasicJsonType,
typename T,
3486 enable_if_t<std::is_convertible<T, BasicJsonType>::value,
int> = 0>
3487 static void construct(BasicJsonType& j,
const std::valarray<T>& arr)
3489 j.m_type = value_t::array;
3490 j.m_value = value_t::array;
3491 j.m_value.array->resize(arr.size());
3492 std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
3493 j.assert_invariant();
3500 template<
typename BasicJsonType>
3501 static void construct(BasicJsonType& j,
const typename BasicJsonType::object_t& obj)
3503 j.m_type = value_t::object;
3505 j.assert_invariant();
3508 template<
typename BasicJsonType>
3509 static void construct(BasicJsonType& j,
typename BasicJsonType::object_t&& obj)
3511 j.m_type = value_t::object;
3512 j.m_value = std::move(obj);
3513 j.assert_invariant();
3516 template<
typename BasicJsonType,
typename CompatibleObjectType,
3517 enable_if_t<
not std::is_same<CompatibleObjectType,
typename BasicJsonType::object_t>::value,
int> = 0>
3518 static void construct(BasicJsonType& j,
const CompatibleObjectType& obj)
3523 j.m_type = value_t::object;
3524 j.m_value.object = j.
template create<
typename BasicJsonType::object_t>(begin(obj), end(obj));
3525 j.assert_invariant();
3533 template<
typename BasicJsonType,
typename T,
3534 enable_if_t<std::is_same<T,
typename BasicJsonType::boolean_t>::value,
int> = 0>
3535 void to_json(BasicJsonType& j, T b)
noexcept 3540 template<
typename BasicJsonType,
typename CompatibleString,
3541 enable_if_t<std::is_constructible<
typename BasicJsonType::string_t, CompatibleString>::value,
int> = 0>
3542 void to_json(BasicJsonType& j,
const CompatibleString& s)
3547 template<
typename BasicJsonType>
3548 void to_json(BasicJsonType& j,
typename BasicJsonType::string_t&& s)
3553 template<
typename BasicJsonType,
typename FloatType,
3554 enable_if_t<std::is_floating_point<FloatType>::value,
int> = 0>
3555 void to_json(BasicJsonType& j, FloatType val)
noexcept 3557 external_constructor<value_t::number_float>::construct(j,
static_cast<
typename BasicJsonType::number_float_t>(val));
3560 template<
typename BasicJsonType,
typename CompatibleNumberUnsignedType,
3561 enable_if_t<is_compatible_integer_type<
typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value,
int> = 0>
3562 void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val)
noexcept 3564 external_constructor<value_t::number_unsigned>::construct(j,
static_cast<
typename BasicJsonType::number_unsigned_t>(val));
3567 template<
typename BasicJsonType,
typename CompatibleNumberIntegerType,
3568 enable_if_t<is_compatible_integer_type<
typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value,
int> = 0>
3569 void to_json(BasicJsonType& j, CompatibleNumberIntegerType val)
noexcept 3571 external_constructor<value_t::number_integer>::construct(j,
static_cast<
typename BasicJsonType::number_integer_t>(val));
3574 template<
typename BasicJsonType,
typename EnumType,
3575 enable_if_t<std::is_enum<EnumType>::value,
int> = 0>
3576 void to_json(BasicJsonType& j, EnumType e)
noexcept 3578 using underlying_type =
typename std::underlying_type<EnumType>::type;
3582 template<
typename BasicJsonType>
3583 void to_json(BasicJsonType& j,
const std::vector<
bool>& e)
3585 external_constructor<value_t::array>::construct(j, e);
3588 template <
typename BasicJsonType,
typename CompatibleArrayType,
3589 enable_if_t<is_compatible_array_type<BasicJsonType,
3590 CompatibleArrayType>::value
and 3591 not is_compatible_object_type<
3592 BasicJsonType, CompatibleArrayType>::value
and 3593 not is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value
and 3594 not is_basic_json<CompatibleArrayType>::value,
3596 void to_json(BasicJsonType& j,
const CompatibleArrayType& arr)
3601 template<
typename BasicJsonType,
typename T,
3602 enable_if_t<std::is_convertible<T, BasicJsonType>::value,
int> = 0>
3603 void to_json(BasicJsonType& j,
const std::valarray<T>& arr)
3605 external_constructor<value_t::array>::construct(j, std::move(arr));
3608 template<
typename BasicJsonType>
3609 void to_json(BasicJsonType& j,
typename BasicJsonType::array_t&& arr)
3614 template<
typename BasicJsonType,
typename CompatibleObjectType,
3615 enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value
and not is_basic_json<CompatibleObjectType>::value,
int> = 0>
3616 void to_json(BasicJsonType& j,
const CompatibleObjectType& obj)
3621 template<
typename BasicJsonType>
3622 void to_json(BasicJsonType& j,
typename BasicJsonType::object_t&& obj)
3628 typename BasicJsonType,
typename T, std::size_t N,
3629 enable_if_t<
not std::is_constructible<
typename BasicJsonType::string_t,
3630 const T(&)[N]>::value,
3632 void to_json(BasicJsonType& j,
const T(&arr)[N])
3637 template<
typename BasicJsonType,
typename... Args>
3638 void to_json(BasicJsonType& j,
const std::pair<Args...>& p)
3640 j = { p.first, p.second };
3644 template <
typename BasicJsonType,
typename T,
3645 enable_if_t<std::is_same<T, iteration_proxy_value<
typename BasicJsonType::iterator>>::value,
int> = 0>
3646 void to_json(BasicJsonType& j,
const T& b)
3648 j = { {b.key(), b.value()} };
3651 template<
typename BasicJsonType,
typename Tuple, std::size_t... Idx>
3652 void to_json_tuple_impl(BasicJsonType& j,
const Tuple& t, index_sequence<Idx...> )
3654 j = { std::get<Idx>(t)... };
3657 template<
typename BasicJsonType,
typename... Args>
3658 void to_json(BasicJsonType& j,
const std::tuple<Args...>& t)
3660 to_json_tuple_impl(j, t, index_sequence_for<Args...> {});
3665 template<
typename BasicJsonType,
typename T>
3666 auto operator()(BasicJsonType& j, T&& val)
const noexcept(
noexcept(to_json(j, std::forward<T>(val))))
3667 ->
decltype(to_json(j, std::forward<T>(val)),
void())
3669 return to_json(j, std::forward<T>(val));
3677 constexpr const auto&
to_json = detail::static_const<detail::to_json_fn>::value;
3685 template<
typename,
typename>
3686 struct adl_serializer
3697 template<
typename BasicJsonType,
typename ValueType>
3702 ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
3714 template <
typename BasicJsonType,
typename ValueType>
3719 ::nlohmann::to_json(j, std::forward<ValueType>(val));
3734 #include <algorithm> 3762 #include <type_traits> 3775 enum class input_format_t { json, cbor, msgpack, ubjson, bson };
3792 struct input_adapter_protocol
3795 virtual std::char_traits<
char>::int_type get_character() = 0;
3796 virtual ~input_adapter_protocol() =
default;
3800 using input_adapter_t = std::shared_ptr<input_adapter_protocol>;
3806 class file_input_adapter :
public input_adapter_protocol
3810 explicit file_input_adapter(std::FILE* f)
noexcept 3815 file_input_adapter(
const file_input_adapter&) =
delete;
3816 file_input_adapter(file_input_adapter&&) =
default;
3817 file_input_adapter& operator=(
const file_input_adapter&) =
delete;
3818 file_input_adapter& operator=(file_input_adapter&&) =
default;
3819 ~file_input_adapter()
override =
default;
3821 std::char_traits<
char>::int_type get_character()
noexcept override 3823 return std::fgetc(m_file);
3841 class input_stream_adapter :
public input_adapter_protocol
3844 ~input_stream_adapter()
override 3848 is.clear(is.rdstate() & std::ios::eofbit);
3851 explicit input_stream_adapter(std::istream& i)
3852 : is(i), sb(*i.rdbuf())
3856 input_stream_adapter(
const input_stream_adapter&) =
delete;
3857 input_stream_adapter& operator=(input_stream_adapter&) =
delete;
3858 input_stream_adapter(input_stream_adapter&&) =
delete;
3859 input_stream_adapter& operator=(input_stream_adapter&&) =
delete;
3864 std::char_traits<
char>::int_type get_character()
override 3866 auto res = sb.sbumpc();
3870 is.clear(is.rdstate() | std::ios::eofbit);
3882 class input_buffer_adapter :
public input_adapter_protocol
3886 input_buffer_adapter(
const char* b,
const std::size_t l)
noexcept 3887 : cursor(b), limit(b + l)
3891 input_buffer_adapter(
const input_buffer_adapter&) =
delete;
3892 input_buffer_adapter& operator=(input_buffer_adapter&) =
delete;
3893 input_buffer_adapter(input_buffer_adapter&&) =
delete;
3894 input_buffer_adapter& operator=(input_buffer_adapter&&) =
delete;
3895 ~input_buffer_adapter()
override =
default;
3897 std::char_traits<
char>::int_type get_character()
noexcept override 3901 return std::char_traits<
char>::to_int_type(*(cursor++));
3904 return std::char_traits<
char>::eof();
3911 const char*
const limit;
3914 template<
typename WideStringType, size_t T>
3915 struct wide_string_input_helper
3918 static void fill_buffer(
const WideStringType& str,
3919 size_t& current_wchar,
3920 std::array<std::char_traits<
char>::int_type, 4>& utf8_bytes,
3921 size_t& utf8_bytes_index,
3922 size_t& utf8_bytes_filled)
3924 utf8_bytes_index = 0;
3926 if (current_wchar == str.size())
3928 utf8_bytes[0] = std::char_traits<
char>::eof();
3929 utf8_bytes_filled = 1;
3934 const auto wc =
static_cast<
unsigned int>(str[current_wchar++]);
3939 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(wc);
3940 utf8_bytes_filled = 1;
3942 else if (wc <= 0x7FF)
3944 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(0xC0u | ((wc >> 6u) & 0x1Fu));
3945 utf8_bytes[1] =
static_cast<std::char_traits<
char>::int_type>(0x80u | (wc & 0x3Fu));
3946 utf8_bytes_filled = 2;
3948 else if (wc <= 0xFFFF)
3950 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(0xE0u | ((wc >> 12u) & 0x0Fu));
3951 utf8_bytes[1] =
static_cast<std::char_traits<
char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
3952 utf8_bytes[2] =
static_cast<std::char_traits<
char>::int_type>(0x80u | (wc & 0x3Fu));
3953 utf8_bytes_filled = 3;
3955 else if (wc <= 0x10FFFF)
3957 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(0xF0u | ((wc >> 18u) & 0x07u));
3958 utf8_bytes[1] =
static_cast<std::char_traits<
char>::int_type>(0x80u | ((wc >> 12u) & 0x3Fu));
3959 utf8_bytes[2] =
static_cast<std::char_traits<
char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
3960 utf8_bytes[3] =
static_cast<std::char_traits<
char>::int_type>(0x80u | (wc & 0x3Fu));
3961 utf8_bytes_filled = 4;
3966 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(wc);
3967 utf8_bytes_filled = 1;
3973 template<
typename WideStringType>
3974 struct wide_string_input_helper<WideStringType, 2>
3977 static void fill_buffer(
const WideStringType& str,
3978 size_t& current_wchar,
3979 std::array<std::char_traits<
char>::int_type, 4>& utf8_bytes,
3980 size_t& utf8_bytes_index,
3981 size_t& utf8_bytes_filled)
3983 utf8_bytes_index = 0;
3985 if (current_wchar == str.size())
3987 utf8_bytes[0] = std::char_traits<
char>::eof();
3988 utf8_bytes_filled = 1;
3993 const auto wc =
static_cast<
unsigned int>(str[current_wchar++]);
3998 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(wc);
3999 utf8_bytes_filled = 1;
4001 else if (wc <= 0x7FF)
4003 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(0xC0u | ((wc >> 6u)));
4004 utf8_bytes[1] =
static_cast<std::char_traits<
char>::int_type>(0x80u | (wc & 0x3Fu));
4005 utf8_bytes_filled = 2;
4007 else if (0xD800 > wc
or wc >= 0xE000)
4009 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(0xE0u | ((wc >> 12u)));
4010 utf8_bytes[1] =
static_cast<std::char_traits<
char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
4011 utf8_bytes[2] =
static_cast<std::char_traits<
char>::int_type>(0x80u | (wc & 0x3Fu));
4012 utf8_bytes_filled = 3;
4016 if (current_wchar < str.size())
4018 const auto wc2 =
static_cast<
unsigned int>(str[current_wchar++]);
4019 const auto charcode = 0x10000u + (((wc & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
4020 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(0xF0u | (charcode >> 18u));
4021 utf8_bytes[1] =
static_cast<std::char_traits<
char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
4022 utf8_bytes[2] =
static_cast<std::char_traits<
char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
4023 utf8_bytes[3] =
static_cast<std::char_traits<
char>::int_type>(0x80u | (charcode & 0x3Fu));
4024 utf8_bytes_filled = 4;
4030 utf8_bytes[0] =
static_cast<std::char_traits<
char>::int_type>(wc);
4031 utf8_bytes_filled = 1;
4038 template<
typename WideStringType>
4039 class wide_string_input_adapter :
public input_adapter_protocol
4042 explicit wide_string_input_adapter(
const WideStringType& w)
noexcept 4046 std::char_traits<
char>::int_type get_character()
noexcept override 4049 if (utf8_bytes_index == utf8_bytes_filled)
4051 fill_buffer<
sizeof(
typename WideStringType::value_type)>();
4053 assert(utf8_bytes_filled > 0);
4054 assert(utf8_bytes_index == 0);
4058 assert(utf8_bytes_filled > 0);
4059 assert(utf8_bytes_index < utf8_bytes_filled);
4060 return utf8_bytes[utf8_bytes_index++];
4067 wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
4071 const WideStringType& str;
4074 std::size_t current_wchar = 0;
4077 std::array<std::char_traits<
char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
4080 std::size_t utf8_bytes_index = 0;
4082 std::size_t utf8_bytes_filled = 0;
4090 input_adapter(std::FILE* file)
4091 : ia(std::make_shared<file_input_adapter>(file)) {}
4093 input_adapter(std::istream& i)
4094 : ia(std::make_shared<input_stream_adapter>(i)) {}
4097 input_adapter(std::istream&& i)
4098 : ia(std::make_shared<input_stream_adapter>(i)) {}
4100 input_adapter(
const std::wstring& ws)
4101 : ia(std::make_shared<wide_string_input_adapter<std::wstring>>(ws)) {}
4103 input_adapter(
const std::u16string& ws)
4104 : ia(std::make_shared<wide_string_input_adapter<std::u16string>>(ws)) {}
4106 input_adapter(
const std::u32string& ws)
4107 : ia(std::make_shared<wide_string_input_adapter<std::u32string>>(ws)) {}
4110 template<
typename CharT,
4111 typename std::enable_if<
4112 std::is_pointer<CharT>::value
and 4113 std::is_integral<
typename std::remove_pointer<CharT>::type>::value
and 4114 sizeof(
typename std::remove_pointer<CharT>::type) == 1,
4116 input_adapter(CharT b, std::size_t l)
4117 : ia(std::make_shared<input_buffer_adapter>(
reinterpret_cast<
const char*>(b), l)) {}
4122 template<
typename CharT,
4123 typename std::enable_if<
4124 std::is_pointer<CharT>::value
and 4125 std::is_integral<
typename std::remove_pointer<CharT>::type>::value
and 4126 sizeof(
typename std::remove_pointer<CharT>::type) == 1,
4128 input_adapter(CharT b)
4129 : input_adapter(
reinterpret_cast<
const char*>(b),
4130 std::strlen(
reinterpret_cast<
const char*>(b))) {}
4133 template<
class IteratorType,
4134 typename std::enable_if<
4135 std::is_same<
typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
4137 input_adapter(IteratorType first, IteratorType last)
4142 const auto is_contiguous = std::accumulate(
4143 first, last, std::pair<
bool,
int>(
true, 0),
4144 [&first](std::pair<
bool,
int> res,
decltype(*first) val)
4146 res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
4149 assert(is_contiguous);
4154 sizeof(
typename iterator_traits<IteratorType>::value_type) == 1,
4155 "each element in the iterator range must have the size of 1 byte");
4157 const auto len =
static_cast<size_t>(std::distance(first, last));
4161 ia = std::make_shared<input_buffer_adapter>(
reinterpret_cast<
const char*>(&(*first)), len);
4166 ia = std::make_shared<input_buffer_adapter>(
nullptr, len);
4171 template<
class T, std::size_t N>
4172 input_adapter(T (&array)[N])
4173 : input_adapter(std::begin(array), std::end(array)) {}
4176 template<
class ContiguousContainer,
typename 4177 std::enable_if<
not std::is_pointer<ContiguousContainer>::value
and 4178 std::is_base_of<std::random_access_iterator_tag,
typename iterator_traits<
decltype(std::begin(std::declval<ContiguousContainer
const>()))>::iterator_category>::value,
4180 input_adapter(
const ContiguousContainer& c)
4181 : input_adapter(std::begin(c), std::end(c)) {}
4183 operator input_adapter_t()
4190 input_adapter_t ia =
nullptr;
4220 template<
typename BasicJsonType>
4224 using number_integer_t =
typename BasicJsonType::number_integer_t;
4226 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
4228 using number_float_t =
typename BasicJsonType::number_float_t;
4230 using string_t =
typename BasicJsonType::string_t;
4236 virtual bool null() = 0;
4243 virtual bool boolean(
bool val) = 0;
4265 virtual bool number_float(number_float_t val,
const string_t& s) = 0;
4273 virtual bool string(string_t& val) = 0;
4289 virtual bool key(string_t& val) = 0;
4303 virtual bool start_array(std::size_t elements) = 0;
4319 const std::string& last_token,
4320 const detail::exception& ex) = 0;
4341 template<
typename BasicJsonType>
4342 class json_sax_dom_parser
4345 using number_integer_t =
typename BasicJsonType::number_integer_t;
4346 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
4347 using number_float_t =
typename BasicJsonType::number_float_t;
4348 using string_t =
typename BasicJsonType::string_t;
4355 explicit json_sax_dom_parser(BasicJsonType& r,
const bool allow_exceptions_ =
true)
4356 : root(r), allow_exceptions(allow_exceptions_)
4360 json_sax_dom_parser(
const json_sax_dom_parser&) =
delete;
4361 json_sax_dom_parser(json_sax_dom_parser&&) =
default;
4362 json_sax_dom_parser& operator=(
const json_sax_dom_parser&) =
delete;
4363 json_sax_dom_parser& operator=(json_sax_dom_parser&&) =
default;
4364 ~json_sax_dom_parser() =
default;
4368 handle_value(
nullptr);
4372 bool boolean(
bool val)
4378 bool number_integer(number_integer_t val)
4384 bool number_unsigned(number_unsigned_t val)
4390 bool number_float(number_float_t val,
const string_t& )
4396 bool string(string_t& val)
4402 bool start_object(std::size_t len)
4404 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
4409 "excessive object size: " + std::to_string(len)));
4415 bool key(string_t& val)
4418 object_element = &(ref_stack.back()->m_value.object->operator[](val));
4424 ref_stack.pop_back();
4428 bool start_array(std::size_t len)
4430 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
4435 "excessive array size: " + std::to_string(len)));
4443 ref_stack.pop_back();
4447 bool parse_error(std::size_t ,
const std::string& ,
4448 const detail::exception& ex)
4451 if (allow_exceptions)
4454 switch ((ex.id / 100) % 100)
4457 JSON_THROW(*
static_cast<
const detail::parse_error*>(&ex));
4459 JSON_THROW(*
static_cast<
const detail::out_of_range*>(&ex));
4462 JSON_THROW(*
static_cast<
const detail::invalid_iterator*>(&ex));
4464 JSON_THROW(*
static_cast<
const detail::type_error*>(&ex));
4466 JSON_THROW(*
static_cast<
const detail::other_error*>(&ex));
4475 constexpr bool is_errored()
const 4487 template<
typename Value>
4489 BasicJsonType* handle_value(Value&& v)
4491 if (ref_stack.empty())
4493 root = BasicJsonType(std::forward<Value>(v));
4497 assert(ref_stack.back()->is_array()
or ref_stack.back()->is_object());
4499 if (ref_stack.back()->is_array())
4501 ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
4502 return &(ref_stack.back()->m_value.array->back());
4505 assert(ref_stack.back()->is_object());
4506 assert(object_element);
4507 *object_element = BasicJsonType(std::forward<Value>(v));
4508 return object_element;
4512 BasicJsonType& root;
4514 std::vector<BasicJsonType*> ref_stack {};
4516 BasicJsonType* object_element =
nullptr;
4518 bool errored =
false;
4520 const bool allow_exceptions =
true;
4523 template<
typename BasicJsonType>
4524 class json_sax_dom_callback_parser
4527 using number_integer_t =
typename BasicJsonType::number_integer_t;
4528 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
4529 using number_float_t =
typename BasicJsonType::number_float_t;
4530 using string_t =
typename BasicJsonType::string_t;
4531 using parser_callback_t =
typename BasicJsonType::parser_callback_t;
4532 using parse_event_t =
typename BasicJsonType::parse_event_t;
4534 json_sax_dom_callback_parser(BasicJsonType& r,
4535 const parser_callback_t cb,
4536 const bool allow_exceptions_ =
true)
4537 : root(r), callback(cb), allow_exceptions(allow_exceptions_)
4539 keep_stack.push_back(
true);
4543 json_sax_dom_callback_parser(
const json_sax_dom_callback_parser&) =
delete;
4544 json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) =
default;
4545 json_sax_dom_callback_parser& operator=(
const json_sax_dom_callback_parser&) =
delete;
4546 json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) =
default;
4547 ~json_sax_dom_callback_parser() =
default;
4551 handle_value(
nullptr);
4555 bool boolean(
bool val)
4561 bool number_integer(number_integer_t val)
4567 bool number_unsigned(number_unsigned_t val)
4573 bool number_float(number_float_t val,
const string_t& )
4579 bool string(string_t& val)
4585 bool start_object(std::size_t len)
4588 const bool keep = callback(
static_cast<
int>(ref_stack.size()), parse_event_t::object_start, discarded);
4589 keep_stack.push_back(keep);
4591 auto val = handle_value(BasicJsonType::value_t::object,
true);
4592 ref_stack.push_back(val.second);
4595 if (ref_stack.back()
and JSON_HEDLEY_UNLIKELY(len != std::size_t(-1)
and len > ref_stack.back()->max_size()))
4597 JSON_THROW(out_of_range::create(408,
"excessive object size: " + std::to_string(len)));
4603 bool key(string_t& val)
4605 BasicJsonType k = BasicJsonType(val);
4608 const bool keep = callback(
static_cast<
int>(ref_stack.size()), parse_event_t::key, k);
4609 key_keep_stack.push_back(keep);
4612 if (keep
and ref_stack.back())
4614 object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
4622 if (ref_stack.back()
and not callback(
static_cast<
int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
4625 *ref_stack.back() = discarded;
4628 assert(
not ref_stack.empty());
4629 assert(
not keep_stack.empty());
4630 ref_stack.pop_back();
4631 keep_stack.pop_back();
4633 if (
not ref_stack.empty()
and ref_stack.back()
and ref_stack.back()->is_object())
4636 for (
auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
4638 if (it->is_discarded())
4640 ref_stack.back()->erase(it);
4649 bool start_array(std::size_t len)
4651 const bool keep = callback(
static_cast<
int>(ref_stack.size()), parse_event_t::array_start, discarded);
4652 keep_stack.push_back(keep);
4654 auto val = handle_value(BasicJsonType::value_t::array,
true);
4655 ref_stack.push_back(val.second);
4658 if (ref_stack.back()
and JSON_HEDLEY_UNLIKELY(len != std::size_t(-1)
and len > ref_stack.back()->max_size()))
4660 JSON_THROW(out_of_range::create(408,
"excessive array size: " + std::to_string(len)));
4670 if (ref_stack.back())
4672 keep = callback(
static_cast<
int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
4676 *ref_stack.back() = discarded;
4680 assert(
not ref_stack.empty());
4681 assert(
not keep_stack.empty());
4682 ref_stack.pop_back();
4683 keep_stack.pop_back();
4686 if (
not keep
and not ref_stack.empty()
and ref_stack.back()->is_array())
4688 ref_stack.back()->m_value.array->pop_back();
4694 bool parse_error(std::size_t ,
const std::string& ,
4695 const detail::exception& ex)
4698 if (allow_exceptions)
4701 switch ((ex.id / 100) % 100)
4704 JSON_THROW(*
static_cast<
const detail::parse_error*>(&ex));
4706 JSON_THROW(*
static_cast<
const detail::out_of_range*>(&ex));
4709 JSON_THROW(*
static_cast<
const detail::invalid_iterator*>(&ex));
4711 JSON_THROW(*
static_cast<
const detail::type_error*>(&ex));
4713 JSON_THROW(*
static_cast<
const detail::other_error*>(&ex));
4722 constexpr bool is_errored()
const 4743 template<
typename Value>
4744 std::pair<
bool, BasicJsonType*> handle_value(Value&& v,
const bool skip_callback =
false)
4746 assert(
not keep_stack.empty());
4750 if (
not keep_stack.back())
4752 return {
false,
nullptr};
4756 auto value = BasicJsonType(std::forward<Value>(v));
4759 const bool keep = skip_callback
or callback(
static_cast<
int>(ref_stack.size()), parse_event_t::value, value);
4764 return {
false,
nullptr};
4767 if (ref_stack.empty())
4769 root = std::move(value);
4770 return {
true, &root};
4775 if (
not ref_stack.back())
4777 return {
false,
nullptr};
4781 assert(ref_stack.back()->is_array()
or ref_stack.back()->is_object());
4784 if (ref_stack.back()->is_array())
4786 ref_stack.back()->m_value.array->push_back(std::move(value));
4787 return {
true, &(ref_stack.back()->m_value.array->back())};
4791 assert(ref_stack.back()->is_object());
4793 assert(
not key_keep_stack.empty());
4794 const bool store_element = key_keep_stack.back();
4795 key_keep_stack.pop_back();
4797 if (
not store_element)
4799 return {
false,
nullptr};
4802 assert(object_element);
4803 *object_element = std::move(value);
4804 return {
true, object_element};
4808 BasicJsonType& root;
4810 std::vector<BasicJsonType*> ref_stack {};
4812 std::vector<
bool> keep_stack {};
4814 std::vector<
bool> key_keep_stack {};
4816 BasicJsonType* object_element =
nullptr;
4818 bool errored =
false;
4820 const parser_callback_t callback =
nullptr;
4822 const bool allow_exceptions =
true;
4824 BasicJsonType discarded = BasicJsonType::value_t::discarded;
4827 template<
typename BasicJsonType>
4828 class json_sax_acceptor
4831 using number_integer_t =
typename BasicJsonType::number_integer_t;
4832 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
4833 using number_float_t =
typename BasicJsonType::number_float_t;
4834 using string_t =
typename BasicJsonType::string_t;
4846 bool number_integer(number_integer_t )
4851 bool number_unsigned(number_unsigned_t )
4856 bool number_float(number_float_t ,
const string_t& )
4861 bool string(string_t& )
4866 bool start_object(std::size_t = std::size_t(-1))
4871 bool key(string_t& )
4881 bool start_array(std::size_t = std::size_t(-1))
4891 bool parse_error(std::size_t ,
const std::string& ,
const detail::exception& )
4918 template <
typename T>
4919 using null_function_t =
decltype(std::declval<T&>().null());
4921 template <
typename T>
4922 using boolean_function_t =
4923 decltype(std::declval<T&>().boolean(std::declval<
bool>()));
4925 template <
typename T,
typename Integer>
4926 using number_integer_function_t =
4927 decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
4929 template <
typename T,
typename Unsigned>
4930 using number_unsigned_function_t =
4931 decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
4933 template <
typename T,
typename Float,
typename String>
4934 using number_float_function_t =
decltype(std::declval<T&>().number_float(
4935 std::declval<Float>(), std::declval<
const String&>()));
4937 template <
typename T,
typename String>
4938 using string_function_t =
4939 decltype(std::declval<T&>().string(std::declval<String&>()));
4941 template <
typename T>
4942 using start_object_function_t =
4943 decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
4945 template <
typename T,
typename String>
4946 using key_function_t =
4947 decltype(std::declval<T&>().key(std::declval<String&>()));
4949 template <
typename T>
4950 using end_object_function_t =
decltype(std::declval<T&>().end_object());
4952 template <
typename T>
4953 using start_array_function_t =
4954 decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
4956 template <
typename T>
4957 using end_array_function_t =
decltype(std::declval<T&>().end_array());
4959 template <
typename T,
typename Exception>
4960 using parse_error_function_t =
decltype(std::declval<T&>().parse_error(
4961 std::declval<std::size_t>(), std::declval<
const std::string&>(),
4962 std::declval<
const Exception&>()));
4964 template <
typename SAX,
typename BasicJsonType>
4968 static_assert(is_basic_json<BasicJsonType>::value,
4969 "BasicJsonType must be of type basic_json<...>");
4971 using number_integer_t =
typename BasicJsonType::number_integer_t;
4972 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
4973 using number_float_t =
typename BasicJsonType::number_float_t;
4974 using string_t =
typename BasicJsonType::string_t;
4975 using exception_t =
typename BasicJsonType::exception;
4978 static constexpr bool value =
4979 is_detected_exact<
bool, null_function_t, SAX>::value &&
4980 is_detected_exact<
bool, boolean_function_t, SAX>::value &&
4981 is_detected_exact<
bool, number_integer_function_t, SAX,
4982 number_integer_t>::value &&
4983 is_detected_exact<
bool, number_unsigned_function_t, SAX,
4984 number_unsigned_t>::value &&
4985 is_detected_exact<
bool, number_float_function_t, SAX, number_float_t,
4987 is_detected_exact<
bool, string_function_t, SAX, string_t>::value &&
4988 is_detected_exact<
bool, start_object_function_t, SAX>::value &&
4989 is_detected_exact<
bool, key_function_t, SAX, string_t>::value &&
4990 is_detected_exact<
bool, end_object_function_t, SAX>::value &&
4991 is_detected_exact<
bool, start_array_function_t, SAX>::value &&
4992 is_detected_exact<
bool, end_array_function_t, SAX>::value &&
4993 is_detected_exact<
bool, parse_error_function_t, SAX, exception_t>::value;
4996 template <
typename SAX,
typename BasicJsonType>
4997 struct is_sax_static_asserts
5000 static_assert(is_basic_json<BasicJsonType>::value,
5001 "BasicJsonType must be of type basic_json<...>");
5003 using number_integer_t =
typename BasicJsonType::number_integer_t;
5004 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
5005 using number_float_t =
typename BasicJsonType::number_float_t;
5006 using string_t =
typename BasicJsonType::string_t;
5007 using exception_t =
typename BasicJsonType::exception;
5010 static_assert(is_detected_exact<
bool, null_function_t, SAX>::value,
5011 "Missing/invalid function: bool null()");
5012 static_assert(is_detected_exact<
bool, boolean_function_t, SAX>::value,
5013 "Missing/invalid function: bool boolean(bool)");
5014 static_assert(is_detected_exact<
bool, boolean_function_t, SAX>::value,
5015 "Missing/invalid function: bool boolean(bool)");
5017 is_detected_exact<
bool, number_integer_function_t, SAX,
5018 number_integer_t>::value,
5019 "Missing/invalid function: bool number_integer(number_integer_t)");
5021 is_detected_exact<
bool, number_unsigned_function_t, SAX,
5022 number_unsigned_t>::value,
5023 "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
5024 static_assert(is_detected_exact<
bool, number_float_function_t, SAX,
5025 number_float_t, string_t>::value,
5026 "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
5028 is_detected_exact<
bool, string_function_t, SAX, string_t>::value,
5029 "Missing/invalid function: bool string(string_t&)");
5030 static_assert(is_detected_exact<
bool, start_object_function_t, SAX>::value,
5031 "Missing/invalid function: bool start_object(std::size_t)");
5032 static_assert(is_detected_exact<
bool, key_function_t, SAX, string_t>::value,
5033 "Missing/invalid function: bool key(string_t&)");
5034 static_assert(is_detected_exact<
bool, end_object_function_t, SAX>::value,
5035 "Missing/invalid function: bool end_object()");
5036 static_assert(is_detected_exact<
bool, start_array_function_t, SAX>::value,
5037 "Missing/invalid function: bool start_array(std::size_t)");
5038 static_assert(is_detected_exact<
bool, end_array_function_t, SAX>::value,
5039 "Missing/invalid function: bool end_array()");
5041 is_detected_exact<
bool, parse_error_function_t, SAX, exception_t>::value,
5042 "Missing/invalid function: bool parse_error(std::size_t, const " 5043 "std::string&, const exception&)");
5062 template<
typename BasicJsonType,
typename SAX = json_sax_dom_parser<BasicJsonType>>
5065 using number_integer_t =
typename BasicJsonType::number_integer_t;
5066 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
5067 using number_float_t =
typename BasicJsonType::number_float_t;
5068 using string_t =
typename BasicJsonType::string_t;
5069 using json_sax_t = SAX;
5077 explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
5079 (
void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
5084 binary_reader(
const binary_reader&) =
delete;
5085 binary_reader(binary_reader&&) =
default;
5086 binary_reader& operator=(
const binary_reader&) =
delete;
5087 binary_reader& operator=(binary_reader&&) =
default;
5088 ~binary_reader() =
default;
5098 bool sax_parse(
const input_format_t format,
5100 const bool strict =
true)
5103 bool result =
false;
5107 case input_format_t::bson:
5108 result = parse_bson_internal();
5111 case input_format_t::cbor:
5112 result = parse_cbor_internal();
5115 case input_format_t::msgpack:
5116 result = parse_msgpack_internal();
5119 case input_format_t::ubjson:
5120 result = parse_ubjson_internal();
5128 if (result
and strict)
5130 if (format == input_format_t::ubjson)
5141 return sax->parse_error(chars_read, get_token_string(),
5142 parse_error::create(110, chars_read, exception_message(format,
"expected end of input; last byte: 0x" + get_token_string(),
"value")));
5156 static constexpr bool little_endianess(
int num = 1)
noexcept 5158 return *
reinterpret_cast<
char*>(&num) == 1;
5170 bool parse_bson_internal()
5172 std::int32_t document_size;
5173 get_number<std::int32_t,
true>(input_format_t::bson, document_size);
5185 return sax->end_object();
5195 bool get_bson_cstr(string_t& result)
5197 auto out = std::back_inserter(result);
5205 if (current == 0x00)
5209 *out++ =
static_cast<
char>(current);
5226 template<
typename NumberType>
5227 bool get_bson_string(
const NumberType len, string_t& result)
5231 auto last_token = get_token_string();
5232 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson,
"string length must be at least 1, is " + std::to_string(len),
"string")));
5235 return get_string(input_format_t::bson, len -
static_cast<NumberType>(1), result)
and get() != std::char_traits<
char>::eof();
5248 bool parse_bson_element_internal(
const int element_type,
5249 const std::size_t element_type_parse_position)
5251 switch (element_type)
5256 return get_number<
double,
true>(input_format_t::bson, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
5263 return get_number<std::int32_t,
true>(input_format_t::bson, len)
and get_bson_string(len, value)
and sax->string(value);
5268 return parse_bson_internal();
5273 return parse_bson_array();
5278 return sax->boolean(get() != 0);
5289 return get_number<std::int32_t,
true>(input_format_t::bson, value)
and sax->number_integer(value);
5295 return get_number<std::int64_t,
true>(input_format_t::bson, value)
and sax->number_integer(value);
5300 std::array<
char, 3> cr{{}};
5301 (std::snprintf)(cr.data(), cr.size(),
"%.2hhX",
static_cast<
unsigned char>(element_type));
5302 return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position,
"Unsupported BSON record type 0x" + std::string(cr.data())));
5319 bool parse_bson_element_list(
const bool is_array)
5322 while (
int element_type = get())
5329 const std::size_t element_type_parse_position = chars_read;
5335 if (
not is_array
and not sax->key(key))
5340 if (
JSON_HEDLEY_UNLIKELY(
not parse_bson_element_internal(element_type, element_type_parse_position)))
5356 bool parse_bson_array()
5358 std::int32_t document_size;
5359 get_number<std::int32_t,
true>(input_format_t::bson, document_size);
5371 return sax->end_array();
5385 bool parse_cbor_internal(
const bool get_char =
true)
5387 switch (get_char ? get() : current)
5390 case std::char_traits<
char>::eof():
5391 return unexpect_eof(input_format_t::cbor,
"value");
5418 return sax->number_unsigned(
static_cast<number_unsigned_t>(current));
5422 std::uint8_t number;
5423 return get_number(input_format_t::cbor, number)
and sax->number_unsigned(number);
5428 std::uint16_t number;
5429 return get_number(input_format_t::cbor, number)
and sax->number_unsigned(number);
5434 std::uint32_t number;
5435 return get_number(input_format_t::cbor, number)
and sax->number_unsigned(number);
5440 std::uint64_t number;
5441 return get_number(input_format_t::cbor, number)
and sax->number_unsigned(number);
5469 return sax->number_integer(
static_cast<std::int8_t>(0x20 - 1 - current));
5473 std::uint8_t number;
5474 return get_number(input_format_t::cbor, number)
and sax->number_integer(
static_cast<number_integer_t>(-1) - number);
5479 std::uint16_t number;
5480 return get_number(input_format_t::cbor, number)
and sax->number_integer(
static_cast<number_integer_t>(-1) - number);
5485 std::uint32_t number;
5486 return get_number(input_format_t::cbor, number)
and sax->number_integer(
static_cast<number_integer_t>(-1) - number);
5491 std::uint64_t number;
5492 return get_number(input_format_t::cbor, number)
and sax->number_integer(
static_cast<number_integer_t>(-1)
5493 -
static_cast<number_integer_t>(number));
5528 return get_cbor_string(s)
and sax->string(s);
5556 return get_cbor_array(
static_cast<std::size_t>(
static_cast<
unsigned int>(current) & 0x1Fu));
5561 return get_number(input_format_t::cbor, len)
and get_cbor_array(
static_cast<std::size_t>(len));
5567 return get_number(input_format_t::cbor, len)
and get_cbor_array(
static_cast<std::size_t>(len));
5573 return get_number(input_format_t::cbor, len)
and get_cbor_array(
static_cast<std::size_t>(len));
5579 return get_number(input_format_t::cbor, len)
and get_cbor_array(
static_cast<std::size_t>(len));
5583 return get_cbor_array(std::size_t(-1));
5610 return get_cbor_object(
static_cast<std::size_t>(
static_cast<
unsigned int>(current) & 0x1Fu));
5615 return get_number(input_format_t::cbor, len)
and get_cbor_object(
static_cast<std::size_t>(len));
5621 return get_number(input_format_t::cbor, len)
and get_cbor_object(
static_cast<std::size_t>(len));
5627 return get_number(input_format_t::cbor, len)
and get_cbor_object(
static_cast<std::size_t>(len));
5633 return get_number(input_format_t::cbor, len)
and get_cbor_object(
static_cast<std::size_t>(len));
5637 return get_cbor_object(std::size_t(-1));
5640 return sax->boolean(
false);
5643 return sax->boolean(
true);
5650 const int byte1_raw = get();
5655 const int byte2_raw = get();
5661 const auto byte1 =
static_cast<
unsigned char>(byte1_raw);
5662 const auto byte2 =
static_cast<
unsigned char>(byte2_raw);
5672 const auto half =
static_cast<
unsigned int>((byte1 << 8u) + byte2);
5673 const double val = [&half]
5675 const int exp = (half >> 10u) & 0x1Fu;
5676 const unsigned int mant = half & 0x3FFu;
5677 assert(0 <= exp
and exp <= 32);
5678 assert(0 <= mant
and mant <= 1024);
5682 return std::ldexp(mant, -24);
5685 ? std::numeric_limits<
double>::infinity()
5686 : std::numeric_limits<
double>::quiet_NaN();
5688 return std::ldexp(mant + 1024, exp - 25);
5691 return sax->number_float((half & 0x8000u) != 0
5692 ?
static_cast<number_float_t>(-val)
5693 :
static_cast<number_float_t>(val),
"");
5699 return get_number(input_format_t::cbor, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
5705 return get_number(input_format_t::cbor, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
5710 auto last_token = get_token_string();
5711 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor,
"invalid byte: 0x" + last_token,
"value")));
5727 bool get_cbor_string(string_t& result)
5762 return get_string(input_format_t::cbor,
static_cast<
unsigned int>(current) & 0x1Fu, result);
5768 return get_number(input_format_t::cbor, len)
and get_string(input_format_t::cbor, len, result);
5774 return get_number(input_format_t::cbor, len)
and get_string(input_format_t::cbor, len, result);
5780 return get_number(input_format_t::cbor, len)
and get_string(input_format_t::cbor, len, result);
5786 return get_number(input_format_t::cbor, len)
and get_string(input_format_t::cbor, len, result);
5791 while (get() != 0xFF)
5794 if (
not get_cbor_string(chunk))
5798 result.append(chunk);
5805 auto last_token = get_token_string();
5806 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor,
"expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token,
"string")));
5816 bool get_cbor_array(
const std::size_t len)
5823 if (len != std::size_t(-1))
5825 for (std::size_t i = 0; i < len; ++i)
5835 while (get() != 0xFF)
5844 return sax->end_array();
5852 bool get_cbor_object(
const std::size_t len)
5860 if (len != std::size_t(-1))
5862 for (std::size_t i = 0; i < len; ++i)
5879 while (get() != 0xFF)
5894 return sax->end_object();
5904 bool parse_msgpack_internal()
5909 case std::char_traits<
char>::eof():
5910 return unexpect_eof(input_format_t::msgpack,
"value");
6041 return sax->number_unsigned(
static_cast<number_unsigned_t>(current));
6060 return get_msgpack_object(
static_cast<std::size_t>(
static_cast<
unsigned int>(current) & 0x0Fu));
6079 return get_msgpack_array(
static_cast<std::size_t>(
static_cast<
unsigned int>(current) & 0x0Fu));
6119 return get_msgpack_string(s)
and sax->string(s);
6126 return sax->boolean(
false);
6129 return sax->boolean(
true);
6134 return get_number(input_format_t::msgpack, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
6140 return get_number(input_format_t::msgpack, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
6145 std::uint8_t number;
6146 return get_number(input_format_t::msgpack, number)
and sax->number_unsigned(number);
6151 std::uint16_t number;
6152 return get_number(input_format_t::msgpack, number)
and sax->number_unsigned(number);
6157 std::uint32_t number;
6158 return get_number(input_format_t::msgpack, number)
and sax->number_unsigned(number);
6163 std::uint64_t number;
6164 return get_number(input_format_t::msgpack, number)
and sax->number_unsigned(number);
6170 return get_number(input_format_t::msgpack, number)
and sax->number_integer(number);
6175 std::int16_t number;
6176 return get_number(input_format_t::msgpack, number)
and sax->number_integer(number);
6181 std::int32_t number;
6182 return get_number(input_format_t::msgpack, number)
and sax->number_integer(number);
6187 std::int64_t number;
6188 return get_number(input_format_t::msgpack, number)
and sax->number_integer(number);
6194 return get_number(input_format_t::msgpack, len)
and get_msgpack_array(
static_cast<std::size_t>(len));
6200 return get_number(input_format_t::msgpack, len)
and get_msgpack_array(
static_cast<std::size_t>(len));
6206 return get_number(input_format_t::msgpack, len)
and get_msgpack_object(
static_cast<std::size_t>(len));
6212 return get_number(input_format_t::msgpack, len)
and get_msgpack_object(
static_cast<std::size_t>(len));
6248 return sax->number_integer(
static_cast<std::int8_t>(current));
6252 auto last_token = get_token_string();
6253 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack,
"invalid byte: 0x" + last_token,
"value")));
6268 bool get_msgpack_string(string_t& result)
6311 return get_string(input_format_t::msgpack,
static_cast<
unsigned int>(current) & 0x1Fu, result);
6317 return get_number(input_format_t::msgpack, len)
and get_string(input_format_t::msgpack, len, result);
6323 return get_number(input_format_t::msgpack, len)
and get_string(input_format_t::msgpack, len, result);
6329 return get_number(input_format_t::msgpack, len)
and get_string(input_format_t::msgpack, len, result);
6334 auto last_token = get_token_string();
6335 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack,
"expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token,
"string")));
6344 bool get_msgpack_array(
const std::size_t len)
6351 for (std::size_t i = 0; i < len; ++i)
6359 return sax->end_array();
6366 bool get_msgpack_object(
const std::size_t len)
6374 for (std::size_t i = 0; i < len; ++i)
6389 return sax->end_object();
6403 bool parse_ubjson_internal(
const bool get_char =
true)
6405 return get_ubjson_value(get_char ? get_ignore_noop() : current);
6422 bool get_ubjson_string(string_t& result,
const bool get_char =
true)
6439 return get_number(input_format_t::ubjson, len)
and get_string(input_format_t::ubjson, len, result);
6445 return get_number(input_format_t::ubjson, len)
and get_string(input_format_t::ubjson, len, result);
6451 return get_number(input_format_t::ubjson, len)
and get_string(input_format_t::ubjson, len, result);
6457 return get_number(input_format_t::ubjson, len)
and get_string(input_format_t::ubjson, len, result);
6463 return get_number(input_format_t::ubjson, len)
and get_string(input_format_t::ubjson, len, result);
6467 auto last_token = get_token_string();
6468 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson,
"expected length type specification (U, i, I, l, L); last byte: 0x" + last_token,
"string")));
6476 bool get_ubjson_size_value(std::size_t& result)
6478 switch (get_ignore_noop())
6482 std::uint8_t number;
6487 result =
static_cast<std::size_t>(number);
6498 result =
static_cast<std::size_t>(number);
6504 std::int16_t number;
6509 result =
static_cast<std::size_t>(number);
6515 std::int32_t number;
6520 result =
static_cast<std::size_t>(number);
6526 std::int64_t number;
6531 result =
static_cast<std::size_t>(number);
6537 auto last_token = get_token_string();
6538 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson,
"expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token,
"size")));
6553 bool get_ubjson_size_type(std::pair<std::size_t,
int>& result)
6555 result.first = string_t::npos;
6562 result.second = get();
6575 auto last_token = get_token_string();
6576 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson,
"expected '#' after type information; last byte: 0x" + last_token,
"size")));
6579 return get_ubjson_size_value(result.first);
6584 return get_ubjson_size_value(result.first);
6594 bool get_ubjson_value(
const int prefix)
6598 case std::char_traits<
char>::eof():
6599 return unexpect_eof(input_format_t::ubjson,
"value");
6602 return sax->boolean(
true);
6604 return sax->boolean(
false);
6611 std::uint8_t number;
6612 return get_number(input_format_t::ubjson, number)
and sax->number_unsigned(number);
6618 return get_number(input_format_t::ubjson, number)
and sax->number_integer(number);
6623 std::int16_t number;
6624 return get_number(input_format_t::ubjson, number)
and sax->number_integer(number);
6629 std::int32_t number;
6630 return get_number(input_format_t::ubjson, number)
and sax->number_integer(number);
6635 std::int64_t number;
6636 return get_number(input_format_t::ubjson, number)
and sax->number_integer(number);
6642 return get_number(input_format_t::ubjson, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
6648 return get_number(input_format_t::ubjson, number)
and sax->number_float(
static_cast<number_float_t>(number),
"");
6660 auto last_token = get_token_string();
6661 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson,
"byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token,
"char")));
6663 string_t s(1,
static_cast<
char>(current));
6664 return sax->string(s);
6670 return get_ubjson_string(s)
and sax->string(s);
6674 return get_ubjson_array();
6677 return get_ubjson_object();
6681 auto last_token = get_token_string();
6682 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson,
"invalid byte: 0x" + last_token,
"value")));
6690 bool get_ubjson_array()
6692 std::pair<std::size_t,
int> size_and_type;
6698 if (size_and_type.first != string_t::npos)
6705 if (size_and_type.second != 0)
6707 if (size_and_type.second !=
'N')
6709 for (std::size_t i = 0; i < size_and_type.first; ++i)
6720 for (std::size_t i = 0; i < size_and_type.first; ++i)
6736 while (current !=
']')
6746 return sax->end_array();
6752 bool get_ubjson_object()
6754 std::pair<std::size_t,
int> size_and_type;
6761 if (size_and_type.first != string_t::npos)
6768 if (size_and_type.second != 0)
6770 for (std::size_t i = 0; i < size_and_type.first; ++i)
6785 for (std::size_t i = 0; i < size_and_type.first; ++i)
6806 while (current !=
'}')
6821 return sax->end_object();
6840 return current = ia->get_character();
6846 int get_ignore_noop()
6852 while (current ==
'N');
6870 template<
typename NumberType,
bool InputIsLittleEndian =
false>
6871 bool get_number(
const input_format_t format, NumberType& result)
6874 std::array<std::uint8_t,
sizeof(NumberType)> vec;
6875 for (std::size_t i = 0; i <
sizeof(NumberType); ++i)
6884 if (is_little_endian != InputIsLittleEndian)
6886 vec[
sizeof(NumberType) - i - 1] =
static_cast<std::uint8_t>(current);
6890 vec[i] =
static_cast<std::uint8_t>(current);
6895 std::memcpy(&result, vec.data(),
sizeof(NumberType));
6913 template<
typename NumberType>
6914 bool get_string(
const input_format_t format,
6915 const NumberType len,
6918 bool success =
true;
6919 std::generate_n(std::back_inserter(result), len, [
this, &success, &format]()
6926 return static_cast<
char>(current);
6937 bool unexpect_eof(
const input_format_t format,
const char* context)
const 6941 return sax->parse_error(chars_read,
"<end of file>",
6942 parse_error::create(110, chars_read, exception_message(format,
"unexpected end of input", context)));
6950 std::string get_token_string()
const 6952 std::array<
char, 3> cr{{}};
6953 (std::snprintf)(cr.data(), cr.size(),
"%.2hhX",
static_cast<
unsigned char>(current));
6954 return std::string{cr.data()};
6963 std::string exception_message(
const input_format_t format,
6964 const std::string& detail,
6965 const std::string& context)
const 6967 std::string error_msg =
"syntax error while parsing ";
6971 case input_format_t::cbor:
6972 error_msg +=
"CBOR";
6975 case input_format_t::msgpack:
6976 error_msg +=
"MessagePack";
6979 case input_format_t::ubjson:
6980 error_msg +=
"UBJSON";
6983 case input_format_t::bson:
6984 error_msg +=
"BSON";
6991 return error_msg +
" " + context +
": " + detail;
6996 input_adapter_t ia =
nullptr;
6999 int current = std::char_traits<
char>::eof();
7002 std::size_t chars_read = 0;
7005 const bool is_little_endian = little_endianess();
7008 json_sax_t* sax =
nullptr;
7023 #include <initializer_list> 7048 template<
typename BasicJsonType>
7051 using number_integer_t =
typename BasicJsonType::number_integer_t;
7052 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
7053 using number_float_t =
typename BasicJsonType::number_float_t;
7054 using string_t =
typename BasicJsonType::string_t;
7058 enum class token_type
7082 static const char* token_type_name(
const token_type t)
noexcept 7086 case token_type::uninitialized:
7087 return "<uninitialized>";
7088 case token_type::literal_true:
7089 return "true literal";
7090 case token_type::literal_false:
7091 return "false literal";
7092 case token_type::literal_null:
7093 return "null literal";
7094 case token_type::value_string:
7095 return "string literal";
7096 case lexer::token_type::value_unsigned:
7097 case lexer::token_type::value_integer:
7098 case lexer::token_type::value_float:
7099 return "number literal";
7100 case token_type::begin_array:
7102 case token_type::begin_object:
7104 case token_type::end_array:
7106 case token_type::end_object:
7108 case token_type::name_separator:
7110 case token_type::value_separator:
7112 case token_type::parse_error:
7113 return "<parse error>";
7114 case token_type::end_of_input:
7115 return "end of input";
7116 case token_type::literal_or_value:
7117 return "'[', '{', or a literal";
7120 return "unknown token";
7125 explicit lexer(detail::input_adapter_t&& adapter)
7126 : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
7129 lexer(
const lexer&) =
delete;
7130 lexer(lexer&&) =
delete;
7131 lexer& operator=(lexer&) =
delete;
7132 lexer& operator=(lexer&&) =
delete;
7142 static char get_decimal_point()
noexcept 7144 const auto loc = localeconv();
7145 assert(loc !=
nullptr);
7146 return (loc->decimal_point ==
nullptr) ?
'.' : *(loc->decimal_point);
7171 assert(current ==
'u');
7174 const auto factors = { 12u, 8u, 4u, 0u };
7175 for (
const auto factor : factors)
7179 if (current >=
'0' and current <=
'9')
7181 codepoint +=
static_cast<
int>((
static_cast<
unsigned int>(current) - 0x30u) << factor);
7183 else if (current >=
'A' and current <=
'F')
7185 codepoint +=
static_cast<
int>((
static_cast<
unsigned int>(current) - 0x37u) << factor);
7187 else if (current >=
'a' and current <=
'f')
7189 codepoint +=
static_cast<
int>((
static_cast<
unsigned int>(current) - 0x57u) << factor);
7197 assert(0x0000 <= codepoint
and codepoint <= 0xFFFF);
7216 bool next_byte_in_range(std::initializer_list<
int> ranges)
7218 assert(ranges.size() == 2
or ranges.size() == 4
or ranges.size() == 6);
7221 for (
auto range = ranges.begin(); range != ranges.end(); ++range)
7230 error_message =
"invalid string: ill-formed UTF-8 byte";
7253 token_type scan_string()
7259 assert(current ==
'\"');
7267 case std::char_traits<
char>::eof():
7269 error_message =
"invalid string: missing closing quote";
7270 return token_type::parse_error;
7276 return token_type::value_string;
7320 const int codepoint1 = get_codepoint();
7321 int codepoint = codepoint1;
7325 error_message =
"invalid string: '\\u' must be followed by 4 hex digits";
7326 return token_type::parse_error;
7330 if (0xD800 <= codepoint1
and codepoint1 <= 0xDBFF)
7335 const int codepoint2 = get_codepoint();
7339 error_message =
"invalid string: '\\u' must be followed by 4 hex digits";
7340 return token_type::parse_error;
7347 codepoint =
static_cast<
int>(
7349 (
static_cast<
unsigned int>(codepoint1) << 10u)
7351 +
static_cast<
unsigned int>(codepoint2)
7359 error_message =
"invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
7360 return token_type::parse_error;
7365 error_message =
"invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
7366 return token_type::parse_error;
7373 error_message =
"invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
7374 return token_type::parse_error;
7379 assert(0x00 <= codepoint
and codepoint <= 0x10FFFF);
7382 if (codepoint < 0x80)
7387 else if (codepoint <= 0x7FF)
7390 add(
static_cast<
int>(0xC0u | (
static_cast<
unsigned int>(codepoint) >> 6u)));
7391 add(
static_cast<
int>(0x80u | (
static_cast<
unsigned int>(codepoint) & 0x3Fu)));
7393 else if (codepoint <= 0xFFFF)
7396 add(
static_cast<
int>(0xE0u | (
static_cast<
unsigned int>(codepoint) >> 12u)));
7397 add(
static_cast<
int>(0x80u | ((
static_cast<
unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7398 add(
static_cast<
int>(0x80u | (
static_cast<
unsigned int>(codepoint) & 0x3Fu)));
7403 add(
static_cast<
int>(0xF0u | (
static_cast<
unsigned int>(codepoint) >> 18u)));
7404 add(
static_cast<
int>(0x80u | ((
static_cast<
unsigned int>(codepoint) >> 12u) & 0x3Fu)));
7405 add(
static_cast<
int>(0x80u | ((
static_cast<
unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7406 add(
static_cast<
int>(0x80u | (
static_cast<
unsigned int>(codepoint) & 0x3Fu)));
7414 error_message =
"invalid string: forbidden character after backslash";
7415 return token_type::parse_error;
7424 error_message =
"invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
7425 return token_type::parse_error;
7430 error_message =
"invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
7431 return token_type::parse_error;
7436 error_message =
"invalid string: control character U+0002 (STX) must be escaped to \\u0002";
7437 return token_type::parse_error;
7442 error_message =
"invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
7443 return token_type::parse_error;
7448 error_message =
"invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
7449 return token_type::parse_error;
7454 error_message =
"invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
7455 return token_type::parse_error;
7460 error_message =
"invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
7461 return token_type::parse_error;
7466 error_message =
"invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
7467 return token_type::parse_error;
7472 error_message =
"invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
7473 return token_type::parse_error;
7478 error_message =
"invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
7479 return token_type::parse_error;
7484 error_message =
"invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
7485 return token_type::parse_error;
7490 error_message =
"invalid string: control character U+000B (VT) must be escaped to \\u000B";
7491 return token_type::parse_error;
7496 error_message =
"invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
7497 return token_type::parse_error;
7502 error_message =
"invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
7503 return token_type::parse_error;
7508 error_message =
"invalid string: control character U+000E (SO) must be escaped to \\u000E";
7509 return token_type::parse_error;
7514 error_message =
"invalid string: control character U+000F (SI) must be escaped to \\u000F";
7515 return token_type::parse_error;
7520 error_message =
"invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
7521 return token_type::parse_error;
7526 error_message =
"invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
7527 return token_type::parse_error;
7532 error_message =
"invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
7533 return token_type::parse_error;
7538 error_message =
"invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
7539 return token_type::parse_error;
7544 error_message =
"invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
7545 return token_type::parse_error;
7550 error_message =
"invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
7551 return token_type::parse_error;
7556 error_message =
"invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
7557 return token_type::parse_error;
7562 error_message =
"invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
7563 return token_type::parse_error;
7568 error_message =
"invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
7569 return token_type::parse_error;
7574 error_message =
"invalid string: control character U+0019 (EM) must be escaped to \\u0019";
7575 return token_type::parse_error;
7580 error_message =
"invalid string: control character U+001A (SUB) must be escaped to \\u001A";
7581 return token_type::parse_error;
7586 error_message =
"invalid string: control character U+001B (ESC) must be escaped to \\u001B";
7587 return token_type::parse_error;
7592 error_message =
"invalid string: control character U+001C (FS) must be escaped to \\u001C";
7593 return token_type::parse_error;
7598 error_message =
"invalid string: control character U+001D (GS) must be escaped to \\u001D";
7599 return token_type::parse_error;
7604 error_message =
"invalid string: control character U+001E (RS) must be escaped to \\u001E";
7605 return token_type::parse_error;
7610 error_message =
"invalid string: control character U+001F (US) must be escaped to \\u001F";
7611 return token_type::parse_error;
7748 return token_type::parse_error;
7758 return token_type::parse_error;
7782 return token_type::parse_error;
7792 return token_type::parse_error;
7802 return token_type::parse_error;
7814 return token_type::parse_error;
7824 return token_type::parse_error;
7832 error_message =
"invalid string: ill-formed UTF-8 byte";
7833 return token_type::parse_error;
7840 static void strtof(
float& f,
const char* str,
char** endptr)
noexcept 7842 f = std::strtof(str, endptr);
7846 static void strtof(
double& f,
const char* str,
char** endptr)
noexcept 7848 f = std::strtod(str, endptr);
7852 static void strtof(
long double& f,
const char* str,
char** endptr)
noexcept 7854 f = std::strtold(str, endptr);
7897 token_type scan_number()
7904 token_type number_type = token_type::value_unsigned;
7912 goto scan_number_minus;
7918 goto scan_number_zero;
7932 goto scan_number_any1;
7942 number_type = token_type::value_integer;
7948 goto scan_number_zero;
7962 goto scan_number_any1;
7967 error_message =
"invalid number; expected digit after '-'";
7968 return token_type::parse_error;
7978 add(decimal_point_char);
7979 goto scan_number_decimal1;
7986 goto scan_number_exponent;
7990 goto scan_number_done;
8009 goto scan_number_any1;
8014 add(decimal_point_char);
8015 goto scan_number_decimal1;
8022 goto scan_number_exponent;
8026 goto scan_number_done;
8029 scan_number_decimal1:
8031 number_type = token_type::value_float;
8046 goto scan_number_decimal2;
8051 error_message =
"invalid number; expected digit after '.'";
8052 return token_type::parse_error;
8056 scan_number_decimal2:
8072 goto scan_number_decimal2;
8079 goto scan_number_exponent;
8083 goto scan_number_done;
8086 scan_number_exponent:
8088 number_type = token_type::value_float;
8095 goto scan_number_sign;
8110 goto scan_number_any2;
8116 "invalid number; expected '+', '-', or digit after exponent";
8117 return token_type::parse_error;
8137 goto scan_number_any2;
8142 error_message =
"invalid number; expected digit after exponent sign";
8143 return token_type::parse_error;
8163 goto scan_number_any2;
8167 goto scan_number_done;
8175 char* endptr =
nullptr;
8179 if (number_type == token_type::value_unsigned)
8181 const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
8184 assert(endptr == token_buffer.data() + token_buffer.size());
8188 value_unsigned =
static_cast<number_unsigned_t>(x);
8189 if (value_unsigned == x)
8191 return token_type::value_unsigned;
8195 else if (number_type == token_type::value_integer)
8197 const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
8200 assert(endptr == token_buffer.data() + token_buffer.size());
8204 value_integer =
static_cast<number_integer_t>(x);
8205 if (value_integer == x)
8207 return token_type::value_integer;
8214 strtof(value_float, token_buffer.data(), &endptr);
8217 assert(endptr == token_buffer.data() + token_buffer.size());
8219 return token_type::value_float;
8228 token_type scan_literal(
const char* literal_text,
const std::size_t length,
8229 token_type return_type)
8231 assert(current == literal_text[0]);
8232 for (std::size_t i = 1; i < length; ++i)
8236 error_message =
"invalid literal";
8237 return token_type::parse_error;
8248 void reset()
noexcept 8250 token_buffer.clear();
8251 token_string.clear();
8252 token_string.push_back(std::char_traits<
char>::to_char_type(current));
8265 std::char_traits<
char>::int_type get()
8267 ++position.chars_read_total;
8268 ++position.chars_read_current_line;
8277 current = ia->get_character();
8282 token_string.push_back(std::char_traits<
char>::to_char_type(current));
8285 if (current ==
'\n')
8287 ++position.lines_read;
8288 position.chars_read_current_line = 0;
8306 --position.chars_read_total;
8309 if (position.chars_read_current_line == 0)
8311 if (position.lines_read > 0)
8313 --position.lines_read;
8318 --position.chars_read_current_line;
8323 assert(
not token_string.empty());
8324 token_string.pop_back();
8331 token_buffer.push_back(std::char_traits<
char>::to_char_type(c));
8340 constexpr number_integer_t get_number_integer()
const noexcept 8342 return value_integer;
8346 constexpr number_unsigned_t get_number_unsigned()
const noexcept 8348 return value_unsigned;
8352 constexpr number_float_t get_number_float()
const noexcept 8358 string_t& get_string()
8360 return token_buffer;
8368 constexpr position_t get_position()
const noexcept 8376 std::string get_token_string()
const 8380 for (
const auto c : token_string)
8382 if (
'\x00' <= c
and c <=
'\x1F')
8385 std::array<
char, 9> cs{{}};
8386 (std::snprintf)(cs.data(), cs.size(),
"<U+%.4X>",
static_cast<
unsigned char>(c));
8387 result += cs.data();
8392 result.push_back(c);
8401 constexpr const char* get_error_message()
const noexcept 8403 return error_message;
8419 return get() == 0xBB
and get() == 0xBF;
8431 if (position.chars_read_total == 0
and not skip_bom())
8433 error_message =
"invalid BOM; must be 0xEF 0xBB 0xBF if given";
8434 return token_type::parse_error;
8442 while (current ==
' ' or current ==
'\t' or current ==
'\n' or current ==
'\r');
8448 return token_type::begin_array;
8450 return token_type::end_array;
8452 return token_type::begin_object;
8454 return token_type::end_object;
8456 return token_type::name_separator;
8458 return token_type::value_separator;
8462 return scan_literal(
"true", 4, token_type::literal_true);
8464 return scan_literal(
"false", 5, token_type::literal_false);
8466 return scan_literal(
"null", 4, token_type::literal_null);
8470 return scan_string();
8484 return scan_number();
8489 case std::char_traits<
char>::eof():
8490 return token_type::end_of_input;
8494 error_message =
"invalid literal";
8495 return token_type::parse_error;
8501 detail::input_adapter_t ia =
nullptr;
8504 std::char_traits<
char>::int_type current = std::char_traits<
char>::eof();
8507 bool next_unget =
false;
8510 position_t position {};
8513 std::vector<
char> token_string {};
8516 string_t token_buffer {};
8519 const char* error_message =
"";
8522 number_integer_t value_integer = 0;
8523 number_unsigned_t value_unsigned = 0;
8524 number_float_t value_float = 0;
8527 const char decimal_point_char =
'.';
8538 #include <functional> 8571 template<
typename BasicJsonType>
8574 using number_integer_t =
typename BasicJsonType::number_integer_t;
8575 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
8576 using number_float_t =
typename BasicJsonType::number_float_t;
8577 using string_t =
typename BasicJsonType::string_t;
8578 using lexer_t = lexer<BasicJsonType>;
8579 using token_type =
typename lexer_t::token_type;
8582 enum class parse_event_t : uint8_t
8598 using parser_callback_t =
8599 std::function<
bool(
int depth, parse_event_t event, BasicJsonType& parsed)>;
8602 explicit parser(detail::input_adapter_t&& adapter,
8603 const parser_callback_t cb =
nullptr,
8604 const bool allow_exceptions_ =
true)
8605 : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
8621 void parse(
const bool strict, BasicJsonType& result)
8625 json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
8626 sax_parse_internal(&sdp);
8627 result.assert_invariant();
8630 if (strict
and (get_token() != token_type::end_of_input))
8632 sdp.parse_error(m_lexer.get_position(),
8633 m_lexer.get_token_string(),
8634 parse_error::create(101, m_lexer.get_position(),
8635 exception_message(token_type::end_of_input,
"value")));
8639 if (sdp.is_errored())
8641 result = value_t::discarded;
8647 if (result.is_discarded())
8654 json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
8655 sax_parse_internal(&sdp);
8656 result.assert_invariant();
8659 if (strict
and (get_token() != token_type::end_of_input))
8661 sdp.parse_error(m_lexer.get_position(),
8662 m_lexer.get_token_string(),
8663 parse_error::create(101, m_lexer.get_position(),
8664 exception_message(token_type::end_of_input,
"value")));
8668 if (sdp.is_errored())
8670 result = value_t::discarded;
8682 bool accept(
const bool strict =
true)
8684 json_sax_acceptor<BasicJsonType> sax_acceptor;
8685 return sax_parse(&sax_acceptor, strict);
8688 template <
typename SAX>
8690 bool sax_parse(SAX* sax,
const bool strict =
true)
8692 (
void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
8693 const bool result = sax_parse_internal(sax);
8696 if (result
and strict
and (get_token() != token_type::end_of_input))
8698 return sax->parse_error(m_lexer.get_position(),
8699 m_lexer.get_token_string(),
8700 parse_error::create(101, m_lexer.get_position(),
8701 exception_message(token_type::end_of_input,
"value")));
8708 template <
typename SAX>
8710 bool sax_parse_internal(SAX* sax)
8714 std::vector<
bool> states;
8716 bool skip_to_state_evaluation =
false;
8720 if (
not skip_to_state_evaluation)
8725 case token_type::begin_object:
8733 if (get_token() == token_type::end_object)
8745 return sax->parse_error(m_lexer.get_position(),
8746 m_lexer.get_token_string(),
8747 parse_error::create(101, m_lexer.get_position(),
8748 exception_message(token_type::value_string,
"object key")));
8758 return sax->parse_error(m_lexer.get_position(),
8759 m_lexer.get_token_string(),
8760 parse_error::create(101, m_lexer.get_position(),
8761 exception_message(token_type::name_separator,
"object separator")));
8765 states.push_back(
false);
8772 case token_type::begin_array:
8780 if (get_token() == token_type::end_array)
8790 states.push_back(
true);
8796 case token_type::value_float:
8798 const auto res = m_lexer.get_number_float();
8802 return sax->parse_error(m_lexer.get_position(),
8803 m_lexer.get_token_string(),
8804 out_of_range::create(406,
"number overflow parsing '" + m_lexer.get_token_string() +
"'"));
8815 case token_type::literal_false:
8824 case token_type::literal_null:
8833 case token_type::literal_true:
8842 case token_type::value_integer:
8851 case token_type::value_string:
8860 case token_type::value_unsigned:
8869 case token_type::parse_error:
8872 return sax->parse_error(m_lexer.get_position(),
8873 m_lexer.get_token_string(),
8874 parse_error::create(101, m_lexer.get_position(),
8875 exception_message(token_type::uninitialized,
"value")));
8880 return sax->parse_error(m_lexer.get_position(),
8881 m_lexer.get_token_string(),
8882 parse_error::create(101, m_lexer.get_position(),
8883 exception_message(token_type::literal_or_value,
"value")));
8889 skip_to_state_evaluation =
false;
8902 if (get_token() == token_type::value_separator)
8921 assert(
not states.empty());
8923 skip_to_state_evaluation =
true;
8927 return sax->parse_error(m_lexer.get_position(),
8928 m_lexer.get_token_string(),
8929 parse_error::create(101, m_lexer.get_position(),
8930 exception_message(token_type::end_array,
"array")));
8935 if (get_token() == token_type::value_separator)
8940 return sax->parse_error(m_lexer.get_position(),
8941 m_lexer.get_token_string(),
8942 parse_error::create(101, m_lexer.get_position(),
8943 exception_message(token_type::value_string,
"object key")));
8954 return sax->parse_error(m_lexer.get_position(),
8955 m_lexer.get_token_string(),
8956 parse_error::create(101, m_lexer.get_position(),
8957 exception_message(token_type::name_separator,
"object separator")));
8977 assert(
not states.empty());
8979 skip_to_state_evaluation =
true;
8983 return sax->parse_error(m_lexer.get_position(),
8984 m_lexer.get_token_string(),
8985 parse_error::create(101, m_lexer.get_position(),
8986 exception_message(token_type::end_object,
"object")));
8992 token_type get_token()
8994 return last_token = m_lexer.scan();
8997 std::string exception_message(
const token_type expected,
const std::string& context)
8999 std::string error_msg =
"syntax error ";
9001 if (
not context.empty())
9003 error_msg +=
"while parsing " + context +
" ";
9008 if (last_token == token_type::parse_error)
9010 error_msg += std::string(m_lexer.get_error_message()) +
"; last read: '" +
9011 m_lexer.get_token_string() +
"'";
9015 error_msg +=
"unexpected " + std::string(lexer_t::token_type_name(last_token));
9018 if (expected != token_type::uninitialized)
9020 error_msg +=
"; expected " + std::string(lexer_t::token_type_name(expected));
9028 const parser_callback_t callback =
nullptr;
9030 token_type last_token = token_type::uninitialized;
9034 const bool allow_exceptions =
true;
9061 class primitive_iterator_t
9064 using difference_type = std::ptrdiff_t;
9065 static constexpr difference_type begin_value = 0;
9066 static constexpr difference_type end_value = begin_value + 1;
9069 difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
9072 constexpr difference_type get_value()
const noexcept 9078 void set_begin()
noexcept 9084 void set_end()
noexcept 9090 constexpr bool is_begin()
const noexcept 9092 return m_it == begin_value;
9096 constexpr bool is_end()
const noexcept 9098 return m_it == end_value;
9101 friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs)
noexcept 9103 return lhs.m_it == rhs.m_it;
9106 friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs)
noexcept 9108 return lhs.m_it < rhs.m_it;
9111 primitive_iterator_t operator+(difference_type n)
noexcept 9113 auto result = *
this;
9118 friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs)
noexcept 9120 return lhs.m_it - rhs.m_it;
9123 primitive_iterator_t& operator++()
noexcept 9129 primitive_iterator_t
const operator++(
int)
noexcept 9131 auto result = *
this;
9136 primitive_iterator_t& operator--()
noexcept 9142 primitive_iterator_t
const operator--(
int)
noexcept 9144 auto result = *
this;
9149 primitive_iterator_t& operator+=(difference_type n)
noexcept 9155 primitive_iterator_t& operator-=(difference_type n)
noexcept 9175 template<
typename BasicJsonType>
struct internal_iterator
9178 typename BasicJsonType::object_t::iterator object_iterator {};
9180 typename BasicJsonType::array_t::iterator array_iterator {};
9182 primitive_iterator_t primitive_iterator {};
9192 #include <type_traits> 9214 template<
typename IteratorType>
class iteration_proxy;
9215 template<
typename IteratorType>
class iteration_proxy_value;
9233 template<
typename BasicJsonType>
9237 friend iter_impl<
typename std::conditional<std::is_const<BasicJsonType>::value,
typename std::remove_const<BasicJsonType>::type,
const BasicJsonType>::type>;
9238 friend BasicJsonType;
9239 friend iteration_proxy<iter_impl>;
9240 friend iteration_proxy_value<iter_impl>;
9242 using object_t =
typename BasicJsonType::object_t;
9243 using array_t =
typename BasicJsonType::array_t;
9245 static_assert(is_basic_json<
typename std::remove_const<BasicJsonType>::type>::value,
9246 "iter_impl only accepts (const) basic_json");
9255 using iterator_category = std::bidirectional_iterator_tag;
9258 using value_type =
typename BasicJsonType::value_type;
9260 using difference_type =
typename BasicJsonType::difference_type;
9262 using pointer =
typename std::conditional<std::is_const<BasicJsonType>::value,
9263 typename BasicJsonType::const_pointer,
9264 typename BasicJsonType::pointer>::type;
9267 typename std::conditional<std::is_const<BasicJsonType>::value,
9268 typename BasicJsonType::const_reference,
9269 typename BasicJsonType::reference>::type;
9272 iter_impl() =
default;
9280 explicit iter_impl(pointer object)
noexcept : m_object(object)
9282 assert(m_object !=
nullptr);
9284 switch (m_object->m_type)
9286 case value_t::object:
9288 m_it.object_iterator =
typename object_t::iterator();
9292 case value_t::array:
9294 m_it.array_iterator =
typename array_t::iterator();
9300 m_it.primitive_iterator = primitive_iterator_t();
9322 iter_impl(
const iter_impl<
const BasicJsonType>& other)
noexcept 9323 : m_object(other.m_object), m_it(other.m_it)
9332 iter_impl& operator=(
const iter_impl<
const BasicJsonType>& other)
noexcept 9334 m_object = other.m_object;
9344 iter_impl(
const iter_impl<
typename std::remove_const<BasicJsonType>::type>& other)
noexcept 9345 : m_object(other.m_object), m_it(other.m_it)
9354 iter_impl& operator=(
const iter_impl<
typename std::remove_const<BasicJsonType>::type>& other)
noexcept 9356 m_object = other.m_object;
9366 void set_begin()
noexcept 9368 assert(m_object !=
nullptr);
9370 switch (m_object->m_type)
9372 case value_t::object:
9374 m_it.object_iterator = m_object->m_value.object->begin();
9378 case value_t::array:
9380 m_it.array_iterator = m_object->m_value.array->begin();
9387 m_it.primitive_iterator.set_end();
9393 m_it.primitive_iterator.set_begin();
9403 void set_end()
noexcept 9405 assert(m_object !=
nullptr);
9407 switch (m_object->m_type)
9409 case value_t::object:
9411 m_it.object_iterator = m_object->m_value.object->end();
9415 case value_t::array:
9417 m_it.array_iterator = m_object->m_value.array->end();
9423 m_it.primitive_iterator.set_end();
9434 reference operator*()
const 9436 assert(m_object !=
nullptr);
9438 switch (m_object->m_type)
9440 case value_t::object:
9442 assert(m_it.object_iterator != m_object->m_value.object->end());
9443 return m_it.object_iterator->second;
9446 case value_t::array:
9448 assert(m_it.array_iterator != m_object->m_value.array->end());
9449 return *m_it.array_iterator;
9453 JSON_THROW(invalid_iterator::create(214,
"cannot get value"));
9462 JSON_THROW(invalid_iterator::create(214,
"cannot get value"));
9471 pointer operator->()
const 9473 assert(m_object !=
nullptr);
9475 switch (m_object->m_type)
9477 case value_t::object:
9479 assert(m_it.object_iterator != m_object->m_value.object->end());
9480 return &(m_it.object_iterator->second);
9483 case value_t::array:
9485 assert(m_it.array_iterator != m_object->m_value.array->end());
9486 return &*m_it.array_iterator;
9496 JSON_THROW(invalid_iterator::create(214,
"cannot get value"));
9505 iter_impl
const operator++(
int)
9507 auto result = *
this;
9516 iter_impl& operator++()
9518 assert(m_object !=
nullptr);
9520 switch (m_object->m_type)
9522 case value_t::object:
9524 std::advance(m_it.object_iterator, 1);
9528 case value_t::array:
9530 std::advance(m_it.array_iterator, 1);
9536 ++m_it.primitive_iterator;
9548 iter_impl
const operator--(
int)
9550 auto result = *
this;
9559 iter_impl& operator--()
9561 assert(m_object !=
nullptr);
9563 switch (m_object->m_type)
9565 case value_t::object:
9567 std::advance(m_it.object_iterator, -1);
9571 case value_t::array:
9573 std::advance(m_it.array_iterator, -1);
9579 --m_it.primitive_iterator;
9591 bool operator==(
const iter_impl& other)
const 9596 JSON_THROW(invalid_iterator::create(212,
"cannot compare iterators of different containers"));
9599 assert(m_object !=
nullptr);
9601 switch (m_object->m_type)
9603 case value_t::object:
9604 return (m_it.object_iterator == other.m_it.object_iterator);
9606 case value_t::array:
9607 return (m_it.array_iterator == other.m_it.array_iterator);
9610 return (m_it.primitive_iterator == other.m_it.primitive_iterator);
9618 bool operator!=(
const iter_impl& other)
const 9620 return not operator==(other);
9627 bool operator<(
const iter_impl& other)
const 9632 JSON_THROW(invalid_iterator::create(212,
"cannot compare iterators of different containers"));
9635 assert(m_object !=
nullptr);
9637 switch (m_object->m_type)
9639 case value_t::object:
9640 JSON_THROW(invalid_iterator::create(213,
"cannot compare order of object iterators"));
9642 case value_t::array:
9643 return (m_it.array_iterator < other.m_it.array_iterator);
9646 return (m_it.primitive_iterator < other.m_it.primitive_iterator);
9654 bool operator<=(
const iter_impl& other)
const 9656 return not other.operator < (*
this);
9663 bool operator>(
const iter_impl& other)
const 9665 return not operator<=(other);
9672 bool operator>=(
const iter_impl& other)
const 9674 return not operator<(other);
9681 iter_impl& operator+=(difference_type i)
9683 assert(m_object !=
nullptr);
9685 switch (m_object->m_type)
9687 case value_t::object:
9688 JSON_THROW(invalid_iterator::create(209,
"cannot use offsets with object iterators"));
9690 case value_t::array:
9692 std::advance(m_it.array_iterator, i);
9698 m_it.primitive_iterator += i;
9710 iter_impl& operator-=(difference_type i)
9712 return operator+=(-i);
9719 iter_impl operator+(difference_type i)
const 9721 auto result = *
this;
9730 friend iter_impl operator+(difference_type i,
const iter_impl& it)
9741 iter_impl operator-(difference_type i)
const 9743 auto result = *
this;
9752 difference_type operator-(
const iter_impl& other)
const 9754 assert(m_object !=
nullptr);
9756 switch (m_object->m_type)
9758 case value_t::object:
9759 JSON_THROW(invalid_iterator::create(209,
"cannot use offsets with object iterators"));
9761 case value_t::array:
9762 return m_it.array_iterator - other.m_it.array_iterator;
9765 return m_it.primitive_iterator - other.m_it.primitive_iterator;
9773 reference operator[](difference_type n)
const 9775 assert(m_object !=
nullptr);
9777 switch (m_object->m_type)
9779 case value_t::object:
9780 JSON_THROW(invalid_iterator::create(208,
"cannot use operator[] for object iterators"));
9782 case value_t::array:
9783 return *std::next(m_it.array_iterator, n);
9786 JSON_THROW(invalid_iterator::create(214,
"cannot get value"));
9795 JSON_THROW(invalid_iterator::create(214,
"cannot get value"));
9804 const typename object_t::key_type& key()
const 9806 assert(m_object !=
nullptr);
9810 return m_it.object_iterator->first;
9813 JSON_THROW(invalid_iterator::create(207,
"cannot use key() for non-object iterators"));
9820 reference value()
const 9827 pointer m_object =
nullptr;
9829 internal_iterator<
typename std::remove_const<BasicJsonType>::type> m_it {};
9869 template<
typename Base>
9870 class json_reverse_iterator :
public std::reverse_iterator<Base>
9873 using difference_type = std::ptrdiff_t;
9875 using base_iterator = std::reverse_iterator<Base>;
9877 using reference =
typename Base::reference;
9880 explicit json_reverse_iterator(
const typename base_iterator::iterator_type& it)
noexcept 9881 : base_iterator(it) {}
9884 explicit json_reverse_iterator(
const base_iterator& it)
noexcept : base_iterator(it) {}
9887 json_reverse_iterator
const operator++(
int)
9889 return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
9893 json_reverse_iterator& operator++()
9895 return static_cast<json_reverse_iterator&>(base_iterator::operator++());
9899 json_reverse_iterator
const operator--(
int)
9901 return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
9905 json_reverse_iterator& operator--()
9907 return static_cast<json_reverse_iterator&>(base_iterator::operator--());
9911 json_reverse_iterator& operator+=(difference_type i)
9913 return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
9917 json_reverse_iterator operator+(difference_type i)
const 9919 return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
9923 json_reverse_iterator operator-(difference_type i)
const 9925 return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
9929 difference_type operator-(
const json_reverse_iterator& other)
const 9931 return base_iterator(*
this) - base_iterator(other);
9935 reference operator[](difference_type n)
const 9937 return *(
this->operator+(n));
9941 auto key()
const ->
decltype(std::declval<Base>().key())
9943 auto it = --
this->base();
9948 reference value()
const 9950 auto it = --
this->base();
9951 return it.operator * ();
9962 #include <algorithm> 9979 template<
typename BasicJsonType>
10028 return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
10030 [](
const std::string & a,
const std::string & b)
10032 return a +
"/" + escape(b);
10417 [](
const unsigned char x)
10444 "' must not begin with '0'"));
10503 ") is out of range"));
10511 "' must not begin with '0'"));
10568 ") is out of range"));
10576 "' must not begin with '0'"));
10627 ") is out of range"));
10635 "' must not begin with '0'"));
10694 "' must not begin with '0'"));
10752 "JSON pointer must be empty or begin with '/' - was: '" +
10967 return not (
lhs ==
rhs);
10978 #include <initializer_list> 10988 template<
typename BasicJsonType>
10992 using value_type = BasicJsonType;
10994 json_ref(value_type&& value)
10995 : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(
true)
10998 json_ref(
const value_type& value)
10999 : value_ref(
const_cast<value_type*>(&value)), is_rvalue(
false)
11002 json_ref(std::initializer_list<json_ref> init)
11003 : owned_value(init), value_ref(&owned_value), is_rvalue(
true)
11008 enable_if_t<std::is_constructible<value_type, Args...>::value,
int> = 0 >
11009 json_ref(Args && ... args)
11010 : owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
11014 json_ref(json_ref&&) =
default;
11015 json_ref(
const json_ref&) =
delete;
11016 json_ref& operator=(
const json_ref&) =
delete;
11017 json_ref& operator=(json_ref&&) =
delete;
11018 ~json_ref() =
default;
11020 value_type moved_or_copied()
const 11024 return std::move(*value_ref);
11029 value_type
const& operator*()
const 11031 return *
static_cast<value_type
const*>(value_ref);
11034 value_type
const* operator->()
const 11036 return static_cast<value_type
const*>(value_ref);
11040 mutable value_type owned_value =
nullptr;
11041 value_type* value_ref =
nullptr;
11042 const bool is_rvalue;
11056 #include <algorithm> 11070 #include <algorithm> 11073 #include <iterator> 11086 template<
typename CharType>
struct output_adapter_protocol
11088 virtual void write_character(CharType c) = 0;
11089 virtual void write_characters(
const CharType* s, std::size_t length) = 0;
11090 virtual ~output_adapter_protocol() =
default;
11094 template<
typename CharType>
11095 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
11098 template<
typename CharType>
11099 class output_vector_adapter :
public output_adapter_protocol<CharType>
11102 explicit output_vector_adapter(std::vector<CharType>& vec)
noexcept 11106 void write_character(CharType c)
override 11112 void write_characters(
const CharType* s, std::size_t length)
override 11114 std::copy(s, s + length, std::back_inserter(v));
11118 std::vector<CharType>& v;
11122 template<
typename CharType>
11123 class output_stream_adapter :
public output_adapter_protocol<CharType>
11126 explicit output_stream_adapter(std::basic_ostream<CharType>& s)
noexcept 11130 void write_character(CharType c)
override 11136 void write_characters(
const CharType* s, std::size_t length)
override 11138 stream.write(s,
static_cast<std::streamsize>(length));
11142 std::basic_ostream<CharType>& stream;
11146 template<
typename CharType,
typename StringType = std::basic_string<CharType>>
11147 class output_string_adapter :
public output_adapter_protocol<CharType>
11150 explicit output_string_adapter(StringType& s)
noexcept 11154 void write_character(CharType c)
override 11160 void write_characters(
const CharType* s, std::size_t length)
override 11162 str.append(s, length);
11169 template<
typename CharType,
typename StringType = std::basic_string<CharType>>
11170 class output_adapter
11173 output_adapter(std::vector<CharType>& vec)
11174 : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
11176 output_adapter(std::basic_ostream<CharType>& s)
11177 : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
11179 output_adapter(StringType& s)
11180 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
11182 operator output_adapter_t<CharType>()
11188 output_adapter_t<CharType> oa =
nullptr;
11205 template<
typename BasicJsonType,
typename CharType>
11206 class binary_writer
11208 using string_t =
typename BasicJsonType::string_t;
11216 explicit binary_writer(output_adapter_t<CharType> adapter) : oa(adapter)
11225 void write_bson(
const BasicJsonType& j)
11229 case value_t::object:
11231 write_bson_object(*j.m_value.object);
11237 JSON_THROW(type_error::create(317,
"to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name())));
11245 void write_cbor(
const BasicJsonType& j)
11249 case value_t::null:
11251 oa->write_character(to_char_type(0xF6));
11255 case value_t::boolean:
11257 oa->write_character(j.m_value.boolean
11258 ? to_char_type(0xF5)
11259 : to_char_type(0xF4));
11263 case value_t::number_integer:
11265 if (j.m_value.number_integer >= 0)
11270 if (j.m_value.number_integer <= 0x17)
11272 write_number(
static_cast<std::uint8_t>(j.m_value.number_integer));
11274 else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
11276 oa->write_character(to_char_type(0x18));
11277 write_number(
static_cast<std::uint8_t>(j.m_value.number_integer));
11279 else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
11281 oa->write_character(to_char_type(0x19));
11282 write_number(
static_cast<std::uint16_t>(j.m_value.number_integer));
11284 else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
11286 oa->write_character(to_char_type(0x1A));
11287 write_number(
static_cast<std::uint32_t>(j.m_value.number_integer));
11291 oa->write_character(to_char_type(0x1B));
11292 write_number(
static_cast<std::uint64_t>(j.m_value.number_integer));
11299 const auto positive_number = -1 - j.m_value.number_integer;
11300 if (j.m_value.number_integer >= -24)
11302 write_number(
static_cast<std::uint8_t>(0x20 + positive_number));
11304 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
11306 oa->write_character(to_char_type(0x38));
11307 write_number(
static_cast<std::uint8_t>(positive_number));
11309 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
11311 oa->write_character(to_char_type(0x39));
11312 write_number(
static_cast<std::uint16_t>(positive_number));
11314 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
11316 oa->write_character(to_char_type(0x3A));
11317 write_number(
static_cast<std::uint32_t>(positive_number));
11321 oa->write_character(to_char_type(0x3B));
11322 write_number(
static_cast<std::uint64_t>(positive_number));
11328 case value_t::number_unsigned:
11330 if (j.m_value.number_unsigned <= 0x17)
11332 write_number(
static_cast<std::uint8_t>(j.m_value.number_unsigned));
11334 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
11336 oa->write_character(to_char_type(0x18));
11337 write_number(
static_cast<std::uint8_t>(j.m_value.number_unsigned));
11339 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
11341 oa->write_character(to_char_type(0x19));
11342 write_number(
static_cast<std::uint16_t>(j.m_value.number_unsigned));
11344 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
11346 oa->write_character(to_char_type(0x1A));
11347 write_number(
static_cast<std::uint32_t>(j.m_value.number_unsigned));
11351 oa->write_character(to_char_type(0x1B));
11352 write_number(
static_cast<std::uint64_t>(j.m_value.number_unsigned));
11357 case value_t::number_float:
11359 oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
11360 write_number(j.m_value.number_float);
11364 case value_t::string:
11367 const auto N = j.m_value.string->size();
11370 write_number(
static_cast<std::uint8_t>(0x60 + N));
11372 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11374 oa->write_character(to_char_type(0x78));
11375 write_number(
static_cast<std::uint8_t>(N));
11377 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
11379 oa->write_character(to_char_type(0x79));
11380 write_number(
static_cast<std::uint16_t>(N));
11382 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
11384 oa->write_character(to_char_type(0x7A));
11385 write_number(
static_cast<std::uint32_t>(N));
11388 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
11390 oa->write_character(to_char_type(0x7B));
11391 write_number(
static_cast<std::uint64_t>(N));
11396 oa->write_characters(
11397 reinterpret_cast<
const CharType*>(j.m_value.string->c_str()),
11398 j.m_value.string->size());
11402 case value_t::array:
11405 const auto N = j.m_value.array->size();
11408 write_number(
static_cast<std::uint8_t>(0x80 + N));
11410 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11412 oa->write_character(to_char_type(0x98));
11413 write_number(
static_cast<std::uint8_t>(N));
11415 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
11417 oa->write_character(to_char_type(0x99));
11418 write_number(
static_cast<std::uint16_t>(N));
11420 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
11422 oa->write_character(to_char_type(0x9A));
11423 write_number(
static_cast<std::uint32_t>(N));
11426 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
11428 oa->write_character(to_char_type(0x9B));
11429 write_number(
static_cast<std::uint64_t>(N));
11434 for (
const auto& el : *j.m_value.array)
11441 case value_t::object:
11444 const auto N = j.m_value.object->size();
11447 write_number(
static_cast<std::uint8_t>(0xA0 + N));
11449 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11451 oa->write_character(to_char_type(0xB8));
11452 write_number(
static_cast<std::uint8_t>(N));
11454 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
11456 oa->write_character(to_char_type(0xB9));
11457 write_number(
static_cast<std::uint16_t>(N));
11459 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
11461 oa->write_character(to_char_type(0xBA));
11462 write_number(
static_cast<std::uint32_t>(N));
11465 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
11467 oa->write_character(to_char_type(0xBB));
11468 write_number(
static_cast<std::uint64_t>(N));
11473 for (
const auto& el : *j.m_value.object)
11475 write_cbor(el.first);
11476 write_cbor(el.second);
11489 void write_msgpack(
const BasicJsonType& j)
11493 case value_t::null:
11495 oa->write_character(to_char_type(0xC0));
11499 case value_t::boolean:
11501 oa->write_character(j.m_value.boolean
11502 ? to_char_type(0xC3)
11503 : to_char_type(0xC2));
11507 case value_t::number_integer:
11509 if (j.m_value.number_integer >= 0)
11514 if (j.m_value.number_unsigned < 128)
11517 write_number(
static_cast<std::uint8_t>(j.m_value.number_integer));
11519 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
11522 oa->write_character(to_char_type(0xCC));
11523 write_number(
static_cast<std::uint8_t>(j.m_value.number_integer));
11525 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
11528 oa->write_character(to_char_type(0xCD));
11529 write_number(
static_cast<std::uint16_t>(j.m_value.number_integer));
11531 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
11534 oa->write_character(to_char_type(0xCE));
11535 write_number(
static_cast<std::uint32_t>(j.m_value.number_integer));
11537 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
11540 oa->write_character(to_char_type(0xCF));
11541 write_number(
static_cast<std::uint64_t>(j.m_value.number_integer));
11546 if (j.m_value.number_integer >= -32)
11549 write_number(
static_cast<std::int8_t>(j.m_value.number_integer));
11551 else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)()
and 11552 j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
11555 oa->write_character(to_char_type(0xD0));
11556 write_number(
static_cast<std::int8_t>(j.m_value.number_integer));
11558 else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)()
and 11559 j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
11562 oa->write_character(to_char_type(0xD1));
11563 write_number(
static_cast<std::int16_t>(j.m_value.number_integer));
11565 else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)()
and 11566 j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
11569 oa->write_character(to_char_type(0xD2));
11570 write_number(
static_cast<std::int32_t>(j.m_value.number_integer));
11572 else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)()
and 11573 j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
11576 oa->write_character(to_char_type(0xD3));
11577 write_number(
static_cast<std::int64_t>(j.m_value.number_integer));
11583 case value_t::number_unsigned:
11585 if (j.m_value.number_unsigned < 128)
11588 write_number(
static_cast<std::uint8_t>(j.m_value.number_integer));
11590 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
11593 oa->write_character(to_char_type(0xCC));
11594 write_number(
static_cast<std::uint8_t>(j.m_value.number_integer));
11596 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
11599 oa->write_character(to_char_type(0xCD));
11600 write_number(
static_cast<std::uint16_t>(j.m_value.number_integer));
11602 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
11605 oa->write_character(to_char_type(0xCE));
11606 write_number(
static_cast<std::uint32_t>(j.m_value.number_integer));
11608 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
11611 oa->write_character(to_char_type(0xCF));
11612 write_number(
static_cast<std::uint64_t>(j.m_value.number_integer));
11617 case value_t::number_float:
11619 oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
11620 write_number(j.m_value.number_float);
11624 case value_t::string:
11627 const auto N = j.m_value.string->size();
11631 write_number(
static_cast<std::uint8_t>(0xA0 | N));
11633 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11636 oa->write_character(to_char_type(0xD9));
11637 write_number(
static_cast<std::uint8_t>(N));
11639 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
11642 oa->write_character(to_char_type(0xDA));
11643 write_number(
static_cast<std::uint16_t>(N));
11645 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
11648 oa->write_character(to_char_type(0xDB));
11649 write_number(
static_cast<std::uint32_t>(N));
11653 oa->write_characters(
11654 reinterpret_cast<
const CharType*>(j.m_value.string->c_str()),
11655 j.m_value.string->size());
11659 case value_t::array:
11662 const auto N = j.m_value.array->size();
11666 write_number(
static_cast<std::uint8_t>(0x90 | N));
11668 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
11671 oa->write_character(to_char_type(0xDC));
11672 write_number(
static_cast<std::uint16_t>(N));
11674 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
11677 oa->write_character(to_char_type(0xDD));
11678 write_number(
static_cast<std::uint32_t>(N));
11682 for (
const auto& el : *j.m_value.array)
11689 case value_t::object:
11692 const auto N = j.m_value.object->size();
11696 write_number(
static_cast<std::uint8_t>(0x80 | (N & 0xF)));
11698 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
11701 oa->write_character(to_char_type(0xDE));
11702 write_number(
static_cast<std::uint16_t>(N));
11704 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
11707 oa->write_character(to_char_type(0xDF));
11708 write_number(
static_cast<std::uint32_t>(N));
11712 for (
const auto& el : *j.m_value.object)
11714 write_msgpack(el.first);
11715 write_msgpack(el.second);
11731 void write_ubjson(
const BasicJsonType& j,
const bool use_count,
11732 const bool use_type,
const bool add_prefix =
true)
11736 case value_t::null:
11740 oa->write_character(to_char_type(
'Z'));
11745 case value_t::boolean:
11749 oa->write_character(j.m_value.boolean
11750 ? to_char_type(
'T')
11751 : to_char_type(
'F'));
11756 case value_t::number_integer:
11758 write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
11762 case value_t::number_unsigned:
11764 write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
11768 case value_t::number_float:
11770 write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
11774 case value_t::string:
11778 oa->write_character(to_char_type(
'S'));
11780 write_number_with_ubjson_prefix(j.m_value.string->size(),
true);
11781 oa->write_characters(
11782 reinterpret_cast<
const CharType*>(j.m_value.string->c_str()),
11783 j.m_value.string->size());
11787 case value_t::array:
11791 oa->write_character(to_char_type(
'['));
11794 bool prefix_required =
true;
11795 if (use_type
and not j.m_value.array->empty())
11798 const CharType first_prefix = ubjson_prefix(j.front());
11799 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
11800 [
this, first_prefix](
const BasicJsonType & v)
11802 return ubjson_prefix(v) == first_prefix;
11807 prefix_required =
false;
11808 oa->write_character(to_char_type(
'$'));
11809 oa->write_character(first_prefix);
11815 oa->write_character(to_char_type(
'#'));
11816 write_number_with_ubjson_prefix(j.m_value.array->size(),
true);
11819 for (
const auto& el : *j.m_value.array)
11821 write_ubjson(el, use_count, use_type, prefix_required);
11826 oa->write_character(to_char_type(
']'));
11832 case value_t::object:
11836 oa->write_character(to_char_type(
'{'));
11839 bool prefix_required =
true;
11840 if (use_type
and not j.m_value.object->empty())
11843 const CharType first_prefix = ubjson_prefix(j.front());
11844 const bool same_prefix = std::all_of(j.begin(), j.end(),
11845 [
this, first_prefix](
const BasicJsonType & v)
11847 return ubjson_prefix(v) == first_prefix;
11852 prefix_required =
false;
11853 oa->write_character(to_char_type(
'$'));
11854 oa->write_character(first_prefix);
11860 oa->write_character(to_char_type(
'#'));
11861 write_number_with_ubjson_prefix(j.m_value.object->size(),
true);
11864 for (
const auto& el : *j.m_value.object)
11866 write_number_with_ubjson_prefix(el.first.size(),
true);
11867 oa->write_characters(
11868 reinterpret_cast<
const CharType*>(el.first.c_str()),
11870 write_ubjson(el.second, use_count, use_type, prefix_required);
11875 oa->write_character(to_char_type(
'}'));
11895 static std::size_t calc_bson_entry_header_size(
const string_t& name)
11897 const auto it = name.find(
static_cast<
typename string_t::value_type>(0));
11901 "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) +
")"));
11904 return 1ul + name.size() + 1u;
11910 void write_bson_entry_header(
const string_t& name,
11911 const std::uint8_t element_type)
11913 oa->write_character(to_char_type(element_type));
11914 oa->write_characters(
11915 reinterpret_cast<
const CharType*>(name.c_str()),
11922 void write_bson_boolean(
const string_t& name,
11925 write_bson_entry_header(name, 0x08);
11926 oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
11932 void write_bson_double(
const string_t& name,
11933 const double value)
11935 write_bson_entry_header(name, 0x01);
11936 write_number<
double,
true>(value);
11942 static std::size_t calc_bson_string_size(
const string_t& value)
11944 return sizeof(std::int32_t) + value.size() + 1ul;
11950 void write_bson_string(
const string_t& name,
11951 const string_t& value)
11953 write_bson_entry_header(name, 0x02);
11955 write_number<std::int32_t,
true>(
static_cast<std::int32_t>(value.size() + 1ul));
11956 oa->write_characters(
11957 reinterpret_cast<
const CharType*>(value.c_str()),
11964 void write_bson_null(
const string_t& name)
11966 write_bson_entry_header(name, 0x0A);
11972 static std::size_t calc_bson_integer_size(
const std::int64_t value)
11974 return (std::numeric_limits<std::int32_t>::min)() <= value
and value <= (std::numeric_limits<std::int32_t>::max)()
11975 ?
sizeof(std::int32_t)
11976 :
sizeof(std::int64_t);
11982 void write_bson_integer(
const string_t& name,
11983 const std::int64_t value)
11985 if ((std::numeric_limits<std::int32_t>::min)() <= value
and value <= (std::numeric_limits<std::int32_t>::max)())
11987 write_bson_entry_header(name, 0x10);
11988 write_number<std::int32_t,
true>(
static_cast<std::int32_t>(value));
11992 write_bson_entry_header(name, 0x12);
11993 write_number<std::int64_t,
true>(
static_cast<std::int64_t>(value));
12000 static constexpr std::size_t calc_bson_unsigned_size(
const std::uint64_t value)
noexcept 12002 return (value <=
static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12003 ?
sizeof(std::int32_t)
12004 :
sizeof(std::int64_t);
12010 void write_bson_unsigned(
const string_t& name,
12011 const std::uint64_t value)
12013 if (value <=
static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12015 write_bson_entry_header(name, 0x10 );
12016 write_number<std::int32_t,
true>(
static_cast<std::int32_t>(value));
12018 else if (value <=
static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
12020 write_bson_entry_header(name, 0x12 );
12021 write_number<std::int64_t,
true>(
static_cast<std::int64_t>(value));
12025 JSON_THROW(out_of_range::create(407,
"integer number " + std::to_string(value) +
" cannot be represented by BSON as it does not fit int64"));
12032 void write_bson_object_entry(
const string_t& name,
12033 const typename BasicJsonType::object_t& value)
12035 write_bson_entry_header(name, 0x03);
12036 write_bson_object(value);
12042 static std::size_t calc_bson_array_size(
const typename BasicJsonType::array_t& value)
12044 std::size_t embedded_document_size = 0ul;
12045 std::size_t array_index = 0ul;
12047 for (
const auto& el : value)
12049 embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el);
12052 return sizeof(std::int32_t) + embedded_document_size + 1ul;
12058 void write_bson_array(
const string_t& name,
12059 const typename BasicJsonType::array_t& value)
12061 write_bson_entry_header(name, 0x04);
12062 write_number<std::int32_t,
true>(
static_cast<std::int32_t>(calc_bson_array_size(value)));
12064 std::size_t array_index = 0ul;
12066 for (
const auto& el : value)
12068 write_bson_element(std::to_string(array_index++), el);
12071 oa->write_character(to_char_type(0x00));
12078 static std::size_t calc_bson_element_size(
const string_t& name,
12079 const BasicJsonType& j)
12081 const auto header_size = calc_bson_entry_header_size(name);
12084 case value_t::object:
12085 return header_size + calc_bson_object_size(*j.m_value.object);
12087 case value_t::array:
12088 return header_size + calc_bson_array_size(*j.m_value.array);
12090 case value_t::boolean:
12091 return header_size + 1ul;
12093 case value_t::number_float:
12094 return header_size + 8ul;
12096 case value_t::number_integer:
12097 return header_size + calc_bson_integer_size(j.m_value.number_integer);
12099 case value_t::number_unsigned:
12100 return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
12102 case value_t::string:
12103 return header_size + calc_bson_string_size(*j.m_value.string);
12105 case value_t::null:
12106 return header_size + 0ul;
12123 void write_bson_element(
const string_t& name,
12124 const BasicJsonType& j)
12128 case value_t::object:
12129 return write_bson_object_entry(name, *j.m_value.object);
12131 case value_t::array:
12132 return write_bson_array(name, *j.m_value.array);
12134 case value_t::boolean:
12135 return write_bson_boolean(name, j.m_value.boolean);
12137 case value_t::number_float:
12138 return write_bson_double(name, j.m_value.number_float);
12140 case value_t::number_integer:
12141 return write_bson_integer(name, j.m_value.number_integer);
12143 case value_t::number_unsigned:
12144 return write_bson_unsigned(name, j.m_value.number_unsigned);
12146 case value_t::string:
12147 return write_bson_string(name, *j.m_value.string);
12149 case value_t::null:
12150 return write_bson_null(name);
12166 static std::size_t calc_bson_object_size(
const typename BasicJsonType::object_t& value)
12168 std::size_t document_size = std::accumulate(value.begin(), value.end(), 0ul,
12169 [](size_t result,
const typename BasicJsonType::object_t::value_type & el)
12171 return result += calc_bson_element_size(el.first, el.second);
12174 return sizeof(std::int32_t) + document_size + 1ul;
12181 void write_bson_object(
const typename BasicJsonType::object_t& value)
12183 write_number<std::int32_t,
true>(
static_cast<std::int32_t>(calc_bson_object_size(value)));
12185 for (
const auto& el : value)
12187 write_bson_element(el.first, el.second);
12190 oa->write_character(to_char_type(0x00));
12197 static constexpr CharType get_cbor_float_prefix(
float )
12199 return to_char_type(0xFA);
12202 static constexpr CharType get_cbor_float_prefix(
double )
12204 return to_char_type(0xFB);
12211 static constexpr CharType get_msgpack_float_prefix(
float )
12213 return to_char_type(0xCA);
12216 static constexpr CharType get_msgpack_float_prefix(
double )
12218 return to_char_type(0xCB);
12226 template<
typename NumberType,
typename std::enable_if<
12227 std::is_floating_point<NumberType>::value,
int>::type = 0>
12228 void write_number_with_ubjson_prefix(
const NumberType n,
12229 const bool add_prefix)
12233 oa->write_character(get_ubjson_float_prefix(n));
12239 template<
typename NumberType,
typename std::enable_if<
12240 std::is_unsigned<NumberType>::value,
int>::type = 0>
12241 void write_number_with_ubjson_prefix(
const NumberType n,
12242 const bool add_prefix)
12244 if (n <=
static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
12248 oa->write_character(to_char_type(
'i'));
12250 write_number(
static_cast<std::uint8_t>(n));
12252 else if (n <= (std::numeric_limits<std::uint8_t>::max)())
12256 oa->write_character(to_char_type(
'U'));
12258 write_number(
static_cast<std::uint8_t>(n));
12260 else if (n <=
static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
12264 oa->write_character(to_char_type(
'I'));
12266 write_number(
static_cast<std::int16_t>(n));
12268 else if (n <=
static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12272 oa->write_character(to_char_type(
'l'));
12274 write_number(
static_cast<std::int32_t>(n));
12276 else if (n <=
static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
12280 oa->write_character(to_char_type(
'L'));
12282 write_number(
static_cast<std::int64_t>(n));
12286 JSON_THROW(out_of_range::create(407,
"integer number " + std::to_string(n) +
" cannot be represented by UBJSON as it does not fit int64"));
12291 template<
typename NumberType,
typename std::enable_if<
12292 std::is_signed<NumberType>::value
and 12293 not std::is_floating_point<NumberType>::value,
int>::type = 0>
12294 void write_number_with_ubjson_prefix(
const NumberType n,
12295 const bool add_prefix)
12297 if ((std::numeric_limits<std::int8_t>::min)() <= n
and n <= (std::numeric_limits<std::int8_t>::max)())
12301 oa->write_character(to_char_type(
'i'));
12303 write_number(
static_cast<std::int8_t>(n));
12305 else if (
static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::min)()) <= n
and n <=
static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::max)()))
12309 oa->write_character(to_char_type(
'U'));
12311 write_number(
static_cast<std::uint8_t>(n));
12313 else if ((std::numeric_limits<std::int16_t>::min)() <= n
and n <= (std::numeric_limits<std::int16_t>::max)())
12317 oa->write_character(to_char_type(
'I'));
12319 write_number(
static_cast<std::int16_t>(n));
12321 else if ((std::numeric_limits<std::int32_t>::min)() <= n
and n <= (std::numeric_limits<std::int32_t>::max)())
12325 oa->write_character(to_char_type(
'l'));
12327 write_number(
static_cast<std::int32_t>(n));
12329 else if ((std::numeric_limits<std::int64_t>::min)() <= n
and n <= (std::numeric_limits<std::int64_t>::max)())
12333 oa->write_character(to_char_type(
'L'));
12335 write_number(
static_cast<std::int64_t>(n));
12340 JSON_THROW(out_of_range::create(407,
"integer number " + std::to_string(n) +
" cannot be represented by UBJSON as it does not fit int64"));
12354 CharType ubjson_prefix(
const BasicJsonType& j)
const noexcept 12358 case value_t::null:
12361 case value_t::boolean:
12362 return j.m_value.boolean ?
'T' :
'F';
12364 case value_t::number_integer:
12366 if ((std::numeric_limits<std::int8_t>::min)() <= j.m_value.number_integer
and j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
12370 if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_value.number_integer
and j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
12374 if ((std::numeric_limits<std::int16_t>::min)() <= j.m_value.number_integer
and j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
12378 if ((std::numeric_limits<std::int32_t>::min)() <= j.m_value.number_integer
and j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
12386 case value_t::number_unsigned:
12388 if (j.m_value.number_unsigned <=
static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
12392 if (j.m_value.number_unsigned <=
static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
12396 if (j.m_value.number_unsigned <=
static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
12400 if (j.m_value.number_unsigned <=
static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12408 case value_t::number_float:
12409 return get_ubjson_float_prefix(j.m_value.number_float);
12411 case value_t::string:
12414 case value_t::array:
12417 case value_t::object:
12425 static constexpr CharType get_ubjson_float_prefix(
float )
12430 static constexpr CharType get_ubjson_float_prefix(
double )
12450 template<
typename NumberType,
bool OutputIsLittleEndian =
false>
12451 void write_number(
const NumberType n)
12454 std::array<CharType,
sizeof(NumberType)> vec;
12455 std::memcpy(vec.data(), &n,
sizeof(NumberType));
12458 if (is_little_endian != OutputIsLittleEndian)
12461 std::reverse(vec.begin(), vec.end());
12464 oa->write_characters(vec.data(),
sizeof(NumberType));
12472 template <
typename C = CharType,
12473 enable_if_t < std::is_signed<C>::value
and std::is_signed<
char>::value > * =
nullptr >
12474 static constexpr CharType to_char_type(std::uint8_t x)
noexcept 12476 return *
reinterpret_cast<
char*>(&x);
12479 template <
typename C = CharType,
12480 enable_if_t < std::is_signed<C>::value
and std::is_unsigned<
char>::value > * =
nullptr >
12481 static CharType to_char_type(std::uint8_t x)
noexcept 12483 static_assert(
sizeof(std::uint8_t) ==
sizeof(CharType),
"size of CharType must be equal to std::uint8_t");
12484 static_assert(std::is_pod<CharType>::value,
"CharType must be POD");
12486 std::memcpy(&result, &x,
sizeof(x));
12490 template<
typename C = CharType,
12491 enable_if_t<std::is_unsigned<C>::value>* =
nullptr>
12492 static constexpr CharType to_char_type(std::uint8_t x)
noexcept 12497 template <
typename InputCharType,
typename C = CharType,
12499 std::is_signed<C>::value
and 12500 std::is_signed<
char>::value
and 12501 std::is_same<
char,
typename std::remove_cv<InputCharType>::type>::value
12503 static constexpr CharType to_char_type(InputCharType x)
noexcept 12510 const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
12513 output_adapter_t<CharType> oa =
nullptr;
12523 #include <algorithm> 12534 #include <type_traits> 12547 #include <type_traits> 12575 namespace dtoa_impl
12578 template <
typename Target,
typename Source>
12579 Target reinterpret_bits(
const Source source)
12581 static_assert(
sizeof(Target) ==
sizeof(Source),
"size mismatch");
12584 std::memcpy(&target, &source,
sizeof(Source));
12590 static constexpr int kPrecision = 64;
12592 std::uint64_t f = 0;
12595 constexpr diyfp(std::uint64_t f_,
int e_)
noexcept : f(f_), e(e_) {}
12601 static diyfp sub(
const diyfp& x,
const diyfp& y)
noexcept 12603 assert(x.e == y.e);
12604 assert(x.f >= y.f);
12606 return {x.f - y.f, x.e};
12613 static diyfp mul(
const diyfp& x,
const diyfp& y)
noexcept 12615 static_assert(kPrecision == 64,
"internal error");
12640 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
12641 const std::uint64_t u_hi = x.f >> 32u;
12642 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
12643 const std::uint64_t v_hi = y.f >> 32u;
12645 const std::uint64_t p0 = u_lo * v_lo;
12646 const std::uint64_t p1 = u_lo * v_hi;
12647 const std::uint64_t p2 = u_hi * v_lo;
12648 const std::uint64_t p3 = u_hi * v_hi;
12650 const std::uint64_t p0_hi = p0 >> 32u;
12651 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
12652 const std::uint64_t p1_hi = p1 >> 32u;
12653 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
12654 const std::uint64_t p2_hi = p2 >> 32u;
12656 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
12667 Q += std::uint64_t{1} << (64u - 32u - 1u);
12669 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
12671 return {h, x.e + y.e + 64};
12678 static diyfp normalize(diyfp x)
noexcept 12682 while ((x.f >> 63u) == 0)
12695 static diyfp normalize_to(
const diyfp& x,
const int target_exponent)
noexcept 12697 const int delta = x.e - target_exponent;
12699 assert(delta >= 0);
12700 assert(((x.f << delta) >> delta) == x.f);
12702 return {x.f << delta, target_exponent};
12719 template <
typename FloatType>
12720 boundaries compute_boundaries(FloatType value)
12722 assert(std::isfinite(value));
12732 static_assert(std::numeric_limits<FloatType>::is_iec559,
12733 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
12735 constexpr int kPrecision = std::numeric_limits<FloatType>::digits;
12736 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
12737 constexpr int kMinExp = 1 - kBias;
12738 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1);
12740 using bits_type =
typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
12742 const std::uint64_t bits = reinterpret_bits<bits_type>(value);
12743 const std::uint64_t E = bits >> (kPrecision - 1);
12744 const std::uint64_t F = bits & (kHiddenBit - 1);
12746 const bool is_denormal = E == 0;
12747 const diyfp v = is_denormal
12748 ? diyfp(F, kMinExp)
12749 : diyfp(F + kHiddenBit,
static_cast<
int>(E) - kBias);
12772 const bool lower_boundary_is_closer = F == 0
and E > 1;
12773 const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
12774 const diyfp m_minus = lower_boundary_is_closer
12775 ? diyfp(4 * v.f - 1, v.e - 2)
12776 : diyfp(2 * v.f - 1, v.e - 1);
12779 const diyfp w_plus = diyfp::normalize(m_plus);
12782 const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
12784 return {diyfp::normalize(v), w_minus, w_plus};
12842 constexpr int kAlpha = -60;
12843 constexpr int kGamma = -32;
12845 struct cached_power
12859 inline cached_power get_cached_power_for_binary_exponent(
int e)
12911 constexpr int kCachedPowersMinDecExp = -300;
12912 constexpr int kCachedPowersDecStep = 8;
12914 static constexpr std::array<cached_power, 79> kCachedPowers =
12917 { 0xAB70FE17C79AC6CA, -1060, -300 },
12918 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
12919 { 0xBE5691EF416BD60C, -1007, -284 },
12920 { 0x8DD01FAD907FFC3C, -980, -276 },
12921 { 0xD3515C2831559A83, -954, -268 },
12922 { 0x9D71AC8FADA6C9B5, -927, -260 },
12923 { 0xEA9C227723EE8BCB, -901, -252 },
12924 { 0xAECC49914078536D, -874, -244 },
12925 { 0x823C12795DB6CE57, -847, -236 },
12926 { 0xC21094364DFB5637, -821, -228 },
12927 { 0x9096EA6F3848984F, -794, -220 },
12928 { 0xD77485CB25823AC7, -768, -212 },
12929 { 0xA086CFCD97BF97F4, -741, -204 },
12930 { 0xEF340A98172AACE5, -715, -196 },
12931 { 0xB23867FB2A35B28E, -688, -188 },
12932 { 0x84C8D4DFD2C63F3B, -661, -180 },
12933 { 0xC5DD44271AD3CDBA, -635, -172 },
12934 { 0x936B9FCEBB25C996, -608, -164 },
12935 { 0xDBAC6C247D62A584, -582, -156 },
12936 { 0xA3AB66580D5FDAF6, -555, -148 },
12937 { 0xF3E2F893DEC3F126, -529, -140 },
12938 { 0xB5B5ADA8AAFF80B8, -502, -132 },
12939 { 0x87625F056C7C4A8B, -475, -124 },
12940 { 0xC9BCFF6034C13053, -449, -116 },
12941 { 0x964E858C91BA2655, -422, -108 },
12942 { 0xDFF9772470297EBD, -396, -100 },
12943 { 0xA6DFBD9FB8E5B88F, -369, -92 },
12944 { 0xF8A95FCF88747D94, -343, -84 },
12945 { 0xB94470938FA89BCF, -316, -76 },
12946 { 0x8A08F0F8BF0F156B, -289, -68 },
12947 { 0xCDB02555653131B6, -263, -60 },
12948 { 0x993FE2C6D07B7FAC, -236, -52 },
12949 { 0xE45C10C42A2B3B06, -210, -44 },
12950 { 0xAA242499697392D3, -183, -36 },
12951 { 0xFD87B5F28300CA0E, -157, -28 },
12952 { 0xBCE5086492111AEB, -130, -20 },
12953 { 0x8CBCCC096F5088CC, -103, -12 },
12954 { 0xD1B71758E219652C, -77, -4 },
12955 { 0x9C40000000000000, -50, 4 },
12956 { 0xE8D4A51000000000, -24, 12 },
12957 { 0xAD78EBC5AC620000, 3, 20 },
12958 { 0x813F3978F8940984, 30, 28 },
12959 { 0xC097CE7BC90715B3, 56, 36 },
12960 { 0x8F7E32CE7BEA5C70, 83, 44 },
12961 { 0xD5D238A4ABE98068, 109, 52 },
12962 { 0x9F4F2726179A2245, 136, 60 },
12963 { 0xED63A231D4C4FB27, 162, 68 },
12964 { 0xB0DE65388CC8ADA8, 189, 76 },
12965 { 0x83C7088E1AAB65DB, 216, 84 },
12966 { 0xC45D1DF942711D9A, 242, 92 },
12967 { 0x924D692CA61BE758, 269, 100 },
12968 { 0xDA01EE641A708DEA, 295, 108 },
12969 { 0xA26DA3999AEF774A, 322, 116 },
12970 { 0xF209787BB47D6B85, 348, 124 },
12971 { 0xB454E4A179DD1877, 375, 132 },
12972 { 0x865B86925B9BC5C2, 402, 140 },
12973 { 0xC83553C5C8965D3D, 428, 148 },
12974 { 0x952AB45CFA97A0B3, 455, 156 },
12975 { 0xDE469FBD99A05FE3, 481, 164 },
12976 { 0xA59BC234DB398C25, 508, 172 },
12977 { 0xF6C69A72A3989F5C, 534, 180 },
12978 { 0xB7DCBF5354E9BECE, 561, 188 },
12979 { 0x88FCF317F22241E2, 588, 196 },
12980 { 0xCC20CE9BD35C78A5, 614, 204 },
12981 { 0x98165AF37B2153DF, 641, 212 },
12982 { 0xE2A0B5DC971F303A, 667, 220 },
12983 { 0xA8D9D1535CE3B396, 694, 228 },
12984 { 0xFB9B7CD9A4A7443C, 720, 236 },
12985 { 0xBB764C4CA7A44410, 747, 244 },
12986 { 0x8BAB8EEFB6409C1A, 774, 252 },
12987 { 0xD01FEF10A657842C, 800, 260 },
12988 { 0x9B10A4E5E9913129, 827, 268 },
12989 { 0xE7109BFBA19C0C9D, 853, 276 },
12990 { 0xAC2820D9623BF429, 880, 284 },
12991 { 0x80444B5E7AA7CF85, 907, 292 },
12992 { 0xBF21E44003ACDD2D, 933, 300 },
12993 { 0x8E679C2F5E44FF8F, 960, 308 },
12994 { 0xD433179D9C8CB841, 986, 316 },
12995 { 0x9E19DB92B4E31BA9, 1013, 324 },
13003 assert(e >= -1500);
13005 const int f = kAlpha - e - 1;
13006 const int k = (f * 78913) / (1 << 18) +
static_cast<
int>(f > 0);
13008 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
13009 assert(index >= 0);
13010 assert(
static_cast<std::size_t>(index) < kCachedPowers.size());
13012 const cached_power cached = kCachedPowers[
static_cast<std::size_t>(index)];
13013 assert(kAlpha <= cached.e + e + 64);
13014 assert(kGamma >= cached.e + e + 64);
13023 inline int find_largest_pow10(
const std::uint32_t n, std::uint32_t& pow10)
13026 if (n >= 1000000000)
13028 pow10 = 1000000000;
13032 else if (n >= 100000000)
13037 else if (n >= 10000000)
13042 else if (n >= 1000000)
13047 else if (n >= 100000)
13052 else if (n >= 10000)
13057 else if (n >= 1000)
13079 inline void grisu2_round(
char* buf,
int len, std::uint64_t dist, std::uint64_t delta,
13080 std::uint64_t rest, std::uint64_t ten_k)
13083 assert(dist <= delta);
13084 assert(rest <= delta);
13107 and delta - rest >= ten_k
13108 and (rest + ten_k < dist
or dist - rest > rest + ten_k - dist))
13110 assert(buf[len - 1] !=
'0');
13120 inline void grisu2_digit_gen(
char* buffer,
int& length,
int& decimal_exponent,
13121 diyfp M_minus, diyfp w, diyfp M_plus)
13123 static_assert(kAlpha >= -60,
"internal error");
13124 static_assert(kGamma <= -32,
"internal error");
13138 assert(M_plus.e >= kAlpha);
13139 assert(M_plus.e <= kGamma);
13141 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f;
13142 std::uint64_t dist = diyfp::sub(M_plus, w ).f;
13151 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
13153 auto p1 =
static_cast<std::uint32_t>(M_plus.f >> -one.e);
13154 std::uint64_t p2 = M_plus.f & (one.f - 1);
13162 std::uint32_t pow10;
13163 const int k = find_largest_pow10(p1, pow10);
13190 const std::uint32_t d = p1 / pow10;
13191 const std::uint32_t r = p1 % pow10;
13197 buffer[length++] =
static_cast<
char>(
'0' + d);
13216 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
13221 decimal_exponent += n;
13232 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
13233 grisu2_round(buffer, length, dist, delta, rest, ten_n);
13283 assert(p2 > delta);
13294 assert(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
13296 const std::uint64_t d = p2 >> -one.e;
13297 const std::uint64_t r = p2 & (one.f - 1);
13304 buffer[length++] =
static_cast<
char>(
'0' + d);
13329 decimal_exponent -= m;
13337 const std::uint64_t ten_m = one.f;
13338 grisu2_round(buffer, length, dist, delta, p2, ten_m);
13361 inline void grisu2(
char* buf,
int& len,
int& decimal_exponent,
13362 diyfp m_minus, diyfp v, diyfp m_plus)
13364 assert(m_plus.e == m_minus.e);
13365 assert(m_plus.e == v.e);
13376 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
13378 const diyfp c_minus_k(cached.f, cached.e);
13381 const diyfp w = diyfp::mul(v, c_minus_k);
13382 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
13383 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
13406 const diyfp M_minus(w_minus.f + 1, w_minus.e);
13407 const diyfp M_plus (w_plus.f - 1, w_plus.e );
13409 decimal_exponent = -cached.k;
13411 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
13419 template <
typename FloatType>
13421 void grisu2(
char* buf,
int& len,
int& decimal_exponent, FloatType value)
13423 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
13424 "internal error: not enough precision");
13426 assert(std::isfinite(value));
13446 const boundaries w = compute_boundaries(
static_cast<
double>(value));
13448 const boundaries w = compute_boundaries(value);
13451 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
13461 inline char* append_exponent(
char* buf,
int e)
13476 auto k =
static_cast<std::uint32_t>(e);
13482 *buf++ =
static_cast<
char>(
'0' + k);
13486 *buf++ =
static_cast<
char>(
'0' + k / 10);
13488 *buf++ =
static_cast<
char>(
'0' + k);
13492 *buf++ =
static_cast<
char>(
'0' + k / 100);
13494 *buf++ =
static_cast<
char>(
'0' + k / 10);
13496 *buf++ =
static_cast<
char>(
'0' + k);
13513 inline char* format_buffer(
char* buf,
int len,
int decimal_exponent,
13514 int min_exp,
int max_exp)
13516 assert(min_exp < 0);
13517 assert(max_exp > 0);
13520 const int n = len + decimal_exponent;
13526 if (k <= n
and n <= max_exp)
13531 std::memset(buf + k,
'0',
static_cast<size_t>(n - k));
13535 return buf + (n + 2);
13538 if (0 < n
and n <= max_exp)
13545 std::memmove(buf + (n + 1), buf + n,
static_cast<size_t>(k - n));
13547 return buf + (k + 1);
13550 if (min_exp < n
and n <= 0)
13555 std::memmove(buf + (2 + -n), buf,
static_cast<size_t>(k));
13558 std::memset(buf + 2,
'0',
static_cast<size_t>(-n));
13559 return buf + (2 + (-n) + k);
13574 std::memmove(buf + 2, buf + 1,
static_cast<size_t>(k - 1));
13580 return append_exponent(buf, n - 1);
13595 template <
typename FloatType>
13598 char* to_chars(
char* first,
const char* last, FloatType value)
13600 static_cast<
void>(last);
13601 assert(std::isfinite(value));
13604 if (std::signbit(value))
13619 assert(last - first >= std::numeric_limits<FloatType>::max_digits10);
13626 int decimal_exponent = 0;
13627 dtoa_impl::grisu2(first, len, decimal_exponent, value);
13629 assert(len <= std::numeric_limits<FloatType>::max_digits10);
13632 constexpr int kMinExp = -4;
13634 constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
13636 assert(last - first >= kMaxExp + 2);
13637 assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
13638 assert(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
13640 return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
13668 enum class error_handler_t
13675 template<
typename BasicJsonType>
13678 using string_t =
typename BasicJsonType::string_t;
13679 using number_float_t =
typename BasicJsonType::number_float_t;
13680 using number_integer_t =
typename BasicJsonType::number_integer_t;
13681 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
13682 static constexpr std::uint8_t UTF8_ACCEPT = 0;
13683 static constexpr std::uint8_t UTF8_REJECT = 1;
13691 serializer(output_adapter_t<
char> s,
const char ichar,
13692 error_handler_t error_handler_ = error_handler_t::strict)
13694 , loc(std::localeconv())
13695 , thousands_sep(loc->thousands_sep ==
nullptr ?
'\0' : * (loc->thousands_sep))
13696 , decimal_point(loc->decimal_point ==
nullptr ?
'\0' : * (loc->decimal_point))
13697 , indent_char(ichar)
13698 , indent_string(512, indent_char)
13699 , error_handler(error_handler_)
13703 serializer(
const serializer&) =
delete;
13704 serializer& operator=(
const serializer&) =
delete;
13705 serializer(serializer&&) =
delete;
13706 serializer& operator=(serializer&&) =
delete;
13707 ~serializer() =
default;
13726 void dump(
const BasicJsonType& val,
const bool pretty_print,
13727 const bool ensure_ascii,
13728 const unsigned int indent_step,
13729 const unsigned int current_indent = 0)
13731 switch (val.m_type)
13733 case value_t::object:
13735 if (val.m_value.object->empty())
13737 o->write_characters(
"{}", 2);
13743 o->write_characters(
"{\n", 2);
13746 const auto new_indent = current_indent + indent_step;
13749 indent_string.resize(indent_string.size() * 2,
' ');
13753 auto i = val.m_value.object->cbegin();
13754 for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
13756 o->write_characters(indent_string.c_str(), new_indent);
13757 o->write_character(
'\"');
13758 dump_escaped(i->first, ensure_ascii);
13759 o->write_characters(
"\": ", 3);
13760 dump(i->second,
true, ensure_ascii, indent_step, new_indent);
13761 o->write_characters(
",\n", 2);
13765 assert(i != val.m_value.object->cend());
13766 assert(std::next(i) == val.m_value.object->cend());
13767 o->write_characters(indent_string.c_str(), new_indent);
13768 o->write_character(
'\"');
13769 dump_escaped(i->first, ensure_ascii);
13770 o->write_characters(
"\": ", 3);
13771 dump(i->second,
true, ensure_ascii, indent_step, new_indent);
13773 o->write_character(
'\n');
13774 o->write_characters(indent_string.c_str(), current_indent);
13775 o->write_character(
'}');
13779 o->write_character(
'{');
13782 auto i = val.m_value.object->cbegin();
13783 for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
13785 o->write_character(
'\"');
13786 dump_escaped(i->first, ensure_ascii);
13787 o->write_characters(
"\":", 2);
13788 dump(i->second,
false, ensure_ascii, indent_step, current_indent);
13789 o->write_character(
',');
13793 assert(i != val.m_value.object->cend());
13794 assert(std::next(i) == val.m_value.object->cend());
13795 o->write_character(
'\"');
13796 dump_escaped(i->first, ensure_ascii);
13797 o->write_characters(
"\":", 2);
13798 dump(i->second,
false, ensure_ascii, indent_step, current_indent);
13800 o->write_character(
'}');
13806 case value_t::array:
13808 if (val.m_value.array->empty())
13810 o->write_characters(
"[]", 2);
13816 o->write_characters(
"[\n", 2);
13819 const auto new_indent = current_indent + indent_step;
13822 indent_string.resize(indent_string.size() * 2,
' ');
13826 for (
auto i = val.m_value.array->cbegin();
13827 i != val.m_value.array->cend() - 1; ++i)
13829 o->write_characters(indent_string.c_str(), new_indent);
13830 dump(*i,
true, ensure_ascii, indent_step, new_indent);
13831 o->write_characters(
",\n", 2);
13835 assert(
not val.m_value.array->empty());
13836 o->write_characters(indent_string.c_str(), new_indent);
13837 dump(val.m_value.array->back(),
true, ensure_ascii, indent_step, new_indent);
13839 o->write_character(
'\n');
13840 o->write_characters(indent_string.c_str(), current_indent);
13841 o->write_character(
']');
13845 o->write_character(
'[');
13848 for (
auto i = val.m_value.array->cbegin();
13849 i != val.m_value.array->cend() - 1; ++i)
13851 dump(*i,
false, ensure_ascii, indent_step, current_indent);
13852 o->write_character(
',');
13856 assert(
not val.m_value.array->empty());
13857 dump(val.m_value.array->back(),
false, ensure_ascii, indent_step, current_indent);
13859 o->write_character(
']');
13865 case value_t::string:
13867 o->write_character(
'\"');
13868 dump_escaped(*val.m_value.string, ensure_ascii);
13869 o->write_character(
'\"');
13873 case value_t::boolean:
13875 if (val.m_value.boolean)
13877 o->write_characters(
"true", 4);
13881 o->write_characters(
"false", 5);
13886 case value_t::number_integer:
13888 dump_integer(val.m_value.number_integer);
13892 case value_t::number_unsigned:
13894 dump_integer(val.m_value.number_unsigned);
13898 case value_t::number_float:
13900 dump_float(val.m_value.number_float);
13904 case value_t::discarded:
13906 o->write_characters(
"<discarded>", 11);
13910 case value_t::null:
13912 o->write_characters(
"null", 4);
13936 void dump_escaped(
const string_t& s,
const bool ensure_ascii)
13938 std::uint32_t codepoint;
13939 std::uint8_t state = UTF8_ACCEPT;
13940 std::size_t bytes = 0;
13943 std::size_t bytes_after_last_accept = 0;
13944 std::size_t undumped_chars = 0;
13946 for (std::size_t i = 0; i < s.size(); ++i)
13948 const auto byte =
static_cast<uint8_t>(s[i]);
13950 switch (decode(state, codepoint, byte))
13958 string_buffer[bytes++] =
'\\';
13959 string_buffer[bytes++] =
'b';
13965 string_buffer[bytes++] =
'\\';
13966 string_buffer[bytes++] =
't';
13972 string_buffer[bytes++] =
'\\';
13973 string_buffer[bytes++] =
'n';
13979 string_buffer[bytes++] =
'\\';
13980 string_buffer[bytes++] =
'f';
13986 string_buffer[bytes++] =
'\\';
13987 string_buffer[bytes++] =
'r';
13993 string_buffer[bytes++] =
'\\';
13994 string_buffer[bytes++] =
'\"';
14000 string_buffer[bytes++] =
'\\';
14001 string_buffer[bytes++] =
'\\';
14009 if ((codepoint <= 0x1F)
or (ensure_ascii
and (codepoint >= 0x7F)))
14011 if (codepoint <= 0xFFFF)
14013 (std::snprintf)(string_buffer.data() + bytes, 7,
"\\u%04x",
14014 static_cast<std::uint16_t>(codepoint));
14019 (std::snprintf)(string_buffer.data() + bytes, 13,
"\\u%04x\\u%04x",
14020 static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
14021 static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu)));
14029 string_buffer[bytes++] = s[i];
14038 if (string_buffer.size() - bytes < 13)
14040 o->write_characters(string_buffer.data(), bytes);
14045 bytes_after_last_accept = bytes;
14046 undumped_chars = 0;
14052 switch (error_handler)
14054 case error_handler_t::strict:
14056 std::string sn(3,
'\0');
14057 (std::snprintf)(&sn[0], sn.size(),
"%.2X", byte);
14058 JSON_THROW(type_error::create(316,
"invalid UTF-8 byte at index " + std::to_string(i) +
": 0x" + sn));
14061 case error_handler_t::ignore:
14062 case error_handler_t::replace:
14068 if (undumped_chars > 0)
14075 bytes = bytes_after_last_accept;
14077 if (error_handler == error_handler_t::replace)
14082 string_buffer[bytes++] =
'\\';
14083 string_buffer[bytes++] =
'u';
14084 string_buffer[bytes++] =
'f';
14085 string_buffer[bytes++] =
'f';
14086 string_buffer[bytes++] =
'f';
14087 string_buffer[bytes++] =
'd';
14091 string_buffer[bytes++] = detail::binary_writer<BasicJsonType,
char>::to_char_type(
'\xEF');
14092 string_buffer[bytes++] = detail::binary_writer<BasicJsonType,
char>::to_char_type(
'\xBF');
14093 string_buffer[bytes++] = detail::binary_writer<BasicJsonType,
char>::to_char_type(
'\xBD');
14099 if (string_buffer.size() - bytes < 13)
14101 o->write_characters(string_buffer.data(), bytes);
14105 bytes_after_last_accept = bytes;
14108 undumped_chars = 0;
14111 state = UTF8_ACCEPT;
14123 if (
not ensure_ascii)
14126 string_buffer[bytes++] = s[i];
14140 o->write_characters(string_buffer.data(), bytes);
14146 switch (error_handler)
14148 case error_handler_t::strict:
14150 std::string sn(3,
'\0');
14151 (std::snprintf)(&sn[0], sn.size(),
"%.2X",
static_cast<std::uint8_t>(s.back()));
14152 JSON_THROW(type_error::create(316,
"incomplete UTF-8 string; last byte: 0x" + sn));
14155 case error_handler_t::ignore:
14158 o->write_characters(string_buffer.data(), bytes_after_last_accept);
14162 case error_handler_t::replace:
14165 o->write_characters(string_buffer.data(), bytes_after_last_accept);
14169 o->write_characters(
"\\ufffd", 6);
14173 o->write_characters(
"\xEF\xBF\xBD", 3);
14192 inline unsigned int count_digits(number_unsigned_t x)
noexcept 14194 unsigned int n_digits = 1;
14203 return n_digits + 1;
14207 return n_digits + 2;
14211 return n_digits + 3;
14227 template<
typename NumberType, detail::enable_if_t<
14228 std::is_same<NumberType, number_unsigned_t>::value
or 14229 std::is_same<NumberType, number_integer_t>::value,
14231 void dump_integer(NumberType x)
14233 static constexpr std::array<std::array<
char, 2>, 100> digits_to_99
14236 {{
'0',
'0'}}, {{
'0',
'1'}}, {{
'0',
'2'}}, {{
'0',
'3'}}, {{
'0',
'4'}}, {{
'0',
'5'}}, {{
'0',
'6'}}, {{
'0',
'7'}}, {{
'0',
'8'}}, {{
'0',
'9'}},
14237 {{
'1',
'0'}}, {{
'1',
'1'}}, {{
'1',
'2'}}, {{
'1',
'3'}}, {{
'1',
'4'}}, {{
'1',
'5'}}, {{
'1',
'6'}}, {{
'1',
'7'}}, {{
'1',
'8'}}, {{
'1',
'9'}},
14238 {{
'2',
'0'}}, {{
'2',
'1'}}, {{
'2',
'2'}}, {{
'2',
'3'}}, {{
'2',
'4'}}, {{
'2',
'5'}}, {{
'2',
'6'}}, {{
'2',
'7'}}, {{
'2',
'8'}}, {{
'2',
'9'}},
14239 {{
'3',
'0'}}, {{
'3',
'1'}}, {{
'3',
'2'}}, {{
'3',
'3'}}, {{
'3',
'4'}}, {{
'3',
'5'}}, {{
'3',
'6'}}, {{
'3',
'7'}}, {{
'3',
'8'}}, {{
'3',
'9'}},
14240 {{
'4',
'0'}}, {{
'4',
'1'}}, {{
'4',
'2'}}, {{
'4',
'3'}}, {{
'4',
'4'}}, {{
'4',
'5'}}, {{
'4',
'6'}}, {{
'4',
'7'}}, {{
'4',
'8'}}, {{
'4',
'9'}},
14241 {{
'5',
'0'}}, {{
'5',
'1'}}, {{
'5',
'2'}}, {{
'5',
'3'}}, {{
'5',
'4'}}, {{
'5',
'5'}}, {{
'5',
'6'}}, {{
'5',
'7'}}, {{
'5',
'8'}}, {{
'5',
'9'}},
14242 {{
'6',
'0'}}, {{
'6',
'1'}}, {{
'6',
'2'}}, {{
'6',
'3'}}, {{
'6',
'4'}}, {{
'6',
'5'}}, {{
'6',
'6'}}, {{
'6',
'7'}}, {{
'6',
'8'}}, {{
'6',
'9'}},
14243 {{
'7',
'0'}}, {{
'7',
'1'}}, {{
'7',
'2'}}, {{
'7',
'3'}}, {{
'7',
'4'}}, {{
'7',
'5'}}, {{
'7',
'6'}}, {{
'7',
'7'}}, {{
'7',
'8'}}, {{
'7',
'9'}},
14244 {{
'8',
'0'}}, {{
'8',
'1'}}, {{
'8',
'2'}}, {{
'8',
'3'}}, {{
'8',
'4'}}, {{
'8',
'5'}}, {{
'8',
'6'}}, {{
'8',
'7'}}, {{
'8',
'8'}}, {{
'8',
'9'}},
14245 {{
'9',
'0'}}, {{
'9',
'1'}}, {{
'9',
'2'}}, {{
'9',
'3'}}, {{
'9',
'4'}}, {{
'9',
'5'}}, {{
'9',
'6'}}, {{
'9',
'7'}}, {{
'9',
'8'}}, {{
'9',
'9'}},
14252 o->write_character(
'0');
14257 auto buffer_ptr = number_buffer.begin();
14259 const bool is_negative = std::is_same<NumberType, number_integer_t>::value
and not(x >= 0);
14260 number_unsigned_t abs_value;
14262 unsigned int n_chars;
14267 abs_value =
static_cast<number_unsigned_t>(std::abs(
static_cast<std::intmax_t>(x)));
14270 n_chars = 1 + count_digits(abs_value);
14274 abs_value =
static_cast<number_unsigned_t>(x);
14275 n_chars = count_digits(abs_value);
14279 assert(n_chars < number_buffer.size() - 1);
14283 buffer_ptr += n_chars;
14287 while (abs_value >= 100)
14289 const auto digits_index =
static_cast<
unsigned>((abs_value % 100));
14291 *(--buffer_ptr) = digits_to_99[digits_index][1];
14292 *(--buffer_ptr) = digits_to_99[digits_index][0];
14295 if (abs_value >= 10)
14297 const auto digits_index =
static_cast<
unsigned>(abs_value);
14298 *(--buffer_ptr) = digits_to_99[digits_index][1];
14299 *(--buffer_ptr) = digits_to_99[digits_index][0];
14303 *(--buffer_ptr) =
static_cast<
char>(
'0' + abs_value);
14306 o->write_characters(number_buffer.data(), n_chars);
14317 void dump_float(number_float_t x)
14320 if (
not std::isfinite(x))
14322 o->write_characters(
"null", 4);
14331 static constexpr bool is_ieee_single_or_double
14332 = (std::numeric_limits<number_float_t>::is_iec559
and std::numeric_limits<number_float_t>::digits == 24
and std::numeric_limits<number_float_t>::max_exponent == 128)
or 14333 (std::numeric_limits<number_float_t>::is_iec559
and std::numeric_limits<number_float_t>::digits == 53
and std::numeric_limits<number_float_t>::max_exponent == 1024);
14335 dump_float(x, std::integral_constant<
bool, is_ieee_single_or_double>());
14338 void dump_float(number_float_t x, std::true_type )
14340 char* begin = number_buffer.data();
14341 char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
14343 o->write_characters(begin,
static_cast<size_t>(end - begin));
14346 void dump_float(number_float_t x, std::false_type )
14349 static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
14352 std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(),
"%.*g", d, x);
14357 assert(
static_cast<std::size_t>(len) < number_buffer.size());
14360 if (thousands_sep !=
'\0')
14362 const auto end = std::remove(number_buffer.begin(),
14363 number_buffer.begin() + len, thousands_sep);
14364 std::fill(end, number_buffer.end(),
'\0');
14365 assert((end - number_buffer.begin()) <= len);
14366 len = (end - number_buffer.begin());
14370 if (decimal_point !=
'\0' and decimal_point !=
'.')
14372 const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
14373 if (dec_pos != number_buffer.end())
14379 o->write_characters(number_buffer.data(),
static_cast<std::size_t>(len));
14382 const bool value_is_int_like =
14383 std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
14386 return c ==
'.' or c ==
'e';
14389 if (value_is_int_like)
14391 o->write_characters(
".0", 2);
14416 static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep,
const std::uint8_t byte)
noexcept 14418 static const std::array<std::uint8_t, 400> utf8d =
14421 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14422 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14423 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14424 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14425 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
14426 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
14427 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
14428 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3,
14429 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8,
14430 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1,
14431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1,
14432 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,
14433 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1,
14434 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
14438 const std::uint8_t type = utf8d[byte];
14440 codep = (state != UTF8_ACCEPT)
14441 ? (byte & 0x3fu) | (codep << 6u)
14442 : (0xFFu >> type) & (byte);
14444 state = utf8d[256u + state * 16u + type];
14450 output_adapter_t<
char> o =
nullptr;
14453 std::array<
char, 64> number_buffer{{}};
14456 const std::lconv* loc =
nullptr;
14458 const char thousands_sep =
'\0';
14460 const char decimal_point =
'\0';
14463 std::array<
char, 512> string_buffer{{}};
14466 const char indent_char;
14468 string_t indent_string;
14471 const error_handler_t error_handler;
14578 template<
typename BasicJsonType>
14579 friend class ::
nlohmann::detail::iter_impl;
14580 template<
typename BasicJsonType,
typename CharType>
14581 friend class ::
nlohmann::detail::binary_writer;
14582 template<
typename BasicJsonType,
typename SAX>
14583 friend class ::
nlohmann::detail::binary_reader;
14584 template<
typename BasicJsonType>
14585 friend class ::
nlohmann::detail::json_sax_dom_parser;
14586 template<
typename BasicJsonType>
14587 friend class ::
nlohmann::detail::json_sax_dom_callback_parser;
14596 using primitive_iterator_t = ::
nlohmann::detail::primitive_iterator_t;
14597 template<
typename BasicJsonType>
14598 using internal_iterator = ::
nlohmann::detail::internal_iterator<BasicJsonType>;
14599 template<
typename BasicJsonType>
14600 using iter_impl = ::
nlohmann::detail::iter_impl<BasicJsonType>;
14601 template<
typename Iterator>
14602 using iteration_proxy = ::
nlohmann::detail::iteration_proxy<Iterator>;
14603 template<
typename Base>
using json_reverse_iterator = ::
nlohmann::detail::json_reverse_iterator<Base>;
14609 template<
typename CharType>
using binary_writer = ::
nlohmann::detail::binary_writer<
basic_json, CharType>;
14614 using value_t = detail::value_t;
14617 template<
typename T,
typename SFINAE>
14618 using json_serializer = JSONSerializer<T, SFINAE>;
14620 using error_handler_t = detail::error_handler_t;
14622 using initializer_list_t = std::initializer_list<detail::json_ref<
basic_json>>;
14624 using input_format_t = detail::input_format_t;
14637 using exception = detail::exception;
14639 using parse_error = detail::parse_error;
14641 using invalid_iterator = detail::invalid_iterator;
14643 using type_error = detail::type_error;
14645 using out_of_range = detail::out_of_range;
14647 using other_error = detail::other_error;
14665 using reference = value_type&;
14667 using const_reference =
const value_type&;
14670 using difference_type = std::ptrdiff_t;
14672 using size_type = std::size_t;
14699 return allocator_type();
14733 result[
"copyright"] =
"(C) 2013-2017 Niels Lohmann";
14734 result[
"name"] =
"JSON for Modern C++";
14735 result[
"url"] =
"https://github.com/nlohmann/json";
14736 result[
"version"][
"string"] =
14745 result[
"platform"] =
"win32";
14746 #elif defined __linux__
14747 result[
"platform"] =
"linux";
14748 #elif defined __APPLE__ 14749 result[
"platform"] =
"apple";
14750 #elif defined __unix__ 14751 result[
"platform"] =
"unix";
14753 result[
"platform"] =
"unknown";
14756 #if defined(__ICC) || defined(__INTEL_COMPILER) 14757 result[
"compiler"] = {{
"family",
"icc"}, {
"version", __INTEL_COMPILER}};
14758 #elif defined(__clang__
) 14759 result[
"compiler"] = {{
"family",
"clang"}, {
"version", __clang_version__}};
14760 #elif defined(__GNUC__) || defined(__GNUG__) 14761 result[
"compiler"] = {{
"family",
"gcc"}, {
"version", std::to_string(__GNUC__) +
"." + std::to_string(__GNUC_MINOR__) +
"." + std::to_string(__GNUC_PATCHLEVEL__)}};
14762 #elif defined(__HP_cc) || defined(__HP_aCC) 14763 result[
"compiler"] =
"hp" 14764 #elif defined(__IBMCPP__) 14765 result[
"compiler"] = {{
"family",
"ilecpp"}, {
"version", __IBMCPP__}};
14766 #elif defined(_MSC_VER) 14767 result[
"compiler"] = {{
"family",
"msvc"}, {
"version", _MSC_VER}};
14768 #elif defined(__PGI) 14769 result[
"compiler"] = {{
"family",
"pgcpp"}, {
"version", __PGI}};
14770 #elif defined(__SUNPRO_CC) 14771 result[
"compiler"] = {{
"family",
"sunpro"}, {
"version", __SUNPRO_CC}};
14773 result[
"compiler"] = {{
"family",
"unknown"}, {
"version",
"unknown"}};
14777 result[
"compiler"][
"c++"] = std::to_string(__cplusplus);
14779 result[
"compiler"][
"c++"] =
"unknown";
14794 #if defined(JSON_HAS_CPP_14) 14988 using string_t = StringType;
15014 using boolean_t = BooleanType;
15086 using number_integer_t = NumberIntegerType;
15157 using number_unsigned_t = NumberUnsignedType;
15225 using number_float_t = NumberFloatType;
15232 template<
typename T,
typename... Args>
15234 static T* create(Args&& ... args)
15236 AllocatorType<T> alloc;
15237 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
15239 auto deleter = [&](T * object)
15241 AllocatorTraits::deallocate(alloc, object, 1);
15243 std::unique_ptr<T,
decltype(deleter)> object(AllocatorTraits::allocate(alloc, 1), deleter);
15244 AllocatorTraits::construct(alloc, object.get(), std::forward<Args>(args)...);
15245 assert(
object !=
nullptr);
15246 return object.release();
15288 number_integer_t number_integer;
15290 number_unsigned_t number_unsigned;
15292 number_float_t number_float;
15295 json_value() =
default;
15297 json_value(boolean_t v)
noexcept : boolean(v) {}
15299 json_value(number_integer_t v)
noexcept : number_integer(v) {}
15301 json_value(number_unsigned_t v)
noexcept : number_unsigned(v) {}
15303 json_value(number_float_t v)
noexcept : number_float(v) {}
15305 json_value(value_t t)
15309 case value_t::object:
15311 object = create<object_t>();
15315 case value_t::array:
15317 array = create<array_t>();
15321 case value_t::string:
15323 string = create<string_t>(
"");
15327 case value_t::boolean:
15329 boolean = boolean_t(
false);
15333 case value_t::number_integer:
15335 number_integer = number_integer_t(0);
15339 case value_t::number_unsigned:
15341 number_unsigned = number_unsigned_t(0);
15345 case value_t::number_float:
15347 number_float = number_float_t(0.0);
15351 case value_t::null:
15362 JSON_THROW(other_error::create(500,
"961c151d2e87f2686a955a9be24d316f1362bf21 3.7.0"));
15370 json_value(
const string_t& value)
15372 string = create<string_t>(value);
15376 json_value(string_t&& value)
15378 string = create<string_t>(std::move(value));
15382 json_value(
const object_t& value)
15384 object = create<object_t>(value);
15388 json_value(object_t&& value)
15390 object = create<object_t>(std::move(value));
15394 json_value(
const array_t& value)
15396 array = create<array_t>(value);
15400 json_value(array_t&& value)
15402 array = create<array_t>(std::move(value));
15405 void destroy(value_t t)
noexcept 15409 case value_t::object:
15411 AllocatorType<object_t> alloc;
15412 std::allocator_traits<
decltype(alloc)>::destroy(alloc, object);
15413 std::allocator_traits<
decltype(alloc)>::deallocate(alloc, object, 1);
15417 case value_t::array:
15419 AllocatorType<array_t> alloc;
15420 std::allocator_traits<
decltype(alloc)>::destroy(alloc, array);
15421 std::allocator_traits<
decltype(alloc)>::deallocate(alloc, array, 1);
15425 case value_t::string:
15427 AllocatorType<string_t> alloc;
15428 std::allocator_traits<
decltype(alloc)>::destroy(alloc, string);
15429 std::allocator_traits<
decltype(alloc)>::deallocate(alloc, string, 1);
15450 void assert_invariant()
const noexcept 15452 assert(m_type != value_t::object
or m_value.object !=
nullptr);
15453 assert(m_type != value_t::array
or m_value.array !=
nullptr);
15454 assert(m_type != value_t::string
or m_value.string !=
nullptr);
15477 using parse_event_t =
typename parser::parse_event_t;
15528 using parser_callback_t =
typename parser::parser_callback_t;
15571 assert_invariant();
15595 assert_invariant();
15655 template <
typename CompatibleType,
15657 detail::enable_if_t<
15658 not detail::is_basic_json<U>::value
and detail::is_compatible_type<basic_json_t, U>::value,
int> = 0>
15663 JSONSerializer<U>::to_json(*
this, std::forward<CompatibleType>(val));
15664 assert_invariant();
15693 template <
typename BasicJsonType,
15694 detail::enable_if_t<
15695 detail::is_basic_json<BasicJsonType>::value
and not std::is_same<basic_json, BasicJsonType>::value,
int> = 0>
15698 using other_boolean_t =
typename BasicJsonType::boolean_t;
15699 using other_number_float_t =
typename BasicJsonType::number_float_t;
15700 using other_number_integer_t =
typename BasicJsonType::number_integer_t;
15701 using other_number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
15702 using other_string_t =
typename BasicJsonType::string_t;
15703 using other_object_t =
typename BasicJsonType::object_t;
15704 using other_array_t =
typename BasicJsonType::array_t;
15706 switch (val.type())
15708 case value_t::boolean:
15709 JSONSerializer<other_boolean_t>::to_json(*
this, val.
template get<other_boolean_t>());
15711 case value_t::number_float:
15712 JSONSerializer<other_number_float_t>::to_json(*
this, val.
template get<other_number_float_t>());
15714 case value_t::number_integer:
15715 JSONSerializer<other_number_integer_t>::to_json(*
this, val.
template get<other_number_integer_t>());
15717 case value_t::number_unsigned:
15718 JSONSerializer<other_number_unsigned_t>::to_json(*
this, val.
template get<other_number_unsigned_t>());
15720 case value_t::string:
15721 JSONSerializer<other_string_t>::to_json(*
this, val.
template get_ref<
const other_string_t&>());
15723 case value_t::object:
15724 JSONSerializer<other_object_t>::to_json(*
this, val.
template get_ref<
const other_object_t&>());
15726 case value_t::array:
15727 JSONSerializer<other_array_t>::to_json(*
this, val.
template get_ref<
const other_array_t&>());
15729 case value_t::null:
15732 case value_t::discarded:
15733 m_type = value_t::discarded;
15738 assert_invariant();
15816 bool type_deduction =
true,
15817 value_t manual_type = value_t::array)
15821 bool is_an_object = std::all_of(init.begin(), init.end(),
15822 [](
const detail::json_ref<
basic_json>& element_ref)
15824 return element_ref->is_array()
and element_ref->size() == 2
and (*element_ref)[0].is_string();
15828 if (
not type_deduction)
15831 if (manual_type == value_t::array)
15833 is_an_object =
false;
15839 JSON_THROW(type_error::create(301,
"cannot create object from initializer list"));
15846 m_type = value_t::object;
15847 m_value = value_t::object;
15849 std::for_each(init.begin(), init.end(), [
this](
const detail::json_ref<
basic_json>& element_ref)
15851 auto element = element_ref.moved_or_copied();
15852 m_value.object->emplace(
15853 std::move(*((*element.m_value.array)[0].m_value.string)),
15854 std::move((*element.m_value.array)[1]));
15860 m_type = value_t::array;
15861 m_value.array = create<array_t>(init.begin(), init.end());
15864 assert_invariant();
15907 return basic_json(init,
false, value_t::array);
15951 return basic_json(init,
false, value_t::object);
15979 m_value.array = create<array_t>(cnt, val);
15980 assert_invariant();
16038 template<
class InputIT,
typename std::enable_if<
16039 std::is_same<InputIT,
typename basic_json_t::iterator>::value
or 16040 std::is_same<InputIT,
typename basic_json_t::const_iterator>::value,
int>::
type = 0>
16997 "get() cannot be used with reference types, you might want to use get_ref()");
16999 "types must be DefaultConstructible when used with get()");
17045 "get() cannot be used with reference types, you might want to use get_ref()");
17286 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__
) || (defined(_MSC_VER) and _MSC_VER <= 1914
)) 17291 ,
int >::
type = 0 >
17702 template<
typename T>
17753 template<
typename T>
18335 template<
typename KeyT>
18352 template<
typename KeyT>
18386 template<
typename KeyT>
18939 bool empty()
const noexcept 19151 void clear()
noexcept 19411 template<
class...
Args>
19429 #ifdef JSON_HAS_CPP_17 19464 template<
class...
Args>
19494 template<
typename...
Args>
20137 return not (
lhs ==
rhs);
20347 return not (
lhs <=
rhs);
20628 template <
typename SAX>
20632 const bool strict =
true)
20819 return "discarded";
20934 static std::vector<uint8_t> to_cbor(
const basic_json& j)
20936 std::vector<uint8_t> result;
20937 to_cbor(j, result);
20941 static void to_cbor(
const basic_json& j, detail::output_adapter<uint8_t> o)
20943 binary_writer<uint8_t>(o).write_cbor(j);
20946 static void to_cbor(
const basic_json& j, detail::output_adapter<
char> o)
20948 binary_writer<
char>(o).write_cbor(j);
21030 static std::vector<uint8_t> to_msgpack(
const basic_json& j)
21032 std::vector<uint8_t> result;
21033 to_msgpack(j, result);
21037 static void to_msgpack(
const basic_json& j, detail::output_adapter<uint8_t> o)
21039 binary_writer<uint8_t>(o).write_msgpack(j);
21042 static void to_msgpack(
const basic_json& j, detail::output_adapter<
char> o)
21044 binary_writer<
char>(o).write_msgpack(j);
21127 static std::vector<uint8_t> to_ubjson(
const basic_json& j,
21128 const bool use_size =
false,
21129 const bool use_type =
false)
21131 std::vector<uint8_t> result;
21132 to_ubjson(j, result, use_size, use_type);
21136 static void to_ubjson(
const basic_json& j, detail::output_adapter<uint8_t> o,
21137 const bool use_size =
false,
const bool use_type =
false)
21139 binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
21142 static void to_ubjson(
const basic_json& j, detail::output_adapter<
char> o,
21143 const bool use_size =
false,
const bool use_type =
false)
21145 binary_writer<
char>(o).write_ubjson(j, use_size, use_type);
21204 static std::vector<uint8_t> to_bson(
const basic_json& j)
21206 std::vector<uint8_t> result;
21207 to_bson(j, result);
21219 static void to_bson(
const basic_json& j, detail::output_adapter<uint8_t> o)
21221 binary_writer<uint8_t>(o).write_bson(j);
21227 static void to_bson(
const basic_json& j, detail::output_adapter<
char> o)
21229 binary_writer<
char>(o).write_bson(j);
21333 static basic_json from_cbor(detail::input_adapter&& i,
21334 const bool strict =
true,
21335 const bool allow_exceptions =
true)
21338 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21339 const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict);
21340 return res ? result :
basic_json(value_t::discarded);
21346 template<
typename A1,
typename A2,
21347 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
21349 static basic_json from_cbor(A1 && a1, A2 && a2,
21350 const bool strict =
true,
21351 const bool allow_exceptions =
true)
21354 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21355 const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::cbor, &sdp, strict);
21356 return res ? result :
basic_json(value_t::discarded);
21442 static basic_json from_msgpack(detail::input_adapter&& i,
21443 const bool strict =
true,
21444 const bool allow_exceptions =
true)
21447 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21448 const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict);
21449 return res ? result :
basic_json(value_t::discarded);
21455 template<
typename A1,
typename A2,
21456 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
21458 static basic_json from_msgpack(A1 && a1, A2 && a2,
21459 const bool strict =
true,
21460 const bool allow_exceptions =
true)
21463 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21464 const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::msgpack, &sdp, strict);
21465 return res ? result :
basic_json(value_t::discarded);
21530 static basic_json from_ubjson(detail::input_adapter&& i,
21531 const bool strict =
true,
21532 const bool allow_exceptions =
true)
21535 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21536 const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict);
21537 return res ? result :
basic_json(value_t::discarded);
21543 template<
typename A1,
typename A2,
21544 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
21546 static basic_json from_ubjson(A1 && a1, A2 && a2,
21547 const bool strict =
true,
21548 const bool allow_exceptions =
true)
21551 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21552 const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::ubjson, &sdp, strict);
21553 return res ? result :
basic_json(value_t::discarded);
21617 static basic_json from_bson(detail::input_adapter&& i,
21618 const bool strict =
true,
21619 const bool allow_exceptions =
true)
21622 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21623 const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict);
21624 return res ? result :
basic_json(value_t::discarded);
21630 template<
typename A1,
typename A2,
21631 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
21633 static basic_json from_bson(A1 && a1, A2 && a2,
21634 const bool strict =
true,
21635 const bool allow_exceptions =
true)
21638 detail::json_sax_dom_parser<
basic_json> sdp(result, allow_exceptions);
21639 const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::bson, &sdp, strict);
21640 return res ? result :
basic_json(value_t::discarded);
21687 reference operator[](
const json_pointer& ptr)
21689 return ptr.get_unchecked(
this);
21715 const_reference operator[](
const json_pointer& ptr)
const 21717 return ptr.get_unchecked(
this);
21758 reference at(
const json_pointer& ptr)
21760 return ptr.get_checked(
this);
21801 const_reference at(
const json_pointer& ptr)
const 21803 return ptr.get_checked(
this);
21831 json_pointer::flatten(
"", *
this, result);
21867 return json_pointer::unflatten(*
this);
21932 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
21934 const auto get_op = [](
const std::string & op)
21938 return patch_operations::add;
21940 if (op ==
"remove")
21942 return patch_operations::remove;
21944 if (op ==
"replace")
21946 return patch_operations::replace;
21950 return patch_operations::move;
21954 return patch_operations::copy;
21958 return patch_operations::test;
21961 return patch_operations::invalid;
21965 const auto operation_add = [&result](json_pointer & ptr,
basic_json val)
21975 json_pointer top_pointer = ptr.top();
21976 if (top_pointer != ptr)
21978 result.at(top_pointer);
21982 const auto last_path = ptr.back();
21986 switch (parent.m_type)
21988 case value_t::null:
21989 case value_t::object:
21992 parent[last_path] = val;
21996 case value_t::array:
21998 if (last_path ==
"-")
22001 parent.push_back(val);
22005 const auto idx = json_pointer::array_index(last_path);
22009 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
22013 parent.insert(parent.begin() +
static_cast<difference_type>(idx), val);
22025 const auto operation_remove = [&result](json_pointer & ptr)
22028 const auto last_path = ptr.back();
22033 if (parent.is_object())
22036 auto it = parent.find(last_path);
22043 JSON_THROW(out_of_range::create(403,
"key '" + last_path +
"' not found"));
22046 else if (parent.is_array())
22049 parent.erase(
static_cast<size_type>(json_pointer::array_index(last_path)));
22056 JSON_THROW(parse_error::create(104, 0,
"JSON patch must be an array of objects"));
22060 for (
const auto& val : json_patch)
22063 const auto get_value = [&val](
const std::string & op,
22064 const std::string & member,
22068 auto it = val.m_value.object->find(member);
22071 const auto error_msg = (op ==
"op") ?
"operation" :
"operation '" + op +
"'";
22076 JSON_THROW(parse_error::create(105, 0, error_msg +
" must have member '" + member +
"'"));
22082 JSON_THROW(parse_error::create(105, 0, error_msg +
" must have string member '" + member +
"'"));
22092 JSON_THROW(parse_error::create(104, 0,
"JSON patch must be an array of objects"));
22096 const std::string op = get_value(
"op",
"op",
true);
22097 const std::string path = get_value(op,
"path",
true);
22098 json_pointer ptr(path);
22100 switch (get_op(op))
22102 case patch_operations::add:
22104 operation_add(ptr, get_value(
"add",
"value",
false));
22108 case patch_operations::remove:
22110 operation_remove(ptr);
22114 case patch_operations::replace:
22117 result.at(ptr) = get_value(
"replace",
"value",
false);
22121 case patch_operations::move:
22123 const std::string from_path = get_value(
"move",
"from",
true);
22124 json_pointer from_ptr(from_path);
22127 basic_json v = result.at(from_ptr);
22133 operation_remove(from_ptr);
22134 operation_add(ptr, v);
22138 case patch_operations::copy:
22140 const std::string from_path = get_value(
"copy",
"from",
true);
22141 const json_pointer from_ptr(from_path);
22144 basic_json v = result.at(from_ptr);
22149 operation_add(ptr, v);
22153 case patch_operations::test:
22155 bool success =
false;
22160 success = (result.at(ptr) == get_value(
"test",
"value",
false));
22170 JSON_THROW(other_error::create(501,
"unsuccessful: " + val.dump()));
22180 JSON_THROW(parse_error::create(105, 0,
"operation value '" + op +
"' is invalid"));
22223 const std::string& path =
"")
22229 if (source == target)
22234 if (source.type() != target.type())
22239 {
"op",
"replace"}, {
"path", path}, {
"value", target}
22244 switch (source.type())
22246 case value_t::array:
22250 while (i < source.size()
and i < target.size())
22253 auto temp_diff = diff(source[i], target[i], path +
"/" + std::to_string(i));
22254 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
22262 const auto end_index =
static_cast<difference_type>(result.size());
22263 while (i < source.size())
22267 result.insert(result.begin() + end_index, object(
22270 {
"path", path +
"/" + std::to_string(i)}
22276 while (i < target.size())
22281 {
"path", path +
"/" + std::to_string(i)},
22282 {
"value", target[i]}
22290 case value_t::object:
22293 for (
auto it = source.cbegin(); it != source.cend(); ++it)
22296 const auto key = json_pointer::escape(it.key());
22298 if (target.find(it.key()) != target.end())
22301 auto temp_diff = diff(it.value(), target[it.key()], path +
"/" + key);
22302 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
22307 result.push_back(object(
22309 {
"op",
"remove"}, {
"path", path +
"/" + key}
22315 for (
auto it = target.cbegin(); it != target.cend(); ++it)
22317 if (source.find(it.key()) == source.end())
22320 const auto key = json_pointer::escape(it.key());
22323 {
"op",
"add"}, {
"path", path +
"/" + key},
22324 {
"value", it.value()}
22337 {
"op",
"replace"}, {
"path", path}, {
"value", target}
22397 void merge_patch(
const basic_json& apply_patch)
22399 if (apply_patch.is_object())
22401 if (
not is_object())
22405 for (
auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
22407 if (it.value().is_null())
22413 operator[](it.key()).merge_patch(it.value());
22419 *
this = apply_patch;
22452 struct hash<nlohmann::json>
22459 std::size_t operator()(
const nlohmann::json& j)
const 22462 const auto& h = hash<nlohmann::json::string_t>();
22463 return h(j.dump());
22471 struct less< ::nlohmann::detail::value_t>
22477 bool operator()(
nlohmann::detail::value_t lhs,
22478 nlohmann::detail::value_t rhs)
const noexcept 22480 return nlohmann::detail::operator<(lhs, rhs);
22490 inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2)
noexcept(
22491 is_nothrow_move_constructible<nlohmann::json>::value
and 22492 is_nothrow_move_assignable<nlohmann::json>::value
22514 inline nlohmann::json operator
"" _json(
const char* s, std::size_t n)
22516 return nlohmann::json::parse(s, s + n);
22533 inline nlohmann::json::json_pointer operator
"" _json_pointer(
const char* s, std::size_t n)
22535 return nlohmann::json::json_pointer(std::string(s, n));
22542 #if defined(__clang__
) || defined(__GNUC__
) || defined(__GNUG__
) 22543 #pragma GCC diagnostic pop 22545 #if defined(__clang__
) 22546 #pragma GCC diagnostic pop 22550 #undef JSON_INTERNAL_CATCH 22554 #undef JSON_HAS_CPP_14 22555 #undef JSON_HAS_CPP_17 22556 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION 22557 #undef NLOHMANN_BASIC_JSON_TPL 22560 #undef JSON_HEDLEY_ALWAYS_INLINE 22561 #undef JSON_HEDLEY_ARM_VERSION 22562 #undef JSON_HEDLEY_ARM_VERSION_CHECK 22563 #undef JSON_HEDLEY_ARRAY_PARAM 22564 #undef JSON_HEDLEY_ASSUME 22565 #undef JSON_HEDLEY_BEGIN_C_DECLS 22566 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE 22567 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN 22568 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE 22569 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE 22570 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION 22571 #undef JSON_HEDLEY_CLANG_HAS_FEATURE 22572 #undef JSON_HEDLEY_CLANG_HAS_WARNING 22573 #undef JSON_HEDLEY_COMPCERT_VERSION 22574 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK 22575 #undef JSON_HEDLEY_CONCAT 22576 #undef JSON_HEDLEY_CONCAT_EX 22577 #undef JSON_HEDLEY_CONST 22578 #undef JSON_HEDLEY_CONSTEXPR 22579 #undef JSON_HEDLEY_CONST_CAST 22580 #undef JSON_HEDLEY_CPP_CAST 22581 #undef JSON_HEDLEY_CRAY_VERSION 22582 #undef JSON_HEDLEY_CRAY_VERSION_CHECK 22583 #undef JSON_HEDLEY_C_DECL 22584 #undef JSON_HEDLEY_DEPRECATED 22585 #undef JSON_HEDLEY_DEPRECATED_FOR 22586 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL 22587 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED 22588 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS 22589 #undef JSON_HEDLEY_DIAGNOSTIC_POP 22590 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH 22591 #undef JSON_HEDLEY_DMC_VERSION 22592 #undef JSON_HEDLEY_DMC_VERSION_CHECK 22593 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION 22594 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK 22595 #undef JSON_HEDLEY_END_C_DECLS 22596 #undef JSON_HEDLEY_FALL_THROUGH 22597 #undef JSON_HEDLEY_FLAGS 22598 #undef JSON_HEDLEY_FLAGS_CAST 22599 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE 22600 #undef JSON_HEDLEY_GCC_HAS_BUILTIN 22601 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE 22602 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE 22603 #undef JSON_HEDLEY_GCC_HAS_EXTENSION 22604 #undef JSON_HEDLEY_GCC_HAS_FEATURE 22605 #undef JSON_HEDLEY_GCC_HAS_WARNING 22606 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK 22607 #undef JSON_HEDLEY_GCC_VERSION 22608 #undef JSON_HEDLEY_GCC_VERSION_CHECK 22609 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE 22610 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN 22611 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE 22612 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE 22613 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION 22614 #undef JSON_HEDLEY_GNUC_HAS_FEATURE 22615 #undef JSON_HEDLEY_GNUC_HAS_WARNING 22616 #undef JSON_HEDLEY_GNUC_VERSION 22617 #undef JSON_HEDLEY_GNUC_VERSION_CHECK 22618 #undef JSON_HEDLEY_HAS_ATTRIBUTE 22619 #undef JSON_HEDLEY_HAS_BUILTIN 22620 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE 22621 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE 22622 #undef JSON_HEDLEY_HAS_EXTENSION 22623 #undef JSON_HEDLEY_HAS_FEATURE 22624 #undef JSON_HEDLEY_HAS_WARNING 22625 #undef JSON_HEDLEY_IAR_VERSION 22626 #undef JSON_HEDLEY_IAR_VERSION_CHECK 22627 #undef JSON_HEDLEY_IBM_VERSION 22628 #undef JSON_HEDLEY_IBM_VERSION_CHECK 22629 #undef JSON_HEDLEY_IMPORT 22630 #undef JSON_HEDLEY_INLINE 22631 #undef JSON_HEDLEY_INTEL_VERSION 22632 #undef JSON_HEDLEY_INTEL_VERSION_CHECK 22633 #undef JSON_HEDLEY_IS_CONSTANT 22634 #undef JSON_HEDLEY_LIKELY 22635 #undef JSON_HEDLEY_MALLOC 22636 #undef JSON_HEDLEY_MESSAGE 22637 #undef JSON_HEDLEY_MSVC_VERSION 22638 #undef JSON_HEDLEY_MSVC_VERSION_CHECK 22639 #undef JSON_HEDLEY_NEVER_INLINE 22640 #undef JSON_HEDLEY_NON_NULL 22641 #undef JSON_HEDLEY_NO_RETURN 22642 #undef JSON_HEDLEY_NO_THROW 22643 #undef JSON_HEDLEY_PELLES_VERSION 22644 #undef JSON_HEDLEY_PELLES_VERSION_CHECK 22645 #undef JSON_HEDLEY_PGI_VERSION 22646 #undef JSON_HEDLEY_PGI_VERSION_CHECK 22647 #undef JSON_HEDLEY_PREDICT 22648 #undef JSON_HEDLEY_PRINTF_FORMAT 22649 #undef JSON_HEDLEY_PRIVATE 22650 #undef JSON_HEDLEY_PUBLIC 22651 #undef JSON_HEDLEY_PURE 22652 #undef JSON_HEDLEY_REINTERPRET_CAST 22653 #undef JSON_HEDLEY_REQUIRE 22654 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR 22655 #undef JSON_HEDLEY_REQUIRE_MSG 22656 #undef JSON_HEDLEY_RESTRICT 22658 #undef JSON_HEDLEY_SENTINEL 22659 #undef JSON_HEDLEY_STATIC_ASSERT 22660 #undef JSON_HEDLEY_STATIC_CAST 22661 #undef JSON_HEDLEY_STRINGIFY 22662 #undef JSON_HEDLEY_STRINGIFY_EX 22663 #undef JSON_HEDLEY_SUNPRO_VERSION 22664 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK 22665 #undef JSON_HEDLEY_TINYC_VERSION 22666 #undef JSON_HEDLEY_TINYC_VERSION_CHECK 22667 #undef JSON_HEDLEY_TI_VERSION 22668 #undef JSON_HEDLEY_TI_VERSION_CHECK 22669 #undef JSON_HEDLEY_UNAVAILABLE 22670 #undef JSON_HEDLEY_UNLIKELY 22671 #undef JSON_HEDLEY_UNPREDICTABLE 22672 #undef JSON_HEDLEY_UNREACHABLE 22673 #undef JSON_HEDLEY_UNREACHABLE_RETURN 22674 #undef JSON_HEDLEY_VERSION 22675 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR 22676 #undef JSON_HEDLEY_VERSION_DECODE_MINOR 22677 #undef JSON_HEDLEY_VERSION_DECODE_REVISION 22678 #undef JSON_HEDLEY_VERSION_ENCODE 22679 #undef JSON_HEDLEY_WARNING #define NLOHMANN_BASIC_JSON_TPL_DECLARATION
basic_json(const BasicJsonType &val)
create a JSON value from an existing one
basic_json(const value_t v)
create an empty value with a given type
virtual bool key(string_t &val)=0
an object key was read
virtual bool number_float(number_float_t val, const string_t &s)=0
an floating-point number was read
basic_json(std::nullptr_t=nullptr) noexcept
create a null object
#define JSON_HEDLEY_FALL_THROUGH
virtual bool end_object()=0
the end of an object was read
#define JSON_HEDLEY_ARM_VERSION_CHECK(major, minor, patch)
virtual bool null()=0
a null value was read
#define JSON_HEDLEY_IAR_VERSION_CHECK(major, minor, patch)
static allocator_type get_allocator()
returns the allocator associated with the container
#define JSON_HEDLEY_CONST
static basic_json meta()
returns version information on the library
#define JSON_HEDLEY_UNLIKELY(expr)
static auto to_json(BasicJsonType &j, ValueType &&val) noexcept(noexcept(::nlohmann::to_json(j, std::forward< ValueType >(val)))) -> decltype(::nlohmann::to_json(j, std::forward< ValueType >(val)), void())
convert any value type to a JSON value
#define NLOHMANN_JSON_VERSION_MAJOR
#define JSON_HEDLEY_UNPREDICTABLE(expr)
virtual bool number_unsigned(number_unsigned_t val)=0
an unsigned integer number was read
#define JSON_HEDLEY_TI_VERSION_CHECK(major, minor, patch)
#define JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
static basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
#define JSON_HEDLEY_PGI_VERSION_CHECK(major, minor, patch)
std::string to_string() const
return a string representation of the JSON pointer
virtual bool start_object(std::size_t elements)=0
the beginning of an object was read
#define JSON_HEDLEY_IS_CONSTANT(expr)
static basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
#define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute, major, minor, patch)
#define JSON_INTERNAL_CATCH(exception)
#define JSON_HEDLEY_HAS_FEATURE(feature)
#define JSON_HEDLEY_GCC_VERSION_CHECK(major, minor, patch)
basic_json(size_type cnt, const basic_json &val)
construct an array with count copies of given value
#define JSON_HEDLEY_DIAGNOSTIC_PUSH
constexpr const auto & from_json
virtual ~json_sax()=default
#define JSON_CATCH(exception)
#define JSON_HEDLEY_HAS_EXTENSION(extension)
#define JSON_HEDLEY_CRAY_VERSION_CHECK(major, minor, patch)
namespace for Niels Lohmann
friend bool operator!=(json_pointer const &lhs, json_pointer const &rhs) noexcept
compares two JSON pointers for inequality
void push_back(initializer_list_t init)
add an object to an object
#define JSON_HEDLEY_PRAGMA(value)
basic_json(initializer_list_t init, bool type_deduction=true, value_t manual_type=value_t::array)
create a container (array or object) from an initializer list
#define JSON_HEDLEY_DEPRECATED(since)
#define NLOHMANN_BASIC_JSON_TPL
#define JSON_HEDLEY_HAS_BUILTIN(builtin)
#define NLOHMANN_JSON_VERSION_PATCH
virtual bool number_integer(number_integer_t val)=0
an integer number was read
virtual bool start_array(std::size_t elements)=0
the beginning of an array was read
#define NLOHMANN_JSON_VERSION_MINOR
static auto from_json(BasicJsonType &&j, ValueType &val) noexcept(noexcept(::nlohmann::from_json(std::forward< BasicJsonType >(j), val))) -> decltype(::nlohmann::from_json(std::forward< BasicJsonType >(j), val), void())
convert a JSON value to any value type
#define JSON_THROW(exception)
#define JSON_HEDLEY_INTEL_VERSION_CHECK(major, minor, patch)
constexpr const auto & to_json
#define JSON_HEDLEY_MSVC_VERSION_CHECK(major, minor, patch)
virtual bool end_array()=0
the end of an array was read
#define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
#define JSON_HEDLEY_IBM_VERSION_CHECK(major, minor, patch)
virtual bool string(string_t &val)=0
a string was read
basic_json(CompatibleType &&val) noexcept(noexcept(JSONSerializer< U >::to_json(std::declval< basic_json_t &>(), std::forward< CompatibleType >(val))))
create a JSON value
#define JSON_HEDLEY_NON_NULL(...)
#define JSON_HEDLEY_LIKELY(expr)
virtual bool boolean(bool val)=0
a boolean value was read
friend struct detail::external_constructor
virtual bool parse_error(std::size_t position, const std::string &last_token, const detail::exception &ex)=0
a parse error occurred
basic_json(basic_json &&other) noexcept
move constructor
#define JSON_HEDLEY_DIAGNOSTIC_POP
#define JSON_HEDLEY_TINYC_VERSION_CHECK(major, minor, patch)
#define JSON_HEDLEY_CONSTEXPR
#define JSON_HEDLEY_HAS_WARNING(warning)
json_pointer(const std::string &s="")
create JSON pointer
#define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major, minor, patch)