Skip to content

JSON for Modern C++ version 3.10.0

Compare
Choose a tag to compare
@nlohmann nlohmann released this 17 Aug 18:41
· 344 commits to develop since this release
f42a74b

Release date: 2021-08-17
SHA-256: 230f3a03cefd586661ebab577a347c973d97a770afb89e22c52abc3c2a19d0a7 (json.hpp), b5e3bfad07feba218a26a4f809fbb0d1e33450524bf5d7244cabc92cf8178c69 (include.zip)

Summary

JSON for Modern C++ 3.10.0 is the first release for over a year. It contains some new features and a lot of minor changes and bug fixes.

Most notably, it introduces extended diagnostics. By defining JSON_DIAGNOSTICS before including the json.hpp, a JSON Pointer is added to exceptions which helps to debug issues with object access, array indices, or mismatching types.

Another important change behind the curtains is a fully overworked CI which performs a lot of checks for every commit which should allow more frequent releases in the future.

All changes are backward-compatible.

💰 Note you can support this project via GitHub sponsors or PayPal.

✨ New Features

  • Add extended diagnostics information by adding a JSON Pointer to the exception messages indicating the exact location of a invalid type errors or out-of-bound errors.

     [json.exception.type_error.302] (/address/housenumber) type must be number, but is string
    

    Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed diagnostics messages, and hence debugging, difficult. To create better diagnostics messages, each JSON value needs a pointer to its parent value such that a global context (i.e., a path from the root value to the value that lead to the exception) can be created. That global context is then provided as a JSON Pointer.

    As this global context comes at the price of storing one additional pointer per JSON value and runtime overhead to maintain the parent relation, extended diagnostics are disabled by default. They can, however, be enabled by defining the preprocessor symbol JSON_DIAGNOSTICS to 1 before including json.hpp. See the documentation for more information. #932 #1508 #2562 #2838 #2866

  • Add a GDB pretty printer to facilitate reading basic_json values in GDB. #1952 #2607

  • Add a new value store to the cbor_tag_handler_t which allows to store the tags of CBOR values as binary subtypes. #2863 #2908

  • Add support for containers with non-default-constructible types. #2574 #2576

🐛 Bug Fixes

  • Fix a regression bug that failed ordered_json to be used when exceptions were switched off. #2347 #2725 #2934
  • Added iterator range insertion for ordered_json. #2490 #2512
  • Change the type of binary subtypes to std::uint64_t to support subtypes >255. Furthermore, the return value of the subtype() function has been fixed to the documented value -1 in case no subtype is given. #2863 #2908
  • Fix move constructor of internal json_ref type which created null values when compiled with -fno-elide-constructors. #2387 #2405
  • Fix the compilation of input_adapter for containers in edge cases. #2553
  • Allow parsing from std::byte containers. #2413 #2546 #2550 #2602 #2869
  • Fix memory leak in to_json in case a JSON value is reused. #2865 #2872
  • Fix compilation error in case symbol EOF was not found. #2755 #2756
  • Fix Compilation error when using NLOHMANN_JSON_SERIALIZE_ENUM with ordered_json on libc++. #2491 #2825

Warnings

A lot of warnings have been fixed in this release. To make sure the library remains warning-free, the CI now breaks in case a warning is found in GCC (261 warning flags), Clang (flag -Weverything with 8 exceptions), or MSVC (flag /W4).

⚡ Improvements

Tests and CI

The CI chain has been completely overworked and now runs mostly on a dedicated Docker Image that contains all relevant tools.

  • Collected all CI calls in a CMake file which can be enabled by setting JSON_CI.
  • Linux now builds with Clang 3.5 to 12 and GCC 4.8 to 11 checking multiple C++ standards. #2540
  • Windows builds with MSVC 2015 to 2019, MinGW (GCC 8), Clang 11 and 12, and Clang-CL 11 checking multiple C++ standards.
  • Mac builds with Xcode 10.2.1 to Xcode 12.4 checking multiple C++ standards. #1798 #2561 #2737 #2790 #2817
  • Use static analysis tools Clang-Tidy, Cppcheck, Valgrind, Google Sanitizers, Clang Static Analyzer, Cpplint, and Facebook Infer.
  • Add internal checks for CMake flags, switched off exceptions, header amalgamation, self-contained headers, and exclusion of certain tests via CTest.
  • Providers: Move most Travis/AppVeyor builds to GitHub Actions. Use Drone CI for aarch64 build. Remove FOSSA. Properly select "Release" build for Travis CI builds. #2375 #2689
  • Remove #define private public hack from test files. Instead, macro JSON_PRIVATE_UNLESS_TESTED is used in the source code which defaults to private, but can be set to public to test internals. #43 #913 #1985 #2352

CMake

  • Fixed issue in CMake file that overwrote CMAKE_CXX_COMPILER when the test suite was compiled. #2344 #2384
  • Only enable CMake options JSON_BuildTests and JSON_Install by default when the library is the main project. #2513 #2514
  • Add CTest label not_reproducible to skip non-reproducible tests. #2324 #2560
  • Formatted CMake files ##2770
  • Add a CMake option JSON_SystemInclude to use SYSTEM in target_include_directories. #2762
  • Add CMake option JSON_FastTests (OFF by default) to which slow test suite.

Documentation

  • Fixed typos in the documentation. #2354 #2754
  • Extended documentation for discarded values and is_discarded() function. #2360 #2363
  • Fix Markdown of README. #2582
  • Fix example in README file. #2625 #2659
  • Fix example in parse exceptions documentation. #2679
  • Overworked documentation of number handling. #2747
  • Add link to Conan Center package to README. #2771
  • Added example for CPM.cmake. #2406
  • Update README to use HTTPS everywhere. #2789
  • Fixed consistency of using declarations in README. #2826
  • Fix documentation of tests that required a Git checkout. #2845
  • Fix code samples in GIF slideshow. #2457
  • Update documentation to reference RFC 8259 as JSON standard.
  • Add section on how to get support to README file.
  • Replaced links to Doxygen documentation with new API documentation.
  • Documented the effect of a bug in Microsoft's STL that makes what() member function of exception objects unusable in case _HAS_EXCEPTIONS=0 is set. #2824

Thirdparty

🔨 Further Changes

  • Use C++14 constructs where available. #2533
  • Fix pkg-config.pc generation. #2690
  • Add possibility to set the C++ standard via macros JSON_HAS_CPP_11, JSON_HAS_CPP_14, JSON_HAS_CPP_17, and JSON_HAS_CPP_20. By defining any of these symbols, the internal check is overridden and the provided C++ version is unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be detected incorrectly. #2730 #2731 #2749
  • Add preprocessor symbol JSON_NO_IO. When defined, headers <cstdio>, <ios>, <iosfwd>, <istream>, and <ostream> are not included and parse functions relying on these headers are excluded. This is relevant for environment where these I/O functions are disallowed for security reasons (e.g., Intel Software Guard Extensions (SGX)). #2728 #2729 #2842 #2861
  • Benchmarks are handled via FetchContent and require CMake version 3.11. Removed Google Benchmark copy. Fix default branch name for Google Benchmarks. #2795 #2796
  • Simplify object parser for CBOR. #2879 #2598
  • Cleaned up maintainer Makefiles

Licensing

  • Clarified license of is_complete_type implementation. #2534
  • License fix for integer_sequence and index_sequence implementation. #2683

🔥 Deprecated functions

Passing iterator pairs or pointer/length pairs to parsing functions (basic_json::parse, basic_json::accept, basic_json::sax_parse, basic_json::from_cbor, basic_json::from_msgpack, basic_json::from_ubjson, basic_json::from_bson) via initializer lists is deprecated. Instead, pass two iterators; for instance, call basic_json::from_cbor(ptr, ptr+len) instead of basic_json::from_cbor({ptr, len}).

The following functions have been deprecated in earlier versions and will be removed in the next major version (i.e., 4.0.0):

All deprecations are annotated with HEDLEY_DEPRECATED_FOR to report which function to use instead.