diff --git a/.circleci/config.yml b/.circleci/config.yml index 91ceaf7f6c..82e5098408 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -44,5 +44,13 @@ workflows: version: 2 build_and_test_all: jobs: - - build_stable - - build_bleeding_edge + - build_stable: + filters: + branches: + ignore: + gh-pages + - build_bleeding_edge: + filters: + branches: + ignore: + gh-pages diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 2eefc1c35b..1a47a885c0 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -4,7 +4,6 @@ on: [push, pull_request] jobs: build: - runs-on: ubuntu-latest steps: diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 5846cf7500..1778c94182 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -15,20 +15,6 @@ jobs: - name: test run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure - clang9: - runs-on: windows-latest - - steps: - - uses: actions/checkout@v1 - - name: install Clang - run: curl -fsSL -o LLVM9.exe https://releases.llvm.org/9.0.0/LLVM-9.0.0-win64.exe ; 7z x LLVM9.exe -y -o"C:/Program Files/LLVM" - - name: cmake - run: cmake -S . -B build -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe" -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure - clang10: runs-on: windows-latest diff --git a/.gitignore b/.gitignore index 7e5a881e33..bf938c8ea8 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ doc/mkdocs/docs/examples doc/mkdocs/site doc/mkdocs/docs/__pycache__/ doc/xml +/doc/docset/nlohmann_json.docset/ diff --git a/.travis.yml b/.travis.yml index 6311adb16c..bdabc88a74 100644 --- a/.travis.yml +++ b/.travis.yml @@ -124,18 +124,6 @@ matrix: # OSX / Clang - - os: osx - osx_image: xcode9.3 - - - os: osx - osx_image: xcode9.4 - - - os: osx - osx_image: xcode10 - - - os: osx - osx_image: xcode10.1 - - os: osx osx_image: xcode10.2 @@ -222,7 +210,7 @@ matrix: compiler: gcc env: - COMPILER=g++-9 - - CXXFLAGS=-std=c++2a + - CXX_STANDARD=17 addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -306,11 +294,11 @@ matrix: compiler: clang env: - COMPILER=clang++-7 - - CXXFLAGS=-std=c++1z + - CXX_STANDARD=17 addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-7'] - packages: ['g++-6', 'clang-7', 'ninja-build'] + packages: ['g++-7', 'clang-7', 'ninja-build'] ################ # build script # @@ -334,6 +322,9 @@ script: # by default, use implicit conversions - if [[ "${IMPLICIT_CONVERSIONS}" == "" ]]; then export IMPLICIT_CONVERSIONS=ON; fi + # append CXX_STANDARD to CMAKE_OPTIONS if required + - CMAKE_OPTIONS+=${CXX_STANDARD:+ -DCMAKE_CXX_STANDARD=$CXX_STANDARD -DCMAKE_CXX_STANDARD_REQUIRED=ON} + # compile and execute unit tests - mkdir -p build && cd build - cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -DJSON_ImplicitConversions=${IMPLICIT_CONVERSIONS} -DJSON_BuildTests=On -GNinja && cmake --build . --config Release diff --git a/CMakeLists.txt b/CMakeLists.txt index fa77a5aed2..44ede3e799 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,15 @@ cmake_minimum_required(VERSION 3.1) ## project(nlohmann_json VERSION 3.9.1 LANGUAGES CXX) +## +## MAIN_PROJECT CHECK +## determine if nlohmann_json is built as a subproject (using add_subdirectory) or if it is the main project +## +set(MAIN_PROJECT OFF) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MAIN_PROJECT ON) +endif() + ## ## INCLUDE ## @@ -21,8 +30,8 @@ if (POLICY CMP0077) cmake_policy(SET CMP0077 NEW) endif () -option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ON) -option(JSON_Install "Install CMake targets during install step." ON) +option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${MAIN_PROJECT}) +option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT}) option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF) option(JSON_ImplicitConversions "Enable implicit conversions." ON) @@ -101,9 +110,8 @@ CONFIGURE_FILE( ## TESTS ## create and configure the unit test target ## -include(CTest) #adds option BUILD_TESTING (default ON) - -if(BUILD_TESTING AND JSON_BuildTests) +if (JSON_BuildTests) + include(CTest) enable_testing() add_subdirectory(test) endif() diff --git a/Makefile b/Makefile index 98fa365afe..0b9aa8213c 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,6 @@ doctest: # -Wno-documentation-unknown-command: code uses user-defined commands like @complexity # -Wno-exit-time-destructors: warning in json code triggered by NLOHMANN_JSON_SERIALIZE_ENUM # -Wno-float-equal: not all comparisons in the tests can be replaced by Approx -# -Wno-keyword-macro: unit-tests use "#define private public" # -Wno-missing-prototypes: for NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE # -Wno-padded: padding is nothing to warn about # -Wno-range-loop-analysis: items tests "for(const auto i...)" @@ -98,7 +97,6 @@ pedantic_clang: -Wno-documentation-unknown-command \ -Wno-exit-time-destructors \ -Wno-float-equal \ - -Wno-keyword-macro \ -Wno-missing-prototypes \ -Wno-padded \ -Wno-range-loop-analysis \ @@ -610,7 +608,7 @@ ChangeLog.md: release: rm -fr release_files mkdir release_files - zip -9 --recurse-paths -X include.zip $(SRCS) $(AMALGAMATED_FILE) meson.build + zip -9 --recurse-paths -X include.zip $(SRCS) $(AMALGAMATED_FILE) meson.build LICENSE.MIT gpg --armor --detach-sig include.zip mv include.zip include.zip.asc release_files gpg --armor --detach-sig $(AMALGAMATED_FILE) @@ -629,7 +627,7 @@ clean: rm -fr json_unit json_benchmarks fuzz fuzz-testing *.dSYM test/*.dSYM oclint_report.html rm -fr benchmarks/files/numbers/*.json rm -fr cmake-3.1.0-Darwin64.tar.gz cmake-3.1.0-Darwin64 - rm -fr cmake-build-coverage cmake-build-benchmarks fuzz-testing cmake-build-clang-analyze cmake-build-pvs-studio cmake-build-infer cmake-build-clang-sanitize cmake_build + rm -fr cmake-build-coverage cmake-build-benchmarks cmake-build-pedantic fuzz-testing cmake-build-clang-analyze cmake-build-pvs-studio cmake-build-infer cmake-build-clang-sanitize cmake_build $(MAKE) clean -Cdoc ########################################################################## diff --git a/README.md b/README.md index ba257d9460..cb7e85a31c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ [![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/3lCHrFUZANONKv7a) [![Documentation](https://img.shields.io/badge/docs-doxygen-blue.svg)](https://nlohmann.github.io/json/doxygen/index.html) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT) -[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnlohmann%2Fjson.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnlohmann%2Fjson?ref=badge_shield) [![GitHub Releases](https://img.shields.io/github/release/nlohmann/json.svg)](https://github.com/nlohmann/json/releases) [![GitHub Downloads](https://img.shields.io/github/downloads/nlohmann/json/total)](https://github.com/nlohmann/json/releases) [![GitHub Issues](https://img.shields.io/github/issues/nlohmann/json.svg)](https://github.com/nlohmann/json/issues) @@ -76,6 +75,7 @@ You can sponsor this library at [GitHub Sponsors](https://github.com/sponsors/nl - [Michael Hartmann](https://github.com/reFX-Mike) - [Stefan Hagen](https://github.com/sthagen) - [Steve Sperandeo](https://github.com/homer6) +- [Robert Jefe Lindstädt](https://github.com/eljefedelrodeodeljefe) Thanks everyone! @@ -231,6 +231,15 @@ Please file issues [here](https://github.com/build2-packaging/nlohmann-json) if If you are using [`wsjcpp`](https://wsjcpp.org), you can use the command `wsjcpp install "https://github.com/nlohmann/json:develop"` to get the latest version. Note you can change the branch ":develop" to an existing tag or another branch. +If you are using [`CPM.cmake`](https://github.com/TheLartians/CPM.cmake), you can check this [`example`](https://github.com/TheLartians/CPM.cmake/tree/master/examples/json). After [adding CPM script](https://github.com/TheLartians/CPM.cmake#adding-cpm) to your project, implement the following snippet to your CMake: + +```cmake +CPMAddPackage( + NAME nlohmann_json + GITHUB_REPOSITORY nlohmann/json + VERSION 3.9.1) +``` + ### Pkg-config If you are using bare Makefiles, you can use `pkg-config` to generate the include flags that point to where the library is installed: @@ -247,7 +256,7 @@ json = dependency('nlohmann_json', required: true) ## Examples -Beside the examples below, you may want to check the [documentation](https://nlohmann.github.io/json/) where each function contains a separate code example (e.g., check out [`emplace()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5338e282d1d02bed389d852dd670d98d.html#a5338e282d1d02bed389d852dd670d98d)). All [example files](https://github.com/nlohmann/json/tree/develop/doc/examples) can be compiled and executed on their own (e.g., file [emplace.cpp](https://github.com/nlohmann/json/blob/develop/doc/examples/emplace.cpp)). +Beside the examples below, you may want to check the [documentation](https://nlohmann.github.io/json/) where each function contains a separate code example (e.g., check out [`emplace()`](https://nlohmann.github.io/json/api/basic_json/emplace/)). All [example files](https://github.com/nlohmann/json/tree/develop/doc/examples) can be compiled and executed on their own (e.g., file [emplace.cpp](https://github.com/nlohmann/json/blob/develop/doc/examples/emplace.cpp)). ### JSON as first-class data type @@ -316,7 +325,7 @@ json j2 = { }; ``` -Note that in all these cases, you never need to "tell" the compiler which JSON value type you want to use. If you want to be explicit or express some edge cases, the functions [`json::array()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a9ad7ec0bc1082ed09d10900fbb20a21f.html#a9ad7ec0bc1082ed09d10900fbb20a21f) and [`json::object()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_aaf509a7c029100d292187068f61c99b8.html#aaf509a7c029100d292187068f61c99b8) will help: +Note that in all these cases, you never need to "tell" the compiler which JSON value type you want to use. If you want to be explicit or express some edge cases, the functions [`json::array()`](https://nlohmann.github.io/json/api/basic_json/array/) and [`json::object()`](https://nlohmann.github.io/json/api/basic_json/object/) will help: ```cpp // a way to express the empty array [] @@ -351,7 +360,7 @@ auto j2 = R"( Note that without appending the `_json` suffix, the passed string literal is not parsed, but just used as JSON string value. That is, `json j = "{ \"happy\": true, \"pi\": 3.141 }"` would just store the string `"{ "happy": true, "pi": 3.141 }"` rather than parsing the actual object. -The above example can also be expressed explicitly using [`json::parse()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a265a473e939184aa42655c9ccdf34e58.html#a265a473e939184aa42655c9ccdf34e58): +The above example can also be expressed explicitly using [`json::parse()`](https://nlohmann.github.io/json/api/basic_json/parse/): ```cpp // parse explicitly @@ -394,9 +403,9 @@ std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.get()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_aa6602bb24022183ab989439e19345d08.html#aa6602bb24022183ab989439e19345d08) returns the originally stored string value. +[`.dump()`](https://nlohmann.github.io/json/api/basic_json/dump/) returns the originally stored string value. -Note the library only supports UTF-8. When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a50ec80b02d0f3f51130d4abb5d1cfdc5.html#a50ec80b02d0f3f51130d4abb5d1cfdc5) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers. +Note the library only supports UTF-8. When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/api/basic_json/dump/) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers. #### To/from streams (e.g. files, string streams) @@ -884,7 +893,7 @@ Some important things: * Those methods **MUST** be in your type's namespace (which can be the global namespace), or the library will not be able to locate them (in this example, they are in namespace `ns`, where `person` is defined). * Those methods **MUST** be available (e.g., proper headers must be included) everywhere you use these conversions. Look at [issue 1108](https://github.com/nlohmann/json/issues/1108) for errors that may occur otherwise. * When using `get()`, `your_type` **MUST** be [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). (There is a way to bypass this requirement described later.) -* In function `from_json`, use function [`at()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a93403e803947b86f4da2d1fb3345cf2c.html#a93403e803947b86f4da2d1fb3345cf2c) to access the object values rather than `operator[]`. In case a key does not exist, `at` throws an exception that you can handle, whereas `operator[]` exhibits undefined behavior. +* In function `from_json`, use function [`at()`](https://nlohmann.github.io/json/api/basic_json/at/) to access the object values rather than `operator[]`. In case a key does not exist, `at` throws an exception that you can handle, whereas `operator[]` exhibits undefined behavior. * You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these. #### Simplify your life with macros @@ -1222,10 +1231,6 @@ The following compilers are currently used in continuous integration at [Travis] | Compiler | Operating System | CI Provider | |-----------------------------------------------------------------|--------------------|----------------| -| Apple Clang 9.1.0 (clang-902.0.39.1); Xcode 9.3 | macOS 10.13.3 | Travis | -| Apple Clang 9.1.0 (clang-902.0.39.2); Xcode 9.4.1 | macOS 10.13.6 | Travis | -| Apple Clang 10.0.0 (clang-1000.11.45.2); Xcode 10.0 | macOS 10.13.6 | Travis | -| Apple Clang 10.0.0 (clang-1000.11.45.5); Xcode 10.1 | macOS 10.13.6 | Travis | | Apple Clang 10.0.1 (clang-1001.0.46.4); Xcode 10.2.1 | macOS 10.14.4 | Travis | | Apple Clang 11.0.0 (clang-1100.0.33.12); Xcode 11.2.1 | macOS 10.14.6 | Travis | | Apple Clang 11.0.3 (clang-1103.0.32.59); Xcode 11.4.1 | macOS 10.15.4 | GitHub Actions | @@ -1574,7 +1579,7 @@ The library supports **Unicode input** as follows: - [Unicode noncharacters](https://www.unicode.org/faq/private_use.html#nonchar1) will not be replaced by the library. - Invalid surrogates (e.g., incomplete pairs such as `\uDEAD`) will yield parse errors. - The strings stored in the library are UTF-8 encoded. When using the default string type (`std::string`), note that its length/size functions return the number of stored bytes rather than the number of characters or glyphs. -- When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a50ec80b02d0f3f51130d4abb5d1cfdc5.html#a50ec80b02d0f3f51130d4abb5d1cfdc5) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers. +- When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/api/basic_json/dump/) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers. ### Comments in JSON @@ -1608,7 +1613,7 @@ Here is a related issue [#1924](https://github.com/nlohmann/json/issues/1924). ### Further notes -- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a233b02b0839ef798942dd46157cc0fe6.html#a233b02b0839ef798942dd46157cc0fe6) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a73ae333487310e3302135189ce8ff5d8.html#a73ae333487310e3302135189ce8ff5d8). Furthermore, you can define `JSON_ASSERT(x)` to replace calls to `assert(x)`. +- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/api/basic_json/operator%5B%5D/) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/api/basic_json/at/). Furthermore, you can define `JSON_ASSERT(x)` to replace calls to `assert(x)`. - As the exact type of a number is not defined in the [JSON specification](https://tools.ietf.org/html/rfc8259.html), this library tries to choose the best fitting C++ number type automatically. As a result, the type `double` may be used to store numbers which may yield [**floating-point exceptions**](https://github.com/nlohmann/json/issues/181) in certain rare situations if floating-point exceptions have been unmasked in the calling code. These exceptions are not caused by the library and need to be fixed in the calling code, such as by re-masking the exceptions prior to calling library functions. - The code can be compiled without C++ **runtime type identification** features; that is, you can use the `-fno-rtti` compiler flag. - **Exceptions** are used widely within the library. They can, however, be switched off with either using the compiler flag `-fno-exceptions` or by defining the symbol `JSON_NOEXCEPTION`. In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER` (overriding `throw`), `JSON_TRY_USER` (overriding `try`), and `JSON_CATCH_USER` (overriding `catch`). Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior. @@ -1628,3 +1633,5 @@ $ ctest --output-on-failure Note that during the `ctest` stage, several JSON test files are downloaded from an [external repository](https://github.com/nlohmann/json_test_data). If policies forbid downloading artifacts during testing, you can download the files yourself and pass the directory with the test files via `-DJSON_TestDataDirectory=path` to CMake. Then, no Internet connectivity is required. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information. In case you have downloaded the library rather than checked out the code via Git, test `cmake_fetch_content_configure`. Please execute `ctest -LE git_required` to skip these tests. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information. + +As Intel compilers use unsafe floating point optimization by default, the unit tests may fail. Use flag [`/fp:precise`](https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-model-fp.html) then. diff --git a/cmake/download_test_data.cmake b/cmake/download_test_data.cmake index a3f3f199f1..f516a7c3b2 100644 --- a/cmake/download_test_data.cmake +++ b/cmake/download_test_data.cmake @@ -48,7 +48,7 @@ message(STATUS "Operating system: ${OS_VERSION_STRINGS}") # determine the compiler (for debug and support purposes) if (MSVC) execute_process(COMMAND ${CMAKE_CXX_COMPILER} OUTPUT_VARIABLE CXX_VERSION_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE CXX_VERSION_RESULT ERROR_STRIP_TRAILING_WHITESPACE) - set(CMAKE_CXX_COMPILER "${CXX_VERSION_RESULT}; MSVC_VERSION=${MSVC_VERSION}; MSVC_TOOLSET_VERSION=${MSVC_TOOLSET_VERSION}") + set(CXX_VERSION_RESULT "${CXX_VERSION_RESULT}; MSVC_VERSION=${MSVC_VERSION}; MSVC_TOOLSET_VERSION=${MSVC_TOOLSET_VERSION}") else() execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE CXX_VERSION_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE) endif() diff --git a/doc/Makefile b/doc/Makefile index 35e8b5aa69..9addd3401f 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -44,6 +44,8 @@ check_output: $(EXAMPLES:.cpp=.test) clean: rm -fr me.nlohmann.json.docset html xml $(EXAMPLES:.cpp=) + $(MAKE) clean -C docset + $(MAKE) clean -C mkdocs ########################################################################## diff --git a/doc/docset/Info.plist b/doc/docset/Info.plist new file mode 100644 index 0000000000..772ec08afa --- /dev/null +++ b/doc/docset/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleIdentifier + nlohmann_json + CFBundleName + JSON for Modern C++ + DocSetPlatformFamily + json + isDashDocset + + dashIndexFilePath + index.html + DashDocSetFallbackURL + https://nlohmann.github.io/json/ + isJavaScriptEnabled + + + diff --git a/doc/docset/Makefile b/doc/docset/Makefile new file mode 100644 index 0000000000..262540a0c2 --- /dev/null +++ b/doc/docset/Makefile @@ -0,0 +1,21 @@ +nlohmann_json.docset: Info.plist docSet.sql + $(MAKE) clean + mkdir -p nlohmann_json.docset/Contents/Resources/Documents/ + cp info.plist nlohmann_json.docset/Contents + # build and copy documentation + $(MAKE) build -C ../mkdocs + cp -r ../mkdocs/site/* nlohmann_json.docset/Contents/Resources/Documents + # patch CSS to hide navigation items + echo "\n\nheader, footer, navi, div.md-sidebar--primary, nav.md-tabs--active, a.md-content__button { display: none; }" >> nlohmann_json.docset/Contents/Resources/Documents/assets/stylesheets/main.b5d04df8.min.css + # fix spacing + echo "\n\ndiv.md-sidebar div.md-sidebar--secondary, div.md-main__inner { top: 0; margin-top: 0 }" >> nlohmann_json.docset/Contents/Resources/Documents/assets/stylesheets/main.b5d04df8.min.css + # remove "JSON for Modern C++" from page titles + find nlohmann_json.docset/Contents/Resources/Documents -type f -exec gsed -i 's| - JSON for Modern C++||' {} + + # clean up + rm nlohmann_json.docset/Contents/Resources/Documents/hooks.py + rm nlohmann_json.docset/Contents/Resources/Documents/sitemap.* + # generate index + sqlite3 nlohmann_json.docset/Contents/Resources/docSet.dsidx < docSet.sql + +clean: + rm -fr nlohmann_json.docset diff --git a/doc/docset/README.md b/doc/docset/README.md new file mode 100644 index 0000000000..b0dd7f81e7 --- /dev/null +++ b/doc/docset/README.md @@ -0,0 +1,13 @@ +# docset + +The folder contains the required files to create a [docset](https://kapeli.com/docsets) which can be used in +documentation browsers like [Dash](https://kapeli.com/dash), [Velocity](https://velocity.silverlakesoftware.com), or +[Zeal](https://zealdocs.org). + +The docset can be created with + +```sh +make nlohmann_json.docset +``` + +The generated folder `nlohmann_json.docset` can then be opened in the documentation browser. diff --git a/doc/docset/docSet.sql b/doc/docset/docSet.sql new file mode 100644 index 0000000000..2436120357 --- /dev/null +++ b/doc/docset/docSet.sql @@ -0,0 +1,140 @@ +DROP TABLE IF EXISTS searchIndex; +CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT); +CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path); + +-- API +INSERT INTO searchIndex(name, type, path) VALUES ('accept', 'Function', 'api/basic_json/accept/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('adl_serializer', 'Class', 'api/adl_serializer/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('array', 'Function', 'api/basic_json/array/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('array_t', 'Type', 'api/basic_json/array_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('at', 'Method', 'api/basic_json/at/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('back', 'Method', 'api/basic_json/back/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('basic_json', 'Class', 'api/basic_json/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('basic_json', 'Constructor', 'api/basic_json/basic_json/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('begin', 'Method', 'api/basic_json/begin/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('binary', 'Function', 'api/basic_json/binary/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('binary_t', 'Type', 'api/basic_json/binary_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('boolean_t', 'Type', 'api/basic_json/boolean_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('cbegin', 'Method', 'api/basic_json/cbegin/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('cbor_tag_handler_t', 'Enum', 'api/basic_json/cbor_tag_handler_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('cend', 'Method', 'api/basic_json/cend/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('clear', 'Method', 'api/basic_json/clear/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('contains', 'Method', 'api/basic_json/contains/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('count', 'Method', 'api/basic_json/count/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('crbegin', 'Method', 'api/basic_json/crbegin/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('crend', 'Method', 'api/basic_json/crend/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('diff', 'Function', 'api/basic_json/diff/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('dump', 'Method', 'api/basic_json/dump/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('emplace', 'Method', 'api/basic_json/emplace/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('emplace_back', 'Method', 'api/basic_json/emplace_back/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('empty', 'Method', 'api/basic_json/empty/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('end', 'Method', 'api/basic_json/end/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('erase', 'Method', 'api/basic_json/erase/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('error_handler_t', 'Enum', 'api/basic_json/error_handler_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('exception', 'Class', 'api/basic_json/exception/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('find', 'Method', 'api/basic_json/find/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('flatten', 'Method', 'api/basic_json/flatten/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('from_bson', 'Function', 'api/basic_json/from_bson/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('from_cbor', 'Function', 'api/basic_json/from_cbor/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('from_msgpack', 'Function', 'api/basic_json/from_msgpack/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('from_ubjson', 'Function', 'api/basic_json/from_ubjson/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('front', 'Method', 'api/basic_json/front/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('get', 'Method', 'api/basic_json/get/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('get_allocator', 'Function', 'api/basic_json/get_allocator/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('get_binary', 'Method', 'api/basic_json/get_binary/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('get_ptr', 'Method', 'api/basic_json/get_ptr/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('get_ref', 'Method', 'api/basic_json/get_ref/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('get_to', 'Method', 'api/basic_json/get_to/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('input_format_t', 'Enum', 'api/basic_json/input_format_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('insert', 'Method', 'api/basic_json/insert/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('invalid_iterator', 'Class', 'api/basic_json/invalid_iterator/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_array', 'Method', 'api/basic_json/is_array/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_binary', 'Method', 'api/basic_json/is_binary/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_boolean', 'Method', 'api/basic_json/is_boolean/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_discarded', 'Method', 'api/basic_json/is_discarded/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_null', 'Method', 'api/basic_json/is_null/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_number', 'Method', 'api/basic_json/is_number/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_number_float', 'Method', 'api/basic_json/is_number_float/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_number_integer', 'Method', 'api/basic_json/is_number_integer/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_number_unsigned', 'Method', 'api/basic_json/is_number_unsigned/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_object', 'Method', 'api/basic_json/is_object/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_primitive', 'Method', 'api/basic_json/is_primitive/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_string', 'Method', 'api/basic_json/is_string/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('is_structured', 'Method', 'api/basic_json/is_structured/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('items', 'Method', 'api/basic_json/items/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('json', 'Class', 'api/json/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('json_pointer', 'Class', 'api/json_pointer/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('json_serializer', 'Type', 'api/basic_json/json_serializer/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('max_size', 'Method', 'api/basic_json/max_size/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('merge_patch', 'Method', 'api/basic_json/merge_patch/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('meta', 'Function', 'api/basic_json/meta/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('number_float_t', 'Type', 'api/basic_json/number_float_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('number_integer_t', 'Type', 'api/basic_json/number_integer_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('number_unsigned_t', 'Type', 'api/basic_json/number_unsigned_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('object', 'Function', 'api/basic_json/object/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('object_comparator_t', 'Type', 'api/basic_json/object_comparator_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('object_t', 'Type', 'api/basic_json/object_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator!=', 'Operator', 'api/basic_json/operator_ne/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator+=', 'Operator', 'api/basic_json/operator+=/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator=', 'Operator', 'api/basic_json/operator=/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator==', 'Operator', 'api/basic_json/operator_eq/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator<', 'Operator', 'api/basic_json/operator_lt/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator<=', 'Operator', 'api/basic_json/operator_le/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator>', 'Operator', 'api/basic_json/operator_gt/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator>=', 'Operator', 'api/basic_json/operator_ge/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator[]', 'Operator', 'api/basic_json/operator[]/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator""_json', 'Literal', 'api/basic_json/operator_literal_json/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator""_json_pointer', 'Literal', 'api/basic_json/operator_literal_json_pointer/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator ValueType', 'Operator', 'api/basic_json/operator_ValueType/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('operator value_t', 'Operator', 'api/basic_json/operator_value_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('ordered_json', 'Class', 'api/ordered_json/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('ordered_map', 'Class', 'api/ordered_map/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('out_of_range', 'Class', 'api/basic_json/out_of_range/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('other_error', 'Class', 'api/basic_json/other_error/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('parse', 'Function', 'api/basic_json/parse/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('parse_error', 'Class', 'api/basic_json/parse_error/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('parse_event_t', 'Enum', 'api/basic_json/parse_event_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('parser_callback_t', 'Type', 'api/basic_json/parser_callback_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('patch', 'Method', 'api/basic_json/patch/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('push_back', 'Method', 'api/basic_json/push_back/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('rbegin', 'Method', 'api/basic_json/rbegin/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('rend', 'Method', 'api/basic_json/rend/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('sax_parse', 'Function', 'api/basic_json/sax_parse/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('size', 'Method', 'api/basic_json/size/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('string_t', 'Type', 'api/basic_json/string_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('type', 'Method', 'api/basic_json/type/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('type_error', 'Class', 'api/basic_json/type_error/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('type_name', 'Method', 'api/basic_json/type_name/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('unflatten', 'Method', 'api/basic_json/unflatten/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('update', 'Method', 'api/basic_json/update/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('to_bson', 'Function', 'api/basic_json/to_bson/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('to_cbor', 'Function', 'api/basic_json/to_cbor/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('to_msgpack', 'Function', 'api/basic_json/to_msgpack/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('to_ubjson', 'Function', 'api/basic_json/to_ubjson/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('value', 'Method', 'api/basic_json/value/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('value_t', 'Enum', 'api/basic_json/value_t/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('~basic_json', 'Method', 'api/basic_json/~basic_json/index.html'); + +-- Features +INSERT INTO searchIndex(name, type, path) VALUES ('Binary Formats', 'Guide', 'features/binary_formats/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('BSON', 'Guide', 'features/binary_formats/bson/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('CBOR', 'Guide', 'features/binary_formats/cbor/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('MessagePack', 'Guide', 'features/binary_formats/messagepack/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('UBJSON', 'Guide', 'features/binary_formats/ubjson/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('Supported Macros', 'Guide', 'features/macros/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('Binary Values', 'Guide', 'features/binary_values/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('Comments', 'Guide', 'features/comments/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('Iterators', 'Guide', 'features/iterators/index.html'); +INSERT INTO searchIndex(name, type, path) VALUES ('Types', 'Guide', 'features/types/index.html'); + +-- Macros +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_ASSERT', 'Macro', 'features/macros/index.html#json_assertx'); +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_CATCH_USER', 'Macro', 'features/macros/index.html#json_catch_userexception'); +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_NOEXCEPTION', 'Macro', 'features/macros/index.html#json_noexception'); +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_SKIP_UNSUPPORTED_COMPILER_CHECK', 'Macro', 'features/macros/index.html#json_skip_unsupported_compiler_check'); +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_THROW_USER', 'Macro', 'features/macros/index.html#json_throw_userexception'); +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_TRY_USER', 'Macro', 'features/macros/index.html#json_try_user'); +INSERT INTO searchIndex(name, type, path) VALUES ('JSON_USE_IMPLICIT_CONVERSIONS', 'Macro', 'features/macros/index.html#json_use_implicit_conversions'); +INSERT INTO searchIndex(name, type, path) VALUES ('NLOHMANN_DEFINE_TYPE_INTRUSIVE', 'Macro', 'features/macros/index.html#nlohmann_define_type_intrusivetype-member'); +INSERT INTO searchIndex(name, type, path) VALUES ('NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE', 'Macro', 'features/macros/index.html#nlohmann_define_type_non_intrusivetype-member'); +INSERT INTO searchIndex(name, type, path) VALUES ('NLOHMANN_JSON_SERIALIZE_ENUM', 'Macro', 'features/macros/index.html#nlohmann_json_serialize_enumtype'); diff --git a/doc/examples/parse__allow_exceptions.cpp b/doc/examples/parse__allow_exceptions.cpp new file mode 100644 index 0000000000..82449a526a --- /dev/null +++ b/doc/examples/parse__allow_exceptions.cpp @@ -0,0 +1,36 @@ +#include +#include + +using json = nlohmann::json; + +int main() +{ + // an invalid JSON text + std::string text = R"( + { + "key": "value without closing quotes + } + )"; + + // parse with exceptions + try + { + json j = json::parse(text); + } + catch (json::parse_error& e) + { + std::cout << e.what() << std::endl; + } + + // parse without exceptions + json j = json::parse(text, nullptr, false); + + if (j.is_discarded()) + { + std::cout << "the input is invalid JSON" << std::endl; + } + else + { + std::cout << "the input is valid JSON: " << j << std::endl; + } +} diff --git a/doc/examples/parse__allow_exceptions.link b/doc/examples/parse__allow_exceptions.link new file mode 100644 index 0000000000..386dfe8e48 --- /dev/null +++ b/doc/examples/parse__allow_exceptions.link @@ -0,0 +1 @@ +online \ No newline at end of file diff --git a/doc/examples/parse__allow_exceptions.output b/doc/examples/parse__allow_exceptions.output new file mode 100644 index 0000000000..d650824d28 --- /dev/null +++ b/doc/examples/parse__allow_exceptions.output @@ -0,0 +1,2 @@ +[json.exception.parse_error.101] parse error at line 4, column 0: syntax error while parsing value - invalid string: control character U+000A (LF) must be escaped to \u000A or \n; last read: '"value without closing quotes' +the input is invalid JSON diff --git a/doc/mkdocs/Makefile b/doc/mkdocs/Makefile index bb97420a13..85bc6a9201 100644 --- a/doc/mkdocs/Makefile +++ b/doc/mkdocs/Makefile @@ -2,6 +2,9 @@ serve: prepare_files venv/bin/mkdocs serve +build: prepare_files + venv/bin/mkdocs build + # create files that are not versioned inside the mkdocs folder prepare_files: clean # build Doxygen @@ -9,7 +12,7 @@ prepare_files: clean # create subfolders mkdir docs/images docs/examples # copy images - cp -vr ../json.gif ../images/range-begin-end.svg ../images/range-rbegin-rend.svg docs/images + cp -vr ../json.gif ../images/range-begin-end.svg ../images/range-rbegin-rend.svg ../images/callback_events.png docs/images # copy examples cp -vr ../examples/*.cpp ../examples/*.output docs/examples diff --git a/doc/mkdocs/docs/api/adl_serializer.md b/doc/mkdocs/docs/api/adl_serializer.md new file mode 100644 index 0000000000..7b79747f57 --- /dev/null +++ b/doc/mkdocs/docs/api/adl_serializer.md @@ -0,0 +1,31 @@ +# adl_serializer + +```cpp +template +struct adl_serializer; +``` + +Serializer that uses ADL ([Argument-Dependent Lookup](https://en.cppreference.com/w/cpp/language/adl)) to choose +`to_json`/`from_json` functions from the types' namespaces. + +It is implemented similar to + +```cpp +template +struct adl_serializer { + template + static void to_json(BasicJsonType& j, const T& value) { + // calls the "to_json" method in T's namespace + } + + template + static void from_json(const BasicJsonType& j, T& value) { + // same thing, but with the "from_json" method + } +}; +``` + +## Member functions + +- **from_json** - convert a JSON value to any value type +- **to_json** - convert any value type to a JSON value diff --git a/doc/mkdocs/docs/api/basic_json/accept.md b/doc/mkdocs/docs/api/basic_json/accept.md new file mode 100644 index 0000000000..b18c5391a1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/accept.md @@ -0,0 +1,90 @@ +# basic_json::accept + +```cpp +// (1) +template +static bool accept(InputType&& i, + const bool ignore_comments = false); + +// (2) +template +static bool accept(IteratorType first, IteratorType last, + const bool ignore_comments = false); +``` + +Checks whether the input is valid JSON. + +1. Reads from a compatible input. +2. Reads from a pair of character iterators + + The value_type of the iterator must be a integral type with size of 1, 2 or 4 bytes, which will be interpreted + respectively as UTF-8, UTF-16 and UTF-32. + +Unlike the [`parse`](parse.md) function, this function neither throws an exception in case of invalid JSON input +(i.e., a parse error) nor creates diagnostic information. + +## Template parameters + +`InputType` +: A compatible input, for instance: + + - an `std::istream` object + - a `FILE` pointer + - a C-style array of characters + - a pointer to a null-terminated string of single byte characters + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators. + +`IteratorType` +: a compatible iterator type + +## Parameters + +`i` (in) +: Input to parse from. + +`ignore_comments` (in) +: whether comments should be ignored and treated like whitespace (`#!cpp true`) or yield a parse error + (`#!cpp false`); (optional, `#!cpp false` by default) + +`first` (in) +: iterator to start of character range + +`last` (in) +: iterator to end of character range + +## Return value + +Whether the input is valid JSON. + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the length of the input. The parser is a predictive LL(1) parser. + +## Notes + +(1) A UTF-8 byte order mark is silently ignored. + +## Examples + +??? example + + The example below demonstrates the `accept()` function reading from a string. + + ```cpp + --8<-- "examples/accept__string.cpp" + ``` + + Output: + + ```json + --8<-- "examples/accept__string.output" + ``` + +## Version history + +- Added in version 3.0.0. +- Ignoring comments via `ignore_comments` added in version 3.9.0. diff --git a/doc/mkdocs/docs/api/basic_json/array.md b/doc/mkdocs/docs/api/basic_json/array.md new file mode 100644 index 0000000000..89113026d3 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/array.md @@ -0,0 +1,55 @@ +# basic_json::array + +```cpp +static basic_json array(initializer_list_t init = {}); +``` + +Creates a JSON array value from a given initializer list. That is, given a list of values `a, b, c`, creates the JSON +value `#!json [a, b, c]`. If the initializer list is empty, the empty array `#!json []` is created. + +## Parameters + +`init` (in) +: initializer list with JSON values to create an array from (optional) + +## Return value + +JSON array value + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of `init`. + +## Notes + +This function is only needed to express two edge cases that cannot be realized with the initializer list constructor +([`basic_json(initializer_list_t, bool, value_t)`](basic_json.md)). These cases are: + +1. creating an array whose elements are all pairs whose first element is a string -- in this case, the initializer list + constructor would create an object, taking the first elements as keys +2. creating an empty array -- passing the empty initializer list to the initializer list constructor yields an empty + object + +## Examples + +??? example + + The following code shows an example for the `array` function. + + ```cpp + --8<-- "examples/array.cpp" + ``` + + Output: + + ```json + --8<-- "examples/array.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/array_t.md b/doc/mkdocs/docs/api/basic_json/array_t.md new file mode 100644 index 0000000000..89e39dbf83 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/array_t.md @@ -0,0 +1,52 @@ +# basic_json::array_t + +```cpp +using array_t = ArrayType>; +``` + +The type used to store JSON arrays. + +[RFC 7159](http://rfc7159.net/rfc7159) describes JSON arrays as follows: +> An array is an ordered sequence of zero or more values. + +To store objects in C++, a type is defined by the template parameters explained below. + +## Template parameters + +`ArrayType` +: container type to store arrays (e.g., `std::vector` or `std::list`) + +`AllocatorType` +: the allocator to use for objects (e.g., `std::allocator`) + +## Notes + +#### Default type + +With the default values for `ArrayType` (`std::vector`) and `AllocatorType` (`std::allocator`), the default value for +`array_t` is: + +```cpp +std::vector< + basic_json, // value_type + std::allocator // allocator_type +> +``` + +#### Limits + +[RFC 7159](http://rfc7159.net/rfc7159) specifies: +> An implementation may set limits on the maximum depth of nesting. + +In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be +introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the +[`max_size`](max_size.md) function of a JSON array. + +#### Storage + +Arrays are stored as pointers in a `basic_json` type. That is, for any access to array values, a pointer of type +`#!cpp array_t*` must be dereferenced. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/at.md b/doc/mkdocs/docs/api/basic_json/at.md new file mode 100644 index 0000000000..75977b1807 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/at.md @@ -0,0 +1,171 @@ +# basic_json::at + +```cpp +// (1) +reference at(size_type idx); +const_reference at(size_type idx) const; + +// (2) +reference at(const typename object_t::key_type& key); +const_reference at(const typename object_t::key_type& key) const; + +// (3) +reference at(const json_pointer& ptr); +const_reference at(const json_pointer& ptr) const; +``` + +1. Returns a reference to the element at specified location `idx`, with bounds checking. +2. Returns a reference to the element at with specified key `key`, with bounds checking. +3. Returns a reference to the element at with specified JSON pointer `ptr`, with bounds checking. + +## Parameters + +`idx` (in) +: index of the element to access + +`key` (in) +: object key of the elements to remove + +`ptr` (in) +: JSON pointer to the desired element + +## Return value + +1. reference to the element at index `idx` +2. reference to the element at key `key` +3. reference to the element pointed to by `ptr` + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.304`](../../home/exceptions.md#jsonexceptiontype_error304) if the JSON value is not an array; + in this case, calling `at` with an index makes no sense. See example below. + - Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) if the index `idx` is out of + range of the array; that is, `idx >= size()`. See example below. +2. The function can throw the following exceptions: + - Throws [`type_error.304`](../../home/exceptions.md#jsonexceptiontype_error304) if the JSON value is not an object; + in this case, calling `at` with a key makes no sense. See example below. + - Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if the key `key` is is not + stored in the object; that is, `find(key) == end()`. See example below. +3. The function can throw the following exceptions: + - Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index in the passed + JSON pointer `ptr` begins with '0'. See example below. + - Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in the passed + JSON pointer `ptr` is not a number. See example below. + - Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) if an array index in the passed + JSON pointer `ptr` is out of range. See example below. + - Throws [`out_of_range.402`](../../home/exceptions.md#jsonexceptionout_of_range402) if the array index '-' is used + in the passed JSON pointer `ptr`. As `at` provides checked access (and no elements are implicitly inserted), the + index '-' is always invalid. See example below. + - Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if the JSON pointer describes a + key of an object which cannot be found. See example below. + - Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can + not be resolved. See example below. + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +1. Constant +2. Logarithmic in the size of the container. +3. Constant + +## Example + +??? example + + The example below shows how array elements can be read and written using `at()`. It also demonstrates the different + exceptions that can be thrown. + + ```cpp + --8<-- "examples/at__size_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/at__size_type.output" + ``` + +??? example + + The example below shows how array elements can be read using `at()`. It also demonstrates the different exceptions + that can be thrown. + + ```cpp + --8<-- "examples/at__size_type_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/at__size_type_const.output" + ``` + +??? example + + The example below shows how object elements can be read and written using `at()`. It also demonstrates the different + exceptions that can be thrown. + + ```cpp + --8<-- "examples/at__object_t_key_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/at__object_t_key_type.output" + ``` + +??? example + + The example below shows how object elements can be read using `at()`. It also demonstrates the different exceptions + that can be thrown. + + ```cpp + --8<-- "examples/at__object_t_key_type_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/at__object_t_key_type_const.output" + ``` + +??? example + + The example below shows how object elements can be read and written using `at()`. It also demonstrates the different + exceptions that can be thrown. + + ```cpp + --8<-- "examples/at_json_pointer.cpp" + ``` + + Output: + + ```json + --8<-- "examples/at_json_pointer.output" + ``` + +??? example + + The example below shows how object elements can be read using `at()`. It also demonstrates the different exceptions + that can be thrown. + + ```cpp + --8<-- "examples/at_json_pointer_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/at_json_pointer_const.output" + ``` + +## Version history + +1. Added in version 1.0.0. +2. Added in version 1.0.0. +3. Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/back.md b/doc/mkdocs/docs/api/basic_json/back.md new file mode 100644 index 0000000000..1484153ae9 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/back.md @@ -0,0 +1,61 @@ +# basic_json::back + +```cpp +reference back(); + +const_reference back() const; +``` + +Returns a reference to the last element in the container. For a JSON container `c`, the expression `c.back()` is +equivalent to + +```cpp +auto tmp = c.end(); +--tmp; +return *tmp; +``` + +## Return value + +In case of a structured type (array or object), a reference to the last element is returned. In case of number, string, +boolean, or binary values, a reference to the value is returned. + +## Exceptions + +If the JSON value is `#!json null`, exception +[`invalid_iterator.214`](../../home/exceptions.md#jsonexceptioninvalid_iterator214) is thrown. + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Constant. + +## Note + +!!! danger + + Calling `back` on an empty array or object is undefined behavior and is **guarded by an assertion**! + +## Example + +??? example + + The following code shows an example for `back()`. + + ```cpp + --8<-- "examples/back.cpp" + ``` + + Output: + + ```json + --8<-- "examples/back.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Adjusted code to return reference to binary values in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/basic_json.md b/doc/mkdocs/docs/api/basic_json/basic_json.md new file mode 100644 index 0000000000..9ff6fbb8df --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/basic_json.md @@ -0,0 +1,394 @@ +# basic_json::basic_json + +```cpp +// 1 +basic_json(const value_t v); + +// 2 +basic_json(std::nullptr_t = nullptr) noexcept; + +// 3 +template +basic_json(CompatibleType&& val) noexcept(noexcept( + JSONSerializer::to_json(std::declval(), + std::forward(val)))); + +// 4 +template +basic_json(const BasicJsonType& val); + +// 5 +basic_json(initializer_list_t init, + bool type_deduction = true, + value_t manual_type = value_t::array); + +// 6 +basic_json(size_type cnt, const basic_json& val); + +// 7 +basic_json(iterator first, iterator last); +basic_json(const_iterator first, const_iterator last); + +// 8 +basic_json(const basic_json& other); + +// 9 +basic_json(basic_json&& other) noexcept; +``` + +1. Create an empty JSON value with a given type. The value will be default initialized with an empty value which depends + on the type: + + Value type | initial value + ----------- | ------------- + null | `#!json null` + boolean | `#!json false` + string | `#!json ""` + number | `#!json 0` + object | `#!json {}` + array | `#!json []` + binary | empty array + +2. Create a `#!json null` JSON value. It either takes a null pointer as parameter (explicitly creating `#!json null`) + or no parameter (implicitly creating `#!json null`). The passed null pointer itself is not read -- it is only used to + choose the right constructor. + +3. This is a "catch all" constructor for all compatible JSON types; that is, types for which a `to_json()` method + exists. The constructor forwards the parameter `val` to that method (to `json_serializer::to_json` method with + `U = uncvref_t`, to be exact). + + Template type `CompatibleType` includes, but is not limited to, the following types: + + - **arrays**: [`array_t`](array_t.md) and all kinds of compatible containers such as `std::vector`, `std::deque`, + `std::list`, `std::forward_list`, `std::array`, `std::valarray`, `std::set`, `std::unordered_set`, `std::multiset`, + and `std::unordered_multiset` with a `value_type` from which a `basic_json` value can be constructed. + - **objects**: [`object_t`](object_t.md) and all kinds of compatible associative containers such as `std::map`, + `std::unordered_map`, `std::multimap`, and `std::unordered_multimap` with a `key_type` compatible to `string_t` + and a `value_type` from which a `basic_json` value can be constructed. + - **strings**: `string_t`, string literals, and all compatible string containers can be used. + - **numbers**: [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md), + [`number_float_t`](number_float_t.md), and all convertible number types such as `int`, `size_t`, `int64_t`, `float` + or `double` can be used. + - **boolean**: `boolean_t` / `bool` can be used. + - **binary**: `binary_t` / `std::vector` may be used; unfortunately because string literals cannot be + distinguished from binary character arrays by the C++ type system, all types compatible with `const char*` will be + directed to the string constructor instead. This is both for backwards compatibility, and due to the fact that a + binary type is not a standard JSON type. + + See the examples below. + +4. This is a constructor for existing `basic_json` types. It does not hijack copy/move constructors, since the parameter + has different template arguments than the current ones. + + The constructor tries to convert the internal `m_value` of the parameter. + +5. Creates a JSON value of type array or object from the passed initializer list `init`. In case `type_deduction` is + `#!cpp true` (default), the type of the JSON value to be created is deducted from the initializer list `init` + according to the following rules: + + 1. If the list is empty, an empty JSON object value `{}` is created. + 2. If the list consists of pairs whose first element is a string, a JSON object value is created where the first + elements of the pairs are treated as keys and the second elements are as values. + 3. In all other cases, an array is created. + + The rules aim to create the best fit between a C++ initializer list and JSON values. The rationale is as follows: + + 1. The empty initializer list is written as `#!cpp {}` which is exactly an empty JSON object. + 2. C++ has no way of describing mapped types other than to list a list of pairs. As JSON requires that keys must be + of type string, rule 2 is the weakest constraint one can pose on initializer lists to interpret them as an + object. + 3. In all other cases, the initializer list could not be interpreted as JSON object type, so interpreting it as JSON + array type is safe. + + With the rules described above, the following JSON values cannot be expressed by an initializer list: + + - the empty array (`#!json []`): use `array(initializer_list_t)` with an empty initializer list in this case + - arrays whose elements satisfy rule 2: use `array(initializer_list_t)` with the same initializer list in this case + +6. Constructs a JSON array value by creating `cnt` copies of a passed value. In case `cnt` is `0`, an empty array is + created. + +7. Constructs the JSON value with the contents of the range `[first, last)`. The semantics depends on the different + types a JSON value can have: + + - In case of a `#!json null` type, [invalid_iterator.206](../../home/exceptions.md#jsonexceptioninvalid_iterator206) + is thrown. + - In case of other primitive types (number, boolean, or string), `first` must be `begin()` and `last` must be + `end()`. In this case, the value is copied. Otherwise, + [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) is thrown. + - In case of structured types (array, object), the constructor behaves as similar versions for `std::vector` or + `std::map`; that is, a JSON array or object is constructed from the values in the range. + +8. Creates a copy of a given JSON value. + +9. Move constructor. Constructs a JSON value with the contents of the given value `other` using move semantics. It + "steals" the resources from `other` and leaves it as JSON `#!json null` value. + +## Template parameters + +`CompatibleType` +: a type such that: + + - `CompatibleType` is not derived from `std::istream`, + - `CompatibleType` is not `basic_json` (to avoid hijacking copy/move constructors), + - `CompatibleType` is not a different `basic_json` type (i.e. with different template arguments) + - `CompatibleType` is not a `basic_json` nested type (e.g., `json_pointer`, `iterator`, etc.) + - `json_serializer` (with `U = uncvref_t`) has a `to_json(basic_json_t&, CompatibleType&&)` + method + +`BasicJsonType`: +: a type such that: + + - `BasicJsonType` is a `basic_json` type. + - `BasicJsonType` has different template arguments than `basic_json_t`. + +## Parameters + +`v` (in) +: the type of the value to create + +`val` (in) +: the value to be forwarded to the respective constructor + +`init` (in) +: initializer list with JSON values + +`type_deduction` (in) +: internal parameter; when set to `#!cpp true`, the type of the JSON value is deducted from the initializer list + `init`; when set to `#!cpp false`, the type provided via `manual_type` is forced. This mode is used by the functions + `array(initializer_list_t)` and `object(initializer_list_t)`. + +`manual_type` (in) +: internal parameter; when `type_deduction` is set to `#!cpp false`, the created JSON value will use the provided type + (only `value_t::array` and `value_t::object` are valid); when `type_deduction` is set to `#!cpp true`, this + parameter has no effect + +`cnt` (in) +: the number of JSON copies of `val` to create + +`first` (in) +: begin of the range to copy from (included) + +`last` (in) +: end of the range to copy from (excluded) + +`other` (in) +: the JSON value to copy/move + +## Exceptions + +1. / +2. The function does not throw exceptions. +3. / +4. / +5. The function can throw the following exceptions: + - Throws [`type_error.301`](../../home/exceptions.md#jsonexceptiontype_error301) if `type_deduction` is + `#!cpp false`, `manual_type` is `value_t::object`, but `init` contains an element which is not a pair whose first + element is a string. In this case, the constructor could not create an object. If `type_deduction` would have been + `#!cpp true`, an array would have been created. See `object(initializer_list_t)` for an example. +6. / +7. The function can throw the following exceptions: + - Throws [`invalid_iterator.201`](../../home/exceptions.md#jsonexceptioninvalid_iterator201) if iterators `first` + and `last` are not compatible (i.e., do not belong to the same JSON value). In this case, the range + `[first, last)` is undefined. + - Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if iterators `first` + and `last` belong to a primitive type (number, boolean, or string), but `first` does not point to the first + element any more. In this case, the range `[first, last)` is undefined. See example code below. + - Throws [`invalid_iterator.206`](../../home/exceptions.md#jsonexceptioninvalid_iterator206) if iterators `first` + and `last` belong to a `#!json null` value. In this case, the range `[first, last)` is undefined. +8. / +9. The function does not throw exceptions. + +## Exception safety + +1. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. +2. No-throw guarantee: this constructor never throws exceptions. +3. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no + `to_json()` function was provided), strong guarantee holds: if an exception is thrown, there are no changes to any + JSON value. +4. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no + `to_json()` function was provided), strong guarantee holds: if an exception is thrown, there are no changes to any + JSON value. +5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. +6. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. +7. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. +8. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. +9. No-throw guarantee: this constructor never throws exceptions. + +## Complexity + +1. Constant. +2. Constant. +3. Usually linear in the size of the passed `val`, also depending on the implementation of the called `to_json()` + method. +4. Usually linear in the size of the passed `val`, also depending on the implementation of the called `to_json()` + method. +5. Linear in the size of the initializer list `init`. +6. Linear in `cnt`. +7. Linear in distance between `first` and `last`. +8. Linear in the size of `other`. +9. Constant. + +## Notes + +- Overload 5: + + !!! note + + When used without parentheses around an empty initializer list, `basic_json()` is called instead of this + function, yielding the JSON `#!json null` value. + +- Overload 7: + + !!! info "Preconditions" + + - Iterators `first` and `last` must be initialized. **This precondition is enforced with an assertion (see + warning).** If assertions are switched off, a violation of this precondition yields undefined behavior. + - Range `[first, last)` is valid. Usually, this precondition cannot be checked efficiently. Only certain edge + cases are detected; see the description of the exceptions above. A violation of this precondition yields + undefined behavior. + + !!! warning + + A precondition is enforced with a runtime assertion that will result in calling `std::abort` if this + precondition is not met. Assertions can be disabled by defining `NDEBUG` at compile time. See + for more information. + +- Overload 8: + + !!! info "Postcondition" + + `#!cpp *this == other` + +- Overload 9: + + !!! info "Postconditions" + + - `#!cpp `*this` has the same value as `other` before the call. + - `other` is a JSON `#!json null` value + +## Example + +??? example + + The following code shows the constructor for different `value_t` values. + + ```cpp + --8<-- "examples/basic_json__value_t.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__value_t.output" + ``` + +??? example + + The following code shows the constructor with and without a null pointer parameter. + + ```cpp + --8<-- "examples/basic_json__nullptr_t.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__nullptr_t.output" + ``` + +??? example + + The following code shows the constructor with several compatible types. + + ```cpp + --8<-- "examples/basic_json__CompatibleType.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__CompatibleType.output" + ``` + +??? example + + The example below shows how JSON values are created from initializer lists. + + ```cpp + --8<-- "examples/basic_json__list_init_t.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__list_init_t.output" + ``` + +??? example + + The following code shows examples for creating arrays with several copies of a given value. + + ```cpp + --8<-- "examples/basic_json__size_type_basic_json.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__size_type_basic_json.output" + ``` + +??? example + + The example below shows several ways to create JSON values by specifying a subrange with iterators. + + ```cpp + --8<-- "examples/basic_json__InputIt_InputIt.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__InputIt_InputIt.output" + ``` + +??? example + + The following code shows an example for the copy constructor. + + ```cpp + --8<-- "examples/basic_json__basic_json.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__basic_json.output" + ``` + +??? example + + The code below shows the move constructor explicitly called via `std::move`. + + ```cpp + --8<-- "examples/basic_json__moveconstructor.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__moveconstructor.output" + ``` + +## Version history + +1. Since version 1.0.0. +2. Since version 1.0.0. +3. Since version 2.1.0. +4. Since version 3.2.0. +5. Since version 1.0.0. +6. Since version 1.0.0. +7. Since version 1.0.0. +8. Since version 1.0.0. +9. Since version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/begin.md b/doc/mkdocs/docs/api/basic_json/begin.md new file mode 100644 index 0000000000..25d93b8f8b --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/begin.md @@ -0,0 +1,42 @@ +# basic_json::begin + +```cpp +iterator begin() noexcept; +const_iterator begin() const noexcept; +``` + +Returns an iterator to the first element. + +![Illustration from cppreference.com](../../images/range-begin-end.svg) + +## Return value + +iterator to the first element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `begin()`. + + ```cpp + --8<-- "examples/begin.cpp" + ``` + + Output: + + ```json + --8<-- "examples/begin.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/binary.md b/doc/mkdocs/docs/api/basic_json/binary.md new file mode 100644 index 0000000000..0b1b9f48bb --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/binary.md @@ -0,0 +1,50 @@ +# basic_json::binary + +```cpp +// (1) +static basic_json binary(const typename binary_t::container_type& init); +static basic_json binary(typename binary_t::container_type&& init); + +// (2) +static basic_json binary(const typename binary_t::container_type& init, + std::uint8_t subtype); +static basic_json binary(typename binary_t::container_type&& init, + std::uint8_t subtype); +``` + +1. Creates a JSON binary array value from a given binary container. +2. Creates a JSON binary array value from a given binary container with subtype. + +Binary values are part of various binary formats, such as CBOR, MessagePack, and BSON. This constructor is used to +create a value for serialization to those formats. + +## Parameters + +`init` (in) +: container containing bytes to use as binary type + +`subtype` (in) +: subtype to use in CBOR, MessagePack, and BSON + +## Return value + +JSON binary array value + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of `init`; constant for `typename binary_t::container_type&& init` versions. + +## Notes + +Note, this function exists because of the difficulty in correctly specifying the correct template overload in the +standard value ctor, as both JSON arrays and JSON binary arrays are backed with some form of a `std::vector`. Because +JSON binary arrays are a non-standard extension it was decided that it would be best to prevent automatic initialization +of a binary array type, for backwards compatibility and so it does not happen on accident. + +## Version history + +- Added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/binary_t.md b/doc/mkdocs/docs/api/basic_json/binary_t.md new file mode 100644 index 0000000000..2d6cd574e3 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/binary_t.md @@ -0,0 +1,67 @@ +# basic_json::binary_t + +```cpp +using binary_t = byte_container_with_subtype; +``` + +This type is a type designed to carry binary data that appears in various serialized formats, such as CBOR's Major Type +2, MessagePack's bin, and BSON's generic binary subtype. This type is NOT a part of standard JSON and exists solely for +compatibility with these binary types. As such, it is simply defined as an ordered sequence of zero or more byte values. + +Additionally, as an implementation detail, the subtype of the binary data is carried around as a `std::uint8_t`, which +is compatible with both of the binary data formats that use binary subtyping, (though the specific numbering is +incompatible with each other, and it is up to the user to translate between them). + +[CBOR's RFC 7049](https://tools.ietf.org/html/rfc7049) describes this type as: +> Major type 2: a byte string. The string's length in bytes is represented following the rules for positive integers +> (major type 0). + +[MessagePack's documentation on the bin type +family](https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family) describes this type as: +> Bin format family stores an byte array in 2, 3, or 5 bytes of extra bytes in addition to the size of the byte array. + +[BSON's specifications](http://bsonspec.org/spec.html) describe several binary types; however, this type is intended to +represent the generic binary type which has the description: +> Generic binary subtype - This is the most commonly used binary subtype and should be the 'default' for drivers and +> tools. + +None of these impose any limitations on the internal representation other than the basic unit of storage be some type of +array whose parts are decomposable into bytes. + +The default representation of this binary format is a `#!cpp std::vector`, which is a very common way to +represent a byte array in modern C++. + +## Template parameters + +`BinaryType` +: container type to store arrays + +## Notes + +#### Default type + +The default values for `BinaryType` is `#!cpp std::vector`. + +#### Storage + +Binary Arrays are stored as pointers in a `basic_json` type. That is, for any access to array values, a pointer of the +type `#!cpp binary_t*` must be dereferenced. + +#### Notes on subtypes + +- CBOR + - Binary values are represented as byte strings. Subtypes are written as tags. + +- MessagePack + - If a subtype is given and the binary array contains exactly 1, 2, 4, 8, or 16 elements, the fixext family (fixext1, + fixext2, fixext4, fixext8) is used. For other sizes, the ext family (ext8, ext16, ext32) is used. The subtype is + then added as singed 8-bit integer. + - If no subtype is given, the bin family (bin8, bin16, bin32) is used. + +- BSON + - If a subtype is given, it is used and added as unsigned 8-bit integer. + - If no subtype is given, the generic binary subtype 0x00 is used. + +## Version history + +- Added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/boolean_t.md b/doc/mkdocs/docs/api/basic_json/boolean_t.md new file mode 100644 index 0000000000..926a5f9f46 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/boolean_t.md @@ -0,0 +1,26 @@ +# basic_json::boolean_t + +```cpp +using boolean_t = BooleanType; +``` + +The type used to store JSON booleans. + +[RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a type which differentiates the two literals +`#!json true` and `#!json false`. + +To store objects in C++, a type is defined by the template parameter `BooleanType` which chooses the type to use. + +## Notes + +#### Default type + +With the default values for `BooleanType` (`#!cpp bool`), the default value for `boolean_t` is `#!cpp bool`. + +#### Storage + +Boolean values are stored directly inside a `basic_json` type. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/cbegin.md b/doc/mkdocs/docs/api/basic_json/cbegin.md new file mode 100644 index 0000000000..132934a8fd --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/cbegin.md @@ -0,0 +1,41 @@ +# basic_json::cbegin + +```cpp +const_iterator cbegin() const noexcept; +``` + +Returns an iterator to the first element. + +![Illustration from cppreference.com](../../images/range-begin-end.svg) + +## Return value + +iterator to the first element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `cbegin()`. + + ```cpp + --8<-- "examples/cbegin.cpp" + ``` + + Output: + + ```json + --8<-- "examples/cbegin.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/cbor_tag_handler_t.md b/doc/mkdocs/docs/api/basic_json/cbor_tag_handler_t.md new file mode 100644 index 0000000000..ea417de55a --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/cbor_tag_handler_t.md @@ -0,0 +1,21 @@ +# basic_json::cbor_tag_handler_t + +```cpp +enum class cbor_tag_handler_t +{ + error, + ignore +}; +``` + +This enumeration is used in the [`from_cbor`](from_cbor.md) function to choose how to treat tags: + +error +: throw a `parse_error` exception in case of a tag + +ignore +: ignore tags + +## Version history + +- Added in version 3.9.0. diff --git a/doc/mkdocs/docs/api/basic_json/cend.md b/doc/mkdocs/docs/api/basic_json/cend.md new file mode 100644 index 0000000000..e5de7e94e8 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/cend.md @@ -0,0 +1,41 @@ +# basic_json::cend + +```cpp +const_iterator cend() const noexcept; +``` + +Returns an iterator to one past the last element. + +![Illustration from cppreference.com](../../images/range-begin-end.svg) + +## Return value + +iterator one past the last element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `cend()`. + + ```cpp + --8<-- "examples/cend.cpp" + ``` + + Output: + + ```json + --8<-- "examples/cend.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/clear.md b/doc/mkdocs/docs/api/basic_json/clear.md new file mode 100644 index 0000000000..5119b83890 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/clear.md @@ -0,0 +1,58 @@ +# basic_json::clear + +```cpp +void clear() noexcept; +``` + +Clears the content of a JSON value and resets it to the default value as if [`basic_json(value_t)`](basic_json.md) would +have been called with the current value type from [`type()`](type.md): + +Value type | initial value +----------- | ------------- +null | `null` +boolean | `false` +string | `""` +number | `0` +binary | An empty byte vector +object | `{}` +array | `[]` + +Has the same effect as calling + +```.cpp +*this = basic_json(type()); +``` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear in the size of the JSON value. + +## Notes + +All iterators, pointers and references related to this container are invalidated. + +## Example + +??? example + + The example below shows the effect of `clear()` to different + JSON types. + + ```cpp + --8<-- "examples/clear.cpp" + ``` + + Output: + + ```json + --8<-- "examples/clear.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Added support for binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/contains.md b/doc/mkdocs/docs/api/basic_json/contains.md new file mode 100644 index 0000000000..1616f0c2d1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/contains.md @@ -0,0 +1,56 @@ +# basic_json::contains + +```cpp +template +bool contains(KeyT && key) const; +``` + +Check whether an element exists in a JSON object with key equivalent to `key`. If the element is not found or the JSON +value is not an object, `#!cpp false` is returned. + +## Template parameters + +`KeyT` +: A type for an object key other than `basic_json::json_pointer`. + +## Parameters + +`key` (in) +: key value to check its existence. + +## Return value + +`#!cpp true` if an element with specified `key` exists. If no such element with such key is found or the JSON value is +not an object, `#!cpp false` is returned. + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +Logarithmic in the size of the JSON object. + +## Notes + +This method always returns `#!cpp false` when executed on a JSON type that is not an object. + +## Example + +??? example + + The example shows how `contains()` is used. + + ```cpp + --8<-- "examples/contains.cpp" + ``` + + Output: + + ```json + --8<-- "examples/contains.output" + ``` + +## Version history + +- Added in version 3.6.0. diff --git a/doc/mkdocs/docs/api/basic_json/count.md b/doc/mkdocs/docs/api/basic_json/count.md new file mode 100644 index 0000000000..34f1605d34 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/count.md @@ -0,0 +1,55 @@ +# basic_json::count + +```cpp +template +size_type count(KeyT&& key) const; +``` + +Returns the number of elements with key `key`. If `ObjectType` is the default `std::map` type, the return value will +always be `0` (`key` was not found) or `1` (`key` was found). + +## Template parameters + +`KeyT` +: A type for an object key. + +## Parameters + +`key` (in) +: key value of the element to count. + +## Return value + +Number of elements with key `key`. If the JSON value is not an object, the return value will be `0`. + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +Logarithmic in the size of the JSON object. + +## Notes + +This method always returns `0` when executed on a JSON type that is not an object. + +## Example + +??? example + + The example shows how `count()` is used. + + ```cpp + --8<-- "examples/count.cpp" + ``` + + Output: + + ```json + --8<-- "examples/count.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/crbegin.md b/doc/mkdocs/docs/api/basic_json/crbegin.md new file mode 100644 index 0000000000..14196627ef --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/crbegin.md @@ -0,0 +1,41 @@ +# basic_json::crbegin + +```cpp +const_reverse_iterator crbegin() const noexcept; +``` + +Returns an iterator to the reverse-beginning; that is, the last element. + +![Illustration from cppreference.com](../../images/range-rbegin-rend.svg) + +## Return value + +reverse iterator to the first element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `crbegin()`. + + ```cpp + --8<-- "examples/crbegin.cpp" + ``` + + Output: + + ```json + --8<-- "examples/crbegin.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/crend.md b/doc/mkdocs/docs/api/basic_json/crend.md new file mode 100644 index 0000000000..f98af7058c --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/crend.md @@ -0,0 +1,42 @@ +# basic_json::rend + +```cpp +const_reverse_iterator crend() const noexcept; +``` + +Returns an iterator to the reverse-end; that is, one before the first element. This element acts as a placeholder, +attempting to access it results in undefined behavior. + +![Illustration from cppreference.com](../../images/range-rbegin-rend.svg) + +## Return value + +reverse iterator to the element following the last element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `eend()`. + + ```cpp + --8<-- "examples/crend.cpp" + ``` + + Output: + + ```json + --8<-- "examples/crend.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/diff.md b/doc/mkdocs/docs/api/basic_json/diff.md new file mode 100644 index 0000000000..736d5fb4bf --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/diff.md @@ -0,0 +1,58 @@ +# basic_json::diff + +```cpp +static basic_json diff(const basic_json& source, + const basic_json& target); +``` + +Creates a [JSON Patch](http://jsonpatch.com) so that value `source` can be changed into the value `target` by calling +[`patch`](patch.md) function. + +For two JSON values `source` and `target`, the following code yields always `#!cpp true`: +```cpp +source.patch(diff(source, target)) == target; +``` + +## Parameters + +`source` (in) +: JSON value to compare from + +`target` (in) +: JSON value to compare against + +## Return value + +a JSON patch to convert the `source` to `target` + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the lengths of `source` and `target`. + +## Note + +Currently, only `remove`, `add`, and `replace` operations are generated. + +## Example + +??? example + + The following code shows how a JSON patch is created as a diff for two JSON values. + + ```cpp + --8<-- "examples/diff.cpp" + ``` + + Output: + + ```json + --8<-- "examples/diff.output" + ``` + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/dump.md b/doc/mkdocs/docs/api/basic_json/dump.md index 9ea6e356af..cad06ca928 100644 --- a/doc/mkdocs/docs/api/basic_json/dump.md +++ b/doc/mkdocs/docs/api/basic_json/dump.md @@ -4,36 +4,30 @@ string_t dump(const int indent = -1, const char indent_char = ' ', const bool ensure_ascii = false, - const error_handler_t error_handler = error_handler_t::strict) const + const error_handler_t error_handler = error_handler_t::strict) const; ``` -Serialization function for JSON values. The function tries to mimic -Python's `json.dumps()` function, and currently supports its `indent` -and `ensure_ascii` parameters. +Serialization function for JSON values. The function tries to mimic Python's `json.dumps()` function, and currently +supports its `indent` and `ensure_ascii` parameters. ## Parameters `indent` (in) -: If `indent` is nonnegative, then array elements and object - members will be pretty-printed with that indent level. An indent level of - `0` will only insert newlines. `-1` (the default) selects the most compact - representation. +: If `indent` is nonnegative, then array elements and object members will be pretty-printed with that indent level. An + indent level of `0` will only insert newlines. `-1` (the default) selects the most compact representation. `indent_char` (in) -: The character to use for indentation if `indent` is - greater than `0`. The default is ` ` (space). +: The character to use for indentation if `indent` is greater than `0`. The default is ` ` (space). `ensure_ascii` (in) -: If `ensure_ascii` is true, all non-ASCII characters - in the output are escaped with `\uXXXX` sequences, and the result consists - of ASCII characters only. +: If `ensure_ascii` is true, all non-ASCII characters in the output are escaped with `\uXXXX` sequences, and the + result consists of ASCII characters only. `error_handler` (in) -: how to react on decoding errors; there are three - possible values: `strict` (throws and exception in case a decoding error - occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD), - and `ignore` (ignore invalid UTF-8 sequences during serialization; all - bytes are copied to the output unchanged). +: how to react on decoding errors; there are three possible values (see [`error_handler_t`](error_handler_t.md): + `strict` (throws and exception in case a decoding error occurs; default), `replace` (replace invalid UTF-8 sequences + with U+FFFD), and `ignore` (ignore invalid UTF-8 sequences during serialization; all bytes are copied to the output + unchanged). ## Return value @@ -41,8 +35,7 @@ string containing the serialization of the JSON value ## Exception safety -Strong guarantee: if an exception is thrown, there are no -changes to any JSON value. +Strong guarantee: if an exception is thrown, there are no changes to any JSON value. ## Complexity @@ -59,9 +52,8 @@ Binary values are serialized as object containing two keys: ??? example - The following example shows the effect of different `indent`, - `indent_char`, and `ensure_ascii` parameters to the result of the - serialization. + The following example shows the effect of different `indent`, `indent_char`, and `ensure_ascii` parameters to the + result of the serialization. ```cpp --8<-- "examples/dump.cpp" diff --git a/doc/mkdocs/docs/api/basic_json/emplace.md b/doc/mkdocs/docs/api/basic_json/emplace.md new file mode 100644 index 0000000000..50a9c92e33 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/emplace.md @@ -0,0 +1,56 @@ +# basic_json::emplace + +```cpp +template +std::pair emplace(Args&& ... args); +``` + +Inserts a new element into a JSON object constructed in-place with the given `args` if there is no element with the key +in the container. If the function is called on a JSON null value, an empty object is created before appending the value +created from `args`. + +## Template parameters + +`Args` +: compatible types to create a `basic_json` object + +## Parameters + +`args` (in) +: arguments to forward to a constructor of `basic_json` + +## Return value + +a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and +a `#!cpp bool` denoting whether the insertion took place. + +## Exceptions + +Throws [`type_error.311`](../../home/exceptions.md#jsonexceptiontype_error311) when called on a type other than JSON +object or `#!json null`; example: `"cannot use emplace() with number"` + +## Complexity + +Logarithmic in the size of the container, O(log(`size()`)). + +## Examples + +??? example + + The example shows how `emplace()` can be used to add elements to a JSON object. Note how the `#!json null` value was + silently converted to a JSON object. Further note how no value is added if there was already one value stored with + the same key. + + ```cpp + --8<-- "examples/emplace.cpp" + ``` + + Output: + + ```json + --8<-- "examples/emplace.output" + ``` + +## Version history + +- Since version 2.0.8. diff --git a/doc/mkdocs/docs/api/basic_json/emplace_back.md b/doc/mkdocs/docs/api/basic_json/emplace_back.md new file mode 100644 index 0000000000..8a8af0c66e --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/emplace_back.md @@ -0,0 +1,54 @@ +# basic_json::emplace_back + +```cpp +template +reference emplace_back(Args&& ... args); +``` + +Creates a JSON value from the passed parameters `args` to the end of the JSON value. If the function is called on a JSON +`#!json null` value, an empty array is created before appending the value created from `args`. + +## Template parameters + +`Args` +: compatible types to create a `basic_json` object + +## Parameters + +`args` (in) +: arguments to forward to a constructor of `basic_json` + +## Return value + +reference to the inserted element + +## Exceptions + +Throws [`type_error.311`](../../home/exceptions.md#jsonexceptiontype_error311) when called on a type other than JSON +array or `#!json null`; example: `"cannot use emplace_back() with number"` + +## Complexity + +Amortized constant. + +## Examples + +??? example + + The example shows how `emplace_back()` can be used to add elements to a JSON array. Note how the `null` value was + silently converted to a JSON array. + + ```cpp + --8<-- "examples/emplace_back.cpp" + ``` + + Output: + + ```json + --8<-- "examples/emplace_back.output" + ``` + +## Version history + +- Since version 2.0.8. +- Returns reference since 3.7.0. diff --git a/doc/mkdocs/docs/api/basic_json/empty.md b/doc/mkdocs/docs/api/basic_json/empty.md new file mode 100644 index 0000000000..151e0739b1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/empty.md @@ -0,0 +1,66 @@ +# basic_json::empty + +```cpp +bool empty() const noexcept; +``` + +Checks if a JSON value has no elements (i.e. whether its [`size()`](size.md) is `0`). + +## Return value + +The return value depends on the different types and is defined as follows: + +Value type | return value +----------- | ------------- +null | `#!cpp true` +boolean | `#!cpp false` +string | `#!cpp false` +number | `#!cpp false` +binary | `#!cpp false` +object | result of function `object_t::empty()` +array | result of function `array_t::empty()` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Constant, as long as [`array_t`](array_t.md) and [`object_t`](object_t.md) satisfy the +[Container](https://en.cppreference.com/w/cpp/named_req/Container) concept; that is, their `empty()` functions have +constant complexity. + +## Possible implementation + +```cpp +bool empty() const noexcept +{ + return size() == 0; +} +``` + +## Notes + +This function does not return whether a string stored as JSON value is empty -- it returns whether the JSON container +itself is empty which is `#!cpp false` in the case of a string. + +## Example + +??? example + + The following code uses `empty()` to check if a JSON object contains any elements. + + ```cpp + --8<-- "examples/empty.cpp" + ``` + + Output: + + ```json + --8<-- "examples/empty.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to return `#!cpp false` for binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/end.md b/doc/mkdocs/docs/api/basic_json/end.md new file mode 100644 index 0000000000..52bfec2e31 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/end.md @@ -0,0 +1,42 @@ +# basic_json::end + +```cpp +iterator end() noexcept; +const_iterator end() const noexcept; +``` + +Returns an iterator to one past the last element. + +![Illustration from cppreference.com](../../images/range-begin-end.svg) + +## Return value + +iterator one past the last element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `end()`. + + ```cpp + --8<-- "examples/end.cpp" + ``` + + Output: + + ```json + --8<-- "examples/end.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/erase.md b/doc/mkdocs/docs/api/basic_json/erase.md new file mode 100644 index 0000000000..3d80f5bfd1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/erase.md @@ -0,0 +1,177 @@ +# basic_json::erase + +```cpp +// (1) +iterator erase(iterator pos); +const_iterator erase(const_iterator pos); + +// (2) +iterator erase(iterator first, iterator last); +const_iterator erase(const_iterator first, const_iterator last); + +// (3) +size_type erase(const typename object_t::key_type& key); + +// (4) +void erase(const size_type idx); +``` + +1. Removes an element from a JSON value specified by iterator `pos`. The iterator `pos` must be valid and + dereferenceable. Thus the `end()` iterator (which is valid, but is not dereferenceable) cannot be used as a value for + `pos`. + + If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`. + +2. Remove an element range specified by `[first; last)` from a JSON value. The iterator `first` does not need to be + dereferenceable if `first == last`: erasing an empty range is a no-op. + + If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`. + +3. Removes an element from a JSON object by key. + +4. Removes an element from a JSON array by index. + +## Parameters + +`pos` (in) +: iterator to the element to remove + +`first` (in) +: iterator to the beginning of the range to remove + +`last` (in) +: iterator past the end of the range to remove + +`key` (in) +: object key of the elements to remove + +`idx` (in) +: array index of the element to remove + +## Return value + +1. Iterator following the last removed element. If the iterator `pos` refers to the last element, the `end()` iterator + is returned. +2. Iterator following the last removed element. If the iterator `last` refers to the last element, the `end()` iterator + is returned. +3. Number of elements removed. If `ObjectType` is the default `std::map` type, the return value will always be `0` + (`key` was not found) or `1` (`key` was found). +4. / + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) if called on a `null` value; + example: `"cannot use erase() with null"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` + - Throws [`invalid_iterator.205`](../../home/exceptions.md#jsonexceptioninvalid_iterator205) if called on a + primitive type with invalid iterator (i.e., any iterator which is not `begin()`); example: `"iterator out of + range"` +2. The function can throw thw following exceptions: + - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) if called on a `null` value; + example: `"cannot use erase() with null"` + - Throws [`invalid_iterator.203`](../../home/exceptions.md#jsonexceptioninvalid_iterator203) if called on iterators + which does not belong to the current JSON value; example: `"iterators do not fit current value"` + - Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if called on a + primitive type with invalid iterators (i.e., if `first != begin()` and `last != end()`); example: `"iterators out + of range"` +3. The function can throw thw following exceptions: + - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than + JSON object; example: `"cannot use erase() with null"` +4. The function can throw thw following exceptions: + - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than + JSON object; example: `"cannot use erase() with null"` + - Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) when `idx >= size()`; example: + `"array index 17 is out of range"` + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +1. The complexity depends on the type: + - objects: amortized constant + - arrays: linear in distance between `pos` and the end of the container + - strings and binary: linear in the length of the member + - other types: constant +2. The complexity depends on the type: + - objects: `log(size()) + std::distance(first, last)` + - arrays: linear in the distance between `first` and `last`, plus linear + in the distance between `last` and end of the container + - strings and binary: linear in the length of the member + - other types: constant +3. `log(size()) + count(key)` +4. Linear in distance between `idx` and the end of the container. + +## Notes + +1. Invalidates iterators and references at or after the point of the + erase, including the `end()` iterator. +2. / +3. References and iterators to the erased elements are invalidated. Other references and iterators are not affected. +4. / + +## Example + +??? example + + The example shows the effect of `erase()` for different JSON types using an iterator. + + ```cpp + --8<-- "examples/erase__IteratorType.cpp" + ``` + + Output: + + ```json + --8<-- "examples/erase__IteratorType.output" + ``` + +??? example + + The example shows the effect of `erase()` for different JSON types using an iterator range. + + ```cpp + --8<-- "examples/erase__IteratorType_IteratorType.cpp" + ``` + + Output: + + ```json + --8<-- "examples/erase__IteratorType_IteratorType.output" + ``` + +??? example + + The example shows the effect of `erase()` for different JSON types using an object key. + + ```cpp + --8<-- "examples/erase__key_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/erase__key_type.output" + ``` + +??? example + + The example shows the effect of `erase()` using an array index. + + ```cpp + --8<-- "examples/erase__size_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/erase__size_type.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Added support for binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/error_handler_t.md b/doc/mkdocs/docs/api/basic_json/error_handler_t.md new file mode 100644 index 0000000000..050dda14db --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/error_handler_t.md @@ -0,0 +1,25 @@ +# basic_json::error_handler_t + +```cpp +enum class error_handler_t { + strict, + replace, + ignore +}; +``` + +This enumeration is used in the [`dump`](dump.md) function to choose how to treat decoding errors while serializing a +`basic_json` value. Three values are differentiated: + +strict +: throw a `type_error` exception in case of invalid UTF-8 + +replace +: replace invalid UTF-8 sequences with U+FFFD (� REPLACEMENT CHARACTER) + +ignore +: ignore invalid UTF-8 sequences; all bytes are copied to the output unchanged + +## Version history + +- Added in version 3.4.0. diff --git a/doc/mkdocs/docs/api/basic_json/exception.md b/doc/mkdocs/docs/api/basic_json/exception.md new file mode 100644 index 0000000000..deedae5a6d --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/exception.md @@ -0,0 +1,65 @@ +# basic_json::exception + +```cpp +class exception : public std::exception; +``` + +This class is an extension of [`std::exception`](https://en.cppreference.com/w/cpp/error/exception) objects with a +member `id` for exception ids. It is used as the base class for all exceptions thrown by the `basic_json` class. This +class can hence be used as "wildcard" to catch exceptions, see example below. + +```plantuml +std::exception <|-- basic_json::exception +basic_json::exception <|-- basic_json::parse_error +basic_json::exception <|-- basic_json::invalid_iterator +basic_json::exception <|-- basic_json::type_error +basic_json::exception <|-- basic_json::out_of_range +basic_json::exception <|-- basic_json::other_error + +interface std::exception {} + +class basic_json::exception #FFFF00 { + + const int id + + const char* what() const +} + +class basic_json::parse_error { + + const std::size_t byte +} +``` + +Subclasses: + +- [`parse_error`](parse_error.md) for exceptions indicating a parse error +- [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators +- [`type_error`](type_error.md) for exceptions indicating executing a member function with a wrong type +- [`out_of_range`](out_of_range.md) for exceptions indicating access out of the defined range +- [`other_error`](other_error.md) for exceptions indicating other library errors + +## Member functions + +- **what** - returns explanatory string + +## Member variables + +- **id** - the id of the exception + +## Example + +??? example + + The following code shows how arbitrary library exceptions can be caught. + + ```cpp + --8<-- "examples/exception.cpp" + ``` + + Output: + + ```json + --8<-- "examples/exception.output" + ``` + +## Version history + +- Since version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/find.md b/doc/mkdocs/docs/api/basic_json/find.md new file mode 100644 index 0000000000..5ff4baf61f --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/find.md @@ -0,0 +1,59 @@ +# basic_json::find + +```cpp +template +iterator find(KeyT&& key); + +template +const_iterator find(KeyT&& key) const +``` + +Finds an element in a JSON object with key equivalent to `key`. If the element is not found or the JSON value is not an +object, `end()` is returned. + +## Template parameters + +`KeyT` +: A type for an object key. + +## Parameters + +`key` (in) +: key value of the element to search for. + +## Return value + +Iterator to an element with key equivalent to `key`. If no such element is found or the JSON value is not an object, +past-the-end (see `end()`) iterator is returned. + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +Logarithmic in the size of the JSON object. + +## Notes + +This method always returns `end()` when executed on a JSON type that is not an object. + +## Example + +??? example + + The example shows how `find()` is used. + + ```cpp + --8<-- "examples/find__key_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/find__key_type.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/flatten.md b/doc/mkdocs/docs/api/basic_json/flatten.md new file mode 100644 index 0000000000..1408f38092 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/flatten.md @@ -0,0 +1,46 @@ +# basic_json::flatten + +```cpp +basic_json flatten() const; +``` + +The function creates a JSON object whose keys are JSON pointers (see [RFC 6901](https://tools.ietf.org/html/rfc6901)) +and whose values are all primitive (see [`is_primitive()`](is_primitive.md) for more information). The original JSON +value can be restored using the [`unflatten()`](unflatten.md) function. + +## Return value + +an object that maps JSON pointers to primitive values + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +Linear in the size the JSON value. + +## Notes + +Empty objects and arrays are flattened to `#!json null` and will not be reconstructed correctly by the +[`unflatten()`](unflatten.md) function. + +## Example + +??? example + + The following code shows how a JSON object is flattened to an object whose keys consist of JSON pointers. + + ```cpp + --8<-- "examples/flatten.cpp" + ``` + + Output: + + ```json + --8<-- "examples/flatten.output" + ``` + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/from_bson.md b/doc/mkdocs/docs/api/basic_json/from_bson.md new file mode 100644 index 0000000000..6df74f39cd --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/from_bson.md @@ -0,0 +1,83 @@ +# basic_json::from_bson + +```cpp +// (1) +template +static basic_json from_bson(InputType&& i, + const bool strict = true, + const bool allow_exceptions = true); +// (2) +template +static basic_json from_bson(IteratorType first, IteratorType last, + const bool strict = true, + const bool allow_exceptions = true); +``` + +Deserializes a given input to a JSON value using the BSON (Binary JSON) serialization format. + +1. Reads from a compatible input. +2. Reads from an iterator range. + +## Template parameters + +`InputType` +: A compatible input, for instance: + + - an `std::istream` object + - a `FILE` pointer + - a C-style array of characters + - a pointer to a null-terminated string of single byte characters + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators. + +`IteratorType` +: a compatible iterator type + +## Parameters + +`i` (in) +: an input in BSON format convertible to an input adapter + +`first` (in) +: iterator to start of the input + +`last` (in) +: iterator to end of the input + +`strict` (in) +: whether to expect the input to be consumed until EOF (`#!cpp true` by default) + +`allow_exceptions` (in) +: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default) + +## Return value + +deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be +`value_t::discarded`. The latter can be checked with [`is_discarded`](is_discarded.md). + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the input. + +## Example + +??? example + + The example shows the deserialization of a byte vector in BSON format to a JSON value. + + ```cpp + --8<-- "examples/from_bson.cpp" + ``` + + Output: + + ```json + --8<-- "examples/from_bson.output" + ``` + +## Version history + +- Added in version 3.4.0. diff --git a/doc/mkdocs/docs/api/basic_json/from_cbor.md b/doc/mkdocs/docs/api/basic_json/from_cbor.md new file mode 100644 index 0000000000..ec186fc2a1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/from_cbor.md @@ -0,0 +1,94 @@ +# basic_json::from_cbor + +```cpp +// (1) +template +static basic_json from_cbor(InputType&& i, + const bool strict = true, + const bool allow_exceptions = true, + const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error); + +// (2) +template +static basic_json from_cbor(IteratorType first, IteratorType last, + const bool strict = true, + const bool allow_exceptions = true, + const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error); +``` + +Deserializes a given input to a JSON value using the CBOR (Concise Binary Object Representation) serialization format. + +1. Reads from a compatible input. +2. Reads from an iterator range. + +## Template parameters + +`InputType` +: A compatible input, for instance: + + - an `std::istream` object + - a `FILE` pointer + - a C-style array of characters + - a pointer to a null-terminated string of single byte characters + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators. + +`IteratorType` +: a compatible iterator type + +## Parameters + +`i` (in) +: an input in CBOR format convertible to an input adapter + +`first` (in) +: iterator to start of the input + +`last` (in) +: iterator to end of the input + +`strict` (in) +: whether to expect the input to be consumed until EOF (`#!cpp true` by default) + +`allow_exceptions` (in) +: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default) + +`tag_handler` (in) +: how to treat CBOR tags (optional, `error` by default); see [`cbor_tag_handler_t`](cbor_tag_handler_t.md) for more + information + +## Return value + +deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be +`value_t::discarded`. The latter can be checked with [`is_discarded`](is_discarded.md). + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the input. + +## Example + +??? example + + The example shows the deserialization of a byte vector in CBOR format to a JSON value. + + ```cpp + --8<-- "examples/from_cbor.cpp" + ``` + + Output: + + ```json + --8<-- "examples/from_cbor.output" + ``` + +## Version history + +- Added in version 2.0.9. +- Parameter `start_index` since version 2.1.1. +- Changed to consume input adapters, removed `start_index` parameter, and added `strict` parameter in version 3.0.0. +- Added `allow_exceptions` parameter in version 3.2.0. +- Added `tag_handler` parameter in version 3.9.0. diff --git a/doc/mkdocs/docs/api/basic_json/from_msgpack.md b/doc/mkdocs/docs/api/basic_json/from_msgpack.md new file mode 100644 index 0000000000..9f68524999 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/from_msgpack.md @@ -0,0 +1,86 @@ +# basic_json::from_msgpack + +```cpp +// (1) +template +static basic_json from_msgpack(InputType&& i, + const bool strict = true, + const bool allow_exceptions = true); +// (2) +template +static basic_json from_msgpack(IteratorType first, IteratorType last, + const bool strict = true, + const bool allow_exceptions = true); +``` + +Deserializes a given input to a JSON value using the MessagePack serialization format. + +1. Reads from a compatible input. +2. Reads from an iterator range. + +## Template parameters + +`InputType` +: A compatible input, for instance: + + - an `std::istream` object + - a `FILE` pointer + - a C-style array of characters + - a pointer to a null-terminated string of single byte characters + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators. + +`IteratorType` +: a compatible iterator type + +## Parameters + +`i` (in) +: an input in MessagePack format convertible to an input adapter + +`first` (in) +: iterator to start of the input + +`last` (in) +: iterator to end of the input + +`strict` (in) +: whether to expect the input to be consumed until EOF (`#!cpp true` by default) + +`allow_exceptions` (in) +: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default) + +## Return value + +deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be +`value_t::discarded`. The latter can be checked with [`is_discarded`](is_discarded.md). + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the input. + +## Example + +??? example + + The example shows the deserialization of a byte vector in MessagePack format to a JSON value. + + ```cpp + --8<-- "examples/from_msgpack.cpp" + ``` + + Output: + + ```json + --8<-- "examples/from_msgpack.output" + ``` + +## Version history + +- Added in version 2.0.9. +- Parameter `start_index` since version 2.1.1. +- Changed to consume input adapters, removed `start_index` parameter, and added `strict` parameter in version 3.0.0. +- Added `allow_exceptions` parameter in version 3.2.0. diff --git a/doc/mkdocs/docs/api/basic_json/from_ubjson.md b/doc/mkdocs/docs/api/basic_json/from_ubjson.md new file mode 100644 index 0000000000..f6213f2937 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/from_ubjson.md @@ -0,0 +1,84 @@ +# basic_json::from_ubjson + +```cpp +// (1) +template +static basic_json from_ubjson(InputType&& i, + const bool strict = true, + const bool allow_exceptions = true); +// (2) +template +static basic_json from_ubjson(IteratorType first, IteratorType last, + const bool strict = true, + const bool allow_exceptions = true); +``` + +Deserializes a given input to a JSON value using the UBJSON (Universal Binary JSON) serialization format. + +1. Reads from a compatible input. +2. Reads from an iterator range. + +## Template parameters + +`InputType` +: A compatible input, for instance: + + - an `std::istream` object + - a `FILE` pointer + - a C-style array of characters + - a pointer to a null-terminated string of single byte characters + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators. + +`IteratorType` +: a compatible iterator type + +## Parameters + +`i` (in) +: an input in UBJSON format convertible to an input adapter + +`first` (in) +: iterator to start of the input + +`last` (in) +: iterator to end of the input + +`strict` (in) +: whether to expect the input to be consumed until EOF (`#!cpp true` by default) + +`allow_exceptions` (in) +: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default) + +## Return value + +deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be +`value_t::discarded`. The latter can be checked with [`is_discarded`](is_discarded.md). + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the input. + +## Example + +??? example + + The example shows the deserialization of a byte vector in UBJSON format to a JSON value. + + ```cpp + --8<-- "examples/from_ubjson.cpp" + ``` + + Output: + + ```json + --8<-- "examples/from_ubjson.output" + ``` + +## Version history + +- Added in version 3.1.0. +- Added `allow_exceptions` parameter in version 3.2.0. diff --git a/doc/mkdocs/docs/api/basic_json/front.md b/doc/mkdocs/docs/api/basic_json/front.md new file mode 100644 index 0000000000..55010fb859 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/front.md @@ -0,0 +1,54 @@ +# basic_json::front + +```cpp +reference front(); +const_reference front() const; +``` + +Returns a reference to the first element in the container. For a JSON container `#!cpp c`, the expression +`#!cpp c.front()` is equivalent to `#!cpp *c.begin()`. + +## Return value + +In case of a structured type (array or object), a reference to the first element is returned. In case of number, string, +boolean, or binary values, a reference to the value is returned. + +## Exceptions + +If the JSON value is `#!json null`, exception +[`invalid_iterator.214`](../../home/exceptions.md#jsonexceptioninvalid_iterator214) is thrown. + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Constant. + +## Note + +!!! danger + + Calling `front` on an empty array or object is undefined behavior and is **guarded by an assertion**! + +## Example + +??? example + + The following code shows an example for `front()`. + + ```cpp + --8<-- "examples/front.cpp" + ``` + + Output: + + ```json + --8<-- "examples/front.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Adjusted code to return reference to binary values in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/get.md b/doc/mkdocs/docs/api/basic_json/get.md new file mode 100644 index 0000000000..6cd6630567 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/get.md @@ -0,0 +1,136 @@ +# basic_json::get + +```cpp +// (1) +template +ValueType get() const noexcept( + noexcept(JSONSerializer::from_json( + std::declval(), std::declval()))); + +// (2) +template +BasicJsonType get() const; + +// (3) +template +PointerType get_ptr(); + +template +constexpr const PointerType get_ptr() const noexcept; +``` + +1. Explicit type conversion between the JSON value and a compatible value which is + [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) and + [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). The value is converted by + calling the `json_serializer` `from_json()` method. + + The function is equivalent to executing + ```cpp + ValueType ret; + JSONSerializer::from_json(*this, ret); + return ret; + ``` + + This overloads is chosen if: + + - `ValueType` is not `basic_json`, + - `json_serializer` has a `from_json()` method of the form + `void from_json(const basic_json&, ValueType&)`, and + - `json_serializer` does not have a `from_json()` method of the form + `ValueType from_json(const basic_json&)` + + If the type is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) and + **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible), the value is + converted by calling the `json_serializer` `from_json()` method. + + The function is then equivalent to executing + ```cpp + return JSONSerializer::from_json(*this); + ``` + + This overloads is chosen if: + + - `ValueType` is not `basic_json` and + - `json_serializer` has a `from_json()` method of the form + `ValueType from_json(const basic_json&)` + + If `json_serializer` has both overloads of `from_json()`, the latter one is chosen. + +2. Overload for `basic_json` specializations. The function is equivalent to executing + ```cpp + return *this; + ``` + +3. Explicit pointer access to the internally stored JSON value. No copies are made. + +## Template parameters + +`ValueType` +: the value type to return + +`BasicJsonType` +: a specialization of `basic_json` + +`PointerType` +: pointer type; must be a pointer to [`array_t`](array_t.md), [`object_t`](object_t.md), [`string_t`](string_t.md), + [`boolean_t`](boolean_t.md), [`number_integer_t`](number_integer_t.md), or + [`number_unsigned_t`](number_unsigned_t.md), [`number_float_t`](number_float_t.md), or [`binary_t`](binary_t.md). + Other types will not compile. + +## Return value + +1. copy of the JSON value, converted to `ValueType` +2. a copy of `#!cpp *this`, converted into `BasicJsonType` +3. pointer to the internally stored JSON value if the requested pointer type fits to the JSON value; `#!cpp nullptr` + otherwise + +## Exceptions + +Depends on what `json_serializer` `from_json()` method throws + +## Notes + +!!! warning + + Writing data to the pointee (overload 3) of the result yields an undefined state. + +## Example + +??? example + + The example below shows several conversions from JSON values + to other types. There a few things to note: (1) Floating-point numbers can + be converted to integers, (2) A JSON array can be converted to a standard + `std::vector`, (3) A JSON object can be converted to C++ + associative containers such as `std::unordered_map`. + + ```cpp + --8<-- "examples/get__ValueType_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/get__ValueType_const.output" + ``` + +??? example + + The example below shows how pointers to internal values of a JSON value can be requested. Note that no type + conversions are made and a `#cpp nullptr` is returned if the value and the requested pointer type does not match. + + ```cpp + --8<-- "examples/get__PointerType.cpp" + ``` + + Output: + + ```json + --8<-- "examples/get__PointerType.output" + ``` + +## Version history + +1. Since version 2.1.0. +2. Since version 2.1.0. Extended to work with other specializations of `basic_json` in version 3.2.0. +3. Since version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/get_allocator.md b/doc/mkdocs/docs/api/basic_json/get_allocator.md new file mode 100644 index 0000000000..d4133af7aa --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/get_allocator.md @@ -0,0 +1,15 @@ +# basic_json::get_allocator + +```cpp +static allocator_type get_allocator(); +``` + +Returns the allocator associated with the container. + +## Return value + +associated allocator + +## Version history + +- Unknown. diff --git a/doc/mkdocs/docs/api/basic_json/get_binary.md b/doc/mkdocs/docs/api/basic_json/get_binary.md new file mode 100644 index 0000000000..46d5b652da --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/get_binary.md @@ -0,0 +1,29 @@ +# basic_json::get_binary + +```cpp +binary_t& get_binary(); + +const binary_t& get_binary() const; +``` + +Returns a reference to the stored binary value. + +## Return value + +Reference to binary value. + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Exceptions + +Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if the value is not binary + +## Complexity + +Constant. + +## Version history + +- Added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/get_ptr.md b/doc/mkdocs/docs/api/basic_json/get_ptr.md new file mode 100644 index 0000000000..c5cee307a4 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/get_ptr.md @@ -0,0 +1,60 @@ +# basic_json::get_ptr + +```cpp +template +PointerType get_ptr(); + +template +constexpr const PointerType get_ptr() const noexcept; +``` + +Implicit pointer access to the internally stored JSON value. No copies are made. + +## Template arguments + +`PointerType` +: pointer type; must be a pointer to [`array_t`](array_t.md), [`object_t`](object_t.md), [`string_t`](string_t.md), + [`boolean_t`](boolean_t.md), [`number_integer_t`](number_integer_t.md), or + [`number_unsigned_t`](number_unsigned_t.md), [`number_float_t`](number_float_t.md), or [`binary_t`](binary_t.md). + Other types will not compile. + +## Return value + +pointer to the internally stored JSON value if the requested pointer type fits to the JSON value; `#!cpp nullptr` +otherwise + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +Constant. + +## Notes + +!!! warning + + Writing data to the pointee of the result yields an undefined state. + +## Example + +??? example + + The example below shows how pointers to internal values of a JSON value can be requested. Note that no type + conversions are made and a `#!cpp nullptr` is returned if the value and the requested pointer type does not match. + + ```cpp + --8<-- "examples/get_ptr.cpp" + ``` + + Output: + + ```json + --8<-- "examples/get_ptr.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/get_ref.md b/doc/mkdocs/docs/api/basic_json/get_ref.md new file mode 100644 index 0000000000..102aff1ef9 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/get_ref.md @@ -0,0 +1,64 @@ +# basic_json::get_ref + +```cpp +template +ReferenceType get_ref(); + +template +const ReferenceType get_ref() const; +``` + +Implicit reference access to the internally stored JSON value. No copies are made. + +## Template arguments + +`ReferenceType` +: reference type; must be a reference to [`array_t`](array_t.md), [`object_t`](object_t.md), + [`string_t`](string_t.md), [`boolean_t`](boolean_t.md), [`number_integer_t`](number_integer_t.md), or + [`number_unsigned_t`](number_unsigned_t.md), [`number_float_t`](number_float_t.md), or [`binary_t`](binary_t.md). + Enforced by static assertion. + +## Return value + +reference to the internally stored JSON value if the requested reference type fits to the JSON value; throws +[`type_error.303`](../../home/exceptions.md#jsonexceptiontype_error303) otherwise + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Exceptions + +Throws [`type_error.303`](../../home/exceptions.md#jsonexceptiontype_error303) if the requested reference type does not +match the stored JSON value type; example: `"incompatible ReferenceType for get_ref, actual type is binary"`. + +## Complexity + +Constant. + +## Notes + +!!! warning + + Writing data to the referee of the result yields an undefined state. + +## Example + +??? example + + The example shows several calls to `get_ref()`. + + ```cpp + --8<-- "examples/get_ref.cpp" + ``` + + Output: + + ```json + --8<-- "examples/get_ref.output" + ``` + +## Version history + +- Added in version 1.1.0. +- Extended to binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/get_to.md b/doc/mkdocs/docs/api/basic_json/get_to.md new file mode 100644 index 0000000000..4a4395bc85 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/get_to.md @@ -0,0 +1,58 @@ +# basic_json::get_to + +```cpp +template +ValueType& get_to(ValueType& v) const noexcept( + noexcept(JSONSerializer::from_json( + std::declval(), v))) +``` + +Explicit type conversion between the JSON value and a compatible value. The value is filled into the input parameter by +calling the `json_serializer` `from_json()` method. + +The function is equivalent to executing +```cpp +ValueType v; +JSONSerializer::from_json(*this, v); +``` + +This overloads is chosen if: + +- `ValueType` is not `basic_json`, +- `json_serializer` has a `from_json()` method of the form `void from_json(const basic_json&, ValueType&)` + +## Template parameters + +`ValueType` +: the value type to return + +## Return value + +the input parameter, allowing chaining calls + +## Exceptions + +Depends on what `json_serializer` `from_json()` method throws + +## Example + +??? example + + The example below shows several conversions from JSON values to other types. There a few things to note: (1) + Floating-point numbers can be converted to integers, (2) A JSON array can be converted to a standard + `#!cpp std::vector`, (3) A JSON object can be converted to C++ associative containers such as + `#cpp std::unordered_map`. + + ```cpp + --8<-- "examples/get_to.cpp" + ``` + + Output: + + ```json + --8<-- "examples/get_to.output" + ``` + +## Version history + +- Since version 3.3.0. diff --git a/doc/mkdocs/docs/api/basic_json/index.md b/doc/mkdocs/docs/api/basic_json/index.md index 496cd68b6e..e8841e8507 100644 --- a/doc/mkdocs/docs/api/basic_json/index.md +++ b/doc/mkdocs/docs/api/basic_json/index.md @@ -1,240 +1,239 @@ # basic_json -!!! note - - This page is under construction. - Defined in header `` ```cpp -template class ObjectType, - template class ArrayType, - class StringType, class BooleanType, class NumberIntegerType, - class NumberUnsignedType, class NumberFloatType, - template class AllocatorType, - template class JSONSerializer, - class BinaryType> -class basic_json +template< + template class ObjectType = std::map, + template class ArrayType = std::vector, + class StringType = std::string, + class BooleanType = bool, + class NumberIntegerType = std::int64_t, + class NumberUnsignedType = std::uint64_t, + class NumberFloatType = double, + template class AllocatorType = std::allocator, + template class JSONSerializer = adl_serializer, + class BinaryType = std::vector +> +class basic_json; ``` ## Specializations -- json -- ordered_json +- [**json**](../json.md) - default specialization +- [**ordered_json**](../ordered_json.md) - specialization that maintains the insertion order of object keys ## Template parameters -- ObjectType -- ArrayType -- StringType -- BooleanType -- NumberIntegerType -- NumberUnsignedType -- NumberFloatType -- AllocatorType -- JSONSerializer -- BinaryType +| Template parameter | Description | Derived type | +| -------------------- | ----------- | ------------ | +| `ObjectType` | type for JSON objects | [`object_t`](object_t.md) | +| `ArrayType` | type for JSON arrays | [`array_t`](array_t.md) | +| `StringType` | type for JSON strings and object keys | [`string_t`](string_t.md) | +| `BooleanType` | type for JSON booleans | [`boolean_t`](boolean_t.md) | +| `NumberIntegerType` | type for JSON integer numbers | [`number_integer_t`](number_integer_t.md) | +| `NumberUnsignedType` | type for JSON unsigned integer numbers | [`number_unsigned_t`](number_unsigned_t.md) | +| `NumberFloatType` | type for JSON floating-point numbers | [`number_float_t`](number_float_t.md) | +| `AllocatorType` | type of the allocator to use | | +| `JSONSerializer` | the serializer to resolve internal calls to `to_json()` and `from_json()` | [`json_serializer`](json_serializer.md) | +| `BinaryType` | type for binary arrays | [`binary_t`](binary_t.md) | ## Iterator invalidation +Todo + ## Member types -- value_t -- json_pointer -- json_serializer -- error_handler_t -- cbor_tag_handler_t +- [**adl_serializer**](../adl_serializer.md) - the default serializer +- [**value_t**](value_t.md) - the JSON type enumeration +- [**json_pointer**](../json_pointer.md) - JSON Pointer implementation +- [**json_serializer**](json_serializer.md) - type of the serializer to for conversions from/to JSON +- [**error_handler_t**](error_handler_t.md) - type to choose behavior on decoding errors +- [**cbor_tag_handler_t**](cbor_tag_handler_t.md) - type to choose how to handle CBOR tags - initializer_list_t -- input_format_t +- [**input_format_t**](input_format_t.md) - type to choose the format to parse - json_sax_t ### Exceptions -- exception -- parse_error -- invalid_iterator -- type_error -- out_of_range -- other_error +- [**exception**](exception.md) - general exception of the `basic_json` class + - [**parse_error**](parse_error.md) - exception indicating a parse error + - [**invalid_iterator**](invalid_iterator.md) - exception indicating errors with iterators + - [**type_error**](type_error.md) - exception indicating executing a member function with a wrong type + - [**out_of_range**](out_of_range.md) - exception indicating access out of the defined range + - [**other_error**](other_error.md) - exception indicating other library errors ### Container types -- value_type -- reference -- const_reference -- difference_type -- size_type -- allocator_type -- pointer -- const_pointer -- iterator -- const_iterator -- reverse_iterator -- const_reverse_iterator +| Type | Definition | +| ------------------------ | ---------- | +| `value_type` | `#!cpp basic_json` | +| `reference` | `#!cpp value_type&` | +| `const_reference` | `#!cpp const value_type&` | +| `difference_type` | `#!cpp std::ptrdiff_t` | +| `size_type` | `#!cpp std::size_t` | +| `allocator_type` | `#!cpp AllocatorType` | +| `pointer` | `#!cpp std::allocator_traits::pointer` | +| `const_pointer` | `#!cpp std::allocator_traits::const_pointer` | +| `iterator` | [LegacyBidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator) | +| `const_iterator` | constant [LegacyBidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator) | +| `reverse_iterator` | reverse iterator, derived from `iterator` | +| `const_reverse_iterator` | reverse iterator, derived from `const_iterator` | +| `iteration_proxy` | helper type for [`items`](items.md) function | ### JSON value data types -- object_comparator_t -- object_t -- array_t -- string_t -- boolean_t -- number_integer_t -- number_unsigned_t -- number_float_t -- binary_t +- [**array_t**](array_t.md) - type for arrays +- [**binary_t**](binary_t.md) - type for binary arrays +- [**boolean_t**](boolean_t.md) - type for booleans +- [**number_float_t**](number_float_t.md) - type for numbers (floating-point) +- [**number_integer_t**](number_integer_t.md) - type for numbers (integer) +- [**number_unsigned_t**](number_unsigned_t.md) - type for numbers (unsigned) +- [**object_comparator_t**](object_comparator_t.md) - comparator for objects +- [**object_t**](object_t.md) - type for objects +- [**string_t**](string_t.md) - type for strings ### Parser callback -- parse_event_t -- parser_callback_t +- [**parse_event_t**](parse_event_t.md) - parser event types +- [**parser_callback_t**](parser_callback_t.md) - per-element parser callback type ## Member functions -- (constructor) -- (destructor) -- binary (static) - explicitly create a binary array -- array (static) - explicitly create an array -- object (static) - explicitly create an object -- operator= - copy assignment +- [(constructor)](basic_json.md) +- [(destructor)](~basic_json.md) +- [**operator=**](operator=.md) - copy assignment +- [**array**](array_t.md) (static) - explicitly create an array +- [**binary**](binary.md) (static) - explicitly create a binary array +- [**object**](object_t.md) (static) - explicitly create an object ### Object inspection Functions to inspect the type of a JSON value. -- type - return the type of the JSON value -- is_primitive - return whether type is primitive -- is_structured - return whether type is structured -- is_null - return whether value is null -- is_boolean - return whether value is a boolean -- is_number - return whether value is a number -- is_number_integer - return whether value is an integer number -- is_number_unsigned - return whether value is an unsigned integer number -- is_number_float - return whether value is a floating-point number -- is_object - return whether value is an object -- is_array - return whether value is an array -- is_string - return whether value is a string -- is_binary - return whether value is a binary array -- is_discarded - return whether value is discarded -- operator value_t - return the type of the JSON value +- [**type**](type.md) - return the type of the JSON value +- [**operator value_t**](operator_value_t.md) - return the type of the JSON value +- [**type_name**](type_name.md) - return the type as string +- [**is_primitive**](is_primitive.md) - return whether type is primitive +- [**is_structured**](is_structured.md) - return whether type is structured +- [**is_null**](is_null.md) - return whether value is null +- [**is_boolean**](is_boolean.md) - return whether value is a boolean +- [**is_number**](is_number.md) - return whether value is a number +- [**is_number_integer**](is_number_integer.md) - return whether value is an integer number +- [**is_number_unsigned**](is_number_unsigned.md) - return whether value is an unsigned integer number +- [**is_number_float**](is_number_float.md) - return whether value is a floating-point number +- [**is_object**](is_object.md) - return whether value is an object +- [**is_array**](is_array.md) - return whether value is an array +- [**is_string**](is_string.md) - return whether value is a string +- [**is_binary**](is_binary.md) - return whether value is a binary array +- [**is_discarded**](is_discarded.md) - return whether value is discarded ### Value access Direct access to the stored value of a JSON value. -- get - get a value -- get_to - get a value -- get_ptr - get a pointer value -- get_ref - get a reference value -- operator ValueType - get a value -- get_binary - get a binary value +- [**get**](get.md) - get a value +- [**get_to**](get_to.md) - get a value and write it to a destination +- [**get_ptr**](get_ptr.md) - get a pointer value +- [**get_ref**](get_ref.md) - get a reference value +- [**operator ValueType**](operator_ValueType.md) - get a value +- [**get_binary**](get_binary.md) - get a binary value ### Element access Access to the JSON value -- at - access specified array element with bounds checking -- at - access specified object element with bounds checking -- operator[] - access specified array element -- operator[] - access specified object element -- value - access specified object element with default value -- front - access the first element -- back - access the last element -- erase - remove elements +- [**at**](at.md) - access specified element with bounds checking +- [**operator[]**](operator[].md) - access specified element +- [**value**](value.md) - access specified object element with default value +- [**front**](front.md) - access the first element +- [**back**](back.md) - access the last element ### Lookup -- find - find an element in a JSON object -- count - returns the number of occurrences of a key in a JSON object -- contains - check the existence of an element in a JSON object +- [**find**](find.md) - find an element in a JSON object +- [**count**](count.md) - returns the number of occurrences of a key in a JSON object +- [**contains**](contains.md) - check the existence of an element in a JSON object ### Iterators -- begin - returns an iterator to the first element -- cbegin - returns a const iterator to the first element -- end - returns an iterator to one past the last element -- cend - returns a const iterator to one past the last element -- rbegin - returns an iterator to the reverse-beginning -- rend - returns an iterator to the reverse-end -- crbegin - returns a const iterator to the reverse-beginning -- crend - returns a const iterator to the reverse-end -- items - wrapper to access iterator member functions in range-based for +- [**begin**](begin.md) - returns an iterator to the first element +- [**cbegin**](cbegin.md) - returns a const iterator to the first element +- [**end**](end.md) - returns an iterator to one past the last element +- [**cend**](cend.md) - returns a const iterator to one past the last element +- [**rbegin**](rbegin.md) - returns an iterator to the reverse-beginning +- [**rend**](rend.md) - returns an iterator to the reverse-end +- [**crbegin**](crbegin.md) - returns a const iterator to the reverse-beginning +- [**crend**](crend.md) - returns a const iterator to the reverse-end +- [**items**](items.md) - wrapper to access iterator member functions in range-based for ### Capacity -- empty - checks whether the container is empty -- size - returns the number of elements -- max_size - returns the maximum possible number of elements +- [**empty**](empty.md) - checks whether the container is empty +- [**size**](size.md) - returns the number of elements +- [**max_size**](max_size.md) - returns the maximum possible number of elements ### Modifiers -- clear - clears the contents -- push_back - add an object to an array -- operator+= - add an object to an array -- push_back - add an object to an object -- operator+= - add an object to an object -- emplace_back - add an object to an array -- emplace - add an object to an object if key does not exist -- insert - inserts element -- update - updates a JSON object from another object, overwriting existing keys +- [**clear**](clear.md) - clears the contents +- [**push_back**](push_back.md) - add a value to an array/object +- [**operator+=**](operator+=.md) - add a value to an array/object +- [**emplace_back**](emplace_back.md) - add a value to an array +- [**emplace**](emplace.md) - add a value to an object if key does not exist +- [**erase**](erase.md) - remove elements +- [**insert**](insert.md) - inserts elements +- [**update**](update.md) - updates a JSON object from another object, overwriting existing keys - swap - exchanges the values ### Lexicographical comparison operators -- operator== - comparison: equal -- operator!= - comparison: not equal -- operator< - comparison: less than -- operator<= - comparison: less than or equal -- operator> - comparison: greater than -- operator>= - comparison: greater than or equal +- [**operator==**](operator_eq.md) - comparison: equal +- [**operator!=**](operator_ne.md) - comparison: not equal +- [**operator<**](operator_lt.md) - comparison: less than +- [**operator<=**](operator_le.md) - comparison: less than or equal +- [**operator>**](operator_gt.md) - comparison: greater than +- [**operator>=**](operator_ge.md) - comparison: greater than or equal -### Serialization +### Serialization / Dumping - [**dump**](dump.md) - serialization - to_string - user-defined to_string function for JSON values -### Deserialization - -- [**parse**](parse.md) - deserialize from a compatible input -- accept - check if the input is valid JSON -- sax_parse - generate SAX events - -### Convenience functions +### Deserialization / Parsing -- type_name - return the type as string +- [**parse**](parse.md) (static) - deserialize from a compatible input +- [**accept**](accept.md) (static) - check if the input is valid JSON +- [**sax_parse**](sax_parse.md) (static) - generate SAX events ### JSON Pointer functions -- at - access specified object element with bounds checking via JSON Pointer -- operator[] - access specified element via JSON Pointer -- value - access specified object element with default value via JSON Pointer -- flatten - return flattened JSON value -- unflatten - unflatten a previously flattened JSON value +- [**flatten**](flatten.md) - return flattened JSON value +- [**unflatten**](unflatten.md) - unflatten a previously flattened JSON value ### JSON Patch functions -- patch - applies a JSON patch -- diff (static) - creates a diff as a JSON patch +- [**patch**](patch.md) - applies a JSON patch +- [**diff**](diff.md) (static) - creates a diff as a JSON patch ### JSON Merge Patch functions -- merge_patch - applies a JSON Merge Patch +- [**merge_patch**](merge_patch.md) - applies a JSON Merge Patch ## Static functions - [**meta**](meta.md) - returns version information on the library -- get_allocator - returns the allocator associated with the container +- [**get_allocator**](get_allocator.md) - returns the allocator associated with the container ### Binary formats -- to_cbor - create a CBOR serialization of a given JSON value -- to_msgpack - create a MessagePack serialization of a given JSON value -- to_ubjson - create a UBJSON serialization of a given JSON value -- to_bson - create a BSON serialization of a given JSON value -- from_cbor - create a JSON value from an input in CBOR format -- from_msgpack - create a JSON value from an input in MessagePack format -- from_ubjson - create a JSON value from an input in UBJSON format -- from_bson - create a JSON value from an input in BSON format +- [**from_bson**](from_bson.md) (static) - create a JSON value from an input in BSON format +- [**from_cbor**](from_cbor.md) (static) - create a JSON value from an input in CBOR format +- [**from_msgpack**](from_msgpack.md) (static) - create a JSON value from an input in MessagePack format +- [**from_ubjson**](from_ubjson.md) (static) - create a JSON value from an input in UBJSON format +- [**to_bson**](to_bson.md) (static) - create a BSON serialization of a given JSON value +- [**to_cbor**](to_cbor.md) (static) - create a CBOR serialization of a given JSON value +- [**to_msgpack**](to_msgpack.md) (static) - create a MessagePack serialization of a given JSON value +- [**to_ubjson**](to_ubjson.md) (static) - create a UBJSON serialization of a given JSON value ## Non-member functions @@ -243,8 +242,8 @@ Access to the JSON value ## Literals -- operator""_json -- operator""_json_pointer +- [**operator""_json**](operator_literal_json.md) - user-defined string literal for JSON values +- [**operator""_json_pointer**](operator_literal_json_pointer.md) - user-defined string literal for JSON pointers ## Helper classes diff --git a/doc/mkdocs/docs/api/basic_json/input_format_t.md b/doc/mkdocs/docs/api/basic_json/input_format_t.md new file mode 100644 index 0000000000..783085d8e2 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/input_format_t.md @@ -0,0 +1,32 @@ +# basic_json::input_format_t + +```cpp +enum class input_format_t { + json, + cbor, + msgpack, + ubjson, + bson +}; +``` + +This enumeration is used in the [`sax_parse`](sax_parse.md) function to choose the input format to parse: + +json +: JSON (JavaScript Object Notation) + +cbor +: CBOR (Concise Binary Object Representation) + +msgpack +: MessagePack + +ubjson +: UBJSON (Universal Binary JSON) + +bson +: BSON (Bin­ary JSON) + +## Version history + +- Added in version 3.2.0. diff --git a/doc/mkdocs/docs/api/basic_json/insert.md b/doc/mkdocs/docs/api/basic_json/insert.md new file mode 100644 index 0000000000..fbd4668520 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/insert.md @@ -0,0 +1,179 @@ +# basic_json::insert + +```cpp +// (1) +iterator insert(const_iterator pos, const basic_json& val); +iterator insert(const_iterator pos, basic_json&& val); + +// (2) +iterator insert(const_iterator pos, size_type cnt, const basic_json& val); + +// (3) +iterator insert(const_iterator pos, const_iterator first, const_iterator last); + +// (4) +iterator insert(const_iterator pos, initializer_list_t ilist); + +// (5) +void insert(const_iterator first, const_iterator last); +``` + +1. Inserts element `val` to array before iterator `pos`. +2. Inserts `cnt` copies of `val` to array before iterator `pos`. +3. Inserts elements from range `[first, last)` to array before iterator `pos`. +4. Inserts elements from initializer list `ilist` to array before iterator `pos`. +5. Inserts elements from range `[first, last)` to object. + +## Parameters + +`pos` (in) +: iterator before which the content will be inserted; may be the `end()` iterator + +`val` (in) +: value to insert + +`cnt` (in) +: number of copies of `val` to insert + +`first` (in) +: begin of the range of elements to insert + +`last` (in) +: end of the range of elements to insert + +`ilist` (in) +: initializer list to insert the values from + +## Return value + +1. iterator pointing to the inserted `val`. +2. iterator pointing to the first element inserted, or `pos` if `#!cpp cnt==0` +3. iterator pointing to the first element inserted, or `pos` if `#!cpp first==last` +4. iterator pointing to the first element inserted, or `pos` if `ilist` is empty +5. / + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than + arrays; example: `"cannot use insert() with string"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` +2. The function can throw thw following exceptions: + - Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than + arrays; example: `"cannot use insert() with string"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` +3. The function can throw thw following exceptions: + - Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than + arrays; example: `"cannot use insert() with string"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` + - Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last` + do not belong to the same JSON value; example: `"iterators do not fit"` + - Throws [`invalid_iterator.211`](../../home/exceptions.md#jsonexceptioninvalid_iterator211) if `first` or `last` + are iterators into container for which insert is called; example: `"passed iterators may not belong to container"` +4. The function can throw thw following exceptions: + - Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than + arrays; example: `"cannot use insert() with string"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` +5. The function can throw thw following exceptions: + - Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than + objects; example: `"cannot use insert() with string"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` + - Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last` + do not belong to the same JSON value; example: `"iterators do not fit"` + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +1. Constant plus linear in the distance between `pos` and end of the container. +2. Linear in `cnt` plus linear in the distance between `pos` and end of the container. +3. Linear in `#!cpp std::distance(first, last)` plus linear in the distance between `pos` and end of the container. +4. Linear in `ilist.size()` plus linear in the distance between `pos` and end of the container. +5. Logarithmic: `O(N*log(size() + N))`, where `N` is the number of elements to insert. + +## Example + +??? example + + The example shows how `insert()` is used. + + ```cpp + --8<-- "examples/insert.cpp" + ``` + + Output: + + ```json + --8<-- "examples/insert.output" + ``` + +??? example + + The example shows how `insert()` is used. + + ```cpp + --8<-- "examples/insert__count.cpp" + ``` + + Output: + + ```json + --8<-- "examples/insert__count.output" + ``` + +??? example + + The example shows how `insert()` is used. + + ```cpp + --8<-- "examples/insert__range.cpp" + ``` + + Output: + + ```json + --8<-- "examples/insert__range.output" + ``` + +??? example + + The example shows how `insert()` is used. + + ```cpp + --8<-- "examples/insert__ilist.cpp" + ``` + + Output: + + ```json + --8<-- "examples/insert__ilist.output" + ``` + +??? example + + The example shows how `insert()` is used. + + ```cpp + --8<-- "examples/insert__range_object.cpp" + ``` + + Output: + + ```json + --8<-- "examples/insert__range_object.output" + ``` + +## Version history + +1. Added in version 1.0.0. +2. Added in version 1.0.0. +3. Added in version 1.0.0. +4. Added in version 1.0.0. +5. Added in version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/invalid_iterator.md b/doc/mkdocs/docs/api/basic_json/invalid_iterator.md new file mode 100644 index 0000000000..b11b27dfc2 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/invalid_iterator.md @@ -0,0 +1,59 @@ +# basic_json::invalid_iterator + +```cpp +class invalid_iterator : public exception; +``` + +This exception is thrown if iterators passed to a library function do not match the expected semantics. + +Exceptions have ids 2xx. + +```plantuml +std::exception <|-- basic_json::exception +basic_json::exception <|-- basic_json::parse_error +basic_json::exception <|-- basic_json::invalid_iterator +basic_json::exception <|-- basic_json::type_error +basic_json::exception <|-- basic_json::out_of_range +basic_json::exception <|-- basic_json::other_error + +interface std::exception {} + +class basic_json::exception { + + const int id + + const char* what() const +} + +class basic_json::parse_error { + + const std::size_t byte +} + +class basic_json::invalid_iterator #FFFF00 {} +``` + +## Member functions + +- **what** - returns explanatory string + +## Member variables + +- **id** - the id of the exception + +## Example + +??? example + + The following code shows how a `invalid_iterator` exception can be caught. + + ```cpp + --8<-- "examples/invalid_iterator.cpp" + ``` + + Output: + + ```json + --8<-- "examples/invalid_iterator.output" + ``` + +## Version history + +- Since version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_array.md b/doc/mkdocs/docs/api/basic_json/is_array.md new file mode 100644 index 0000000000..a303795618 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_array.md @@ -0,0 +1,39 @@ +# basic_json::is_array + +```cpp +constexpr bool is_array() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is an array. + +## Return value + +`#!cpp true` if type is an array, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_array()` for all JSON types. + + ```cpp + --8<-- "examples/is_array.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_array.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_binary.md b/doc/mkdocs/docs/api/basic_json/is_binary.md new file mode 100644 index 0000000000..f2285bb594 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_binary.md @@ -0,0 +1,39 @@ +# basic_json::is_binary + +```cpp +constexpr bool is_binary() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is binary array. + +## Return value + +`#!cpp true` if type is binary, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_binary()` for all JSON types. + + ```cpp + --8<-- "examples/is_binary.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_binary.output" + ``` + +## Version history + +- Added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_boolean.md b/doc/mkdocs/docs/api/basic_json/is_boolean.md new file mode 100644 index 0000000000..32bb8c458f --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_boolean.md @@ -0,0 +1,39 @@ +# basic_json::is_boolean + +```cpp +constexpr bool is_boolean() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is `#!json true` or `#!json false`. + +## Return value + +`#!cpp true` if type is boolean, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_boolean()` for all JSON types. + + ```cpp + --8<-- "examples/is_boolean.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_boolean.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_discarded.md b/doc/mkdocs/docs/api/basic_json/is_discarded.md new file mode 100644 index 0000000000..b733f623cb --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_discarded.md @@ -0,0 +1,72 @@ +# basic_json::is_discarded + +```cpp +constexpr bool is_discarded() const noexcept; +``` + +This function returns `#!cpp true` for a JSON value if either: + +- the value was discarded during parsing with a callback function (see [`parser_callback_t`](parser_callback_t.md)), or +- the value is the result of parsing invalid JSON with parameter `allow_exceptions` set to `#!cpp false`; see + [`parse`](parse.md) for more information. + +## Return value + +`#!cpp true` if type is discarded, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Notes + +!!! note + + Discarded values are never compared equal with [`operator==`](operator_eq.md). That is, checking whether a JSON + value `j` is discarded will only work via: + + ```cpp + j.is_discarded() + ``` + + because + + ```cpp + j == json::value_t::discarded + ``` + + will always be `#!cpp false`. + +!!! note + + When a value is discarded by a callback function (see [`parser_callback_t`](parser_callback_t.md)) during parsing, + then it is removed when it is part of a structured value. For instance, if the second value of an array is discared, + instead of `#!json [null, discarded, false]`, the array `#!json [null, false]` is returned. Only if the top-level + value is discarded, the return value of the `parse` call is discarded. + +This function will always be `#!cpp false` for JSON values after parsing. That is, discarded values can only occur +during parsing, but will be removed when inside a structured value or replaced by null in other cases. + +## Example + +??? example + + The following code exemplifies `is_discarded()` for all JSON types. + + ```cpp + --8<-- "examples/is_discarded.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_discarded.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_null.md b/doc/mkdocs/docs/api/basic_json/is_null.md new file mode 100644 index 0000000000..8072642a95 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_null.md @@ -0,0 +1,39 @@ +# basic_json::is_null + +```cpp +constexpr bool is_null() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is `#!json null`. + +## Return value + +`#!cpp true` if type is `#!json null`, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_null()` for all JSON types. + + ```cpp + --8<-- "examples/is_null.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_null.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_number.md b/doc/mkdocs/docs/api/basic_json/is_number.md new file mode 100644 index 0000000000..9bcb131448 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_number.md @@ -0,0 +1,50 @@ +# basic_json::is_number + +```cpp +constexpr bool is_number() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is a number. This includes both integer (signed and +unsigned) and floating-point values. + +## Return value + +`#!cpp true` if type is number (regardless whether integer, unsigned integer or floating-type), `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Possible implementation + +```cpp +constexpr bool is_number() const noexcept +{ + return is_number_integer() || is_number_float(); +} +``` + +## Example + +??? example + + The following code exemplifies `is_number()` for all JSON types. + + ```cpp + --8<-- "examples/is_number.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_number.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to also return `#!cpp true` for unsigned integers in 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_number_float.md b/doc/mkdocs/docs/api/basic_json/is_number_float.md new file mode 100644 index 0000000000..d709bf71b1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_number_float.md @@ -0,0 +1,40 @@ +# basic_json::is_number_float + +```cpp +constexpr bool is_number_float() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is a floating-point number. This excludes signed and +unsigned integer values. + +## Return value + +`#!cpp true` if type is a floating-point number, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_number_float()` for all JSON types. + + ```cpp + --8<-- "examples/is_number_float.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_number_float.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_number_integer.md b/doc/mkdocs/docs/api/basic_json/is_number_integer.md new file mode 100644 index 0000000000..c308c92968 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_number_integer.md @@ -0,0 +1,41 @@ +# basic_json::is_number_integer + +```cpp +constexpr bool is_number_integer() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is a signed or unsigned integer number. This excludes +floating-point values. + +## Return value + +`#!cpp true` if type is an integer or unsigned integer number, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_number_integer()` for all JSON types. + + ```cpp + --8<-- "examples/is_number_integer.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_number_integer.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to also return `#!cpp true` for unsigned integers in 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_number_unsigned.md b/doc/mkdocs/docs/api/basic_json/is_number_unsigned.md new file mode 100644 index 0000000000..56493eae60 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_number_unsigned.md @@ -0,0 +1,40 @@ +# basic_json::is_number_unsigned + +```cpp +constexpr bool is_number_unsigned() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is an unsigned integer number. This excludes +floating-point and signed integer values. + +## Return value + +`#!cpp true` if type is an unsigned integer number, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_number_unsigned()` for all JSON types. + + ```cpp + --8<-- "examples/is_number_unsigned.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_number_unsigned.output" + ``` + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_object.md b/doc/mkdocs/docs/api/basic_json/is_object.md new file mode 100644 index 0000000000..22a15634cc --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_object.md @@ -0,0 +1,39 @@ +# basic_json::is_object + +```cpp +constexpr bool is_object() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is an object. + +## Return value + +`#!cpp true` if type is an object, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_object()` for all JSON types. + + ```cpp + --8<-- "examples/is_object.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_object.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_primitive.md b/doc/mkdocs/docs/api/basic_json/is_primitive.md new file mode 100644 index 0000000000..37be35c7f1 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_primitive.md @@ -0,0 +1,60 @@ +# basic_json::is_primitive + +```cpp +constexpr bool is_primitive() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON type is primitive (string, number, boolean, `#!json null`, +binary). + +## Return value + +`#!cpp true` if type is primitive (string, number, boolean, `#!json null`, or binary), `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Possible implementation + +```cpp +constexpr bool is_primitive() const noexcept +{ + return is_null() || is_string() || is_boolean() || is_number() || is_binary(); +} +``` + +## Notes + +The term *primitive* stems from [RFC 8259](https://tools.ietf.org/html/rfc8259): + +> JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and +> arrays). + +This library extends primitive types to binary types, because binary types are roughly comparable to strings. Hence, +`is_primitive()` returns `#!cpp true` for binary values. + +## Example + +??? example + + The following code exemplifies `is_primitive()` for all JSON types. + + ```cpp + --8<-- "examples/is_primitive.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_primitive.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to return `#!cpp true` for binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_string.md b/doc/mkdocs/docs/api/basic_json/is_string.md new file mode 100644 index 0000000000..af725fb872 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_string.md @@ -0,0 +1,39 @@ +# basic_json::is_string + +```cpp +constexpr bool is_string() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON value is a string. + +## Return value + +`#!cpp true` if type is a string, `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `is_string()` for all JSON types. + + ```cpp + --8<-- "examples/is_string.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_string.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/is_structured.md b/doc/mkdocs/docs/api/basic_json/is_structured.md new file mode 100644 index 0000000000..3975798847 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/is_structured.md @@ -0,0 +1,48 @@ +# basic_json::is_structured + +```cpp +constexpr bool is_structured() const noexcept; +``` + +This function returns `#!cpp true` if and only if the JSON type is structured (array or object). + +## Return value + +`#!cpp true` if type is structured (array or object), `#!cpp false` otherwise. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Notes + +The term *structured* stems from [RFC 8259](https://tools.ietf.org/html/rfc8259): + +> JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and +> arrays). + +Note that though strings are containers in C++, they are treated as primitive values in JSON. + +## Example + +??? example + + The following code exemplifies `is_structured()` for all JSON types. + + ```cpp + --8<-- "examples/is_structured.cpp" + ``` + + Output: + + ```json + --8<-- "examples/is_structured.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/items.md b/doc/mkdocs/docs/api/basic_json/items.md new file mode 100644 index 0000000000..d5c7113649 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/items.md @@ -0,0 +1,96 @@ +# basic_json::items + +```cpp +iteration_proxy items() noexcept; +iteration_proxy items() const noexcept; +``` + +This function allows to access `iterator::key()` and `iterator::value()` during range-based for loops. In these loops, a +reference to the JSON values is returned, so there is no access to the underlying iterator. + +For loop without `items()` function: + +```cpp +for (auto it = j_object.begin(); it != j_object.end(); ++it) +{ + std::cout << "key: " << it.key() << ", value:" << it.value() << '\n'; +} +``` + +Range-based for loop without `items()` function: + +```cpp +for (auto it : j_object) +{ + // "it" is of type json::reference and has no key() member + std::cout << "value: " << it << '\n'; +} +``` + +Range-based for loop with `items()` function: + +```cpp +for (auto& el : j_object.items()) +{ + std::cout << "key: " << el.key() << ", value:" << el.value() << '\n'; +} +``` + +The `items()` function also allows to use +[structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding) (C++17): + +```cpp +for (auto& [key, val] : j_object.items()) +{ + std::cout << "key: " << key << ", value:" << val << '\n'; +} +``` + +## Return value + +iteration proxy object wrapping the current value with an interface to use in range-based for loops + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Constant. + +## Notes + +When iterating over an array, `key()` will return the index of the element as string (see example). For primitive types +(e.g., numbers), `key()` returns an empty string. + +!!! warning + + Using `items()` on temporary objects is dangerous. Make sure the object's lifetime exeeds the iteration. See + for more information. + +## Example + +??? example + + The following code shows an example for `items()`. + + ```cpp + --8<-- "examples/items.cpp" + ``` + + Output: + + ```json + --8<-- "examples/items.output" + ``` + +## Version history + +- Added in version 3.0.0. +- Added structured binding support in version 3.5.0. + +!!! note + + This function replaces the static function `iterator_wrapper` which was introduced in version 1.0.0, but has been + deprecated in version 3.1.0. Function `iterator_wrapper` will be removed in version 4.0.0. Please replace all + occurrences of `#!cpp iterator_wrapper(j)` with `#!cpp j.items()`. diff --git a/doc/mkdocs/docs/api/basic_json/json_serializer.md b/doc/mkdocs/docs/api/basic_json/json_serializer.md new file mode 100644 index 0000000000..89379acf4a --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/json_serializer.md @@ -0,0 +1,24 @@ +# basic_json::json_serializer + +```cpp +template +using json_serializer = JSONSerializer; +``` + +## Template parameters + +`T` +: type to convert; will be used in the `to_json`/`from_json` functions + +`SFINAE` +: type to add compile type checks via SFINAE; usually `#!cpp void` + +## Notes + +#### Default type + +The default values for `json_serializer` is [`adl_serializer`](../adl_serializer.md). + +## Version history + +- Since version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/max_size.md b/doc/mkdocs/docs/api/basic_json/max_size.md new file mode 100644 index 0000000000..21fffa717d --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/max_size.md @@ -0,0 +1,58 @@ +# basic_json::max_size + +```cpp +size_type max_size() const noexcept; +``` + +Returns the maximum number of elements a JSON value is able to hold due to system or library implementation limitations, +i.e. `std::distance(begin(), end())` for the JSON value. + +## Return value + +The return value depends on the different types and is defined as follows: + +Value type | return value +----------- | ------------- +null | `0` (same as [`size()`](size.md)) +boolean | `1` (same as [`size()`](size.md)) +string | `1` (same as [`size()`](size.md)) +number | `1` (same as [`size()`](size.md)) +binary | `1` (same as [`size()`](size.md)) +object | result of function `object_t::max_size()` +array | result of function `array_t::max_size()` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Constant, as long as [`array_t`](array_t.md) and [`object_t`](object_t.md) satisfy the +[Container](https://en.cppreference.com/w/cpp/named_req/Container) concept; that is, their `max_size()` functions have +constant complexity. + +## Notes + +This function does not return the maximal length of a string stored as JSON value -- it returns the maximal number of +string elements the JSON value can store which is `1`. + +## Example + +??? example + + The following code calls `max_size()` on the different value types. Note the output is implementation specific. + + ```cpp + --8<-- "examples/max_size.cpp" + ``` + + Output: + + ```json + --8<-- "examples/max_size.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to return `1` for binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/merge_patch.md b/doc/mkdocs/docs/api/basic_json/merge_patch.md new file mode 100644 index 0000000000..e865e89d81 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/merge_patch.md @@ -0,0 +1,58 @@ +# basic_json::merge_patch + +```cpp +void merge_patch(const basic_json& apply_patch); +``` + +The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of +modifications to a target resource's content. This function applies a merge patch to the current JSON value. + +The function implements the following algorithm from Section 2 of +[RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396): + +```python +define MergePatch(Target, Patch): + if Patch is an Object: + if Target is not an Object: + Target = {} // Ignore the contents and set it to an empty Object + for each Name/Value pair in Patch: + if Value is null: + if Name exists in Target: + remove the Name/Value pair from Target + else: + Target[Name] = MergePatch(Target[Name], Value) + return Target + else: + return Patch +``` + +Thereby, `Target` is the current object; that is, the patch is applied to the current value. + +## Parameters + +`apply_patch` (in) +: the patch to apply + +## Complexity + +Linear in the lengths of `apply_patch`. + +## Example + +??? example + + The following code shows how a JSON Merge Patch is applied to a JSON document. + + ```cpp + --8<-- "examples/merge_patch.cpp" + ``` + + Output: + + ```json + --8<-- "examples/merge_patch.output" + ``` + +## Version history + +- Added in version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/meta.md b/doc/mkdocs/docs/api/basic_json/meta.md index fd57754554..807c2aa730 100644 --- a/doc/mkdocs/docs/api/basic_json/meta.md +++ b/doc/mkdocs/docs/api/basic_json/meta.md @@ -4,8 +4,8 @@ static basic_json meta(); ``` -This function returns a JSON object with information about the library, -including the version number and information on the platform and compiler. +This function returns a JSON object with information about the library, including the version number and information on +the platform and compiler. ## Return value @@ -22,8 +22,7 @@ key | description ## Exception safety -Strong guarantee: if an exception is thrown, there are no -changes to any JSON value. +Strong guarantee: if an exception is thrown, there are no changes to any JSON value. ## Complexity @@ -43,3 +42,7 @@ Output: ```json --8<-- "examples/meta.output" ``` + +## Version history + +- Added in version 2.1.0. diff --git a/doc/mkdocs/docs/api/basic_json/number_float_t.md b/doc/mkdocs/docs/api/basic_json/number_float_t.md new file mode 100644 index 0000000000..c6785eb260 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/number_float_t.md @@ -0,0 +1,54 @@ +# basic_json::number_float_t + +```cpp +using number_float_t = NumberFloatType; +``` + +The type used to store JSON numbers (floating-point). + +[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +> The representation of numbers is similar to that used in most programming languages. A number is represented in base +> 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may +> be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that +> cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. + +This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is +known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different +types, [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md) and `number_float_t` are +used. + +To store floating-point numbers in C++, a type is defined by the template parameter `NumberFloatType` which chooses the +type to use. + +## Notes + +#### Default type + +With the default values for `NumberFloatType` (`double`), the default value for `number_float_t` is `#!cpp double`. + +#### Default behavior + +- The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in floating-point literals will be + ignored. Internally, the value will be stored as decimal number. For instance, the C++ floating-point literal `01.2` + will be serialized to `1.2`. During deserialization, leading zeros yield an error. +- Not-a-number (NaN) values will be serialized to `null`. + +#### Limits + +[RFC 7159](http://rfc7159.net/rfc7159) states: +> This specification allows implementations to set limits on the range and precision of numbers accepted. Since software +> that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good +> interoperability can be achieved by implementations that expect no more precision or range than these provide, in the +> sense that implementations will approximate JSON numbers within the expected precision. + +This implementation does exactly follow this approach, as it uses double precision floating-point numbers. Note values +smaller than `-1.79769313486232e+308` and values greater than `1.79769313486232e+308` will be stored as NaN internally +and be serialized to `null`. + +#### Storage + +Floating-point number values are stored directly inside a `basic_json` type. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/number_integer_t.md b/doc/mkdocs/docs/api/basic_json/number_integer_t.md new file mode 100644 index 0000000000..c10cb7803a --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/number_integer_t.md @@ -0,0 +1,60 @@ +# basic_json::number_integer_t + +```cpp +using number_integer_t = NumberIntegerType; +``` + +The type used to store JSON numbers (integers). + +[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +> The representation of numbers is similar to that used in most programming languages. A number is represented in base +> 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may +> be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that +> cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. + +This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is +known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different +types, `number_integer_t`, [`number_unsigned_t`](number_unsigned_t.md) and [`number_float_t`](number_float_t.md) are +used. + +To store integer numbers in C++, a type is defined by the template parameter `NumberIntegerType` which chooses the type +to use. + +## Notes + +#### Default type + +With the default values for `NumberIntegerType` (`std::int64_t`), the default value for `number_integer_t` is +`#!cpp std::int64_t`. + +#### Default behavior + +- The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in integer literals lead to an + interpretation as octal number. Internally, the value will be stored as decimal number. For instance, the C++ integer + literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error. +- Not-a-number (NaN) values will be serialized to `null`. + +#### Limits + +[RFC 7159](http://rfc7159.net/rfc7159) specifies: +> An implementation may set limits on the range and precision of numbers. + +When the default type is used, the maximal integer number that can be stored is `9223372036854775807` (INT64_MAX) and +the minimal integer number that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers that are out of +range will yield over/underflow when used in a constructor. During deserialization, too large or small integer numbers +will be automatically be stored as [`number_unsigned_t`](number_unsigned_t.md) or [`number_float_t`](number_float_t.md). + +[RFC 7159](http://rfc7159.net/rfc7159) further states: +> Note that when such software is used, numbers that are integers and are in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are +> interoperable in the sense that implementations will agree exactly on their numeric values. + +As this range is a subrange of the exactly supported range [INT64_MIN, INT64_MAX], this class's integer type is +interoperable. + +#### Storage + +Integer number values are stored directly inside a `basic_json` type. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md b/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md new file mode 100644 index 0000000000..a28e25351f --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md @@ -0,0 +1,60 @@ +# basic_json::number_unsigned_t + +```cpp +using number_unsigned_t = NumberUnsignedType; +``` + +The type used to store JSON numbers (unsigned). + +[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +> The representation of numbers is similar to that used in most programming languages. A number is represented in base +> 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may +> be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that +> cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. + +This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is +known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different +types, [`number_integer_t`](number_integer_t.md), `number_unsigned_t` and [`number_float_t`](number_float_t.md) are +used. + +To store unsigned integer numbers in C++, a type is defined by the template parameter `NumberUnsignedType` which chooses +the type to use. + +## Notes + +#### Default type + +With the default values for `NumberUnsignedType` (`std::uint64_t`), the default value for `number_unsigned_t` is +`#!cpp std::uint64_t`. + +#### Default behavior + +- The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in integer literals lead to an + interpretation as octal number. Internally, the value will be stored as decimal number. For instance, the C++ integer + literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error. +- Not-a-number (NaN) values will be serialized to `null`. + +#### Limits + +[RFC 7159](http://rfc7159.net/rfc7159) specifies: +> An implementation may set limits on the range and precision of numbers. + +When the default type is used, the maximal integer number that can be stored is `18446744073709551615` (UINT64_MAX) and +the minimal integer number that can be stored is `0`. Integer numbers that are out of range will yield over/underflow +when used in a constructor. During deserialization, too large or small integer numbers will be automatically be stored +as [`number_integer_t`](number_integer_t.md) or [`number_float_t`](number_float_t.md). + +[RFC 7159](http://rfc7159.net/rfc7159) further states: +> Note that when such software is used, numbers that are integers and are in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are +> interoperable in the sense that implementations will agree exactly on their numeric values. + +As this range is a subrange (when considered in conjunction with the `number_integer_t` type) of the exactly supported +range [0, UINT64_MAX], this class's integer type is interoperable. + +#### Storage + +Integer number values are stored directly inside a `basic_json` type. + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/object.md b/doc/mkdocs/docs/api/basic_json/object.md new file mode 100644 index 0000000000..4aae6fe2a6 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/object.md @@ -0,0 +1,58 @@ +# basic_json::object + +```cpp +static basic_json object(initializer_list_t init = {}); +``` + +Creates a JSON object value from a given initializer list. The initializer lists elements must be pairs, and their first +elements must be strings. If the initializer list is empty, the empty object `#!json {}` is created. + +## Parameters + +`init` (in) +: initializer list with JSON values to create an object from (optional) + +## Return value + +JSON object value + +## Exceptions + +Throws [`type_error.301`](../../home/exceptions.md#jsonexceptiontype_error301) if `init` is not a list of pairs whose +first elements are strings. In this case, no object can be created. When such a value is passed to +`basic_json(initializer_list_t, bool, value_t)`, an array would have been created from the passed initializer list +`init`. See example below. + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of `init`. + +## Notes + +This function is only added for symmetry reasons. In contrast to the related function `array(initializer_list_t)`, there +are no cases which can only be expressed by this function. That is, any initializer list `init` can also be passed to +the initializer list constructor `basic_json(initializer_list_t, bool, value_t)`. + +## Examples + +??? example + + The following code shows an example for the `object` function. + + ```cpp + --8<-- "examples/object.cpp" + ``` + + Output: + + ```json + --8<-- "examples/object.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/object_comparator_t.md b/doc/mkdocs/docs/api/basic_json/object_comparator_t.md new file mode 100644 index 0000000000..8fb8656c8d --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/object_comparator_t.md @@ -0,0 +1,18 @@ +# basic_json::object_comparator_t + +```cpp +// until C++14 +using object_comparator_t = std::less; + +// since C++14 +using object_comparator_t = std::less<>; +``` + +The comparator used in [`object_t`](object_t.md). + +When C++14 is detected, a transparent com parator is used which, when combined with perfect forwarding on find() and +count() calls, prevents unnecessary string construction. + +## Version history + +- Unknown. diff --git a/doc/mkdocs/docs/api/basic_json/object_t.md b/doc/mkdocs/docs/api/basic_json/object_t.md new file mode 100644 index 0000000000..efc18c41f3 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/object_t.md @@ -0,0 +1,97 @@ +# basic_json::object_t + +```cpp +using object_t = ObjectType>>; +``` + +The type used to store JSON objects. + +[RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows: +> An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a +> string, number, boolean, null, object, or array. + +To store objects in C++, a type is defined by the template parameters described below. + +## Template parameters + +`ObjectType` +: the container to store objects (e.g., `std::map` or `std::unordered_map`) + +`StringType` +: the type of the keys or names (e.g., `std::string`). The comparison function `std::less` is used to + order elements inside the container. + +`AllocatorType` +: the allocator to use for objects (e.g., `std::allocator`) + +## Notes + +#### Default type + +With the default values for `ObjectType` (`std::map`), `StringType` (`std::string`), and `AllocatorType` +(`std::allocator`), the default value for `object_t` is: + +```cpp +// until C++14 +std::map< + std::string, // key_type + basic_json, // value_type + std::less, // key_compare + std::allocator> // allocator_type +> + +// since C++14 +std::map< + std::string, // key_type + basic_json, // value_type + std::less<>, // key_compare + std::allocator> // allocator_type +> +``` + +See [`object_comparator_t`](object_comparator_t.md) for more information. + +#### Behavior + +The choice of `object_t` influences the behavior of the JSON class. With the default type, objects have the following +behavior: + +- When all names are unique, objects will be interoperable in the sense that all software implementations receiving that + object will agree on the name-value mappings. +- When the names within an object are not unique, it is unspecified which one of the values for a given key will be + chosen. For instance, `#!json {"key": 2, "key": 1}` could be equal to either `#!json {"key": 1}` or + `#!json {"key": 2}`. +- Internally, name/value pairs are stored in lexicographical order of the names. Objects will also be serialized (see + [`dump`](dump.md)) in this order. For instance, `#!json {"b": 1, "a": 2}` and `#!json {"a": 2, "b": 1}` will be stored + and serialized as `#!json {"a": 2, "b": 1}`. +- When comparing objects, the order of the name/value pairs is irrelevant. This makes objects interoperable in the sense + that they will not be affected by these differences. For instance, `#!json {"b": 1, "a": 2}` and + `#!json {"a": 2, "b": 1}` will be treated as equal. + +#### Limits + +[RFC 7159](http://rfc7159.net/rfc7159) specifies: +> An implementation may set limits on the maximum depth of nesting. + +In this class, the object's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be +introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the +[`max_size`](max_size.md) function of a JSON object. + +#### Storage + +Objects are stored as pointers in a `basic_json` type. That is, for any access to object values, a pointer of type +`object_t*` must be dereferenced. + +#### Object key order + +The order name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may +return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in +alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to +[RFC 7159](http://rfc7159.net/rfc7159), because any order implements the specified "unordered" nature of JSON objects. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator+=.md b/doc/mkdocs/docs/api/basic_json/operator+=.md new file mode 100644 index 0000000000..59001a58fe --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator+=.md @@ -0,0 +1,113 @@ +# basic_json::operator+= + +```cpp +// (1) +reference operator+=(basic_json&& val); +reference operator+=(const basic_json& val); + +// (2) +reference operator+=(const typename object_t::value_type& val); + +// (3) +reference operator+=(initializer_list_t init); +``` + +1. Appends the given element `val` to the end of the JSON array. If the function is called on a JSON null value, an + empty array is created before appending `val`. + +2. Inserts the given element `val` to the JSON object. If the function is called on a JSON null value, an empty object + is created before inserting `val`. + +3. This function allows to use `operator+=` with an initializer list. In case + + 1. the current value is an object, + 2. the initializer list `init` contains only two elements, and + 3. the first element of `init` is a string, + + `init` is converted into an object element and added using `operator+=(const typename object_t::value_type&)`. + Otherwise, `init` is converted to a JSON value and added using `operator+=(basic_json&&)`. + +## Parameters + +`val` (in) +: the value to add to the JSON array/object + +`init` (in) +: an initializer list + +## Return value + +`#!cpp *this` + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than + JSON array or null; example: `"cannot use operator+=() with number"` +2. The function can throw the following exceptions: + - Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than + JSON object or null; example: `"cannot use operator+=() with number"` + +## Complexity + +1. Amortized constant. +2. Logarithmic in the size of the container, O(log(`size()`)). +3. Linear in the size of the initializer list `init`. + +## Notes + +(3) This function is required to resolve an ambiguous overload error, because pairs like `{"key", "value"}` can be both +interpreted as `object_t::value_type` or `std::initializer_list`, see +[#235](https://github.com/nlohmann/json/issues/235) for more information. + +## Examples + +??? example + + The example shows how `push_back()` and `+=` can be used to add elements to a JSON array. Note how the `null` value + was silently converted to a JSON array. + + ```cpp + --8<-- "examples/push_back.cpp" + ``` + + Output: + + ```json + --8<-- "examples/push_back.output" + ``` + +??? example + + The example shows how `push_back()` and `+=` can be used to add elements to a JSON object. Note how the `null` value + was silently converted to a JSON object. + + ```cpp + --8<-- "examples/push_back__object_t__value.cpp" + ``` + + Output: + + ```json + --8<-- "examples/push_back__object_t__value.output" + ``` + +??? example + + The example shows how initializer lists are treated as objects when possible. + + ```cpp + --8<-- "examples/push_back__initializer_list.cpp" + ``` + + Output: + + ```json + --8<-- "examples/push_back__initializer_list.output" + ``` + +## Version history + +1. Since version 1.0.0. +2. Since version 1.0.0. +2. Since version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator=.md b/doc/mkdocs/docs/api/basic_json/operator=.md new file mode 100644 index 0000000000..340f8eaf3b --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator=.md @@ -0,0 +1,43 @@ +# basic_json::operator= + +```cpp +basic_json& operator=(basic_json other) noexcept ( + std::is_nothrow_move_constructible::value && + std::is_nothrow_move_assignable::value && + std::is_nothrow_move_constructible::value && + std::is_nothrow_move_assignable::value +); +``` + +Copy assignment operator. Copies a JSON value via the "copy and swap" strategy: It is expressed in terms of the copy +constructor, destructor, and the `swap()` member function. + +## Parameters + +`other` (in) +: value to copy from + +## Complexity + +Linear. + +## Example + +??? example + + The code below shows and example for the copy assignment. It creates a copy of value `a` which is then swapped with + `b`. Finally, the copy of `a` (which is the null value after the swap) is destroyed. + + ```cpp + --8<-- "examples/basic_json__copyassignment.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__copyassignment.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator[].md b/doc/mkdocs/docs/api/basic_json/operator[].md new file mode 100644 index 0000000000..4b36f1a913 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator[].md @@ -0,0 +1,191 @@ +# basic_json::operator[] + +```cpp +// (1) +reference operator[](size_type idx); +const_reference operator[](size_type idx) const; + +// (2) +reference operator[](const typename object_t::key_type& key); +const_reference operator[](const typename object_t::key_type& key) const; +template +reference operator[](T* key); +template +const_reference operator[](T* key) const; + +// (3) +reference operator[](const json_pointer& ptr); +const_reference operator[](const json_pointer& ptr) const; +``` + +1. Returns a reference to the element at specified location `idx`. +2. Returns a reference to the element at with specified key `key`. +3. Returns a reference to the element at with specified JSON pointer `ptr`. + +## Template parameters + +`T` +: string literal convertible to `object_t::key_type` + +## Parameters + +`idx` (in) +: index of the element to access + +`key` (in) +: object key of the elements to remove + +`ptr` (in) +: JSON pointer to the desired element + +## Return value + +1. reference to the element at index `idx` +2. reference to the element at key `key` +3. reference to the element pointed to by `ptr` + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an array + or null; in that cases, using the `[]` operator with an index makes no sense. +2. The function can throw the following exceptions: + - Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an array + or null; in that cases, using the `[]` operator with an index makes no sense. +3. The function can throw the following exceptions: + - Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index in the passed + JSON pointer `ptr` begins with '0'. + - Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in the passed + JSON pointer `ptr` is not a number. + - Throws [`out_of_range.402`](../../home/exceptions.md#jsonexceptionout_of_range402) if the array index '-' is used + in the passed JSON pointer `ptr` for the const version. + - Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can + not be resolved. + +## Notes + +!!! danger + + 1. If the element with key `idx` does not exist, the behavior is undefined. + 2. If the element with key `key` does not exist, the behavior is undefined and is **guarded by an assertion**! + +1. The non-const version may add values: If `idx` is beyond the range of the array (i.e., `idx >= size()`), then the + array is silently filled up with `#!json null` values to make `idx` a valid reference to the last stored element. In + case the value was `#!json null` before, it is converted to an array. + +2. If `key` is not found in the object, then it is silently added to the object and filled with a `#!json null` value to + make `key` a valid reference. In case the value was `#!json null` before, it is converted to an object. + +3. `null` values are created in arrays and objects if necessary. + + In particular: + + - If the JSON pointer points to an object key that does not exist, it is created an filled with a `#!json null` + value before a reference to it is returned. + - If the JSON pointer points to an array index that does not exist, it is created an filled with a `#!json null` + value before a reference to it is returned. All indices between the current maximum and the given index are also + filled with `#!json null`. + - The special value `-` is treated as a synonym for the index past the end. + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +1. Constant if `idx` is in the range of the array. Otherwise linear in `idx - size()`. +2. Logarithmic in the size of the container. +3. Constant + +## Example + +??? example + + The example below shows how array elements can be read and written using `[]` operator. Note the addition of + `#!json null` values. + + ```cpp + --8<-- "examples/operatorarray__size_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operatorarray__size_type.output" + ``` + +??? example + + The example below shows how array elements can be read using the `[]` operator. + + ```cpp + --8<-- "examples/operatorarray__size_type_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operatorarray__size_type_const.output" + ``` + +??? example + + The example below shows how object elements can be read and written using the `[]` operator. + + ```cpp + --8<-- "examples/operatorarray__key_type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operatorarray__key_type.output" + ``` + +??? example + + The example below shows how object elements can be read using the `[]` operator. + + ```cpp + --8<-- "examples/operatorarray__key_type_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operatorarray__key_type_const.output" + ``` + +??? example + + The example below shows how values can be read and written using JSON Pointers. + + ```cpp + --8<-- "examples/operatorjson_pointer.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operatorjson_pointer.output" + ``` + +??? example + + The example below shows how values can be read using JSON Pointers. + + ```cpp + --8<-- "examples/operatorjson_pointer_const.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operatorjson_pointer_const.output" + ``` + +## Version history + +1. Added in version 1.0.0. +2. Added in version 1.0.0. Overloads for `T* key` added in version 1.1.0. +3. Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_ValueType.md b/doc/mkdocs/docs/api/basic_json/operator_ValueType.md new file mode 100644 index 0000000000..cfb5e64c85 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_ValueType.md @@ -0,0 +1,72 @@ +# basic_json::operator ValueType + +```cpp +template +JSON_EXPLICIT operator ValueType() const; +``` + +Implicit type conversion between the JSON value and a compatible value. The call is realized by calling +[`get()`](get.md). See [Notes](#notes) for the meaning of `JSON_EXPLICIT`. + +## Template parameters + +`ValueType` +: the value type to return + +## Return value + +copy of the JSON value, converted to `ValueType` + +## Exceptions + +Depends on what `json_serializer` `from_json()` method throws + +## Complexity + +Linear in the size of the JSON value. + +## Notes + +By default `JSON_EXPLICIT` defined to the empty string, so the signature is: + +```cpp +template +operator ValueType() const; +``` + +If [`JSON_USE_IMPLICIT_CONVERSIONS`](../../features/macros.md#json_use_implicit_conversions) is set to `0`, +`JSON_EXPLICIT` is defined to `#!cpp explicit`: + +```cpp +template +explicit operator ValueType() const; +``` + +That is, implicit conversions can be switched off by defining +[`JSON_USE_IMPLICIT_CONVERSIONS`](../../features/macros.md#json_use_implicit_conversions) to `0`. + +## Example + +??? example + + The example below shows several conversions from JSON values + to other types. There a few things to note: (1) Floating-point numbers can + be converted to integers, (2) A JSON array can be converted to a standard + `std::vector`, (3) A JSON object can be converted to C++ + associative containers such as `std::unordered_map`. + + ```cpp + --8<-- "examples/operator__ValueType.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__ValueType.output" + ``` + +## Version history + +- Since version 1.0.0. +- Macros `JSON_EXPLICIT`/[`JSON_USE_IMPLICIT_CONVERSIONS`](../../features/macros.md#json_use_implicit_conversions) added + in version 3.9.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_eq.md b/doc/mkdocs/docs/api/basic_json/operator_eq.md new file mode 100644 index 0000000000..34cf5537d5 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_eq.md @@ -0,0 +1,106 @@ +# basic_json::operator== + +```cpp +bool operator==(const_reference lhs, const_reference rhs) noexcept; + +template +bool operator==(const_reference lhs, const ScalarType rhs) noexcept; + +template +bool operator==(ScalarType lhs, const const_reference rhs) noexcept; +``` + +Compares two JSON values for equality according to the following rules: + +- Two JSON values are equal if (1) they are not discarded, (2) they are from the same type, and (3) their stored values + are the same according to their respective `operator==`. +- Integer and floating-point numbers are automatically converted before comparison. Note that two NaN values are always + treated as unequal. + +## Template parameters + +`ScalarType` +: a scalar type according to `std::is_scalar::value` + +## Parameters + +`lhs` (in) +: first value to consider + +`rhs` (in) +: second value to consider + +## Return value + +whether the values `lhs` and `rhs` are equal + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear. + +## Notes + +!!! note + + - NaN values never compare equal to themselves or to other NaN values. + - JSON `#!cpp null` values are all equal. + - Discarded values never compare equal to themselves. + +!!! note + + Floating-point numbers inside JSON values numbers are compared with `json::number_float_t::operator==` which is + `double::operator==` by default. To compare floating-point while respecting an epsilon, an alternative + [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39) + could be used, for instance + + ```cpp + template::value, T>::type> + inline bool is_same(T a, T b, T epsilon = std::numeric_limits::epsilon()) noexcept + { + return std::abs(a - b) <= epsilon; + } + ``` + + Or you can self-defined operator equal function like this: + + ```cpp + bool my_equal(const_reference lhs, const_reference rhs) + { + const auto lhs_type lhs.type(); + const auto rhs_type rhs.type(); + if (lhs_type == rhs_type) + { + switch(lhs_type) + // self_defined case + case value_t::number_float: + return std::abs(lhs - rhs) <= std::numeric_limits::epsilon(); + // other cases remain the same with the original + ... + } + ... + } + ``` + +## Example + +??? example + + The example demonstrates comparing several JSON types. + + ```cpp + --8<-- "examples/operator__equal.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__equal.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_ge.md b/doc/mkdocs/docs/api/basic_json/operator_ge.md new file mode 100644 index 0000000000..19834c0d88 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_ge.md @@ -0,0 +1,59 @@ +# basic_json::operator>= + +```cpp +bool operator>=(const_reference lhs, const_reference rhs) noexcept, + +template +bool operator>=(const_reference lhs, const ScalarType rhs) noexcept; + +template +bool operator>=(ScalarType lhs, const const_reference rhs) noexcept; +``` + +Compares whether one JSON value `lhs` is greater than or equal to another JSON value `rhs` by calculating +`#!cpp !(lhs < rhs)`. + +## Template parameters + +`ScalarType` +: a scalar type according to `std::is_scalar::value` + +## Parameters + +`lhs` (in) +: first value to consider + +`rhs` (in) +: second value to consider + +## Return value + +whether `lhs` is less than or equal to `rhs` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear. + +## Example + +??? example + + The example demonstrates comparing several JSON types. + + ```cpp + --8<-- "examples/operator__greaterequal.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__greaterequal.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_gt.md b/doc/mkdocs/docs/api/basic_json/operator_gt.md new file mode 100644 index 0000000000..b9e32629d6 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_gt.md @@ -0,0 +1,58 @@ +# basic_json::operator> + +```cpp +bool operator>(const_reference lhs, const_reference rhs) noexcept, + +template +bool operator>(const_reference lhs, const ScalarType rhs) noexcept; + +template +bool operator>(ScalarType lhs, const const_reference rhs) noexcept; +``` + +Compares whether one JSON value `lhs` is greater than another JSON value `rhs` by calculating `#!cpp !(lhs <= rhs)`. + +## Template parameters + +`ScalarType` +: a scalar type according to `std::is_scalar::value` + +## Parameters + +`lhs` (in) +: first value to consider + +`rhs` (in) +: second value to consider + +## Return value + +whether `lhs` is greater than `rhs` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear. + +## Example + +??? example + + The example demonstrates comparing several JSON types. + + ```cpp + --8<-- "examples/operator__greater.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__greater.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_le.md b/doc/mkdocs/docs/api/basic_json/operator_le.md new file mode 100644 index 0000000000..452ee49666 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_le.md @@ -0,0 +1,59 @@ +# basic_json::operator<= + +```cpp +bool operator<=(const_reference lhs, const_reference rhs) noexcept, + +template +bool operator<=(const_reference lhs, const ScalarType rhs) noexcept; + +template +bool operator<=(ScalarType lhs, const const_reference rhs) noexcept; +``` + +Compares whether one JSON value `lhs` is less than or equal to another JSON value `rhs` by calculating +`#cpp !(rhs < lhs)`. + +## Template parameters + +`ScalarType` +: a scalar type according to `std::is_scalar::value` + +## Parameters + +`lhs` (in) +: first value to consider + +`rhs` (in) +: second value to consider + +## Return value + +whether `lhs` is less than or equal to `rhs` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear. + +## Example + +??? example + + The example demonstrates comparing several JSON types. + + ```cpp + --8<-- "examples/operator__lessequal.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__lessequal.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_literal_json.md b/doc/mkdocs/docs/api/basic_json/operator_literal_json.md new file mode 100644 index 0000000000..8c96067b9f --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_literal_json.md @@ -0,0 +1,32 @@ +# basic_json::operator""_json + +```cpp +json operator "" _json(const char* s, std::size_t n) +``` + +This operator implements a user-defined string literal for JSON objects. It can be used by adding `#!cpp _json` to a +string literal and returns a [`json`](../json.md) object if no parse error occurred. + +## Parameters + +`s` (in) +: a string representation of a JSON object + +`n` (in) +: length of string `s` + +## Return value + +[`json`](../json.md) value parsed from `s` + +## Exceptions + +The function can throw anything that [`parse(s, s+n)`](parse.md) would throw. + +## Complexity + +Linear. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md b/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md new file mode 100644 index 0000000000..0316b7c61f --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md @@ -0,0 +1,32 @@ +# basic_json::operator""_json_pointer + +```cpp +json_pointer operator "" _json_pointer(const char* s, std::size_t n) +``` + +This operator implements a user-defined string literal for JSON Pointers. It can be used by adding `#!cpp _json_pointer` +to a string literal and returns a [`json_pointer`](../json_pointer.md) object if no parse error occurred. + +## Parameters + +`s` (in) +: a string representation of a JSON Pointer + +`n` (in) +: length of string `s` + +## Return value + +[`json_pointer`](../json_pointer.md) value parsed from `s` + +## Exceptions + +The function can throw anything that [`json_pointer::json_pointer`](../json_pointer.md) would throw. + +## Complexity + +Linear. + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_lt.md b/doc/mkdocs/docs/api/basic_json/operator_lt.md new file mode 100644 index 0000000000..e8d2fb359b --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_lt.md @@ -0,0 +1,73 @@ +# basic_json::operator< + +```cpp +bool operator<(const_reference lhs, const_reference rhs) noexcept, + +template +bool operator<(const_reference lhs, const ScalarType rhs) noexcept; + +template +bool operator<(ScalarType lhs, const const_reference rhs) noexcept; +``` + +Compares whether one JSON value `lhs` is less than another JSON value `rhs` according to the following rules: + +- If `lhs` and `rhs` have the same type, the values are compared using the default `<` operator. +- Integer and floating-point numbers are automatically converted before comparison +- Discarded values a +- In case `lhs` and `rhs` have different types, the values are ignored and the order of the types is considered, which + is: + 1. null + 2. boolean + 3. number (all types) + 4. object + 5. array + 6. string + 7. binary + + For instance, any boolean value is considered less than any string. + +## Template parameters + +`ScalarType` +: a scalar type according to `std::is_scalar::value` + +## Parameters + +`lhs` (in) +: first value to consider + +`rhs` (in) +: second value to consider + +## Return value + +whether `lhs` is less than `rhs` + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear. + +## Example + +??? example + + The example demonstrates comparing several JSON types. + + ```cpp + --8<-- "examples/operator__less.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__less.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_ne.md b/doc/mkdocs/docs/api/basic_json/operator_ne.md new file mode 100644 index 0000000000..5a86201ef5 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_ne.md @@ -0,0 +1,57 @@ +# basic_json::operator!= + +```cpp +bool operator!=(const_reference lhs, const_reference rhs) noexcept; + +template +bool operator!=(const_reference lhs, const ScalarType rhs) noexcept; + +template +bool operator!=(ScalarType lhs, const const_reference rhs) noexcept; +``` + +Compares two JSON values for inequality by calculating `#!cpp !(lhs == rhs)`. + +## Template parameters + +`ScalarType` +: a scalar type according to `std::is_scalar::value` + +## Parameters + +`lhs` (in) +: first value to consider + +`rhs` (in) +: second value to consider + +## Return value + +whether the values `lhs` and `rhs` are not equal + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Linear. + +## Example + +The example demonstrates comparing several JSON +types. + +```cpp +--8<-- "examples/operator__notequal.cpp" +``` + +Output: + +```json +--8<-- "examples/operator__notequal.output" +``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_value_t.md b/doc/mkdocs/docs/api/basic_json/operator_value_t.md new file mode 100644 index 0000000000..52125146c3 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/operator_value_t.md @@ -0,0 +1,54 @@ +# basic_json::operator value_t + +```cpp +constexpr operator value_t() const noexcept; +``` + +Return the type of the JSON value as a value from the [`value_t`](value_t.md) enumeration. + +## Return value + +the type of the JSON value + +Value type | return value +------------------------- | ------------------------- +`#!json null` | `value_t::null` +boolean | `value_t::boolean` +string | `value_t::string` +number (integer) | `value_t::number_integer` +number (unsigned integer) | `value_t::number_unsigned` +number (floating-point) | `value_t::number_float` +object | `value_t::object` +array | `value_t::array` +binary | `value_t::binary` +discarded | `value_t::discarded` + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `operator value_t()` for all JSON types. + + ```cpp + --8<-- "examples/operator__value_t.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator__value_t.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Added unsigned integer type in version 2.0.0. +- Added binary type in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/other_error.md b/doc/mkdocs/docs/api/basic_json/other_error.md new file mode 100644 index 0000000000..492b2d4843 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/other_error.md @@ -0,0 +1,59 @@ +# basic_json::other_error + +```cpp +class other_error : public exception; +``` + +This exception is thrown in case of errors that cannot be classified with the other exception types. + +Exceptions have ids 5xx. + +```plantuml +std::exception <|-- basic_json::exception +basic_json::exception <|-- basic_json::parse_error +basic_json::exception <|-- basic_json::invalid_iterator +basic_json::exception <|-- basic_json::type_error +basic_json::exception <|-- basic_json::out_of_range +basic_json::exception <|-- basic_json::other_error + +interface std::exception {} + +class basic_json::exception { + + const int id + + const char* what() const +} + +class basic_json::parse_error { + + const std::size_t byte +} + +class basic_json::other_error #FFFF00 {} +``` + +## Member functions + +- **what** - returns explanatory string + +## Member variables + +- **id** - the id of the exception + +## Example + +??? example + + The following code shows how a `other_error` exception can be caught. + + ```cpp + --8<-- "examples/other_error.cpp" + ``` + + Output: + + ```json + --8<-- "examples/other_error.output" + ``` + +## Version history + +- Since version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/out_of_range.md b/doc/mkdocs/docs/api/basic_json/out_of_range.md new file mode 100644 index 0000000000..47ead87c26 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/out_of_range.md @@ -0,0 +1,60 @@ +# basic_json::out_of_range + +```cpp +class out_of_range : public exception; +``` + +This exception is thrown in case a library function is called on an input parameter that exceeds the expected range, for +instance in case of array indices or nonexisting object keys. + +Exceptions have ids 4xx. + +```plantuml +std::exception <|-- basic_json::exception +basic_json::exception <|-- basic_json::parse_error +basic_json::exception <|-- basic_json::invalid_iterator +basic_json::exception <|-- basic_json::type_error +basic_json::exception <|-- basic_json::out_of_range +basic_json::exception <|-- basic_json::other_error + +interface std::exception {} + +class basic_json::exception { + + const int id + + const char* what() const +} + +class basic_json::parse_error { + + const std::size_t byte +} + +class basic_json::out_of_range #FFFF00 {} +``` + +## Member functions + +- **what** - returns explanatory string + +## Member variables + +- **id** - the id of the exception + +## Example + +??? example + + The following code shows how a `out_of_range` exception can be caught. + + ```cpp + --8<-- "examples/out_of_range.cpp" + ``` + + Output: + + ```json + --8<-- "examples/out_of_range.output" + ``` + +## Version history + +- Since version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/parse.md b/doc/mkdocs/docs/api/basic_json/parse.md index f89df426b0..1f991b3817 100644 --- a/doc/mkdocs/docs/api/basic_json/parse.md +++ b/doc/mkdocs/docs/api/basic_json/parse.md @@ -6,22 +6,21 @@ template static basic_json parse(InputType&& i, const parser_callback_t cb = nullptr, const bool allow_exceptions = true, - const bool ignore_comments = false) + const bool ignore_comments = false); // (2) template -static basic_json parse(IteratorType first, - IteratorType last, +static basic_json parse(IteratorType first, IteratorType last, const parser_callback_t cb = nullptr, const bool allow_exceptions = true, - const bool ignore_comments = false) + const bool ignore_comments = false); ``` 1. Deserialize from a compatible input. 2. Deserialize from a pair of character iterators - The value_type of the iterator must be a integral type with size of 1, 2 or - 4 bytes, which will be interpreted respectively as UTF-8, UTF-16 and UTF-32. + The value_type of the iterator must be a integral type with size of 1, 2 or 4 bytes, which will be interpreted + respectively as UTF-8, UTF-16 and UTF-32. ## Template parameters @@ -32,11 +31,10 @@ static basic_json parse(IteratorType first, - a `FILE` pointer - a C-style array of characters - a pointer to a null-terminated string of single byte characters - - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of - iterators. + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators. `IteratorType` -: Description +: a compatible iterator type ## Parameters @@ -44,17 +42,15 @@ static basic_json parse(IteratorType first, : Input to parse from. `cb` (in) -: a parser callback function of type `parser_callback_t` - which is used to control the deserialization by filtering unwanted values - (optional) +: a parser callback function of type [`parser_callback_t`](parser_callback_t.md) which is used to control the + deserialization by filtering unwanted values (optional) `allow_exceptions` (in) : whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default) `ignore_comments` (in) -: whether comments should be ignored and treated - like whitespace (`#!cpp true`) or yield a parse error (`#!cpp false`); (optional, `#!cpp false` by - default) +: whether comments should be ignored and treated like whitespace (`#!cpp true`) or yield a parse error + (`#!cpp false`); (optional, `#!cpp false` by default) `first` (in) : iterator to start of character range @@ -64,16 +60,18 @@ static basic_json parse(IteratorType first, ## Return value -Deserialized JSON value; in case of a parse error and `allow_exceptions` -set to `#!cpp false`, the return value will be `value_t::discarded`. +Deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be +`value_t::discarded`. The latter can be checked with [`is_discarded`](is_discarded.md). ## Exception safety +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + ## Complexity -Linear in the length of the input. The parser is a predictive -LL(1) parser. The complexity can be higher if the parser callback function -`cb` or reading from (1) the input `i` or (2) the iterator range [`first`, `last`] has a super-linear complexity. +Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the parser +callback function `cb` or reading from (1) the input `i` or (2) the iterator range [`first`, `last`] has a +super-linear complexity. ## Notes @@ -81,10 +79,9 @@ LL(1) parser. The complexity can be higher if the parser callback function ## Examples -??? example +??? example "Parsing from a charater array" - The example below demonstrates the `parse()` function reading - from an array. + The example below demonstrates the `parse()` function reading from an array. ```cpp --8<-- "examples/parse__array__parser_callback_t.cpp" @@ -96,10 +93,9 @@ LL(1) parser. The complexity can be higher if the parser callback function --8<-- "examples/parse__array__parser_callback_t.output" ``` -??? example +??? example "Parsing from a string" - The example below demonstrates the `parse()` function with - and without callback function. + The example below demonstrates the `parse()` function with and without callback function. ```cpp --8<-- "examples/parse__string__parser_callback_t.cpp" @@ -111,10 +107,9 @@ LL(1) parser. The complexity can be higher if the parser callback function --8<-- "examples/parse__string__parser_callback_t.output" ``` -??? example +??? example "Parsing from an input stream" - The example below demonstrates the `parse()` function with - and without callback function. + The example below demonstrates the `parse()` function with and without callback function. ```cpp --8<-- "examples/parse__istream__parser_callback_t.cpp" @@ -126,10 +121,9 @@ LL(1) parser. The complexity can be higher if the parser callback function --8<-- "examples/parse__istream__parser_callback_t.output" ``` -??? example +??? example "Parsing from a contiguous container" - The example below demonstrates the `parse()` function reading - from a contiguous container. + The example below demonstrates the `parse()` function reading from a contiguous container. ```cpp --8<-- "examples/parse__contiguouscontainer__parser_callback_t.cpp" @@ -141,6 +135,22 @@ LL(1) parser. The complexity can be higher if the parser callback function --8<-- "examples/parse__contiguouscontainer__parser_callback_t.output" ``` -## History +??? example "Effect of `allow_exceptions` parameter" + + The example below demonstrates the effect of the `allow_exceptions` parameter in the ´parse()` function. + + ```cpp + --8<-- "examples/parse__allow_exceptions.cpp" + ``` + + Output: + + ```json + --8<-- "examples/parse__allow_exceptions.output" + ``` + +## Version history -(1) version 2.0.3 (contiguous containers); version 3.9.0 allowed to ignore comments. +- Added in version 1.0.0. +- Overload for contiguous containers (1) added in version 2.0.3. +- Ignoring comments via `ignore_comments` added in version 3.9.0. diff --git a/doc/mkdocs/docs/api/basic_json/parse_error.md b/doc/mkdocs/docs/api/basic_json/parse_error.md new file mode 100644 index 0000000000..de0ee40102 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/parse_error.md @@ -0,0 +1,64 @@ +# basic_json::parse_error + +```cpp +class parse_error : public exception; +``` + +This exception is thrown by the library when a parse error occurs. Parse errors can occur during the deserialization of +JSON text, BSON, CBOR, MessagePack, UBJSON, as well as when using JSON Patch. + +Exceptions have ids 1xx. + +```plantuml +std::exception <|-- basic_json::exception +basic_json::exception <|-- basic_json::parse_error +basic_json::exception <|-- basic_json::invalid_iterator +basic_json::exception <|-- basic_json::type_error +basic_json::exception <|-- basic_json::out_of_range +basic_json::exception <|-- basic_json::other_error + +interface std::exception {} + +class basic_json::exception { + + const int id + + const char* what() const +} + +class basic_json::parse_error #FFFF00 { + + const std::size_t byte +} +``` + +## Member functions + +- **what** - returns explanatory string + +## Member variables + +- **id** - the id of the exception +- **byte** - byte index of the parse error + +## Note + +For an input with _n_ bytes, 1 is the index of the first character and _n_+1 is the index of the terminating null byte +or the end of file. This also holds true when reading a byte vector for binary formats. + +## Example + +??? example + + The following code shows how a `parse_error` exception can be caught. + + ```cpp + --8<-- "examples/parse_error.cpp" + ``` + + Output: + + ```json + --8<-- "examples/parse_error.output" + ``` + +## Version history + +- Since version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/parse_event_t.md b/doc/mkdocs/docs/api/basic_json/parse_event_t.md new file mode 100644 index 0000000000..867de48bd8 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/parse_event_t.md @@ -0,0 +1,29 @@ +# basic_json::parse_event_t + +```cpp +enum class parse_event_t : std::uint8_t { + object_start, + object_end, + array_start, + array_end, + key, + value +}; +``` + +The parser callback distinguishes the following events: + +- `object_start`: the parser read `{` and started to process a JSON object +- `key`: the parser read a key of a value in an object +- `object_end`: the parser read `}` and finished processing a JSON object +- `array_start`: the parser read `[` and started to process a JSON array +- `array_end`: the parser read `]` and finished processing a JSON array +- `value`: the parser finished reading a JSON value + +## Example + +![Example when certain parse events are triggered](../../images/callback_events.png) + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/parser_callback_t.md b/doc/mkdocs/docs/api/basic_json/parser_callback_t.md new file mode 100644 index 0000000000..aeb7c2706f --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/parser_callback_t.md @@ -0,0 +1,73 @@ +# basic_json::parser_callback_t + +```cpp +template +using parser_callback_t = + std::function; +``` + +With a parser callback function, the result of parsing a JSON text can be influenced. When passed to +[`parse`](parse.md), it is called on certain events (passed as [`parse_event_t`](parse_event_t.md) via parameter +`event`) with a set recursion depth `depth` and context JSON value `parsed`. The return value of the callback function +is a boolean indicating whether the element that emitted the callback shall be kept or not. + +We distinguish six scenarios (determined by the event type) in which the callback function can be called. The following +table describes the values of the parameters `depth`, `event`, and `parsed`. + +parameter `event` | description | parameter `depth` | parameter `parsed` +------------------ | ----------- | ------------------ | ------------------- +`parse_event_t::object_start` | the parser read `{` and started to process a JSON object | depth of the parent of the JSON object | a JSON value with type discarded +`parse_event_t::key` | the parser read a key of a value in an object | depth of the currently parsed JSON object | a JSON string containing the key +`parse_event_t::object_end` | the parser read `}` and finished processing a JSON object | depth of the parent of the JSON object | the parsed JSON object +`parse_event_t::array_start` | the parser read `[` and started to process a JSON array | depth of the parent of the JSON array | a JSON value with type discarded +`parse_event_t::array_end` | the parser read `]` and finished processing a JSON array | depth of the parent of the JSON array | the parsed JSON array +`parse_event_t::value` | the parser finished reading a JSON value | depth of the value | the parsed JSON value + +![Example when certain parse events are triggered](../../images/callback_events.png) + +Discarding a value (i.e., returning `#!cpp false`) has different effects depending on the context in which function was +called: + +- Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never + read. +- In case a value outside a structured type is skipped, it is replaced with `null`. This case happens if the top-level + element is skipped. + +## Parameters + +`depth` (in) +: the depth of the recursion during parsing + +`event` (in) +: an event of type [`parse_event_t`](parse_event_t.md) indicating the context in + the callback function has been called + +`parsed` (in, out) +: the current intermediate parse result; note that + writing to this value has no effect for `parse_event_t::key` events + +## Return value + +Whether the JSON value which called the function during parsing should be kept (`#!cpp true`) or not (`#!cpp false`). In +the latter case, it is either skipped completely or replaced by an empty discarded object. + +# Example + +??? example + + The example below demonstrates the `parse()` function with + and without callback function. + + ```cpp + --8<-- "examples/parse__string__parser_callback_t.cpp" + ``` + + Output: + + ```json + --8<-- "examples/parse__string__parser_callback_t.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/patch.md b/doc/mkdocs/docs/api/basic_json/patch.md new file mode 100644 index 0000000000..d73e9c0d35 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/patch.md @@ -0,0 +1,66 @@ +# basic_json::patch + +```cpp +basic_json patch(const basic_json& json_patch) const; +``` + +[JSON Patch](http://jsonpatch.com) defines a JSON document structure for expressing a sequence of operations to apply to +a JSON) document. With this function, a JSON Patch is applied to the current JSON value by executing all operations from +the patch. + +## Parameters + +`json_patch` (in) +: JSON patch document + +## Return value + +patched document + +## Exceptions + +- Throws [`parse_error.104`](../../home/exceptions.md#jsonexceptionparse_error104) if the JSON patch does not consist of + an array of objects. +- Throws [`parse_error.105`](../../home/exceptions.md#jsonexceptionparse_error105) if the JSON patch is malformed (e.g., + mandatory attributes are missing); example: `"operation add must have member path"`. +- Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) if an array index is out of range. +- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a JSON pointer inside the patch + could not be resolved successfully in the current JSON value; example: `"key baz not found"`. +- Throws [`out_of_range.405`](../../home/exceptions.md#jsonexceptionout_of_range405) if JSON pointer has no parent + ("add", "remove", "move") +- Throws [`out_of_range.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was + unsuccessful. + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the JSON value and the length of the JSON patch. As usually only a fraction of the JSON value is +affected by the patch, the complexity can usually be neglected. + +## Note + +The application of a patch is atomic: Either all operations succeed and the patched document is returned or an exception +is thrown. In any case, the original value is not changed: the patch is applied to a copy of the value. + +## Example + +??? example + + The following code shows how a JSON patch is applied to a value. + + ```cpp + --8<-- "examples/patch.cpp" + ``` + + Output: + + ```json + --8<-- "examples/patch.output" + ``` + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/push_back.md b/doc/mkdocs/docs/api/basic_json/push_back.md new file mode 100644 index 0000000000..c10c94a3eb --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/push_back.md @@ -0,0 +1,109 @@ +# basic_json::push_back + +```cpp +// (1) +void push_back(basic_json&& val); +void push_back(const basic_json& val); + +// (2) +void push_back(const typename object_t::value_type& val); + +// (3) +void push_back(initializer_list_t init); +``` + +1. Appends the given element `val` to the end of the JSON array. If the function is called on a JSON null value, an + empty array is created before appending `val`. + +2. Inserts the given element `val` to the JSON object. If the function is called on a JSON null value, an empty object + is created before inserting `val`. + +3. This function allows to use `push_back` with an initializer list. In case + + 1. the current value is an object, + 2. the initializer list `init` contains only two elements, and + 3. the first element of `init` is a string, + + `init` is converted into an object element and added using `push_back(const typename object_t::value_type&)`. + Otherwise, `init` is converted to a JSON value and added using `push_back(basic_json&&)`. + +## Parameters + +`val` (in) +: the value to add to the JSON array/object + +`init` (in) +: an initializer list + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than + JSON array or null; example: `"cannot use push_back() with number"` +2. The function can throw the following exceptions: + - Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than + JSON object or null; example: `"cannot use push_back() with number"` + +## Complexity + +1. Amortized constant. +2. Logarithmic in the size of the container, O(log(`size()`)). +3. Linear in the size of the initializer list `init`. + +## Notes + +(3) This function is required to resolve an ambiguous overload error, because pairs like `{"key", "value"}` can be both + interpreted as `object_t::value_type` or `std::initializer_list`, see + [#235](https://github.com/nlohmann/json/issues/235) for more information. + +## Examples + +??? example + + The example shows how `push_back()` and `+=` can be used to add elements to a JSON array. Note how the `null` value + was silently converted to a JSON array. + + ```cpp + --8<-- "examples/push_back.cpp" + ``` + + Output: + + ```json + --8<-- "examples/push_back.output" + ``` + +??? example + + The example shows how `push_back()` and `+=` can be used to add elements to a JSON object. Note how the `null` value + was silently converted to a JSON object. + + ```cpp + --8<-- "examples/push_back__object_t__value.cpp" + ``` + + Output: + + ```json + --8<-- "examples/push_back__object_t__value.output" + ``` + +??? example + + The example shows how initializer lists are treated as objects when possible. + + ```cpp + --8<-- "examples/push_back__initializer_list.cpp" + ``` + + Output: + + ```json + --8<-- "examples/push_back__initializer_list.output" + ``` + +## Version history + +1. Since version 1.0.0. +2. Since version 1.0.0. +2. Since version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/rbegin.md b/doc/mkdocs/docs/api/basic_json/rbegin.md new file mode 100644 index 0000000000..57957b3b40 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/rbegin.md @@ -0,0 +1,42 @@ +# basic_json::rbegin + +```cpp +reverse_iterator rbegin() noexcept; +const_reverse_iterator rbegin() const noexcept; +``` + +Returns an iterator to the reverse-beginning; that is, the last element. + +![Illustration from cppreference.com](../../images/range-rbegin-rend.svg) + +## Return value + +reverse iterator to the first element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `rbegin()`. + + ```cpp + --8<-- "examples/rbegin.cpp" + ``` + + Output: + + ```json + --8<-- "examples/rbegin.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/rend.md b/doc/mkdocs/docs/api/basic_json/rend.md new file mode 100644 index 0000000000..5d5f213101 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/rend.md @@ -0,0 +1,43 @@ +# basic_json::rend + +```cpp +reverse_iterator rend() noexcept; +const_reverse_iterator rend() const noexcept; +``` + +Returns an iterator to the reverse-end; that is, one before the first element. This element acts as a placeholder, +attempting to access it results in undefined behavior. + +![Illustration from cppreference.com](../../images/range-rbegin-rend.svg) + +## Return value + +reverse iterator to the element following the last element + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code shows an example for `eend()`. + + ```cpp + --8<-- "examples/rend.cpp" + ``` + + Output: + + ```json + --8<-- "examples/rend.output" + ``` + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/sax_parse.md b/doc/mkdocs/docs/api/basic_json/sax_parse.md new file mode 100644 index 0000000000..7a6dbad3c6 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/sax_parse.md @@ -0,0 +1,109 @@ +# basic_json::sax_parse + +```cpp +// (1) +template +static bool sax_parse(InputType&& i, + SAX* sax, + input_format_t format = input_format_t::json, + const bool strict = true, + const bool ignore_comments = false); + +// (2) +template +static bool sax_parse(IteratorType first, IteratorType last, + SAX* sax, + input_format_t format = input_format_t::json, + const bool strict = true, + const bool ignore_comments = false); +``` + +Read from input and generate SAX events + +1. Read from a compatible input. +2. Read from a pair of character iterators + + The value_type of the iterator must be a integral type with size of 1, 2 or 4 bytes, which will be interpreted + respectively as UTF-8, UTF-16 and UTF-32. + +The SAX event lister must follow the interface of `json_sax`. + +## Template parameters + +`InputType` +: A compatible input, for instance: + + - an `std::istream` object + - a `FILE` pointer + - a C-style array of characters + - a pointer to a null-terminated string of single byte characters + - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of + iterators. + +`IteratorType` +: Description + +`SAX` +: Description + +## Parameters + +`i` (in) +: Input to parse from. + +`sax` (in) +: SAX event listener + +`format` (in) +: the format to parse (JSON, CBOR, MessagePack, or UBJSON) (optional, `input_format_t::json` by default), see + [`input_format_t`](input_format_t.md) for more information + +`strict` (in) +: whether the input has to be consumed completely (optional, `#!cpp true` by default) + +`ignore_comments` (in) +: whether comments should be ignored and treated like whitespace (`#!cpp true`) or yield a parse error + (`#!cpp false`); (optional, `#!cpp false` by default) + +`first` (in) +: iterator to start of character range + +`last` (in) +: iterator to end of character range + +## Return value + +return value of the last processed SAX event + +## Exception safety + +## Complexity + +Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the SAX +consumer `sax` has a super-linear complexity. + +## Notes + +A UTF-8 byte order mark is silently ignored. + +## Examples + +??? example + + The example below demonstrates the `sax_parse()` function reading from string and processing the events with a + user-defined SAX event consumer. + + ```cpp + --8<-- "examples/sax_parse.cpp" + ``` + + Output: + + ```json + --8<-- "examples/sax_parse.output" + ``` + +## Version history + +- Added in version 3.2.0. +- Ignoring comments via `ignore_comments` added in version 3.9.0. diff --git a/doc/mkdocs/docs/api/basic_json/size.md b/doc/mkdocs/docs/api/basic_json/size.md new file mode 100644 index 0000000000..36a9daeb6c --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/size.md @@ -0,0 +1,57 @@ +# basic_json::size + +```cpp +size_type size() const noexcept; +``` + +Returns the number of elements in a JSON value. + +## Return value + +The return value depends on the different types and is defined as follows: + +Value type | return value +----------- | ------------- +null | `0` +boolean | `1` +string | `1` +number | `1` +binary | `1` +object | result of function object_t::size() +array | result of function array_t::size() + +## Exception safety + +No-throw guarantee: this function never throws exceptions. + +## Complexity + +Constant, as long as [`array_t`](array_t.md) and [`object_t`](object_t.md) satisfy the +[Container](https://en.cppreference.com/w/cpp/named_req/Container) concept; that is, their `size()` functions have +constant complexity. + +## Notes + +This function does not return the length of a string stored as JSON value -- it returns the number of elements in the +JSON value which is `1` in the case of a string. + +## Example + +??? example + + The following code calls `size()` on the different value types. + + ```cpp + --8<-- "examples/size.cpp" + ``` + + Output: + + ```json + --8<-- "examples/size.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Extended to return `1` for binary types in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/string_t.md b/doc/mkdocs/docs/api/basic_json/string_t.md new file mode 100644 index 0000000000..acdd351b54 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/string_t.md @@ -0,0 +1,50 @@ +# basic_json::string_t + +```cpp +using string_t = StringType; +``` + +The type used to store JSON strings. + +[RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows: +> A string is a sequence of zero or more Unicode characters. + +To store objects in C++, a type is defined by the template parameter described below. Unicode values are split by the +JSON class into byte-sized characters during deserialization. + +## Template parameters + +`StringType` +: the container to store strings (e.g., `std::string`). Note this container is used for keys/names in objects, see + [object_t](object_t.md). + +## Notes + +#### Default type + +With the default values for `StringType` (`std::string`), the default value for `string_t` is `#!cpp std::string`. + +#### Encoding + +Strings are stored in UTF-8 encoding. Therefore, functions like `std::string::size()` or `std::string::length()` return +the number of bytes in the string rather than the number of characters or glyphs. + +#### String comparison + +[RFC 7159](http://rfc7159.net/rfc7159) states: +> Software implementations are typically required to test names of object members for equality. Implementations that +> transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, +> code unit by code unit, are interoperable in the sense that implementations will agree in all cases on equality or +> inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may +> incorrectly find that `"a\\b"` and `"a\u005Cb"` are not equal. + +This implementation is interoperable as it does compare strings code unit by code unit. + +#### Storage + +String values are stored as pointers in a `basic_json` type. That is, for any access to string values, a pointer of type +`string_t*` must be dereferenced. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/to_bson.md b/doc/mkdocs/docs/api/basic_json/to_bson.md new file mode 100644 index 0000000000..747a4aa316 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/to_bson.md @@ -0,0 +1,57 @@ +# basic_json::to_bson + +```cpp +// (1) +static std::vector to_bson(const basic_json& j); + +// (2) +static void to_bson(const basic_json& j, detail::output_adapter o); +static void to_bson(const basic_json& j, detail::output_adapter o); +``` + +BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a +so-called document). + +1. Returns a byte vector containing the BSON serialization. +2. Writes the BSON serialization to an output adapter. + +## Parameters + +`j` (in) +: JSON value to serialize + +`o` (in) +: output adapter to write serialization to + +## Return value + +1. BSON serialization as byte vector +2. / + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the JSON value `j`. + +## Example + +??? example + + The example shows the serialization of a JSON value to a byte vector in BSON format. + + ```cpp + --8<-- "examples/to_bson.cpp" + ``` + + Output: + + ```json + --8<-- "examples/to_bson.output" + ``` + +## Version history + +- Added in version 3.4.0. diff --git a/doc/mkdocs/docs/api/basic_json/to_cbor.md b/doc/mkdocs/docs/api/basic_json/to_cbor.md new file mode 100644 index 0000000000..fd0eaa1fe9 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/to_cbor.md @@ -0,0 +1,59 @@ +# basic_json::to_cbor + +```cpp +// (1) +static std::vector to_cbor(const basic_json& j); + +// (2) +static void to_cbor(const basic_json& j, detail::output_adapter o); +static void to_cbor(const basic_json& j, detail::output_adapter o); +``` + +Serializes a given JSON value `j` to a byte vector using the CBOR (Concise Binary Object Representation) serialization +format. CBOR is a binary serialization format which aims to be more compact than JSON itself, yet more efficient to +parse. + +1. Returns a byte vector containing the CBOR serialization. +2. Writes the CBOR serialization to an output adapter. + +## Parameters + +`j` (in) +: JSON value to serialize + +`o` (in) +: output adapter to write serialization to + +## Return value + +1. CBOR serialization as byte vector +2. / + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the JSON value `j`. + +## Example + +??? example + + The example shows the serialization of a JSON value to a byte vector in CBOR format. + + ```cpp + --8<-- "examples/to_cbor.cpp" + ``` + + Output: + + ```json + --8<-- "examples/to_cbor.output" + ``` + +## Version history + +- Added in version 2.0.9. +- Compact representation of floating-point numbers added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/to_msgpack.md b/doc/mkdocs/docs/api/basic_json/to_msgpack.md new file mode 100644 index 0000000000..1d438d7cdb --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/to_msgpack.md @@ -0,0 +1,57 @@ +# basic_json::to_msgpack + +```cpp +// (1) +static std::vector to_msgpack(const basic_json& j); + +// (2) +static void to_msgpack(const basic_json& j, detail::output_adapter o); +static void to_msgpack(const basic_json& j, detail::output_adapter o); +``` + +Serializes a given JSON value `j` to a byte vector using the MessagePack serialization format. MessagePack is a binary +serialization format which aims to be more compact than JSON itself, yet more efficient to parse. + +1. Returns a byte vector containing the MessagePack serialization. +2. Writes the MessagePack serialization to an output adapter. + +## Parameters + +`j` (in) +: JSON value to serialize + +`o` (in) +: output adapter to write serialization to + +## Return value + +1. MessagePack serialization as byte vector +2. / + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the JSON value `j`. + +## Example + +??? example + + The example shows the serialization of a JSON value to a byte vector in MessagePack format. + + ```cpp + --8<-- "examples/to_msgpack.cpp" + ``` + + Output: + + ```json + --8<-- "examples/to_msgpack.output" + ``` + +## Version history + +- Added in version 2.0.9. diff --git a/doc/mkdocs/docs/api/basic_json/to_ubjson.md b/doc/mkdocs/docs/api/basic_json/to_ubjson.md new file mode 100644 index 0000000000..a12a62555e --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/to_ubjson.md @@ -0,0 +1,68 @@ +# basic_json::to_ubjson + +```cpp +// (1) +static std::vector to_ubjson(const basic_json& j, + const bool use_size = false, + const bool use_type = false); + +// (2) +static void to_ubjson(const basic_json& j, detail::output_adapter o, + const bool use_size = false, const bool use_type = false); +static void to_ubjson(const basic_json& j, detail::output_adapter o, + const bool use_size = false, const bool use_type = false); +``` + +Serializes a given JSON value `j` to a byte vector using the UBJSON (Universal Binary JSON) serialization format. UBJSON +aims to be more compact than JSON itself, yet more efficient to parse. + +1. Returns a byte vector containing the UBJSON serialization. +2. Writes the UBJSON serialization to an output adapter. + +## Parameters + +`j` (in) +: JSON value to serialize + +`o` (in) +: output adapter to write serialization to + +`use_size` (in) +: whether to add size annotations to container types; optional, `#!cpp false` by default. + +`use_type` (in) +: whether to add type annotations to container types (must be combined with `#!cpp use_size = true`); optional, + `#!cpp false` by default. + +## Return value + +1. UBJSON serialization as byte vector +2. / + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. + +## Complexity + +Linear in the size of the JSON value `j`. + +## Example + +??? example + + The example shows the serialization of a JSON value to a byte vector in UBJSON format. + + ```cpp + --8<-- "examples/to_ubjson.cpp" + ``` + + Output: + + ```json + --8<-- "examples/to_ubjson.output" + ``` + +## Version history + +- Added in version 3.1.0. diff --git a/doc/mkdocs/docs/api/basic_json/type.md b/doc/mkdocs/docs/api/basic_json/type.md new file mode 100644 index 0000000000..b3482117bf --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/type.md @@ -0,0 +1,54 @@ +# basic_json::type + +```cpp +constexpr value_t type() const noexcept; +``` + +Return the type of the JSON value as a value from the [`value_t`](value_t.md) enumeration. + +## Return value + +the type of the JSON value + +Value type | return value +------------------------- | ------------------------- +`#!json null` | `value_t::null` +boolean | `value_t::boolean` +string | `value_t::string` +number (integer) | `value_t::number_integer` +number (unsigned integer) | `value_t::number_unsigned` +number (floating-point) | `value_t::number_float` +object | `value_t::object` +array | `value_t::array` +binary | `value_t::binary` +discarded | `value_t::discarded` + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `type()` for all JSON types. + + ```cpp + --8<-- "examples/type.cpp" + ``` + + Output: + + ```json + --8<-- "examples/type.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Added unsigned integer type in version 2.0.0. +- Added binary type in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/type_error.md b/doc/mkdocs/docs/api/basic_json/type_error.md new file mode 100644 index 0000000000..ea0154e399 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/type_error.md @@ -0,0 +1,60 @@ +# basic_json::type_error + +```cpp +class type_error : public exception; +``` + +This exception is thrown in case of a type error; that is, a library function is executed on a JSON value whose type +does not match the expected semantics. + +Exceptions have ids 3xx. + +```plantuml +std::exception <|-- basic_json::exception +basic_json::exception <|-- basic_json::parse_error +basic_json::exception <|-- basic_json::invalid_iterator +basic_json::exception <|-- basic_json::type_error +basic_json::exception <|-- basic_json::out_of_range +basic_json::exception <|-- basic_json::other_error + +interface std::exception {} + +class basic_json::exception { + + const int id + + const char* what() const +} + +class basic_json::parse_error { + + const std::size_t byte +} + +class basic_json::type_error #FFFF00 {} +``` + +## Member functions + +- **what** - returns explanatory string + +## Member variables + +- **id** - the id of the exception + +## Example + +??? example + + The following code shows how a `type_error` exception can be caught. + + ```cpp + --8<-- "examples/type_error.cpp" + ``` + + Output: + + ```json + --8<-- "examples/type_error.output" + ``` + +## Version history + +- Since version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/type_name.md b/doc/mkdocs/docs/api/basic_json/type_name.md new file mode 100644 index 0000000000..1a17526103 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/type_name.md @@ -0,0 +1,54 @@ +# basic_json::type_name + +```cpp +const char* type_name() const noexcept; +``` + +Returns the type name as string to be used in error messages -- usually to indicate that a function was called on a +wrong JSON type. + +## Return value + +a string representation of a the type ([`value_t`](value_t.md)): + +Value type | return value +-------------------------------------------------- | ------------------------- +`#!json null` | `"null"` +boolean | `"boolean"` +string | `"string"` +number (integer, unsigned integer, floating-point) | `"number"` +object | `"object` +array | `"array` +binary | `"binary` +discarded | `"discarded` + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Constant. + +## Example + +??? example + + The following code exemplifies `type_name()` for all JSON types. + + ```cpp + --8<-- "examples/type_name.cpp" + ``` + + Output: + + ```json + --8<-- "examples/type_name.output" + ``` + +## Version history + +- Added in version 1.0.0. +- Part of the public API version since 2.1.0. +- Changed return value to `const char*` and added `noexcept` in version 3.0.0. +- Added support for binary type in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/unflatten.md b/doc/mkdocs/docs/api/basic_json/unflatten.md new file mode 100644 index 0000000000..379428639b --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/unflatten.md @@ -0,0 +1,57 @@ +# basic_json::unflatten + +```cpp +basic_json unflatten() const; +``` + +The function restores the arbitrary nesting of a JSON value that has been flattened before using the +[`flatten()`](flatten.md) function. The JSON value must meet certain constraints: + +1. The value must be an object. +2. The keys must be JSON pointers (see [RFC 6901](https://tools.ietf.org/html/rfc6901)) +3. The mapped values must be primitive JSON types. + +## Return value + +the original JSON from a flattened version + +## Exceptions + +The function can throw the following exceptions: + +- Throws [`type_error.314`](../../home/exceptions.md#jsonexceptiontype_error314) if value is not an object +- Throws [`type_error.315`](../../home/exceptions.md#jsonexceptiontype_error315) if object values are not primitive + +## Exception safety + +Strong exception safety: if an exception occurs, the original value stays intact. + +## Complexity + +Linear in the size the JSON value. + +## Notes + +Empty objects and arrays are flattened by [`flatten()`](flatten.md) to `#!json null` values and can not unflattened to +their original type. Apart from this example, for a JSON value `j`, the following is always true: +`#!cpp j == j.flatten().unflatten()`. + +## Example + +??? example + + The following code shows how a flattened JSON object is unflattened into the original nested JSON object. + + ```cpp + --8<-- "examples/unflatten.cpp" + ``` + + Output: + + ```json + --8<-- "examples/unflatten.output" + ``` + +## Version history + +- Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/update.md b/doc/mkdocs/docs/api/basic_json/update.md new file mode 100644 index 0000000000..a1837c860e --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/update.md @@ -0,0 +1,78 @@ +# basic_json::update + +```cpp +// (1) +void update(const_reference j); + +// (2) +void update(const_iterator first, const_iterator last); +``` + +1. Inserts all values from JSON object `j` and overwrites existing keys. +2. Inserts all values from from range `[first, last)` and overwrites existing keys. + +The function is motivated by Python's [dict.update](https://docs.python.org/3.6/library/stdtypes.html#dict.update) +function. + +## Parameters + +`j` (in) +: JSON object to read values from + +`first` (in) +: begin of the range of elements to insert + +`last` (in) +: end of the range of elements to insert + +## Exceptions + +1. The function can throw the following exceptions: + - Throws [`type_error.312`](../../home/exceptions.md#jsonexceptiontype_error312) if called on JSON values other than + objects; example: `"cannot use update() with string"` +2. The function can throw thw following exceptions: + - Throws [`type_error.312`](../../home/exceptions.md#jsonexceptiontype_error312) if called on JSON values other than + objects; example: `"cannot use update() with string"` + - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an + iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` + - Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last` + do not belong to the same JSON value; example: `"iterators do not fit"` + +## Complexity + +1. O(N*log(size() + N)), where N is the number of elements to insert. +2. O(N*log(size() + N)), where N is the number of elements to insert. + +## Example + +??? example + + The example shows how `update()` is used. + + ```cpp + --8<-- "examples/update.cpp" + ``` + + Output: + + ```json + --8<-- "examples/update.output" + ``` + +??? example + + The example shows how `update()` is used. + + ```cpp + --8<-- "examples/update__range.cpp" + ``` + + Output: + + ```json + --8<-- "examples/update__range.output" + ``` + +## Version history + +- Added in version 3.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/value.md b/doc/mkdocs/docs/api/basic_json/value.md new file mode 100644 index 0000000000..8fdbc238f6 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/value.md @@ -0,0 +1,121 @@ +# basic_json::value + +```cpp +// (1) +template +ValueType value(const typename object_t::key_type& key, + const ValueType& default_value) const; + +// (2) +template +ValueType value(const json_pointer& ptr, + const ValueType& default_value) const; +``` + +1. Returns either a copy of an object's element at the specified key `key` or a given default value if no element with + key `key` exists. + + The function is basically equivalent to executing + ```cpp + try { + return at(key); + } catch(out_of_range) { + return default_value; + } + ``` + +2. Returns either a copy of an object's element at the specified JSON pointer `ptr` or a given default value if no value + at `ptr` exists. + + The function is basically equivalent to executing + ```cpp + try { + return at(ptr); + } catch(out_of_range) { + return default_value; + } + ``` + +Unlike [`operator[]`](operator[].md), this function does not implicitly add an element to the position defined by +`key`/`ptr` key. This function is furthermore also applicable to const objects. + +## Template parameters + +`ValueType` +: type compatible to JSON values, for instance `#!cpp int` for JSON integer numbers, `#!cpp bool` for JSON booleans, + or `#!cpp std::vector` types for JSON arrays. Note the type of the expected value at `key`/`ptr` and the default + value `default_value` must be compatible. + +## Parameters + +`key` (in) +: key of the element to access + +`default_value` (in) +: the value to return if key/ptr found no value + +`ptr` (in) +: a JSON pointer to the element to access + +## Return value + +1. copy of the element at key `key` or `default_value` if `key` is not found +1. copy of the element at JSON Pointer `ptr` or `default_value` if no value for `ptr` is found + +## Exception safety + +Strong guarantee: if an exception is thrown, there are no +changes to any JSON value. + +## Exceptions + +1. The function can throw thw following exceptions: + - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match + the type of the value at `key` + - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object; + in that case, using `value()` with a key makes no sense. +2. The function can throw thw following exceptions: + - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match + the type of the value at `ptr` + - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object; + in that case, using `value()` with a key makes no sense. + +## Complexity + +1. Logarithmic in the size of the container. +2. Logarithmic in the size of the container. + +## Example + +??? example + + The example below shows how object elements can be queried with a default value. + + ```cpp + --8<-- "examples/basic_json__value.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__value.output" + ``` + +??? example + + The example below shows how object elements can be queried with a default value. + + ```cpp + --8<-- "examples/basic_json__value_ptr.cpp" + ``` + + Output: + + ```json + --8<-- "examples/basic_json__value_ptr.output" + ``` + +## Version history + +1. Added in version 1.0.0. +2. Added in version 2.0.2. diff --git a/doc/mkdocs/docs/api/basic_json/value_t.md b/doc/mkdocs/docs/api/basic_json/value_t.md new file mode 100644 index 0000000000..5fa9501fc6 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/value_t.md @@ -0,0 +1,36 @@ +# basic_json::value_t + +```cpp +enum class value_t : std::uint8_t { + null, + object, + array, + string, + boolean, + number_integer, + number_unsigned, + number_float, + binary, + discarded +}; +``` + +This enumeration collects the different JSON types. It is internally used to distinguish the stored values, and the +functions [`is_null`](is_null.md), [`is_object`](is_object.md), [`is_array`](is_array.md), [`is_string`](is_string.md), +[`is_boolean`](is_boolean.md), [`is_number`](is_number.md) (with [`is_number_integer`](is_number_integer.md), +[`is_number_unsigned`](is_number_unsigned.md), and [`is_number_float`](is_number_float.md)), +[`is_discarded`](is_discarded.md), [`is_binary`](is_binary.md), [`is_primitive`](is_primitive.md), and +[`is_structured`](is_structured.md) rely on it. + +## Note + +There are three enumeration entries (number_integer, number_unsigned, and number_float), because the library +distinguishes these three types for numbers: [`number_unsigned_t`](number_unsigned_t.md) is used for unsigned integers, +[`number_integer_t`](number_integer_t.md) is used for signed integers, and [`number_float_t`](number_float_t.md) is used +for floating-point numbers or to approximate integers which do not fit in the limits of their respective type. + +## Version history + +- Added in version 1.0.0. +- Added unsigned integer type in version 2.0.0. +- Added binary type in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/~basic_json.md b/doc/mkdocs/docs/api/basic_json/~basic_json.md new file mode 100644 index 0000000000..b089b0d794 --- /dev/null +++ b/doc/mkdocs/docs/api/basic_json/~basic_json.md @@ -0,0 +1,19 @@ +# basic_json::~basic_json + +```cpp +~basic_json() noexcept +``` + +Destroys the JSON value and frees all allocated memory. + +## Exception safety + +No-throw guarantee: this member function never throws exceptions. + +## Complexity + +Linear. + +## Version history + +- Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/json.md b/doc/mkdocs/docs/api/json.md new file mode 100644 index 0000000000..0aff6d2c0f --- /dev/null +++ b/doc/mkdocs/docs/api/json.md @@ -0,0 +1,5 @@ +# json + +```cpp +using json = basic_json<>; +``` diff --git a/doc/mkdocs/docs/api/json_pointer.md b/doc/mkdocs/docs/api/json_pointer.md new file mode 100644 index 0000000000..5aa0399cd3 --- /dev/null +++ b/doc/mkdocs/docs/api/json_pointer.md @@ -0,0 +1,24 @@ +# json_pointer + +```cpp +template +class json_pointer; +``` + +## Template parameters + +`BasicJsonType` +: a specialization of [`basic_json`](basic_json/index.md) + +## Member functions + +- (constructor) +- **to_string** - return a string representation of the JSON pointer +- **operator std::string**- return a string representation of the JSON pointer +- **operator/=** - append to the end of the JSON pointer +- **operator/** - create JSON Pointer by appending +- **parent_pointer** - returns the parent of this JSON pointer +- **pop_back** - remove last reference token +- **back** - return last reference token +- **push_back** - append an unescaped token at the end of the pointer +- **empty** - return whether pointer points to the root document diff --git a/doc/mkdocs/docs/api/ordered_json.md b/doc/mkdocs/docs/api/ordered_json.md new file mode 100644 index 0000000000..8ce8dfe3e9 --- /dev/null +++ b/doc/mkdocs/docs/api/ordered_json.md @@ -0,0 +1,5 @@ +# ordered_json + +```cpp +using ordered_json = basic_json; +``` diff --git a/doc/mkdocs/docs/api/ordered_map.md b/doc/mkdocs/docs/api/ordered_map.md new file mode 100644 index 0000000000..1a99b636f8 --- /dev/null +++ b/doc/mkdocs/docs/api/ordered_map.md @@ -0,0 +1,7 @@ +# ordered_map + +```cpp +template, + class Allocator = std::allocator>> +struct ordered_map : std::vector, Allocator>; +``` diff --git a/doc/mkdocs/docs/features/element_access/checked_access.md b/doc/mkdocs/docs/features/element_access/checked_access.md index 095dff2d63..19c75254d1 100644 --- a/doc/mkdocs/docs/features/element_access/checked_access.md +++ b/doc/mkdocs/docs/features/element_access/checked_access.md @@ -45,7 +45,7 @@ The return value is a reference, so it can be modify the original value. } ``` -When accessing an invalid index (i.e., and index greater than or equal to the array size) or the passed object key is non-existing, an exception is thrown. +When accessing an invalid index (i.e., an index greater than or equal to the array size) or the passed object key is non-existing, an exception is thrown. ??? example diff --git a/doc/mkdocs/docs/features/element_access/unchecked_access.md b/doc/mkdocs/docs/features/element_access/unchecked_access.md index f8667c817c..fff7f2b320 100644 --- a/doc/mkdocs/docs/features/element_access/unchecked_access.md +++ b/doc/mkdocs/docs/features/element_access/unchecked_access.md @@ -47,7 +47,7 @@ The return value is a reference, so it can be modify the original value. In case } ``` -When accessing an invalid index (i.e., and index greater than or equal to the array size), the JSON array is resized such that the passed index is the new maximal index. Intermediate values are filled with `#!json null`. +When accessing an invalid index (i.e., an index greater than or equal to the array size), the JSON array is resized such that the passed index is the new maximal index. Intermediate values are filled with `#!json null`. ??? example diff --git a/doc/mkdocs/docs/home/exceptions.md b/doc/mkdocs/docs/home/exceptions.md index e1e1d13b97..0475f53e2b 100644 --- a/doc/mkdocs/docs/home/exceptions.md +++ b/doc/mkdocs/docs/home/exceptions.md @@ -322,7 +322,7 @@ The iterators passed to constructor `basic_json(InputIT first, InputIT last)` ar ### json.exception.invalid_iterator.202 -In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. +In an [erase](../api/basic_json/erase.md) or insert function, the passed iterator `pos` does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. !!! failure "Example message" @@ -335,7 +335,7 @@ In an erase or insert function, the passed iterator @a pos does not belong to th ### json.exception.invalid_iterator.203 -Either iterator passed to function `erase(IteratorType` first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. +Either iterator passed to function [`erase(IteratorType first, IteratorType last`)](../api/basic_json/erase.md) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. !!! failure "Example message" @@ -345,7 +345,7 @@ Either iterator passed to function `erase(IteratorType` first, IteratorType last ### json.exception.invalid_iterator.204 -When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (`begin(),` `end()),` because this is the only way the single stored value is expressed. All other ranges are invalid. +When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an [erase](../api/basic_json/erase.md) function, this range has to be exactly (`begin(),` `end()),` because this is the only way the single stored value is expressed. All other ranges are invalid. !!! failure "Example message" @@ -355,7 +355,7 @@ When an iterator range for a primitive type (number, boolean, or string) is pass ### json.exception.invalid_iterator.205 -When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the `begin()` iterator, because it is the only way to address the stored value. All other iterators are invalid. +When an iterator for a primitive type (number, boolean, or string) is passed to an [erase](../api/basic_json/erase.md) function, the iterator has to be the `begin()` iterator, because it is the only way to address the stored value. All other iterators are invalid. !!! failure "Example message" @@ -454,7 +454,6 @@ Cannot get value for iterator: Either the iterator belongs to a null value or it [json.exception.invalid_iterator.214] cannot get value ``` - ## Type errors This exception is thrown in case of a type error; that is, a library function is executed on a JSON value whose type does not match the expected semantics. @@ -549,7 +548,7 @@ The `value()` member functions can only be executed for certain JSON types. ### json.exception.type_error.307 -The `erase()` member functions can only be executed for certain JSON types. +The [`erase()`](../api/basic_json/erase.md) member functions can only be executed for certain JSON types. !!! failure "Example message" @@ -684,7 +683,6 @@ The dynamic type of the object cannot be represented in the requested serializat Encapsulate the JSON value in an object. That is, instead of serializing `#!json true`, serialize `#!json {"value": true}` - ## Out of range This exception is thrown in case a library function is called on an input parameter that exceeds the expected range, for instance in case of array indices or nonexisting object keys. diff --git a/doc/mkdocs/docs/home/license.md b/doc/mkdocs/docs/home/license.md index f7d0aa82ef..4cd6ca2cc3 100644 --- a/doc/mkdocs/docs/home/license.md +++ b/doc/mkdocs/docs/home/license.md @@ -1,10 +1,10 @@ # License - + -The class is licensed under the [MIT License](http://opensource.org/licenses/MIT): +The class is licensed under the [MIT License](https://opensource.org/licenses/MIT): -Copyright © 2013-2020 [Niels Lohmann](http://nlohmann.me) +Copyright © 2013-2020 [Niels Lohmann](https://nlohmann.me) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -14,8 +14,8 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I * * * -The class contains the UTF-8 Decoder from Bjoern Hoehrmann which is licensed under the [MIT License](http://opensource.org/licenses/MIT) (see above). Copyright © 2008-2009 [Björn Hoehrmann](http://bjoern.hoehrmann.de/) +The class contains the UTF-8 Decoder from Bjoern Hoehrmann which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2008-2009 [Björn Hoehrmann](http://bjoern.hoehrmann.de/) -The class contains a slightly modified version of the Grisu2 algorithm from Florian Loitsch which is licensed under the [MIT License](http://opensource.org/licenses/MIT) (see above). Copyright © 2009 [Florian Loitsch](http://florian.loitsch.com/) +The class contains a slightly modified version of the Grisu2 algorithm from Florian Loitsch which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2009 [Florian Loitsch](https://florian.loitsch.com/) -The class contains a copy of [Hedley](https://nemequ.github.io/hedley/) from Evan Nemerson which is licensed as [CC0-1.0](http://creativecommons.org/publicdomain/zero/1.0/). +The class contains a copy of [Hedley](https://nemequ.github.io/hedley/) from Evan Nemerson which is licensed as [CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/). diff --git a/doc/mkdocs/docs/index.md b/doc/mkdocs/docs/index.md index 9e5e54a42c..39c52d748c 100644 --- a/doc/mkdocs/docs/index.md +++ b/doc/mkdocs/docs/index.md @@ -2,6 +2,6 @@ !!! note - This page is under construction. You probably want to see the [Doxygen documentation](doxygen). + This page is under construction. ![](images/json.gif) diff --git a/doc/mkdocs/mkdocs.yml b/doc/mkdocs/mkdocs.yml index 2837e8caef..e1552d39b8 100644 --- a/doc/mkdocs/mkdocs.yml +++ b/doc/mkdocs/mkdocs.yml @@ -1,7 +1,7 @@ # Project information site_name: JSON for Modern C++ site_author: Niels Lohmann -site_url: https://squidfunk.github.io/mkdocs-material/ +site_url: https://json.nlohmann.me/ # Repository repo_name: nlohmann/json @@ -72,9 +72,116 @@ nav: - API: - basic_json: - api/basic_json/index.md + - api/basic_json/accept.md + - api/basic_json/array.md + - api/basic_json/array_t.md + - api/basic_json/at.md + - api/basic_json/back.md + - api/basic_json/basic_json.md + - api/basic_json/~basic_json.md + - api/basic_json/begin.md + - api/basic_json/binary.md + - api/basic_json/binary_t.md + - api/basic_json/boolean_t.md + - api/basic_json/cbegin.md + - api/basic_json/cbor_tag_handler_t.md + - api/basic_json/cend.md + - api/basic_json/clear.md + - api/basic_json/contains.md + - api/basic_json/count.md + - api/basic_json/crbegin.md + - api/basic_json/crend.md + - api/basic_json/diff.md - api/basic_json/dump.md + - api/basic_json/emplace.md + - api/basic_json/emplace_back.md + - api/basic_json/empty.md + - api/basic_json/end.md + - api/basic_json/erase.md + - api/basic_json/error_handler_t.md + - api/basic_json/exception.md + - api/basic_json/find.md + - api/basic_json/flatten.md + - api/basic_json/from_bson.md + - api/basic_json/from_cbor.md + - api/basic_json/from_msgpack.md + - api/basic_json/from_ubjson.md + - api/basic_json/front.md + - api/basic_json/get.md + - api/basic_json/get_allocator.md + - api/basic_json/get_binary.md + - api/basic_json/get_ptr.md + - api/basic_json/get_ref.md + - api/basic_json/get_to.md + - api/basic_json/input_format_t.md + - api/basic_json/insert.md + - api/basic_json/invalid_iterator.md + - api/basic_json/is_array.md + - api/basic_json/is_binary.md + - api/basic_json/is_boolean.md + - api/basic_json/is_discarded.md + - api/basic_json/is_null.md + - api/basic_json/is_number.md + - api/basic_json/is_number_float.md + - api/basic_json/is_number_integer.md + - api/basic_json/is_number_unsigned.md + - api/basic_json/is_object.md + - api/basic_json/is_primitive.md + - api/basic_json/is_string.md + - api/basic_json/is_structured.md + - api/basic_json/items.md + - api/basic_json/json_serializer.md + - api/basic_json/max_size.md - api/basic_json/meta.md + - api/basic_json/merge_patch.md + - api/basic_json/number_float_t.md + - api/basic_json/number_integer_t.md + - api/basic_json/number_unsigned_t.md + - api/basic_json/object.md + - api/basic_json/object_comparator_t.md + - api/basic_json/object_t.md + - api/basic_json/operator_ValueType.md + - api/basic_json/operator_value_t.md + - api/basic_json/operator[].md + - api/basic_json/operator=.md + - api/basic_json/operator_eq.md + - api/basic_json/operator_ne.md + - api/basic_json/operator_lt.md + - api/basic_json/operator_le.md + - api/basic_json/operator_gt.md + - api/basic_json/operator_ge.md + - api/basic_json/operator+=.md + - api/basic_json/operator_literal_json.md + - api/basic_json/operator_literal_json_pointer.md + - api/basic_json/out_of_range.md + - api/basic_json/other_error.md - api/basic_json/parse.md + - api/basic_json/parse_error.md + - api/basic_json/parse_event_t.md + - api/basic_json/parser_callback_t.md + - api/basic_json/patch.md + - api/basic_json/push_back.md + - api/basic_json/rbegin.md + - api/basic_json/rend.md + - api/basic_json/sax_parse.md + - api/basic_json/size.md + - api/basic_json/string_t.md + - api/basic_json/to_bson.md + - api/basic_json/to_cbor.md + - api/basic_json/to_msgpack.md + - api/basic_json/to_ubjson.md + - api/basic_json/type.md + - api/basic_json/type_error.md + - api/basic_json/type_name.md + - api/basic_json/unflatten.md + - api/basic_json/update.md + - api/basic_json/value.md + - api/basic_json/value_t.md + - api/adl_serializer.md + - api/json.md + - api/json_pointer.md + - api/ordered_map.md + - api/ordered_json.md # Extras extra: diff --git a/include/nlohmann/byte_container_with_subtype.hpp b/include/nlohmann/byte_container_with_subtype.hpp index 69f9feb212..ee3ab4011b 100644 --- a/include/nlohmann/byte_container_with_subtype.hpp +++ b/include/nlohmann/byte_container_with_subtype.hpp @@ -39,15 +39,15 @@ class byte_container_with_subtype : public BinaryType : container_type(std::move(b)) {} - byte_container_with_subtype(const container_type& b, std::uint8_t subtype) noexcept(noexcept(container_type(b))) + byte_container_with_subtype(const container_type& b, std::uint8_t subtype_) noexcept(noexcept(container_type(b))) : container_type(b) - , m_subtype(subtype) + , m_subtype(subtype_) , m_has_subtype(true) {} - byte_container_with_subtype(container_type&& b, std::uint8_t subtype) noexcept(noexcept(container_type(std::move(b)))) + byte_container_with_subtype(container_type&& b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b)))) : container_type(std::move(b)) - , m_subtype(subtype) + , m_subtype(subtype_) , m_has_subtype(true) {} @@ -80,9 +80,9 @@ class byte_container_with_subtype : public BinaryType @since version 3.8.0 */ - void set_subtype(std::uint8_t subtype) noexcept + void set_subtype(std::uint8_t subtype_) noexcept { - m_subtype = subtype; + m_subtype = subtype_; m_has_subtype = true; } diff --git a/include/nlohmann/detail/hash.hpp b/include/nlohmann/detail/hash.hpp index 4094cc94fc..d2d5d332d3 100644 --- a/include/nlohmann/detail/hash.hpp +++ b/include/nlohmann/detail/hash.hpp @@ -83,19 +83,19 @@ std::size_t hash(const BasicJsonType& j) return combine(type, h); } - case nlohmann::detail::value_t::number_unsigned: + case BasicJsonType::value_t::number_unsigned: { const auto h = std::hash {}(j.template get()); return combine(type, h); } - case nlohmann::detail::value_t::number_float: + case BasicJsonType::value_t::number_float: { const auto h = std::hash {}(j.template get()); return combine(type, h); } - case nlohmann::detail::value_t::binary: + case BasicJsonType::value_t::binary: { auto seed = combine(type, j.get_binary().size()); const auto h = std::hash {}(j.get_binary().has_subtype()); @@ -108,8 +108,9 @@ std::size_t hash(const BasicJsonType& j) return seed; } - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + JSON_ASSERT(false); // LCOV_EXCL_LINE + return 0; // LCOV_EXCL_LINE } } diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 4c94d1ccfd..6ae5882c7e 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -752,8 +752,9 @@ class binary_reader return parse_cbor_internal(true, tag_handler); } - default: // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE } } diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index e9a394d4cc..b4faa88a5d 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -163,7 +163,7 @@ class iter_impl return *this; } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief set the iterator to the first value @pre The iterator is initialized; i.e. `m_object != nullptr`. @@ -627,7 +627,7 @@ class iter_impl return operator*(); } - private: + JSON_PRIVATE_UNLESS_TESTED: /// associated JSON instance pointer m_object = nullptr; /// the actual iterator of the associated instance diff --git a/include/nlohmann/detail/iterators/primitive_iterator.hpp b/include/nlohmann/detail/iterators/primitive_iterator.hpp index 28d6f1a65d..16dcc9f97f 100644 --- a/include/nlohmann/detail/iterators/primitive_iterator.hpp +++ b/include/nlohmann/detail/iterators/primitive_iterator.hpp @@ -23,6 +23,7 @@ class primitive_iterator_t static constexpr difference_type begin_value = 0; static constexpr difference_type end_value = begin_value + 1; + JSON_PRIVATE_UNLESS_TESTED: /// iterator as signed integer type difference_type m_it = (std::numeric_limits::min)(); diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 78bc3a3a38..865376cf18 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -375,6 +375,7 @@ class json_pointer return static_cast(res); } + JSON_PRIVATE_UNLESS_TESTED: json_pointer top() const { if (JSON_HEDLEY_UNLIKELY(empty())) @@ -387,6 +388,7 @@ class json_pointer return result; } + private: /*! @brief create and return a reference to the pointed to value @@ -823,6 +825,7 @@ class json_pointer {} } + JSON_PRIVATE_UNLESS_TESTED: /// escape "~" to "~0" and "/" to "~1" static std::string escape(std::string s) { @@ -838,6 +841,7 @@ class json_pointer replace_substring(s, "~0", "~"); } + private: /*! @param[in] reference_string the reference string to the current value @param[in] value the value to consider diff --git a/include/nlohmann/detail/json_ref.hpp b/include/nlohmann/detail/json_ref.hpp index c9bf6cb228..26a4903828 100644 --- a/include/nlohmann/detail/json_ref.hpp +++ b/include/nlohmann/detail/json_ref.hpp @@ -17,19 +17,14 @@ class json_ref json_ref(value_type&& value) : owned_value(std::move(value)) - , value_ref(&owned_value) - , is_rvalue(true) {} json_ref(const value_type& value) - : value_ref(const_cast(&value)) - , is_rvalue(false) + : value_ref(&value) {} json_ref(std::initializer_list init) : owned_value(init) - , value_ref(&owned_value) - , is_rvalue(true) {} template < @@ -37,8 +32,6 @@ class json_ref enable_if_t::value, int> = 0 > json_ref(Args && ... args) : owned_value(std::forward(args)...) - , value_ref(&owned_value) - , is_rvalue(true) {} // class should be movable only @@ -50,27 +43,26 @@ class json_ref value_type moved_or_copied() const { - if (is_rvalue) + if (value_ref == nullptr) { - return std::move(*value_ref); + return std::move(owned_value); } return *value_ref; } value_type const& operator*() const { - return *static_cast(value_ref); + return value_ref ? *value_ref : owned_value; } value_type const* operator->() const { - return static_cast(value_ref); + return &** this; } private: mutable value_type owned_value = nullptr; - value_type* value_ref = nullptr; - const bool is_rvalue = true; + value_type const* value_ref = nullptr; }; } // namespace detail } // namespace nlohmann diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 7e3070bf6e..b14f35e046 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -91,6 +91,13 @@ #define JSON_ASSERT(x) assert(x) #endif +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + /*! @brief macro to briefly define a mapping between an enum and JSON @def NLOHMANN_JSON_SERIALIZE_ENUM diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index eb7065113d..5ac66f5af8 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -14,6 +14,7 @@ #undef JSON_CATCH #undef JSON_THROW #undef JSON_TRY +#undef JSON_PRIVATE_UNLESS_TESTED #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION diff --git a/include/nlohmann/detail/meta/cpp_future.hpp b/include/nlohmann/detail/meta/cpp_future.hpp index dd929ee142..4ba1a55714 100644 --- a/include/nlohmann/detail/meta/cpp_future.hpp +++ b/include/nlohmann/detail/meta/cpp_future.hpp @@ -2,19 +2,32 @@ #include // size_t #include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +#include // index_sequence, make_index_sequence, index_sequence_for + +#include namespace nlohmann { namespace detail { -// alias templates to reduce boilerplate -template -using enable_if_t = typename std::enable_if::type; template using uncvref_t = typename std::remove_cv::type>::type; -// implementation of C++14 index_sequence and affiliates +#ifdef JSON_HAS_CPP_14 + +// the following utilities are natively available in C++14 +using std::enable_if_t; +using std::index_sequence; +using std::make_index_sequence; +using std::index_sequence_for; + +#else + +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + // source: https://stackoverflow.com/a/32223343 template struct index_sequence @@ -45,6 +58,8 @@ template<> struct make_index_sequence<1> : index_sequence<0> {}; template using index_sequence_for = make_index_sequence; +#endif + // dispatch utility (taken from ranges-v3) template struct priority_tag : priority_tag < N - 1 > {}; template<> struct priority_tag<0> {}; @@ -58,5 +73,6 @@ struct static_const template constexpr T static_const::value; + } // namespace detail } // namespace nlohmann diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index ac143becf5..56c15a3f66 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -168,7 +168,9 @@ struct is_iterator_traits> is_detected::value; }; -// source: https://stackoverflow.com/a/37193089/4116453 +// The following implementation of is_complete_type is taken from +// https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/ +// and is written by Xiang Fan who agreed to using it in this library. template struct is_complete_type : std::false_type {}; @@ -186,7 +188,6 @@ struct is_compatible_object_type_impl < enable_if_t < is_detected::value&& is_detected::value >> { - using object_t = typename BasicJsonType::object_t; // macOS's is_constructible does not play well with nonesuch... diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 865b8904e2..0a34c8011e 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -362,7 +362,7 @@ class serializer } } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief dump escaped string @@ -625,6 +625,7 @@ class serializer } } + private: /*! @brief count digits @@ -880,6 +881,7 @@ class serializer } }; + JSON_ASSERT(byte < utf8d.size()); const std::uint8_t type = utf8d[byte]; codep = (state != UTF8_ACCEPT) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a9e6c76d11..8c9bef03df 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -73,6 +73,10 @@ SOFTWARE. #include #include +#if defined(JSON_HAS_CPP_17) + #include +#endif + /*! @brief namespace for Niels Lohmann @see https://github.com/nlohmann @@ -189,6 +193,7 @@ class basic_json /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -204,6 +209,7 @@ class basic_json std::move(cb), allow_exceptions, ignore_comments); } + private: using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; template using internal_iterator = ::nlohmann::detail::internal_iterator; @@ -220,6 +226,7 @@ class basic_json using binary_reader = ::nlohmann::detail::binary_reader; template using binary_writer = ::nlohmann::detail::binary_writer; + JSON_PRIVATE_UNLESS_TESTED: using serializer = ::nlohmann::detail::serializer; public: @@ -920,20 +927,21 @@ class basic_json AllocatorType alloc; using AllocatorTraits = std::allocator_traits>; - auto deleter = [&](T * object) + auto deleter = [&](T * obj) { - AllocatorTraits::deallocate(alloc, object, 1); + AllocatorTraits::deallocate(alloc, obj, 1); }; - std::unique_ptr object(AllocatorTraits::allocate(alloc, 1), deleter); - AllocatorTraits::construct(alloc, object.get(), std::forward(args)...); - JSON_ASSERT(object != nullptr); - return object.release(); + std::unique_ptr obj(AllocatorTraits::allocate(alloc, 1), deleter); + AllocatorTraits::construct(alloc, obj.get(), std::forward(args)...); + JSON_ASSERT(obj != nullptr); + return obj.release(); } //////////////////////// // JSON value storage // //////////////////////// + JSON_PRIVATE_UNLESS_TESTED: /*! @brief a JSON value @@ -1210,6 +1218,7 @@ class basic_json } }; + private: /*! @brief checks the class invariants @@ -6947,7 +6956,7 @@ class basic_json } - private: + JSON_PRIVATE_UNLESS_TESTED: ////////////////////// // member variables // ////////////////////// @@ -7793,7 +7802,7 @@ class basic_json string | 0x02 | string document | 0x03 | object array | 0x04 | array - binary | 0x05 | still unsupported + binary | 0x05 | binary undefined | 0x06 | still unsupported ObjectId | 0x07 | still unsupported boolean | 0x08 | boolean diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 5767909152..330677c4d6 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -5,6 +5,8 @@ #include // pair #include // vector +#include + namespace nlohmann { @@ -64,7 +66,7 @@ template , } } - throw std::out_of_range("key not found"); + JSON_THROW(std::out_of_range("key not found")); } const T& at(const Key& key) const @@ -77,7 +79,7 @@ template , } } - throw std::out_of_range("key not found"); + JSON_THROW(std::out_of_range("key not found")); } size_type erase(const Key& key) @@ -166,6 +168,19 @@ template , Container::push_back(value); return {--this->end(), true}; } + + template + using require_input_iter = typename std::enable_if::iterator_category, + std::input_iterator_tag>::value>::type; + + template> + void insert(InputIt first, InputIt last) + { + for (auto it = first; it != last; ++it) + { + insert(*it); + } + } }; } // namespace nlohmann diff --git a/include/nlohmann/thirdparty/hedley/hedley.hpp b/include/nlohmann/thirdparty/hedley/hedley.hpp index 521c78f1a4..c1fa16dbb6 100644 --- a/include/nlohmann/thirdparty/hedley/hedley.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley.hpp @@ -10,11 +10,11 @@ * SPDX-License-Identifier: CC0-1.0 */ -#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 13) +#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 14) #if defined(JSON_HEDLEY_VERSION) #undef JSON_HEDLEY_VERSION #endif -#define JSON_HEDLEY_VERSION 13 +#define JSON_HEDLEY_VERSION 14 #if defined(JSON_HEDLEY_STRINGIFY_EX) #undef JSON_HEDLEY_STRINGIFY_EX @@ -87,18 +87,18 @@ #if defined(JSON_HEDLEY_MSVC_VERSION) #undef JSON_HEDLEY_MSVC_VERSION #endif -#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) +#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL) #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100) -#elif defined(_MSC_FULL_VER) +#elif defined(_MSC_FULL_VER) && !defined(__ICL) #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10) -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) && !defined(__ICL) #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0) #endif #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK) #undef JSON_HEDLEY_MSVC_VERSION_CHECK #endif -#if !defined(_MSC_VER) +#if !defined(JSON_HEDLEY_MSVC_VERSION) #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0) #elif defined(_MSC_VER) && (_MSC_VER >= 1400) #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) @@ -111,9 +111,9 @@ #if defined(JSON_HEDLEY_INTEL_VERSION) #undef JSON_HEDLEY_INTEL_VERSION #endif -#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL) #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE) -#elif defined(__INTEL_COMPILER) +#elif defined(__INTEL_COMPILER) && !defined(__ICL) #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) #endif @@ -126,6 +126,22 @@ #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0) #endif +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #undef JSON_HEDLEY_INTEL_CL_VERSION +#endif +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL) + #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0) +#endif + +#if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK) + #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0) +#endif + #if defined(JSON_HEDLEY_PGI_VERSION) #undef JSON_HEDLEY_PGI_VERSION #endif @@ -678,6 +694,72 @@ #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) #endif +#if \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + defined(__clang__) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ + (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) + #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_PRAGMA(value) __pragma(value) +#else + #define JSON_HEDLEY_PRAGMA(value) +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) + #undef JSON_HEDLEY_DIAGNOSTIC_PUSH +#endif +#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) + #undef JSON_HEDLEY_DIAGNOSTIC_POP +#endif +#if defined(__clang__) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) + #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) +#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#else + #define JSON_HEDLEY_DIAGNOSTIC_PUSH + #define JSON_HEDLEY_DIAGNOSTIC_POP +#endif + /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) @@ -686,12 +768,22 @@ #if defined(__cplusplus) # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat") # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions") -# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ +# if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions") +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ JSON_HEDLEY_DIAGNOSTIC_PUSH \ _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ xpr \ JSON_HEDLEY_DIAGNOSTIC_POP +# endif # else # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ JSON_HEDLEY_DIAGNOSTIC_PUSH \ @@ -756,7 +848,7 @@ # define JSON_HEDLEY_CPP_CAST(T, expr) \ JSON_HEDLEY_DIAGNOSTIC_PUSH \ _Pragma("diag_suppress=Pe137") \ - JSON_HEDLEY_DIAGNOSTIC_POP \ + JSON_HEDLEY_DIAGNOSTIC_POP # else # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr)) # endif @@ -764,70 +856,6 @@ # define JSON_HEDLEY_CPP_CAST(T, expr) (expr) #endif -#if \ - (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ - defined(__clang__) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ - JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ - (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) - #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_PRAGMA(value) __pragma(value) -#else - #define JSON_HEDLEY_PRAGMA(value) -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) - #undef JSON_HEDLEY_DIAGNOSTIC_PUSH -#endif -#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) - #undef JSON_HEDLEY_DIAGNOSTIC_POP -#endif -#if defined(__clang__) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) - #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) -#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") -#elif \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") -#else - #define JSON_HEDLEY_DIAGNOSTIC_PUSH - #define JSON_HEDLEY_DIAGNOSTIC_POP -#endif - #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED) #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED #endif @@ -835,6 +863,10 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445") #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) @@ -873,6 +905,8 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161)) #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675") #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) @@ -902,8 +936,12 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292)) #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098") #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus) @@ -938,12 +976,11 @@ #if defined(JSON_HEDLEY_DEPRECATED_FOR) #undef JSON_HEDLEY_DEPRECATED_FOR #endif -#if JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) +#if \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) -#elif defined(__cplusplus) && (__cplusplus >= 201402L) - #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) #elif \ JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ @@ -958,6 +995,9 @@ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) +#elif defined(__cplusplus) && (__cplusplus >= 201402L) + #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) #elif \ JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ @@ -977,7 +1017,8 @@ #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) + JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated) #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) @@ -1006,13 +1047,7 @@ #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG) #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG #endif -#if (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) - #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) -#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) - #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) -#elif \ +#if \ JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ @@ -1031,6 +1066,12 @@ JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) +#elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) #elif defined(_Check_return_) /* SAL */ #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_ #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_ @@ -1083,7 +1124,9 @@ #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;") @@ -1115,7 +1158,8 @@ #endif #if \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_ASSUME(expr) __assume(expr) #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume) #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr) @@ -1247,7 +1291,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr)) #endif #if \ - JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) || \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) @@ -1255,7 +1299,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 ) # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 ) #elif \ - JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) || \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ @@ -1319,7 +1363,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(14, 0, 0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_MALLOC __declspec(restrict) #else #define JSON_HEDLEY_MALLOC @@ -1400,6 +1446,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ @@ -1430,6 +1477,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_INLINE __inline__ #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \ JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ @@ -1464,7 +1512,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) # define JSON_HEDLEY_ALWAYS_INLINE __forceinline #elif defined(__cplusplus) && \ ( \ @@ -1504,7 +1554,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0) #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline") @@ -1567,6 +1619,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) #define JSON_HEDLEY_NO_THROW __declspec(nothrow) #else @@ -1731,7 +1784,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if \ !defined(__cplusplus) && ( \ (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \ - JSON_HEDLEY_HAS_FEATURE(c_static_assert) || \ + (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ defined(_Static_assert) \ @@ -1739,7 +1792,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message) #elif \ (defined(__cplusplus) && (__cplusplus >= 201103L)) || \ - JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) + JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message)) #else # define JSON_HEDLEY_STATIC_ASSERT(expr, message) @@ -1799,7 +1853,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg)) #else # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg) @@ -1837,6 +1893,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) +#else + #define JSON_HEDLEY_FLAGS #endif #if defined(JSON_HEDLEY_FLAGS_CAST) @@ -1856,7 +1914,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if defined(JSON_HEDLEY_EMPTY_BASES) #undef JSON_HEDLEY_EMPTY_BASES #endif -#if JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0) +#if \ + (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases) #else #define JSON_HEDLEY_EMPTY_BASES diff --git a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp index 5fc2e31bb9..88ee044da1 100644 --- a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp @@ -74,6 +74,8 @@ #undef JSON_HEDLEY_IBM_VERSION_CHECK #undef JSON_HEDLEY_IMPORT #undef JSON_HEDLEY_INLINE +#undef JSON_HEDLEY_INTEL_CL_VERSION +#undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK #undef JSON_HEDLEY_INTEL_VERSION #undef JSON_HEDLEY_INTEL_VERSION_CHECK #undef JSON_HEDLEY_IS_CONSTANT diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 65051cdef3..9e988ab427 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -119,11 +119,11 @@ struct position_t * SPDX-License-Identifier: CC0-1.0 */ -#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 13) +#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 14) #if defined(JSON_HEDLEY_VERSION) #undef JSON_HEDLEY_VERSION #endif -#define JSON_HEDLEY_VERSION 13 +#define JSON_HEDLEY_VERSION 14 #if defined(JSON_HEDLEY_STRINGIFY_EX) #undef JSON_HEDLEY_STRINGIFY_EX @@ -196,18 +196,18 @@ struct position_t #if defined(JSON_HEDLEY_MSVC_VERSION) #undef JSON_HEDLEY_MSVC_VERSION #endif -#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) +#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL) #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100) -#elif defined(_MSC_FULL_VER) +#elif defined(_MSC_FULL_VER) && !defined(__ICL) #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10) -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) && !defined(__ICL) #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0) #endif #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK) #undef JSON_HEDLEY_MSVC_VERSION_CHECK #endif -#if !defined(_MSC_VER) +#if !defined(JSON_HEDLEY_MSVC_VERSION) #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0) #elif defined(_MSC_VER) && (_MSC_VER >= 1400) #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) @@ -220,9 +220,9 @@ struct position_t #if defined(JSON_HEDLEY_INTEL_VERSION) #undef JSON_HEDLEY_INTEL_VERSION #endif -#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL) #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE) -#elif defined(__INTEL_COMPILER) +#elif defined(__INTEL_COMPILER) && !defined(__ICL) #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) #endif @@ -235,6 +235,22 @@ struct position_t #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0) #endif +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #undef JSON_HEDLEY_INTEL_CL_VERSION +#endif +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL) + #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0) +#endif + +#if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK) + #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0) +#endif + #if defined(JSON_HEDLEY_PGI_VERSION) #undef JSON_HEDLEY_PGI_VERSION #endif @@ -787,6 +803,72 @@ struct position_t #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) #endif +#if \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + defined(__clang__) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ + (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) + #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_PRAGMA(value) __pragma(value) +#else + #define JSON_HEDLEY_PRAGMA(value) +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) + #undef JSON_HEDLEY_DIAGNOSTIC_PUSH +#endif +#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) + #undef JSON_HEDLEY_DIAGNOSTIC_POP +#endif +#if defined(__clang__) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) + #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) +#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#else + #define JSON_HEDLEY_DIAGNOSTIC_PUSH + #define JSON_HEDLEY_DIAGNOSTIC_POP +#endif + /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) @@ -795,12 +877,22 @@ struct position_t #if defined(__cplusplus) # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat") # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions") -# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ +# if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions") +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ JSON_HEDLEY_DIAGNOSTIC_PUSH \ _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \ xpr \ JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# endif # else # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ JSON_HEDLEY_DIAGNOSTIC_PUSH \ @@ -865,7 +957,7 @@ struct position_t # define JSON_HEDLEY_CPP_CAST(T, expr) \ JSON_HEDLEY_DIAGNOSTIC_PUSH \ _Pragma("diag_suppress=Pe137") \ - JSON_HEDLEY_DIAGNOSTIC_POP \ + JSON_HEDLEY_DIAGNOSTIC_POP # else # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr)) # endif @@ -873,70 +965,6 @@ struct position_t # define JSON_HEDLEY_CPP_CAST(T, expr) (expr) #endif -#if \ - (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ - defined(__clang__) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ - JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ - (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) - #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_PRAGMA(value) __pragma(value) -#else - #define JSON_HEDLEY_PRAGMA(value) -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) - #undef JSON_HEDLEY_DIAGNOSTIC_PUSH -#endif -#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) - #undef JSON_HEDLEY_DIAGNOSTIC_POP -#endif -#if defined(__clang__) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) - #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) -#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") -#elif \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") -#else - #define JSON_HEDLEY_DIAGNOSTIC_PUSH - #define JSON_HEDLEY_DIAGNOSTIC_POP -#endif - #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED) #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED #endif @@ -944,6 +972,10 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445") #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) @@ -982,6 +1014,8 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161)) #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675") #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) @@ -1011,8 +1045,12 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292)) #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098") #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus) @@ -1047,12 +1085,11 @@ struct position_t #if defined(JSON_HEDLEY_DEPRECATED_FOR) #undef JSON_HEDLEY_DEPRECATED_FOR #endif -#if JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) +#if \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) -#elif defined(__cplusplus) && (__cplusplus >= 201402L) - #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) #elif \ JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ @@ -1067,6 +1104,9 @@ struct position_t JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) +#elif defined(__cplusplus) && (__cplusplus >= 201402L) + #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) #elif \ JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ @@ -1086,7 +1126,8 @@ struct position_t #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) + JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated) #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) @@ -1115,13 +1156,7 @@ struct position_t #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG) #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG #endif -#if (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) - #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) -#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) - #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) -#elif \ +#if \ JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ @@ -1140,6 +1175,12 @@ struct position_t JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) +#elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) #elif defined(_Check_return_) /* SAL */ #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_ #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_ @@ -1192,7 +1233,9 @@ struct position_t #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;") @@ -1224,7 +1267,8 @@ struct position_t #endif #if \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_ASSUME(expr) __assume(expr) #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume) #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr) @@ -1356,7 +1400,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr)) #endif #if \ - JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) || \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) @@ -1364,7 +1408,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 ) # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 ) #elif \ - JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) || \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ @@ -1428,7 +1472,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(14, 0, 0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_MALLOC __declspec(restrict) #else #define JSON_HEDLEY_MALLOC @@ -1509,6 +1555,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ @@ -1539,6 +1586,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_INLINE __inline__ #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \ JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ @@ -1573,7 +1621,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) # define JSON_HEDLEY_ALWAYS_INLINE __forceinline #elif defined(__cplusplus) && \ ( \ @@ -1613,7 +1663,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0) #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline") @@ -1676,6 +1728,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) #define JSON_HEDLEY_NO_THROW __declspec(nothrow) #else @@ -1840,7 +1893,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if \ !defined(__cplusplus) && ( \ (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \ - JSON_HEDLEY_HAS_FEATURE(c_static_assert) || \ + (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ defined(_Static_assert) \ @@ -1848,7 +1901,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message) #elif \ (defined(__cplusplus) && (__cplusplus >= 201103L)) || \ - JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) + JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message)) #else # define JSON_HEDLEY_STATIC_ASSERT(expr, message) @@ -1908,7 +1962,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg)) #else # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg) @@ -1946,6 +2002,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) +#else + #define JSON_HEDLEY_FLAGS #endif #if defined(JSON_HEDLEY_FLAGS_CAST) @@ -1965,7 +2023,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if defined(JSON_HEDLEY_EMPTY_BASES) #undef JSON_HEDLEY_EMPTY_BASES #endif -#if JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0) +#if \ + (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases) #else #define JSON_HEDLEY_EMPTY_BASES @@ -2108,6 +2168,13 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_ASSERT(x) assert(x) #endif +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + /*! @brief macro to briefly define a mapping between an enum and JSON @def NLOHMANN_JSON_SERIALIZE_ENUM @@ -2677,19 +2744,33 @@ class other_error : public exception #include // size_t #include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +#include // index_sequence, make_index_sequence, index_sequence_for + +// #include + namespace nlohmann { namespace detail { -// alias templates to reduce boilerplate -template -using enable_if_t = typename std::enable_if::type; template using uncvref_t = typename std::remove_cv::type>::type; -// implementation of C++14 index_sequence and affiliates +#ifdef JSON_HAS_CPP_14 + +// the following utilities are natively available in C++14 +using std::enable_if_t; +using std::index_sequence; +using std::make_index_sequence; +using std::index_sequence_for; + +#else + +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + // source: https://stackoverflow.com/a/32223343 template struct index_sequence @@ -2720,6 +2801,8 @@ template<> struct make_index_sequence<1> : index_sequence<0> {}; template using index_sequence_for = make_index_sequence; +#endif + // dispatch utility (taken from ranges-v3) template struct priority_tag : priority_tag < N - 1 > {}; template<> struct priority_tag<0> {}; @@ -2733,6 +2816,7 @@ struct static_const template constexpr T static_const::value; + } // namespace detail } // namespace nlohmann @@ -3115,7 +3199,9 @@ struct is_iterator_traits> is_detected::value; }; -// source: https://stackoverflow.com/a/37193089/4116453 +// The following implementation of is_complete_type is taken from +// https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/ +// and is written by Xiang Fan who agreed to using it in this library. template struct is_complete_type : std::false_type {}; @@ -3133,7 +3219,6 @@ struct is_compatible_object_type_impl < enable_if_t < is_detected::value&& is_detected::value >> { - using object_t = typename BasicJsonType::object_t; // macOS's is_constructible does not play well with nonesuch... @@ -4487,15 +4572,15 @@ class byte_container_with_subtype : public BinaryType : container_type(std::move(b)) {} - byte_container_with_subtype(const container_type& b, std::uint8_t subtype) noexcept(noexcept(container_type(b))) + byte_container_with_subtype(const container_type& b, std::uint8_t subtype_) noexcept(noexcept(container_type(b))) : container_type(b) - , m_subtype(subtype) + , m_subtype(subtype_) , m_has_subtype(true) {} - byte_container_with_subtype(container_type&& b, std::uint8_t subtype) noexcept(noexcept(container_type(std::move(b)))) + byte_container_with_subtype(container_type&& b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b)))) : container_type(std::move(b)) - , m_subtype(subtype) + , m_subtype(subtype_) , m_has_subtype(true) {} @@ -4528,9 +4613,9 @@ class byte_container_with_subtype : public BinaryType @since version 3.8.0 */ - void set_subtype(std::uint8_t subtype) noexcept + void set_subtype(std::uint8_t subtype_) noexcept { - m_subtype = subtype; + m_subtype = subtype_; m_has_subtype = true; } @@ -4705,19 +4790,19 @@ std::size_t hash(const BasicJsonType& j) return combine(type, h); } - case nlohmann::detail::value_t::number_unsigned: + case BasicJsonType::value_t::number_unsigned: { const auto h = std::hash {}(j.template get()); return combine(type, h); } - case nlohmann::detail::value_t::number_float: + case BasicJsonType::value_t::number_float: { const auto h = std::hash {}(j.template get()); return combine(type, h); } - case nlohmann::detail::value_t::binary: + case BasicJsonType::value_t::binary: { auto seed = combine(type, j.get_binary().size()); const auto h = std::hash {}(j.get_binary().has_subtype()); @@ -4730,8 +4815,9 @@ std::size_t hash(const BasicJsonType& j) return seed; } - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + JSON_ASSERT(false); // LCOV_EXCL_LINE + return 0; // LCOV_EXCL_LINE } } @@ -8430,8 +8516,9 @@ class binary_reader return parse_cbor_internal(true, tag_handler); } - default: // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE } } @@ -10679,6 +10766,7 @@ class primitive_iterator_t static constexpr difference_type begin_value = 0; static constexpr difference_type end_value = begin_value + 1; + JSON_PRIVATE_UNLESS_TESTED: /// iterator as signed integer type difference_type m_it = (std::numeric_limits::min)(); @@ -10971,7 +11059,7 @@ class iter_impl return *this; } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief set the iterator to the first value @pre The iterator is initialized; i.e. `m_object != nullptr`. @@ -11435,7 +11523,7 @@ class iter_impl return operator*(); } - private: + JSON_PRIVATE_UNLESS_TESTED: /// associated JSON instance pointer m_object = nullptr; /// the actual iterator of the associated instance @@ -11950,6 +12038,7 @@ class json_pointer return static_cast(res); } + JSON_PRIVATE_UNLESS_TESTED: json_pointer top() const { if (JSON_HEDLEY_UNLIKELY(empty())) @@ -11962,6 +12051,7 @@ class json_pointer return result; } + private: /*! @brief create and return a reference to the pointed to value @@ -12398,6 +12488,7 @@ class json_pointer {} } + JSON_PRIVATE_UNLESS_TESTED: /// escape "~" to "~0" and "/" to "~1" static std::string escape(std::string s) { @@ -12413,6 +12504,7 @@ class json_pointer replace_substring(s, "~0", "~"); } + private: /*! @param[in] reference_string the reference string to the current value @param[in] value the value to consider @@ -12570,19 +12662,14 @@ class json_ref json_ref(value_type&& value) : owned_value(std::move(value)) - , value_ref(&owned_value) - , is_rvalue(true) {} json_ref(const value_type& value) - : value_ref(const_cast(&value)) - , is_rvalue(false) + : value_ref(&value) {} json_ref(std::initializer_list init) : owned_value(init) - , value_ref(&owned_value) - , is_rvalue(true) {} template < @@ -12590,8 +12677,6 @@ class json_ref enable_if_t::value, int> = 0 > json_ref(Args && ... args) : owned_value(std::forward(args)...) - , value_ref(&owned_value) - , is_rvalue(true) {} // class should be movable only @@ -12603,27 +12688,26 @@ class json_ref value_type moved_or_copied() const { - if (is_rvalue) + if (value_ref == nullptr) { - return std::move(*value_ref); + return std::move(owned_value); } return *value_ref; } value_type const& operator*() const { - return *static_cast(value_ref); + return value_ref ? *value_ref : owned_value; } value_type const* operator->() const { - return static_cast(value_ref); + return &** this; } private: mutable value_type owned_value = nullptr; - value_type* value_ref = nullptr; - const bool is_rvalue = true; + value_type const* value_ref = nullptr; }; } // namespace detail } // namespace nlohmann @@ -15838,7 +15922,7 @@ class serializer } } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief dump escaped string @@ -16101,6 +16185,7 @@ class serializer } } + private: /*! @brief count digits @@ -16356,6 +16441,7 @@ class serializer } }; + JSON_ASSERT(byte < utf8d.size()); const std::uint8_t type = utf8d[byte]; codep = (state != UTF8_ACCEPT) @@ -16434,6 +16520,9 @@ class serializer #include // pair #include // vector +// #include + + namespace nlohmann { @@ -16493,7 +16582,7 @@ template , } } - throw std::out_of_range("key not found"); + JSON_THROW(std::out_of_range("key not found")); } const T& at(const Key& key) const @@ -16506,7 +16595,7 @@ template , } } - throw std::out_of_range("key not found"); + JSON_THROW(std::out_of_range("key not found")); } size_type erase(const Key& key) @@ -16595,11 +16684,28 @@ template , Container::push_back(value); return {--this->end(), true}; } + + template + using require_input_iter = typename std::enable_if::iterator_category, + std::input_iterator_tag>::value>::type; + + template> + void insert(InputIt first, InputIt last) + { + for (auto it = first; it != last; ++it) + { + insert(*it); + } + } }; } // namespace nlohmann +#if defined(JSON_HAS_CPP_17) + #include +#endif + /*! @brief namespace for Niels Lohmann @see https://github.com/nlohmann @@ -16716,6 +16822,7 @@ class basic_json /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -16731,6 +16838,7 @@ class basic_json std::move(cb), allow_exceptions, ignore_comments); } + private: using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; template using internal_iterator = ::nlohmann::detail::internal_iterator; @@ -16747,6 +16855,7 @@ class basic_json using binary_reader = ::nlohmann::detail::binary_reader; template using binary_writer = ::nlohmann::detail::binary_writer; + JSON_PRIVATE_UNLESS_TESTED: using serializer = ::nlohmann::detail::serializer; public: @@ -17447,20 +17556,21 @@ class basic_json AllocatorType alloc; using AllocatorTraits = std::allocator_traits>; - auto deleter = [&](T * object) + auto deleter = [&](T * obj) { - AllocatorTraits::deallocate(alloc, object, 1); + AllocatorTraits::deallocate(alloc, obj, 1); }; - std::unique_ptr object(AllocatorTraits::allocate(alloc, 1), deleter); - AllocatorTraits::construct(alloc, object.get(), std::forward(args)...); - JSON_ASSERT(object != nullptr); - return object.release(); + std::unique_ptr obj(AllocatorTraits::allocate(alloc, 1), deleter); + AllocatorTraits::construct(alloc, obj.get(), std::forward(args)...); + JSON_ASSERT(obj != nullptr); + return obj.release(); } //////////////////////// // JSON value storage // //////////////////////// + JSON_PRIVATE_UNLESS_TESTED: /*! @brief a JSON value @@ -17737,6 +17847,7 @@ class basic_json } }; + private: /*! @brief checks the class invariants @@ -23474,7 +23585,7 @@ class basic_json } - private: + JSON_PRIVATE_UNLESS_TESTED: ////////////////////// // member variables // ////////////////////// @@ -24320,7 +24431,7 @@ class basic_json string | 0x02 | string document | 0x03 | object array | 0x04 | array - binary | 0x05 | still unsupported + binary | 0x05 | binary undefined | 0x06 | still unsupported ObjectId | 0x07 | still unsupported boolean | 0x08 | boolean @@ -25330,6 +25441,7 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_CATCH #undef JSON_THROW #undef JSON_TRY +#undef JSON_PRIVATE_UNLESS_TESTED #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION @@ -25413,6 +25525,8 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_HEDLEY_IBM_VERSION_CHECK #undef JSON_HEDLEY_IMPORT #undef JSON_HEDLEY_INLINE +#undef JSON_HEDLEY_INTEL_CL_VERSION +#undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK #undef JSON_HEDLEY_INTEL_VERSION #undef JSON_HEDLEY_INTEL_VERSION_CHECK #undef JSON_HEDLEY_IS_CONSTANT diff --git a/test/cmake_add_subdirectory/project/CMakeLists.txt b/test/cmake_add_subdirectory/project/CMakeLists.txt index 21357b6857..caab6c4e16 100644 --- a/test/cmake_add_subdirectory/project/CMakeLists.txt +++ b/test/cmake_add_subdirectory/project/CMakeLists.txt @@ -11,3 +11,10 @@ target_link_libraries(with_namespace_target nlohmann_json::nlohmann_json) add_executable(without_namespace_target main.cpp) target_link_libraries(without_namespace_target nlohmann_json) + +if(NOT MSVC) + add_executable(without_exceptions main.cpp) + target_link_libraries(without_exceptions nlohmann_json::nlohmann_json) + target_compile_definitions(without_exceptions PRIVATE JSON_NOEXCEPTION) + target_compile_options(without_exceptions PRIVATE -fno-exceptions) +endif() diff --git a/test/src/unit-allocator.cpp b/test/src/unit-allocator.cpp index 40361ce00f..ad78b8f9ea 100644 --- a/test/src/unit-allocator.cpp +++ b/test/src/unit-allocator.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private namespace { diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp index 6b2e76dad0..3be72c7d47 100644 --- a/test/src/unit-bson.cpp +++ b/test/src/unit-bson.cpp @@ -867,8 +867,9 @@ TEST_CASE("Negative size of binary value") 0x00 // end marker }; - CHECK_THROWS_AS(json::from_bson(input), json::parse_error); - CHECK_THROWS_WITH(json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1"); + json _; + CHECK_THROWS_AS(_ = json::from_bson(input), json::parse_error); + CHECK_THROWS_WITH(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1"); } TEST_CASE("Unsupported BSON input") diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index ca4b781a12..9ed80c8f1a 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1591,14 +1591,16 @@ TEST_CASE("CBOR") { // array with three empty byte strings std::vector input = {0x83, 0x40, 0x40, 0x40}; - CHECK_NOTHROW(json::from_cbor(input)); + json _; + CHECK_NOTHROW(_ = json::from_cbor(input)); } SECTION("binary in object") { // object mapping "foo" to empty byte string std::vector input = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x40}; - CHECK_NOTHROW(json::from_cbor(input)); + json _; + CHECK_NOTHROW(_ = json::from_cbor(input)); } SECTION("SAX callback with binary") @@ -2551,8 +2553,9 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), b); // check that parsing fails in error mode - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); // check that parsing succeeds and gets original value in ignore mode auto j_tagged = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore); @@ -2570,8 +2573,9 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xD8); // tag // check that parsing fails in error mode - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); // check that parsing succeeds and gets original value in ignore mode auto j_tagged = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore); @@ -2585,9 +2589,10 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xD8); // tag // check that parsing fails in all modes - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); } } @@ -2602,8 +2607,9 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xD9); // tag // check that parsing fails in error mode - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); // check that parsing succeeds and gets original value in ignore mode auto j_tagged = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore); @@ -2618,9 +2624,10 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xD9); // tag // check that parsing fails in all modes - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); } } @@ -2637,8 +2644,9 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xDA); // tag // check that parsing fails in error mode - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); // check that parsing succeeds and gets original value in ignore mode auto j_tagged = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore); @@ -2655,9 +2663,10 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xDA); // tag // check that parsing fails in all modes - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); } } @@ -2678,8 +2687,9 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xDB); // tag // check that parsing fails in error mode - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); // check that parsing succeeds and gets original value in ignore mode auto j_tagged = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore); @@ -2700,9 +2710,10 @@ TEST_CASE("Tagged values") v_tagged.insert(v_tagged.begin(), 0xDB); // tag // check that parsing fails in all modes - CHECK_THROWS_AS(json::from_cbor(v_tagged), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); - CHECK_THROWS_AS(json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::error), json::parse_error); + CHECK_THROWS_AS(_ = json::from_cbor(v_tagged, true, true, json::cbor_tag_handler_t::ignore), json::parse_error); } } @@ -2717,8 +2728,9 @@ TEST_CASE("Tagged values") CHECK(vec == std::vector {0xA1, 0x66, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79, 0xD8, 0x2A, 0x44, 0xCA, 0xFE, 0xBA, 0xBE}); // parse error when parsing tagged value - CHECK_THROWS_AS(json::from_cbor(vec), json::parse_error); - CHECK_THROWS_WITH(json::from_cbor(vec), "[json.exception.parse_error.112] parse error at byte 9: syntax error while parsing CBOR value: invalid byte: 0xD8"); + json _; + CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error); + CHECK_THROWS_WITH(_ = json::from_cbor(vec), "[json.exception.parse_error.112] parse error at byte 9: syntax error while parsing CBOR value: invalid byte: 0xD8"); // binary without subtype when tags are ignored json jb = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::ignore); diff --git a/test/src/unit-class_const_iterator.cpp b/test/src/unit-class_const_iterator.cpp index 608e6a544e..a972fd4c72 100644 --- a/test/src/unit-class_const_iterator.cpp +++ b/test/src/unit-class_const_iterator.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("const_iterator class") { diff --git a/test/src/unit-class_iterator.cpp b/test/src/unit-class_iterator.cpp index c06ef0a484..b4ef11e436 100644 --- a/test/src/unit-class_iterator.cpp +++ b/test/src/unit-class_iterator.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("iterator class") { diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index 15b390cfb8..1a4f8ed754 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private namespace { diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 07044ed56f..d0335c9484 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include @@ -1880,7 +1879,8 @@ TEST_CASE("parser class") SECTION("error messages for comments") { - CHECK_THROWS_WITH_AS(json::parse("/a", nullptr, true, true), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid comment; expecting '/' or '*' after '/'; last read: '/a'", json::parse_error); - CHECK_THROWS_WITH_AS(json::parse("/*", nullptr, true, true), "[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - invalid comment; missing closing '*/'; last read: '/*'", json::parse_error); + json _; + CHECK_THROWS_WITH_AS(_ = json::parse("/a", nullptr, true, true), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid comment; expecting '/' or '*' after '/'; last read: '/a'", json::parse_error); + CHECK_THROWS_WITH_AS(_ = json::parse("/*", nullptr, true, true), "[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - invalid comment; missing closing '*/'; last read: '/*'", json::parse_error); } } diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index 33bd92c3b6..70b3e40470 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -30,10 +30,9 @@ SOFTWARE. #include "doctest_compatibility.h" DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-convenience.cpp b/test/src/unit-convenience.cpp index 162ad56d86..c75edac4ea 100644 --- a/test/src/unit-convenience.cpp +++ b/test/src/unit-convenience.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 2053d47301..0d5baa409d 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include @@ -49,18 +48,6 @@ using nlohmann::json; #define JSON_HAS_CPP_14 #endif -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - -#if defined(JSON_HAS_CPP_17) - #include -#endif - TEST_CASE("value conversion") { SECTION("get an object (explicit)") diff --git a/test/src/unit-deserialization.cpp b/test/src/unit-deserialization.cpp index 716564c0a4..d2db7e80c3 100644 --- a/test/src/unit-deserialization.cpp +++ b/test/src/unit-deserialization.cpp @@ -537,7 +537,8 @@ TEST_CASE("deserialization") SECTION("with empty range") { std::vector v; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); SaxEventLogger l; @@ -553,7 +554,8 @@ TEST_CASE("deserialization") SECTION("case 1") { uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u'}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -569,7 +571,8 @@ TEST_CASE("deserialization") SECTION("case 2") { uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1'}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -585,7 +588,8 @@ TEST_CASE("deserialization") SECTION("case 3") { uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1', '1', '1', '1', '1', '1', '1', '1'}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -601,7 +605,8 @@ TEST_CASE("deserialization") SECTION("case 4") { uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', 'u', '1', '1', '1', '1', '1', '1', '1', '1', '\\'}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -617,7 +622,8 @@ TEST_CASE("deserialization") SECTION("case 5") { uint8_t v[] = {'\"', 0x7F, 0xC1}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -652,7 +658,8 @@ TEST_CASE("deserialization") SECTION("case 7") { uint8_t v[] = {'\"', 0x7F, 0xDF, 0xC0}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -668,7 +675,8 @@ TEST_CASE("deserialization") SECTION("case 8") { uint8_t v[] = {'\"', 0x7F, 0xE0, 0x9F}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -684,7 +692,8 @@ TEST_CASE("deserialization") SECTION("case 9") { uint8_t v[] = {'\"', 0x7F, 0xEF, 0xC0}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -700,7 +709,8 @@ TEST_CASE("deserialization") SECTION("case 10") { uint8_t v[] = {'\"', 0x7F, 0xED, 0x7F}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -716,7 +726,8 @@ TEST_CASE("deserialization") SECTION("case 11") { uint8_t v[] = {'\"', 0x7F, 0xF0, 0x8F}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -732,7 +743,8 @@ TEST_CASE("deserialization") SECTION("case 12") { uint8_t v[] = {'\"', 0x7F, 0xF0, 0xC0}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -748,7 +760,8 @@ TEST_CASE("deserialization") SECTION("case 13") { uint8_t v[] = {'\"', 0x7F, 0xF3, 0x7F}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -764,7 +777,8 @@ TEST_CASE("deserialization") SECTION("case 14") { uint8_t v[] = {'\"', 0x7F, 0xF3, 0xC0}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -780,7 +794,8 @@ TEST_CASE("deserialization") SECTION("case 15") { uint8_t v[] = {'\"', 0x7F, 0xF4, 0x7F}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; @@ -796,7 +811,8 @@ TEST_CASE("deserialization") SECTION("case 16") { uint8_t v[] = {'{', '\"', '\"', ':', '1', '1'}; - CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); + json _; + CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); json j_error; diff --git a/test/src/unit-iterators1.cpp b/test/src/unit-iterators1.cpp index 45a84a07f4..1ff8958fad 100644 --- a/test/src/unit-iterators1.cpp +++ b/test/src/unit-iterators1.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("iterators 1") { diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 61b6e323b6..14d8cd1830 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("JSON pointers") { diff --git a/test/src/unit-ordered_json.cpp b/test/src/unit-ordered_json.cpp index 9b242c825d..9bd1187e47 100644 --- a/test/src/unit-ordered_json.cpp +++ b/test/src/unit-ordered_json.cpp @@ -76,4 +76,18 @@ TEST_CASE("ordered_json") CHECK(multi_ordered.dump() == "{\"z\":1,\"m\":2,\"y\":4}"); CHECK(multi_ordered.erase("m") == 1); CHECK(multi_ordered.dump() == "{\"z\":1,\"y\":4}"); + + // Ranged insert test. + // It seems that values shouldn't be overwritten. Only new values are added + json j1 {{"c", 1}, {"b", 2}, {"a", 3}}; + const json j2 {{"c", 77}, {"d", 42}, {"a", 4}}; + j1.insert( j2.cbegin(), j2.cend() ); + CHECK(j1.size() == 4); + CHECK(j1.dump() == "{\"a\":3,\"b\":2,\"c\":1,\"d\":42}"); + + ordered_json oj1 {{"c", 1}, {"b", 2}, {"a", 3}}; + const ordered_json oj2 {{"c", 77}, {"d", 42}, {"a", 4}}; + oj1.insert( oj2.cbegin(), oj2.cend() ); + CHECK(oj1.size() == 4); + CHECK(oj1.dump() == "{\"c\":1,\"b\":2,\"a\":3,\"d\":42}"); } diff --git a/test/src/unit-regression1.cpp b/test/src/unit-regression1.cpp index 18820d1717..9dcc75b092 100644 --- a/test/src/unit-regression1.cpp +++ b/test/src/unit-regression1.cpp @@ -33,10 +33,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") // for some reason including this after the json header leads to linker errors with VS 2017... #include -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 69e3dea9bc..ca50cdd41d 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -33,10 +33,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") // for some reason including this after the json header leads to linker errors with VS 2017... #include -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index 42954479ec..f60477a825 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -805,12 +805,13 @@ TEST_CASE("UBJSON") std::vector vec1 = {'H', 'i', '1'}; CHECK(json::from_ubjson(vec1, true, false).is_discarded()); + json _; std::vector vec2 = {'H', 'i', 2, '1', 'A', '3'}; - CHECK_THROWS_WITH_AS(json::from_ubjson(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A", json::parse_error); + CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A", json::parse_error); std::vector vec3 = {'H', 'i', 2, '1', '.'}; - CHECK_THROWS_WITH_AS(json::from_ubjson(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1.", json::parse_error); + CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1.", json::parse_error); std::vector vec4 = {'H', 2, '1', '0'}; - CHECK_THROWS_WITH_AS(json::from_ubjson(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x02", json::parse_error); + CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x02", json::parse_error); } SECTION("serialization") diff --git a/test/src/unit-unicode.cpp b/test/src/unit-unicode.cpp index 8e122d5dd3..acaca2888d 100644 --- a/test/src/unit-unicode.cpp +++ b/test/src/unit-unicode.cpp @@ -32,10 +32,9 @@ SOFTWARE. // for some reason including this after the json header leads to linker errors with VS 2017... #include -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include @@ -1202,7 +1201,8 @@ TEST_CASE("Unicode" * doctest::skip()) SECTION("with an iterator") { std::string i = "\xef\xbb\xbf{\n \"foo\": true\n}"; - CHECK_NOTHROW(json::parse(i.begin(), i.end())); + json _; + CHECK_NOTHROW(_ = json::parse(i.begin(), i.end())); } } diff --git a/test/thirdparty/doctest/LICENSE.txt b/test/thirdparty/doctest/LICENSE.txt index a204721468..155dbe9428 100755 --- a/test/thirdparty/doctest/LICENSE.txt +++ b/test/thirdparty/doctest/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016-2019 Viktor Kirilov +Copyright (c) 2016-2020 Viktor Kirilov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +SOFTWARE. \ No newline at end of file diff --git a/test/thirdparty/doctest/doctest.h b/test/thirdparty/doctest/doctest.h old mode 100755 new mode 100644 index 2f0ff2131f..ae9c4d4109 --- a/test/thirdparty/doctest/doctest.h +++ b/test/thirdparty/doctest/doctest.h @@ -4,7 +4,7 @@ // // doctest.h - the lightest feature-rich C++ single-header testing framework for unit tests and TDD // -// Copyright (c) 2016-2019 Viktor Kirilov +// Copyright (c) 2016-2020 Viktor Kirilov // // Distributed under the MIT Software License // See accompanying file LICENSE.txt or copy at @@ -47,9 +47,9 @@ // ================================================================================================= #define DOCTEST_VERSION_MAJOR 2 -#define DOCTEST_VERSION_MINOR 3 -#define DOCTEST_VERSION_PATCH 7 -#define DOCTEST_VERSION_STR "2.3.7" +#define DOCTEST_VERSION_MINOR 4 +#define DOCTEST_VERSION_PATCH 3 +#define DOCTEST_VERSION_STR "2.4.3" #define DOCTEST_VERSION \ (DOCTEST_VERSION_MAJOR * 10000 + DOCTEST_VERSION_MINOR * 100 + DOCTEST_VERSION_PATCH) @@ -301,11 +301,23 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' #define DOCTEST_NOINLINE __declspec(noinline) #define DOCTEST_UNUSED #define DOCTEST_ALIGNMENT(x) -#else // MSVC +#elif DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 5, 0) +#define DOCTEST_NOINLINE +#define DOCTEST_UNUSED +#define DOCTEST_ALIGNMENT(x) +#else #define DOCTEST_NOINLINE __attribute__((noinline)) #define DOCTEST_UNUSED __attribute__((unused)) #define DOCTEST_ALIGNMENT(x) __attribute__((aligned(x))) -#endif // MSVC +#endif + +#ifndef DOCTEST_NORETURN +#define DOCTEST_NORETURN [[noreturn]] +#endif // DOCTEST_NORETURN + +#ifndef DOCTEST_NOEXCEPT +#define DOCTEST_NOEXCEPT noexcept +#endif // DOCTEST_NOEXCEPT // ================================================================================================= // == FEATURE DETECTION END ======================================================================== @@ -347,8 +359,20 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' #ifndef DOCTEST_BREAK_INTO_DEBUGGER // should probably take a look at https://github.com/scottt/debugbreak -#ifdef DOCTEST_PLATFORM_MAC +#ifdef DOCTEST_PLATFORM_LINUX +#if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) +// Break at the location of the failing check if possible +#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) +#else +#include +#define DOCTEST_BREAK_INTO_DEBUGGER() raise(SIGTRAP) +#endif +#elif defined(DOCTEST_PLATFORM_MAC) +#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) +#else +#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("brk #0"); +#endif #elif DOCTEST_MSVC #define DOCTEST_BREAK_INTO_DEBUGGER() __debugbreak() #elif defined(__MINGW32__) @@ -357,7 +381,7 @@ extern "C" __declspec(dllimport) void __stdcall DebugBreak(); DOCTEST_GCC_SUPPRESS_WARNING_POP #define DOCTEST_BREAK_INTO_DEBUGGER() ::DebugBreak() #else // linux -#define DOCTEST_BREAK_INTO_DEBUGGER() ((void)0) +#define DOCTEST_BREAK_INTO_DEBUGGER() (static_cast(0)) #endif // linux #endif // DOCTEST_BREAK_INTO_DEBUGGER @@ -367,6 +391,9 @@ DOCTEST_GCC_SUPPRESS_WARNING_POP #endif // DOCTEST_CONFIG_USE_IOSFWD #ifdef DOCTEST_CONFIG_USE_STD_HEADERS +#ifndef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS +#define DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS +#endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS #include #include #include @@ -629,7 +656,7 @@ DOCTEST_INTERFACE const char* skipPathFromFilename(const char* file); struct DOCTEST_INTERFACE TestCaseData { - const char* m_file; // the file in which the test was registered + String m_file; // the file in which the test was registered unsigned m_line; // the line where the test was registered const char* m_name; // name of the test case const char* m_test_suite; // the test suite in which the test was added @@ -720,7 +747,9 @@ struct ContextOptions //!OCLINT too many fields bool gnu_file_line; // if line numbers should be surrounded with :x: and not (x): bool no_path_in_filenames; // if the path to files should be removed from the output bool no_line_numbers; // if source code line numbers should be omitted from the output + bool no_debug_output; // no output in the debug console when a debugger is attached bool no_skipped_summary; // don't print "skipped" in the summary !!! UNDOCUMENTED !!! + bool no_time_in_output; // omit any time/timestamps from output !!! UNDOCUMENTED !!! bool help; // to print the help bool version; // to print the version @@ -731,7 +760,6 @@ struct ContextOptions //!OCLINT too many fields }; namespace detail { -#if defined(DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING) || defined(DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS) template struct enable_if {}; @@ -739,7 +767,6 @@ namespace detail { template struct enable_if { typedef TYPE type; }; -#endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING) || DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS // clang-format off template struct remove_reference { typedef T type; }; @@ -748,6 +775,14 @@ namespace detail { template struct remove_const { typedef T type; }; template struct remove_const { typedef T type; }; +#ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS + template struct is_enum : public std::is_enum {}; + template struct underlying_type : public std::underlying_type {}; +#else + // Use compiler intrinsics + template struct is_enum { constexpr static bool value = __is_enum(T); }; + template struct underlying_type { typedef __underlying_type(T) type; }; +#endif // clang-format on template @@ -756,33 +791,23 @@ namespace detail { { static const bool value = false; }; namespace has_insertion_operator_impl { - typedef char no; - typedef char yes[2]; + std::ostream &os(); + template + DOCTEST_REF_WRAP(T) val(); - struct any_t - { - template - // cppcheck-suppress noExplicitConstructor - any_t(const DOCTEST_REF_WRAP(T)); + template + struct check { + static constexpr bool value = false; }; - yes& testStreamable(std::ostream&); - no testStreamable(no); - - no operator<<(const std::ostream&, const any_t&); - - template - struct has_insertion_operator - { - static std::ostream& s; - static const DOCTEST_REF_WRAP(T) t; - static const bool value = sizeof(decltype(testStreamable(s << t))) == sizeof(yes); + template + struct check(), void())> { + static constexpr bool value = true; }; } // namespace has_insertion_operator_impl - template - struct has_insertion_operator : has_insertion_operator_impl::has_insertion_operator - {}; + template + using has_insertion_operator = has_insertion_operator_impl::check; DOCTEST_INTERFACE void my_memcpy(void* dest, const void* src, unsigned num); @@ -846,7 +871,7 @@ struct StringMaker } }; -template +template ::value, bool>::type = true> String toString(const DOCTEST_REF_WRAP(T) value) { return StringMaker::convert(value); } @@ -873,6 +898,12 @@ DOCTEST_INTERFACE String toString(int long long in); DOCTEST_INTERFACE String toString(int long long unsigned in); DOCTEST_INTERFACE String toString(std::nullptr_t in); +template ::value, bool>::type = true> +String toString(const DOCTEST_REF_WRAP(T) value) { + typedef typename detail::underlying_type::type UT; + return toString(static_cast(value)); +} + #if DOCTEST_MSVC >= DOCTEST_COMPILER(19, 20, 0) // see this issue on why this is needed: https://github.com/onqtam/doctest/issues/183 DOCTEST_INTERFACE String toString(const std::string& in); @@ -987,7 +1018,7 @@ namespace detail { DOCTEST_INTERFACE bool checkIfShouldThrow(assertType::Enum at); #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS - [[noreturn]] + DOCTEST_NORETURN #endif // DOCTEST_CONFIG_NO_EXCEPTIONS DOCTEST_INTERFACE void throwException(); @@ -1005,6 +1036,7 @@ namespace detail { template String stringifyBinaryExpr(const DOCTEST_REF_WRAP(L) lhs, const char* op, const DOCTEST_REF_WRAP(R) rhs) { + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) return toString(lhs) + op + toString(rhs); } @@ -1092,6 +1124,7 @@ namespace detail { #define DOCTEST_COMPARISON_RETURN_TYPE bool #else // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING #define DOCTEST_COMPARISON_RETURN_TYPE typename enable_if::value || can_use_op::value, bool>::type + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) inline bool eq(const char* lhs, const char* rhs) { return String(lhs) == String(rhs); } inline bool ne(const char* lhs, const char* rhs) { return String(lhs) != String(rhs); } inline bool lt(const char* lhs, const char* rhs) { return String(lhs) < String(rhs); } @@ -1284,12 +1317,12 @@ namespace detail { template struct RelationalComparator { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return op(lhs, rhs); } }; // clang-format on - DOCTEST_BINARY_RELATIONAL_OP(0, eq) - DOCTEST_BINARY_RELATIONAL_OP(1, ne) - DOCTEST_BINARY_RELATIONAL_OP(2, gt) - DOCTEST_BINARY_RELATIONAL_OP(3, lt) - DOCTEST_BINARY_RELATIONAL_OP(4, ge) - DOCTEST_BINARY_RELATIONAL_OP(5, le) + DOCTEST_BINARY_RELATIONAL_OP(0, doctest::detail::eq) + DOCTEST_BINARY_RELATIONAL_OP(1, doctest::detail::ne) + DOCTEST_BINARY_RELATIONAL_OP(2, doctest::detail::gt) + DOCTEST_BINARY_RELATIONAL_OP(3, doctest::detail::lt) + DOCTEST_BINARY_RELATIONAL_OP(4, doctest::detail::ge) + DOCTEST_BINARY_RELATIONAL_OP(5, doctest::detail::le) struct DOCTEST_INTERFACE ResultBuilder : public AssertData { @@ -1416,9 +1449,9 @@ namespace detail { } catch(T ex) { // NOLINT res = m_translateFunction(ex); //!OCLINT parameter reassignment return true; - } catch(...) {} //!OCLINT - empty catch statement -#endif // DOCTEST_CONFIG_NO_EXCEPTIONS - ((void)res); // to silence -Wunused-parameter + } catch(...) {} //!OCLINT - empty catch statement +#endif // DOCTEST_CONFIG_NO_EXCEPTIONS + static_cast(res); // to silence -Wunused-parameter return false; } @@ -1511,12 +1544,24 @@ namespace detail { MessageBuilder() = delete; ~MessageBuilder(); + // the preferred way of chaining parameters for stringification template - MessageBuilder& operator<<(const T& in) { + MessageBuilder& operator,(const T& in) { toStream(m_stream, in); return *this; } + // kept here just for backwards-compatibility - the comma operator should be preferred now + template + MessageBuilder& operator<<(const T& in) { return this->operator,(in); } + + // the `,` operator has the lowest operator precedence - if `<<` is used by the user then + // the `,` operator will be called last which is not what we want and thus the `*` operator + // is used first (has higher operator precedence compared to `<<`) so that we guarantee that + // an operator of the MessageBuilder class is called first before the rest of the parameters + template + MessageBuilder& operator*(const T& in) { return this->operator,(in); } + bool log(); void react(); }; @@ -1737,12 +1782,12 @@ int registerReporter(const char* name, int priority, bool isReporter) { #endif // DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS #ifdef DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS -#define DOCTEST_CAST_TO_VOID(x) \ +#define DOCTEST_CAST_TO_VOID(...) \ DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wuseless-cast") \ - static_cast(x); \ + static_cast(__VA_ARGS__); \ DOCTEST_GCC_SUPPRESS_WARNING_POP #else // DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS -#define DOCTEST_CAST_TO_VOID(x) x; +#define DOCTEST_CAST_TO_VOID(...) __VA_ARGS__; #endif // DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS // registers the test by initializing a dummy var with a function @@ -1932,38 +1977,38 @@ int registerReporter(const char* name, int priority, bool isReporter) { DOCTEST_GLOBAL_NO_WARNINGS_END() typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_) // for logging -#define DOCTEST_INFO(expression) \ +#define DOCTEST_INFO(...) \ DOCTEST_INFO_IMPL(DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), \ - DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), expression) + DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), __VA_ARGS__) -#define DOCTEST_INFO_IMPL(lambda_name, mb_name, s_name, expression) \ +#define DOCTEST_INFO_IMPL(lambda_name, mb_name, s_name, ...) \ DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4626) \ auto lambda_name = [&](std::ostream* s_name) { \ doctest::detail::MessageBuilder mb_name(__FILE__, __LINE__, doctest::assertType::is_warn); \ mb_name.m_stream = s_name; \ - mb_name << expression; \ + mb_name * __VA_ARGS__; \ }; \ DOCTEST_MSVC_SUPPRESS_WARNING_POP \ auto DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_) = doctest::detail::MakeContextScope(lambda_name) -#define DOCTEST_CAPTURE(x) DOCTEST_INFO(#x " := " << x) +#define DOCTEST_CAPTURE(x) DOCTEST_INFO(#x " := ", x) -#define DOCTEST_ADD_AT_IMPL(type, file, line, mb, x) \ +#define DOCTEST_ADD_AT_IMPL(type, file, line, mb, ...) \ do { \ doctest::detail::MessageBuilder mb(file, line, doctest::assertType::type); \ - mb << x; \ + mb * __VA_ARGS__; \ DOCTEST_ASSERT_LOG_AND_REACT(mb); \ - } while((void)0, 0) + } while(false) // clang-format off -#define DOCTEST_ADD_MESSAGE_AT(file, line, x) DOCTEST_ADD_AT_IMPL(is_warn, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), x) -#define DOCTEST_ADD_FAIL_CHECK_AT(file, line, x) DOCTEST_ADD_AT_IMPL(is_check, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), x) -#define DOCTEST_ADD_FAIL_AT(file, line, x) DOCTEST_ADD_AT_IMPL(is_require, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), x) +#define DOCTEST_ADD_MESSAGE_AT(file, line, ...) DOCTEST_ADD_AT_IMPL(is_warn, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), __VA_ARGS__) +#define DOCTEST_ADD_FAIL_CHECK_AT(file, line, ...) DOCTEST_ADD_AT_IMPL(is_check, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), __VA_ARGS__) +#define DOCTEST_ADD_FAIL_AT(file, line, ...) DOCTEST_ADD_AT_IMPL(is_require, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), __VA_ARGS__) // clang-format on -#define DOCTEST_MESSAGE(x) DOCTEST_ADD_MESSAGE_AT(__FILE__, __LINE__, x) -#define DOCTEST_FAIL_CHECK(x) DOCTEST_ADD_FAIL_CHECK_AT(__FILE__, __LINE__, x) -#define DOCTEST_FAIL(x) DOCTEST_ADD_FAIL_AT(__FILE__, __LINE__, x) +#define DOCTEST_MESSAGE(...) DOCTEST_ADD_MESSAGE_AT(__FILE__, __LINE__, __VA_ARGS__) +#define DOCTEST_FAIL_CHECK(...) DOCTEST_ADD_FAIL_CHECK_AT(__FILE__, __LINE__, __VA_ARGS__) +#define DOCTEST_FAIL(...) DOCTEST_ADD_FAIL_AT(__FILE__, __LINE__, __VA_ARGS__) #define DOCTEST_TO_LVALUE(...) __VA_ARGS__ // Not removed to keep backwards compatibility. @@ -1982,7 +2027,7 @@ int registerReporter(const char* name, int priority, bool isReporter) { #define DOCTEST_ASSERT_IMPLEMENT_1(assert_type, ...) \ do { \ DOCTEST_ASSERT_IMPLEMENT_2(assert_type, __VA_ARGS__); \ - } while((void)0, 0) + } while(false) #else // DOCTEST_CONFIG_SUPER_FAST_ASSERTS @@ -2006,12 +2051,12 @@ int registerReporter(const char* name, int priority, bool isReporter) { #define DOCTEST_REQUIRE_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_REQUIRE_FALSE, __VA_ARGS__) // clang-format off -#define DOCTEST_WARN_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(DT_WARN, cond); } while((void)0, 0) -#define DOCTEST_CHECK_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(DT_CHECK, cond); } while((void)0, 0) -#define DOCTEST_REQUIRE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(DT_REQUIRE, cond); } while((void)0, 0) -#define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(DT_WARN_FALSE, cond); } while((void)0, 0) -#define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(DT_CHECK_FALSE, cond); } while((void)0, 0) -#define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(DT_REQUIRE_FALSE, cond); } while((void)0, 0) +#define DOCTEST_WARN_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_WARN, cond); } while(false) +#define DOCTEST_CHECK_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_CHECK, cond); } while(false) +#define DOCTEST_REQUIRE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_REQUIRE, cond); } while(false) +#define DOCTEST_WARN_FALSE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_WARN_FALSE, cond); } while(false) +#define DOCTEST_CHECK_FALSE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_CHECK_FALSE, cond); } while(false) +#define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_REQUIRE_FALSE, cond); } while(false) // clang-format on #define DOCTEST_ASSERT_THROWS_AS(expr, assert_type, message, ...) \ @@ -2021,73 +2066,73 @@ int registerReporter(const char* name, int priority, bool isReporter) { __LINE__, #expr, #__VA_ARGS__, message); \ try { \ DOCTEST_CAST_TO_VOID(expr) \ - } catch(const doctest::detail::remove_const< \ - doctest::detail::remove_reference<__VA_ARGS__>::type>::type&) { \ + } catch(const typename doctest::detail::remove_const< \ + typename doctest::detail::remove_reference<__VA_ARGS__>::type>::type&) { \ _DOCTEST_RB.translateException(); \ _DOCTEST_RB.m_threw_as = true; \ } catch(...) { _DOCTEST_RB.translateException(); } \ DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \ } \ - } while((void)0, 0) + } while(false) -#define DOCTEST_ASSERT_THROWS_WITH(expr, assert_type, ...) \ +#define DOCTEST_ASSERT_THROWS_WITH(expr, expr_str, assert_type, ...) \ do { \ if(!doctest::getContextOptions()->no_throw) { \ doctest::detail::ResultBuilder _DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ - __LINE__, #expr, "", __VA_ARGS__); \ + __LINE__, expr_str, "", __VA_ARGS__); \ try { \ DOCTEST_CAST_TO_VOID(expr) \ } catch(...) { _DOCTEST_RB.translateException(); } \ DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \ } \ - } while((void)0, 0) + } while(false) -#define DOCTEST_ASSERT_NOTHROW(expr, assert_type) \ +#define DOCTEST_ASSERT_NOTHROW(assert_type, ...) \ do { \ doctest::detail::ResultBuilder _DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ - __LINE__, #expr); \ + __LINE__, #__VA_ARGS__); \ try { \ - DOCTEST_CAST_TO_VOID(expr) \ + DOCTEST_CAST_TO_VOID(__VA_ARGS__) \ } catch(...) { _DOCTEST_RB.translateException(); } \ DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \ - } while((void)0, 0) + } while(false) // clang-format off -#define DOCTEST_WARN_THROWS(expr) DOCTEST_ASSERT_THROWS_WITH(expr, DT_WARN_THROWS, "") -#define DOCTEST_CHECK_THROWS(expr) DOCTEST_ASSERT_THROWS_WITH(expr, DT_CHECK_THROWS, "") -#define DOCTEST_REQUIRE_THROWS(expr) DOCTEST_ASSERT_THROWS_WITH(expr, DT_REQUIRE_THROWS, "") +#define DOCTEST_WARN_THROWS(...) DOCTEST_ASSERT_THROWS_WITH((__VA_ARGS__), #__VA_ARGS__, DT_WARN_THROWS, "") +#define DOCTEST_CHECK_THROWS(...) DOCTEST_ASSERT_THROWS_WITH((__VA_ARGS__), #__VA_ARGS__, DT_CHECK_THROWS, "") +#define DOCTEST_REQUIRE_THROWS(...) DOCTEST_ASSERT_THROWS_WITH((__VA_ARGS__), #__VA_ARGS__, DT_REQUIRE_THROWS, "") #define DOCTEST_WARN_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_WARN_THROWS_AS, "", __VA_ARGS__) #define DOCTEST_CHECK_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_CHECK_THROWS_AS, "", __VA_ARGS__) #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_REQUIRE_THROWS_AS, "", __VA_ARGS__) -#define DOCTEST_WARN_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, DT_WARN_THROWS_WITH, __VA_ARGS__) -#define DOCTEST_CHECK_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, DT_CHECK_THROWS_WITH, __VA_ARGS__) -#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, DT_REQUIRE_THROWS_WITH, __VA_ARGS__) +#define DOCTEST_WARN_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, #expr, DT_WARN_THROWS_WITH, __VA_ARGS__) +#define DOCTEST_CHECK_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, #expr, DT_CHECK_THROWS_WITH, __VA_ARGS__) +#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, #expr, DT_REQUIRE_THROWS_WITH, __VA_ARGS__) #define DOCTEST_WARN_THROWS_WITH_AS(expr, message, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_WARN_THROWS_WITH_AS, message, __VA_ARGS__) #define DOCTEST_CHECK_THROWS_WITH_AS(expr, message, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_CHECK_THROWS_WITH_AS, message, __VA_ARGS__) #define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, message, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_REQUIRE_THROWS_WITH_AS, message, __VA_ARGS__) -#define DOCTEST_WARN_NOTHROW(expr) DOCTEST_ASSERT_NOTHROW(expr, DT_WARN_NOTHROW) -#define DOCTEST_CHECK_NOTHROW(expr) DOCTEST_ASSERT_NOTHROW(expr, DT_CHECK_NOTHROW) -#define DOCTEST_REQUIRE_NOTHROW(expr) DOCTEST_ASSERT_NOTHROW(expr, DT_REQUIRE_NOTHROW) - -#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_THROWS(expr); } while((void)0, 0) -#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_THROWS(expr); } while((void)0, 0) -#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_THROWS(expr); } while((void)0, 0) -#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_THROWS_AS(expr, ex); } while((void)0, 0) -#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_THROWS_AS(expr, ex); } while((void)0, 0) -#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_THROWS_AS(expr, ex); } while((void)0, 0) -#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_THROWS_WITH(expr, with); } while((void)0, 0) -#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_THROWS_WITH(expr, with); } while((void)0, 0) -#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_THROWS_WITH(expr, with); } while((void)0, 0) -#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_THROWS_WITH_AS(expr, with, ex); } while((void)0, 0) -#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ex); } while((void)0, 0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ex); } while((void)0, 0) -#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_NOTHROW(expr); } while((void)0, 0) -#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_NOTHROW(expr); } while((void)0, 0) -#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_NOTHROW(expr); } while((void)0, 0) +#define DOCTEST_WARN_NOTHROW(...) DOCTEST_ASSERT_NOTHROW(DT_WARN_NOTHROW, __VA_ARGS__) +#define DOCTEST_CHECK_NOTHROW(...) DOCTEST_ASSERT_NOTHROW(DT_CHECK_NOTHROW, __VA_ARGS__) +#define DOCTEST_REQUIRE_NOTHROW(...) DOCTEST_ASSERT_NOTHROW(DT_REQUIRE_NOTHROW, __VA_ARGS__) + +#define DOCTEST_WARN_THROWS_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS(expr); } while(false) +#define DOCTEST_CHECK_THROWS_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS(expr); } while(false) +#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS(expr); } while(false) +#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS_AS(expr, ex); } while(false) +#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS_AS(expr, ex); } while(false) +#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS_AS(expr, ex); } while(false) +#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS_WITH(expr, with); } while(false) +#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS_WITH(expr, with); } while(false) +#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS_WITH(expr, with); } while(false) +#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS_WITH_AS(expr, with, ex); } while(false) +#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ex); } while(false) +#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ex); } while(false) +#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_NOTHROW(expr); } while(false) +#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_NOTHROW(expr); } while(false) +#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_NOTHROW(expr); } while(false) // clang-format on #ifndef DOCTEST_CONFIG_SUPER_FAST_ASSERTS @@ -2100,7 +2145,7 @@ int registerReporter(const char* name, int priority, bool isReporter) { _DOCTEST_RB.binary_assert( \ __VA_ARGS__)) \ DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \ - } while((void)0, 0) + } while(false) #define DOCTEST_UNARY_ASSERT(assert_type, ...) \ do { \ @@ -2108,7 +2153,7 @@ int registerReporter(const char* name, int priority, bool isReporter) { __LINE__, #__VA_ARGS__); \ DOCTEST_WRAP_IN_TRY(_DOCTEST_RB.unary_assert(__VA_ARGS__)) \ DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \ - } while((void)0, 0) + } while(false) #else // DOCTEST_CONFIG_SUPER_FAST_ASSERTS @@ -2184,37 +2229,37 @@ int registerReporter(const char* name, int priority, bool isReporter) { #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS -#define DOCTEST_WARN_THROWS(expr) ((void)0) -#define DOCTEST_CHECK_THROWS(expr) ((void)0) -#define DOCTEST_REQUIRE_THROWS(expr) ((void)0) -#define DOCTEST_WARN_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_WARN_NOTHROW(expr) ((void)0) -#define DOCTEST_CHECK_NOTHROW(expr) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW(expr) ((void)0) - -#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) ((void)0) +#define DOCTEST_WARN_THROWS(...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS(...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS(...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_AS(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_AS(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) (static_cast(0)) +#define DOCTEST_WARN_NOTHROW(...) (static_cast(0)) +#define DOCTEST_CHECK_NOTHROW(...) (static_cast(0)) +#define DOCTEST_REQUIRE_NOTHROW(...) (static_cast(0)) + +#define DOCTEST_WARN_THROWS_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast(0)) +#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, ...) (static_cast(0)) #else // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS @@ -2305,86 +2350,86 @@ int registerReporter(const char* name, int priority, bool isReporter) { #define DOCTEST_REGISTER_REPORTER(name, priority, reporter) #define DOCTEST_REGISTER_LISTENER(name, priority, reporter) -#define DOCTEST_INFO(x) ((void)0) -#define DOCTEST_CAPTURE(x) ((void)0) -#define DOCTEST_ADD_MESSAGE_AT(file, line, x) ((void)0) -#define DOCTEST_ADD_FAIL_CHECK_AT(file, line, x) ((void)0) -#define DOCTEST_ADD_FAIL_AT(file, line, x) ((void)0) -#define DOCTEST_MESSAGE(x) ((void)0) -#define DOCTEST_FAIL_CHECK(x) ((void)0) -#define DOCTEST_FAIL(x) ((void)0) - -#define DOCTEST_WARN(...) ((void)0) -#define DOCTEST_CHECK(...) ((void)0) -#define DOCTEST_REQUIRE(...) ((void)0) -#define DOCTEST_WARN_FALSE(...) ((void)0) -#define DOCTEST_CHECK_FALSE(...) ((void)0) -#define DOCTEST_REQUIRE_FALSE(...) ((void)0) - -#define DOCTEST_WARN_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_CHECK_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_REQUIRE_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) ((void)0) - -#define DOCTEST_WARN_THROWS(expr) ((void)0) -#define DOCTEST_CHECK_THROWS(expr) ((void)0) -#define DOCTEST_REQUIRE_THROWS(expr) ((void)0) -#define DOCTEST_WARN_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_WARN_NOTHROW(expr) ((void)0) -#define DOCTEST_CHECK_NOTHROW(expr) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW(expr) ((void)0) - -#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) ((void)0) - -#define DOCTEST_WARN_EQ(...) ((void)0) -#define DOCTEST_CHECK_EQ(...) ((void)0) -#define DOCTEST_REQUIRE_EQ(...) ((void)0) -#define DOCTEST_WARN_NE(...) ((void)0) -#define DOCTEST_CHECK_NE(...) ((void)0) -#define DOCTEST_REQUIRE_NE(...) ((void)0) -#define DOCTEST_WARN_GT(...) ((void)0) -#define DOCTEST_CHECK_GT(...) ((void)0) -#define DOCTEST_REQUIRE_GT(...) ((void)0) -#define DOCTEST_WARN_LT(...) ((void)0) -#define DOCTEST_CHECK_LT(...) ((void)0) -#define DOCTEST_REQUIRE_LT(...) ((void)0) -#define DOCTEST_WARN_GE(...) ((void)0) -#define DOCTEST_CHECK_GE(...) ((void)0) -#define DOCTEST_REQUIRE_GE(...) ((void)0) -#define DOCTEST_WARN_LE(...) ((void)0) -#define DOCTEST_CHECK_LE(...) ((void)0) -#define DOCTEST_REQUIRE_LE(...) ((void)0) - -#define DOCTEST_WARN_UNARY(...) ((void)0) -#define DOCTEST_CHECK_UNARY(...) ((void)0) -#define DOCTEST_REQUIRE_UNARY(...) ((void)0) -#define DOCTEST_WARN_UNARY_FALSE(...) ((void)0) -#define DOCTEST_CHECK_UNARY_FALSE(...) ((void)0) -#define DOCTEST_REQUIRE_UNARY_FALSE(...) ((void)0) +#define DOCTEST_INFO(...) (static_cast(0)) +#define DOCTEST_CAPTURE(x) (static_cast(0)) +#define DOCTEST_ADD_MESSAGE_AT(file, line, ...) (static_cast(0)) +#define DOCTEST_ADD_FAIL_CHECK_AT(file, line, ...) (static_cast(0)) +#define DOCTEST_ADD_FAIL_AT(file, line, ...) (static_cast(0)) +#define DOCTEST_MESSAGE(...) (static_cast(0)) +#define DOCTEST_FAIL_CHECK(...) (static_cast(0)) +#define DOCTEST_FAIL(...) (static_cast(0)) + +#define DOCTEST_WARN(...) (static_cast(0)) +#define DOCTEST_CHECK(...) (static_cast(0)) +#define DOCTEST_REQUIRE(...) (static_cast(0)) +#define DOCTEST_WARN_FALSE(...) (static_cast(0)) +#define DOCTEST_CHECK_FALSE(...) (static_cast(0)) +#define DOCTEST_REQUIRE_FALSE(...) (static_cast(0)) + +#define DOCTEST_WARN_MESSAGE(cond, ...) (static_cast(0)) +#define DOCTEST_CHECK_MESSAGE(cond, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_MESSAGE(cond, ...) (static_cast(0)) +#define DOCTEST_WARN_FALSE_MESSAGE(cond, ...) (static_cast(0)) +#define DOCTEST_CHECK_FALSE_MESSAGE(cond, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, ...) (static_cast(0)) + +#define DOCTEST_WARN_THROWS(...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS(...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS(...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_AS(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_AS(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) (static_cast(0)) +#define DOCTEST_WARN_NOTHROW(...) (static_cast(0)) +#define DOCTEST_CHECK_NOTHROW(...) (static_cast(0)) +#define DOCTEST_REQUIRE_NOTHROW(...) (static_cast(0)) + +#define DOCTEST_WARN_THROWS_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast(0)) +#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast(0)) +#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, ...) (static_cast(0)) +#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, ...) (static_cast(0)) + +#define DOCTEST_WARN_EQ(...) (static_cast(0)) +#define DOCTEST_CHECK_EQ(...) (static_cast(0)) +#define DOCTEST_REQUIRE_EQ(...) (static_cast(0)) +#define DOCTEST_WARN_NE(...) (static_cast(0)) +#define DOCTEST_CHECK_NE(...) (static_cast(0)) +#define DOCTEST_REQUIRE_NE(...) (static_cast(0)) +#define DOCTEST_WARN_GT(...) (static_cast(0)) +#define DOCTEST_CHECK_GT(...) (static_cast(0)) +#define DOCTEST_REQUIRE_GT(...) (static_cast(0)) +#define DOCTEST_WARN_LT(...) (static_cast(0)) +#define DOCTEST_CHECK_LT(...) (static_cast(0)) +#define DOCTEST_REQUIRE_LT(...) (static_cast(0)) +#define DOCTEST_WARN_GE(...) (static_cast(0)) +#define DOCTEST_CHECK_GE(...) (static_cast(0)) +#define DOCTEST_REQUIRE_GE(...) (static_cast(0)) +#define DOCTEST_WARN_LE(...) (static_cast(0)) +#define DOCTEST_CHECK_LE(...) (static_cast(0)) +#define DOCTEST_REQUIRE_LE(...) (static_cast(0)) + +#define DOCTEST_WARN_UNARY(...) (static_cast(0)) +#define DOCTEST_CHECK_UNARY(...) (static_cast(0)) +#define DOCTEST_REQUIRE_UNARY(...) (static_cast(0)) +#define DOCTEST_WARN_UNARY_FALSE(...) (static_cast(0)) +#define DOCTEST_CHECK_UNARY_FALSE(...) (static_cast(0)) +#define DOCTEST_REQUIRE_UNARY_FALSE(...) (static_cast(0)) #endif // DOCTEST_CONFIG_DISABLE @@ -2724,9 +2769,7 @@ DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN #include #include #include -#ifdef DOCTEST_CONFIG_POSIX_SIGNALS #include -#endif // DOCTEST_CONFIG_POSIX_SIGNALS #include #include #include @@ -2751,7 +2794,11 @@ DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN #ifdef __AFXDLL #include #else +#if defined(__MINGW32__) || defined(__MINGW64__) +#include +#else // MINGW #include +#endif // MINGW #endif #include @@ -2762,6 +2809,12 @@ DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN #endif // DOCTEST_PLATFORM_WINDOWS +// this is a fix for https://github.com/onqtam/doctest/issues/348 +// https://mail.gnome.org/archives/xml/2012-January/msg00000.html +#if !defined(HAVE_UNISTD_H) && !defined(STDOUT_FILENO) +#define STDOUT_FILENO fileno(stdout) +#endif // HAVE_UNISTD_H + DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END // counts the number of elements in a C array @@ -2913,7 +2966,7 @@ typedef timer_large_integer::type ticks_t; //unsigned int getElapsedMilliseconds() const { // return static_cast(getElapsedMicroseconds() / 1000); //} - double getElapsedSeconds() const { return static_cast((getCurrentTicks() - m_ticks)) / 1000000.0; } + double getElapsedSeconds() const { return static_cast(getCurrentTicks() - m_ticks) / 1000000.0; } private: ticks_t m_ticks = 0; @@ -3031,6 +3084,7 @@ String::String() { String::~String() { if(!isOnStack()) delete[] data.ptr; + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) } String::String(const char* in) @@ -3072,6 +3126,7 @@ String& String::operator+=(const String& other) { if(total_size < len) { // append to the current stack space memcpy(buf + my_old_size, other.c_str(), other_size + 1); + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) setLast(last - total_size); } else { // alloc new chunk @@ -3113,6 +3168,7 @@ String& String::operator+=(const String& other) { return *this; } +// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) String String::operator+(const String& other) const { return String(*this) += other; } String::String(String&& other) { @@ -3267,6 +3323,7 @@ DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wnull-dereference") DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wnull-dereference") // depending on the current options this will remove the path of filenames const char* skipPathFromFilename(const char* file) { +#ifndef DOCTEST_CONFIG_DISABLE if(getContextOptions()->no_path_in_filenames) { auto back = std::strrchr(file, '\\'); auto forward = std::strrchr(file, '/'); @@ -3276,6 +3333,7 @@ const char* skipPathFromFilename(const char* file) { return forward + 1; } } +#endif // DOCTEST_CONFIG_DISABLE return file; } DOCTEST_CLANG_SUPPRESS_WARNING_POP @@ -3294,6 +3352,7 @@ IContextScope::~IContextScope() = default; #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING String toString(char* in) { return toString(static_cast(in)); } +// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) String toString(const char* in) { return String("\"") + (in ? in : "{null string}") + "\""; } #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING String toString(bool in) { return in ? "true" : "false"; } @@ -3366,6 +3425,7 @@ bool operator>(double lhs, const Approx& rhs) { return lhs > rhs.m_value && lhs bool operator>(const Approx& lhs, double rhs) { return lhs.m_value > rhs && lhs != rhs; } String toString(const Approx& in) { + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) return String("Approx( ") + doctest::toString(in.m_value) + " )"; } const ContextOptions* getContextOptions() { return DOCTEST_BRANCH_ON_DISABLED(nullptr, g_cs); } @@ -3450,7 +3510,7 @@ namespace detail { } #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS - [[noreturn]] void throwException() { + DOCTEST_NORETURN void throwException() { g_cs->shouldLogCurrentException = false; throw TestFailureException(); } // NOLINT(cert-err60-cpp) @@ -3464,8 +3524,8 @@ namespace { // matching of a string against a wildcard mask (case sensitivity configurable) taken from // https://www.codeproject.com/Articles/1088/Wildcard-string-compare-globbing int wildcmp(const char* str, const char* wild, bool caseSensitive) { - const char* cp = nullptr; - const char* mp = nullptr; + const char* cp = str; + const char* mp = wild; while((*str) && (*wild != '*')) { if((caseSensitive ? (*wild != *str) : (tolower(*wild) != tolower(*str))) && @@ -3554,6 +3614,10 @@ namespace detail { DOCTEST_ITERATE_THROUGH_REPORTERS(subcase_start, m_signature); } + DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4996) // std::uncaught_exception is deprecated in C++17 + DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") + DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") + Subcase::~Subcase() { if(m_entered) { // only mark the subcase stack as passed if no subcases have been skipped @@ -3561,7 +3625,7 @@ namespace detail { g_cs->subcasesPassed.insert(g_cs->subcasesStack); g_cs->subcasesStack.pop_back(); -#if __cplusplus >= 201703L && defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411 +#if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L if(std::uncaught_exceptions() > 0 #else if(std::uncaught_exception() @@ -3578,6 +3642,10 @@ namespace detail { } } + DOCTEST_CLANG_SUPPRESS_WARNING_POP + DOCTEST_GCC_SUPPRESS_WARNING_POP + DOCTEST_MSVC_SUPPRESS_WARNING_POP + Subcase::operator bool() const { return m_entered; } Result::Result(bool passed, const String& decomposition) @@ -3652,9 +3720,12 @@ namespace detail { bool TestCase::operator<(const TestCase& other) const { if(m_line != other.m_line) return m_line < other.m_line; - const int file_cmp = std::strcmp(m_file, other.m_file); + const int file_cmp = m_file.compare(other.m_file); if(file_cmp != 0) return file_cmp < 0; + const int name_cmp = strcmp(m_name, other.m_name); + if(name_cmp != 0) + return name_cmp < 0; return m_template_id < other.m_template_id; } } // namespace detail @@ -3662,13 +3733,9 @@ namespace { using namespace detail; // for sorting tests by file/line bool fileOrderComparator(const TestCase* lhs, const TestCase* rhs) { -#if DOCTEST_MSVC // this is needed because MSVC gives different case for drive letters // for __FILE__ when evaluated in a header and a source file - const int res = doctest::stricmp(lhs->m_file, rhs->m_file); -#else // MSVC - const int res = std::strcmp(lhs->m_file, rhs->m_file); -#endif // MSVC + const int res = lhs->m_file.compare(rhs->m_file, bool(DOCTEST_MSVC)); if(res != 0) return res < 0; if(lhs->m_line != rhs->m_line) @@ -3723,8 +3790,8 @@ namespace { DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") void color_to_stream(std::ostream& s, Color::Enum code) { - ((void)s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS - ((void)code); // for DOCTEST_CONFIG_COLORS_NONE + static_cast(s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS + static_cast(code); // for DOCTEST_CONFIG_COLORS_NONE #ifdef DOCTEST_CONFIG_COLORS_ANSI if(g_no_colors || (isatty(STDOUT_FILENO) == false && getContextOptions()->force_colors == false)) @@ -3830,7 +3897,28 @@ namespace detail { #ifdef DOCTEST_IS_DEBUGGER_ACTIVE bool isDebuggerActive() { return DOCTEST_IS_DEBUGGER_ACTIVE(); } #else // DOCTEST_IS_DEBUGGER_ACTIVE -#ifdef DOCTEST_PLATFORM_MAC +#ifdef DOCTEST_PLATFORM_LINUX + class ErrnoGuard { + public: + ErrnoGuard() : m_oldErrno(errno) {} + ~ErrnoGuard() { errno = m_oldErrno; } + private: + int m_oldErrno; + }; + // See the comments in Catch2 for the reasoning behind this implementation: + // https://github.com/catchorg/Catch2/blob/v2.13.1/include/internal/catch_debugger.cpp#L79-L102 + bool isDebuggerActive() { + ErrnoGuard guard; + std::ifstream in("/proc/self/status"); + for(std::string line; std::getline(in, line);) { + static const int PREFIX_LEN = 11; + if(line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0) { + return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; + } + } + return false; + } +#elif defined(DOCTEST_PLATFORM_MAC) // The following function is taken directly from the following technical note: // https://developer.apple.com/library/archive/qa/qa1361/_index.html // Returns true if the current process is being debugged (either @@ -3857,7 +3945,7 @@ namespace detail { // We're being debugged if the P_TRACED flag is set. return ((info.kp_proc.p_flag & P_TRACED) != 0); } -#elif DOCTEST_MSVC || defined(__MINGW32__) +#elif DOCTEST_MSVC || defined(__MINGW32__) || defined(__MINGW64__) bool isDebuggerActive() { return ::IsDebuggerPresent() != 0; } #else bool isDebuggerActive() { return false; } @@ -3897,11 +3985,15 @@ namespace detail { g_infoContexts.push_back(this); } + DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4996) // std::uncaught_exception is deprecated in C++17 + DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") + DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") + // destroy cannot be inlined into the destructor because that would mean calling stringify after // ContextScope has been destroyed (base class destructors run after derived class destructors). // Instead, ContextScope calls this method directly from its destructor. void ContextScopeBase::destroy() { -#if __cplusplus >= 201703L && defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411 +#if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L if(std::uncaught_exceptions() > 0) { #else if(std::uncaught_exception()) { @@ -3913,19 +4005,13 @@ namespace detail { g_infoContexts.pop_back(); } + DOCTEST_CLANG_SUPPRESS_WARNING_POP + DOCTEST_GCC_SUPPRESS_WARNING_POP + DOCTEST_MSVC_SUPPRESS_WARNING_POP } // namespace detail namespace { using namespace detail; - std::ostream& file_line_to_stream(std::ostream& s, const char* file, int line, - const char* tail = "") { - const auto opt = getContextOptions(); - s << Color::LightGrey << skipPathFromFilename(file) << (opt->gnu_file_line ? ":" : "(") - << (opt->no_line_numbers ? 0 : line) // 0 or the real num depending on the option - << (opt->gnu_file_line ? ":" : "):") << tail; - return s; - } - #if !defined(DOCTEST_CONFIG_POSIX_SIGNALS) && !defined(DOCTEST_CONFIG_WINDOWS_SEH) struct FatalConditionHandler { @@ -3946,10 +4032,12 @@ namespace { // Windows can easily distinguish between SO and SigSegV, // but SigInt, SigTerm, etc are handled differently. SignalDefs signalDefs[] = { - {EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal"}, - {EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow"}, - {EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal"}, - {EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error"}, + {static_cast(EXCEPTION_ILLEGAL_INSTRUCTION), + "SIGILL - Illegal instruction signal"}, + {static_cast(EXCEPTION_STACK_OVERFLOW), "SIGSEGV - Stack overflow"}, + {static_cast(EXCEPTION_ACCESS_VIOLATION), + "SIGSEGV - Segmentation violation signal"}, + {static_cast(EXCEPTION_INT_DIVIDE_BY_ZERO), "Divide by zero error"}, }; struct FatalConditionHandler @@ -3975,6 +4063,28 @@ namespace { previousTop = SetUnhandledExceptionFilter(handleException); // Pass in guarantee size to be filled SetThreadStackGuarantee(&guaranteeSize); + + // On Windows uncaught exceptions from another thread, exceptions from + // destructors, or calls to std::terminate are not a SEH exception + + // The terminal handler gets called when: + // - std::terminate is called FROM THE TEST RUNNER THREAD + // - an exception is thrown from a destructor FROM THE TEST RUNNER THREAD + original_terminate_handler = std::get_terminate(); + std::set_terminate([]() noexcept { + reportFatal("Terminate handler called"); + std::exit(EXIT_FAILURE); // explicitly exit - otherwise the SIGABRT handler may be called as well + }); + + // SIGABRT is raised when: + // - std::terminate is called FROM A DIFFERENT THREAD + // - an exception is thrown from a destructor FROM A DIFFERENT THREAD + // - an uncaught exception is thrown FROM A DIFFERENT THREAD + prev_sigabrt_handler = std::signal(SIGABRT, [](int signal) noexcept { + if(signal == SIGABRT) { + reportFatal("SIGABRT - Abort (abnormal termination) signal"); + } + }); } static void reset() { @@ -3984,17 +4094,23 @@ namespace { SetThreadStackGuarantee(&guaranteeSize); previousTop = nullptr; isSet = false; + std::set_terminate(original_terminate_handler); + std::signal(SIGABRT, prev_sigabrt_handler); } } ~FatalConditionHandler() { reset(); } private: + static void (*prev_sigabrt_handler)(int); + static std::terminate_handler original_terminate_handler; static bool isSet; static ULONG guaranteeSize; static LPTOP_LEVEL_EXCEPTION_FILTER previousTop; }; + void (*FatalConditionHandler::prev_sigabrt_handler)(int); + std::terminate_handler FatalConditionHandler::original_terminate_handler; bool FatalConditionHandler::isSet = false; ULONG FatalConditionHandler::guaranteeSize = 0; LPTOP_LEVEL_EXCEPTION_FILTER FatalConditionHandler::previousTop = nullptr; @@ -4194,6 +4310,7 @@ namespace detail { // ################################################################################### DOCTEST_ASSERT_OUT_OF_TESTS(result.m_decomp); DOCTEST_ASSERT_IN_TESTS(result.m_decomp); + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) } MessageBuilder::MessageBuilder(const char* file, int line, assertType::Enum severity) { @@ -4232,7 +4349,7 @@ namespace { using namespace detail; template - [[noreturn]] void throw_exception(Ex const& e) { + DOCTEST_NORETURN void throw_exception(Ex const& e) { #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS throw e; #else // DOCTEST_CONFIG_NO_EXCEPTIONS @@ -4242,9 +4359,11 @@ namespace { #endif // DOCTEST_CONFIG_NO_EXCEPTIONS } +#ifndef DOCTEST_INTERNAL_ERROR #define DOCTEST_INTERNAL_ERROR(msg) \ throw_exception(std::logic_error( \ __FILE__ ":" DOCTEST_TOSTR(__LINE__) ": Internal doctest error: " msg)) +#endif // DOCTEST_INTERNAL_ERROR // clang-format off @@ -4275,8 +4394,8 @@ namespace { public: ScopedElement( XmlWriter* writer ); - ScopedElement( ScopedElement&& other ) noexcept; - ScopedElement& operator=( ScopedElement&& other ) noexcept; + ScopedElement( ScopedElement&& other ) DOCTEST_NOEXCEPT; + ScopedElement& operator=( ScopedElement&& other ) DOCTEST_NOEXCEPT; ~ScopedElement(); @@ -4493,11 +4612,11 @@ namespace { : m_writer( writer ) {} - XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) noexcept + XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) DOCTEST_NOEXCEPT : m_writer( other.m_writer ){ other.m_writer = nullptr; } - XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) noexcept { + XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) DOCTEST_NOEXCEPT { if ( m_writer ) { m_writer->endElement(); } @@ -4676,7 +4795,7 @@ namespace { tc = ∈ xml.startElement("TestCase") .writeAttribute("name", in.m_name) - .writeAttribute("filename", skipPathFromFilename(in.m_file)) + .writeAttribute("filename", skipPathFromFilename(in.m_file.c_str())) .writeAttribute("line", line(in.m_line)) .writeAttribute("description", in.m_description); @@ -4707,7 +4826,7 @@ namespace { for(unsigned i = 0; i < in.num_data; ++i) { xml.scopedElement("TestCase").writeAttribute("name", in.data[i]->m_name) .writeAttribute("testsuite", in.data[i]->m_test_suite) - .writeAttribute("filename", skipPathFromFilename(in.data[i]->m_file)) + .writeAttribute("filename", skipPathFromFilename(in.data[i]->m_file.c_str())) .writeAttribute("line", line(in.data[i]->m_line)); } xml.scopedElement("OverallResultsTestCases") @@ -4863,6 +4982,277 @@ namespace { DOCTEST_REGISTER_REPORTER("xml", 0, XmlReporter); + void fulltext_log_assert_to_stream(std::ostream& s, const AssertData& rb) { + if((rb.m_at & (assertType::is_throws_as | assertType::is_throws_with)) == + 0) //!OCLINT bitwise operator in conditional + s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << " ) " + << Color::None; + + if(rb.m_at & assertType::is_throws) { //!OCLINT bitwise operator in conditional + s << (rb.m_threw ? "threw as expected!" : "did NOT throw at all!") << "\n"; + } else if((rb.m_at & assertType::is_throws_as) && + (rb.m_at & assertType::is_throws_with)) { //!OCLINT + s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \"" + << rb.m_exception_string << "\", " << rb.m_exception_type << " ) " << Color::None; + if(rb.m_threw) { + if(!rb.m_failed) { + s << "threw as expected!\n"; + } else { + s << "threw a DIFFERENT exception! (contents: " << rb.m_exception << ")\n"; + } + } else { + s << "did NOT throw at all!\n"; + } + } else if(rb.m_at & + assertType::is_throws_as) { //!OCLINT bitwise operator in conditional + s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", " + << rb.m_exception_type << " ) " << Color::None + << (rb.m_threw ? (rb.m_threw_as ? "threw as expected!" : + "threw a DIFFERENT exception: ") : + "did NOT throw at all!") + << Color::Cyan << rb.m_exception << "\n"; + } else if(rb.m_at & + assertType::is_throws_with) { //!OCLINT bitwise operator in conditional + s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \"" + << rb.m_exception_string << "\" ) " << Color::None + << (rb.m_threw ? (!rb.m_failed ? "threw as expected!" : + "threw a DIFFERENT exception: ") : + "did NOT throw at all!") + << Color::Cyan << rb.m_exception << "\n"; + } else if(rb.m_at & assertType::is_nothrow) { //!OCLINT bitwise operator in conditional + s << (rb.m_threw ? "THREW exception: " : "didn't throw!") << Color::Cyan + << rb.m_exception << "\n"; + } else { + s << (rb.m_threw ? "THREW exception: " : + (!rb.m_failed ? "is correct!\n" : "is NOT correct!\n")); + if(rb.m_threw) + s << rb.m_exception << "\n"; + else + s << " values: " << assertString(rb.m_at) << "( " << rb.m_decomp << " )\n"; + } + } + + // TODO: + // - log_message() + // - respond to queries + // - honor remaining options + // - more attributes in tags + struct JUnitReporter : public IReporter + { + XmlWriter xml; + std::mutex mutex; + Timer timer; + std::vector deepestSubcaseStackNames; + + struct JUnitTestCaseData + { +DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") // gmtime + static std::string getCurrentTimestamp() { + // Beware, this is not reentrant because of backward compatibility issues + // Also, UTC only, again because of backward compatibility (%z is C++11) + time_t rawtime; + std::time(&rawtime); + auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); + + std::tm* timeInfo; + timeInfo = std::gmtime(&rawtime); + + char timeStamp[timeStampSize]; + const char* const fmt = "%Y-%m-%dT%H:%M:%SZ"; + + std::strftime(timeStamp, timeStampSize, fmt, timeInfo); + return std::string(timeStamp); + } +DOCTEST_CLANG_SUPPRESS_WARNING_POP + + struct JUnitTestMessage + { + JUnitTestMessage(const std::string& _message, const std::string& _type, const std::string& _details) + : message(_message), type(_type), details(_details) {} + + JUnitTestMessage(const std::string& _message, const std::string& _details) + : message(_message), type(), details(_details) {} + + std::string message, type, details; + }; + + struct JUnitTestCase + { + JUnitTestCase(const std::string& _classname, const std::string& _name) + : classname(_classname), name(_name), time(0), failures() {} + + std::string classname, name; + double time; + std::vector failures, errors; + }; + + void add(const std::string& classname, const std::string& name) { + testcases.emplace_back(classname, name); + } + + void appendSubcaseNamesToLastTestcase(std::vector nameStack) { + for(auto& curr: nameStack) + if(curr.size()) + testcases.back().name += std::string("/") + curr.c_str(); + } + + void addTime(double time) { + if(time < 1e-4) + time = 0; + testcases.back().time = time; + totalSeconds += time; + } + + void addFailure(const std::string& message, const std::string& type, const std::string& details) { + testcases.back().failures.emplace_back(message, type, details); + ++totalFailures; + } + + void addError(const std::string& message, const std::string& details) { + testcases.back().errors.emplace_back(message, details); + ++totalErrors; + } + + std::vector testcases; + double totalSeconds = 0; + int totalErrors = 0, totalFailures = 0; + }; + + JUnitTestCaseData testCaseData; + + // caching pointers/references to objects of these types - safe to do + const ContextOptions& opt; + const TestCaseData* tc = nullptr; + + JUnitReporter(const ContextOptions& co) + : xml(*co.cout) + , opt(co) {} + + unsigned line(unsigned l) const { return opt.no_line_numbers ? 0 : l; } + + // ========================================================================================= + // WHAT FOLLOWS ARE OVERRIDES OF THE VIRTUAL METHODS OF THE REPORTER INTERFACE + // ========================================================================================= + + void report_query(const QueryData&) override {} + + void test_run_start() override {} + + void test_run_end(const TestRunStats& p) override { + // remove .exe extension - mainly to have the same output on UNIX and Windows + std::string binary_name = skipPathFromFilename(opt.binary_name.c_str()); +#ifdef DOCTEST_PLATFORM_WINDOWS + if(binary_name.rfind(".exe") != std::string::npos) + binary_name = binary_name.substr(0, binary_name.length() - 4); +#endif // DOCTEST_PLATFORM_WINDOWS + xml.startElement("testsuites"); + xml.startElement("testsuite").writeAttribute("name", binary_name) + .writeAttribute("errors", testCaseData.totalErrors) + .writeAttribute("failures", testCaseData.totalFailures) + .writeAttribute("tests", p.numAsserts); + if(opt.no_time_in_output == false) { + xml.writeAttribute("time", testCaseData.totalSeconds); + xml.writeAttribute("timestamp", JUnitTestCaseData::getCurrentTimestamp()); + } + if(opt.no_version == false) + xml.writeAttribute("doctest_version", DOCTEST_VERSION_STR); + + for(const auto& testCase : testCaseData.testcases) { + xml.startElement("testcase") + .writeAttribute("classname", testCase.classname) + .writeAttribute("name", testCase.name); + if(opt.no_time_in_output == false) + xml.writeAttribute("time", testCase.time); + // This is not ideal, but it should be enough to mimic gtest's junit output. + xml.writeAttribute("status", "run"); + + for(const auto& failure : testCase.failures) { + xml.scopedElement("failure") + .writeAttribute("message", failure.message) + .writeAttribute("type", failure.type) + .writeText(failure.details, false); + } + + for(const auto& error : testCase.errors) { + xml.scopedElement("error") + .writeAttribute("message", error.message) + .writeText(error.details); + } + + xml.endElement(); + } + xml.endElement(); + xml.endElement(); + } + + void test_case_start(const TestCaseData& in) override { + testCaseData.add(skipPathFromFilename(in.m_file.c_str()), in.m_name); + timer.start(); + } + + void test_case_reenter(const TestCaseData& in) override { + testCaseData.addTime(timer.getElapsedSeconds()); + testCaseData.appendSubcaseNamesToLastTestcase(deepestSubcaseStackNames); + deepestSubcaseStackNames.clear(); + + timer.start(); + testCaseData.add(skipPathFromFilename(in.m_file.c_str()), in.m_name); + } + + void test_case_end(const CurrentTestCaseStats&) override { + testCaseData.addTime(timer.getElapsedSeconds()); + testCaseData.appendSubcaseNamesToLastTestcase(deepestSubcaseStackNames); + deepestSubcaseStackNames.clear(); + } + + void test_case_exception(const TestCaseException& e) override { + std::lock_guard lock(mutex); + testCaseData.addError("exception", e.error_string.c_str()); + } + + void subcase_start(const SubcaseSignature& in) override { + std::lock_guard lock(mutex); + deepestSubcaseStackNames.push_back(in.m_name); + } + + void subcase_end() override {} + + void log_assert(const AssertData& rb) override { + if(!rb.m_failed) // report only failures & ignore the `success` option + return; + + std::lock_guard lock(mutex); + + std::ostringstream os; + os << skipPathFromFilename(rb.m_file) << (opt.gnu_file_line ? ":" : "(") + << line(rb.m_line) << (opt.gnu_file_line ? ":" : "):") << std::endl; + + fulltext_log_assert_to_stream(os, rb); + log_contexts(os); + testCaseData.addFailure(rb.m_decomp.c_str(), assertString(rb.m_at), os.str()); + } + + void log_message(const MessageData&) override {} + + void test_case_skipped(const TestCaseData&) override {} + + void log_contexts(std::ostringstream& s) { + int num_contexts = get_num_active_contexts(); + if(num_contexts) { + auto contexts = get_active_contexts(); + + s << " logged: "; + for(int i = 0; i < num_contexts; ++i) { + s << (i == 0 ? "" : " "); + contexts[i]->stringify(&s); + s << std::endl; + } + } + } + }; + + DOCTEST_REGISTER_REPORTER("junit", 0, JUnitReporter); + struct Whitespace { int nrSpaces; @@ -4881,6 +5271,7 @@ namespace { std::ostream& s; bool hasLoggedCurrentTestStart; std::vector subcasesStack; + size_t currentSubcaseLevel; std::mutex mutex; // caching pointers/references to objects of these types - safe to do @@ -4939,23 +5330,40 @@ namespace { s << "\n"; } + // this was requested to be made virtual so users could override it + virtual void file_line_to_stream(const char* file, int line, + const char* tail = "") { + s << Color::LightGrey << skipPathFromFilename(file) << (opt.gnu_file_line ? ":" : "(") + << (opt.no_line_numbers ? 0 : line) // 0 or the real num depending on the option + << (opt.gnu_file_line ? ":" : "):") << tail; + } + void logTestStart() { if(hasLoggedCurrentTestStart) return; separator_to_stream(); - file_line_to_stream(s, tc->m_file, tc->m_line, "\n"); + file_line_to_stream(tc->m_file.c_str(), tc->m_line, "\n"); if(tc->m_description) s << Color::Yellow << "DESCRIPTION: " << Color::None << tc->m_description << "\n"; if(tc->m_test_suite && tc->m_test_suite[0] != '\0') s << Color::Yellow << "TEST SUITE: " << Color::None << tc->m_test_suite << "\n"; if(strncmp(tc->m_name, " Scenario:", 11) != 0) - s << Color::None << "TEST CASE: "; + s << Color::Yellow << "TEST CASE: "; s << Color::None << tc->m_name << "\n"; - for(auto& curr : subcasesStack) - if(curr.m_name[0] != '\0') - s << " " << curr.m_name << "\n"; + for(size_t i = 0; i < currentSubcaseLevel; ++i) { + if(subcasesStack[i].m_name[0] != '\0') + s << " " << subcasesStack[i].m_name << "\n"; + } + + if(currentSubcaseLevel != subcasesStack.size()) { + s << Color::Yellow << "\nDEEPEST SUBCASE STACK REACHED (DIFFERENT FROM THE CURRENT ONE):\n" << Color::None; + for(size_t i = 0; i < subcasesStack.size(); ++i) { + if(subcasesStack[i].m_name[0] != '\0') + s << " " << subcasesStack[i].m_name << "\n"; + } + } s << "\n"; @@ -5166,25 +5574,28 @@ namespace { separator_to_stream(); s << std::dec; + auto totwidth = int(std::ceil(log10((std::max(p.numTestCasesPassingFilters, static_cast(p.numAsserts))) + 1))); + auto passwidth = int(std::ceil(log10((std::max(p.numTestCasesPassingFilters - p.numTestCasesFailed, static_cast(p.numAsserts - p.numAssertsFailed))) + 1))); + auto failwidth = int(std::ceil(log10((std::max(p.numTestCasesFailed, static_cast(p.numAssertsFailed))) + 1))); const bool anythingFailed = p.numTestCasesFailed > 0 || p.numAssertsFailed > 0; - s << Color::Cyan << "[doctest] " << Color::None << "test cases: " << std::setw(6) + s << Color::Cyan << "[doctest] " << Color::None << "test cases: " << std::setw(totwidth) << p.numTestCasesPassingFilters << " | " << ((p.numTestCasesPassingFilters == 0 || anythingFailed) ? Color::None : Color::Green) - << std::setw(6) << p.numTestCasesPassingFilters - p.numTestCasesFailed << " passed" + << std::setw(passwidth) << p.numTestCasesPassingFilters - p.numTestCasesFailed << " passed" << Color::None << " | " << (p.numTestCasesFailed > 0 ? Color::Red : Color::None) - << std::setw(6) << p.numTestCasesFailed << " failed" << Color::None << " | "; + << std::setw(failwidth) << p.numTestCasesFailed << " failed" << Color::None << " |"; if(opt.no_skipped_summary == false) { const int numSkipped = p.numTestCases - p.numTestCasesPassingFilters; - s << (numSkipped == 0 ? Color::None : Color::Yellow) << std::setw(6) << numSkipped + s << " " << (numSkipped == 0 ? Color::None : Color::Yellow) << numSkipped << " skipped" << Color::None; } s << "\n"; - s << Color::Cyan << "[doctest] " << Color::None << "assertions: " << std::setw(6) + s << Color::Cyan << "[doctest] " << Color::None << "assertions: " << std::setw(totwidth) << p.numAsserts << " | " << ((p.numAsserts == 0 || anythingFailed) ? Color::None : Color::Green) - << std::setw(6) << (p.numAsserts - p.numAssertsFailed) << " passed" << Color::None - << " | " << (p.numAssertsFailed > 0 ? Color::Red : Color::None) << std::setw(6) + << std::setw(passwidth) << (p.numAsserts - p.numAssertsFailed) << " passed" << Color::None + << " | " << (p.numAssertsFailed > 0 ? Color::Red : Color::None) << std::setw(failwidth) << p.numAssertsFailed << " failed" << Color::None << " |\n"; s << Color::Cyan << "[doctest] " << Color::None << "Status: " << (p.numTestCasesFailed > 0 ? Color::Red : Color::Green) @@ -5194,9 +5605,13 @@ namespace { void test_case_start(const TestCaseData& in) override { hasLoggedCurrentTestStart = false; tc = ∈ + subcasesStack.clear(); + currentSubcaseLevel = 0; } - void test_case_reenter(const TestCaseData&) override {} + void test_case_reenter(const TestCaseData&) override { + subcasesStack.clear(); + } void test_case_end(const CurrentTestCaseStats& st) override { // log the preamble of the test case only if there is something @@ -5235,7 +5650,7 @@ namespace { void test_case_exception(const TestCaseException& e) override { logTestStart(); - file_line_to_stream(s, tc->m_file, tc->m_line, " "); + file_line_to_stream(tc->m_file.c_str(), tc->m_line, " "); successOrFailColoredStringToStream(false, e.is_crash ? assertType::is_require : assertType::is_check); s << Color::Red << (e.is_crash ? "test case CRASHED: " : "test case THREW exception: ") @@ -5256,12 +5671,13 @@ namespace { void subcase_start(const SubcaseSignature& subc) override { std::lock_guard lock(mutex); subcasesStack.push_back(subc); + ++currentSubcaseLevel; hasLoggedCurrentTestStart = false; } void subcase_end() override { std::lock_guard lock(mutex); - subcasesStack.pop_back(); + --currentSubcaseLevel; hasLoggedCurrentTestStart = false; } @@ -5273,55 +5689,10 @@ namespace { logTestStart(); - file_line_to_stream(s, rb.m_file, rb.m_line, " "); + file_line_to_stream(rb.m_file, rb.m_line, " "); successOrFailColoredStringToStream(!rb.m_failed, rb.m_at); - if((rb.m_at & (assertType::is_throws_as | assertType::is_throws_with)) == - 0) //!OCLINT bitwise operator in conditional - s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << " ) " - << Color::None; - - if(rb.m_at & assertType::is_throws) { //!OCLINT bitwise operator in conditional - s << (rb.m_threw ? "threw as expected!" : "did NOT throw at all!") << "\n"; - } else if((rb.m_at & assertType::is_throws_as) && - (rb.m_at & assertType::is_throws_with)) { //!OCLINT - s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \"" - << rb.m_exception_string << "\", " << rb.m_exception_type << " ) " << Color::None; - if(rb.m_threw) { - if(!rb.m_failed) { - s << "threw as expected!\n"; - } else { - s << "threw a DIFFERENT exception! (contents: " << rb.m_exception << ")\n"; - } - } else { - s << "did NOT throw at all!\n"; - } - } else if(rb.m_at & - assertType::is_throws_as) { //!OCLINT bitwise operator in conditional - s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", " - << rb.m_exception_type << " ) " << Color::None - << (rb.m_threw ? (rb.m_threw_as ? "threw as expected!" : - "threw a DIFFERENT exception: ") : - "did NOT throw at all!") - << Color::Cyan << rb.m_exception << "\n"; - } else if(rb.m_at & - assertType::is_throws_with) { //!OCLINT bitwise operator in conditional - s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \"" - << rb.m_exception_string << "\" ) " << Color::None - << (rb.m_threw ? (!rb.m_failed ? "threw as expected!" : - "threw a DIFFERENT exception: ") : - "did NOT throw at all!") - << Color::Cyan << rb.m_exception << "\n"; - } else if(rb.m_at & assertType::is_nothrow) { //!OCLINT bitwise operator in conditional - s << (rb.m_threw ? "THREW exception: " : "didn't throw!") << Color::Cyan - << rb.m_exception << "\n"; - } else { - s << (rb.m_threw ? "THREW exception: " : - (!rb.m_failed ? "is correct!\n" : "is NOT correct!\n")); - if(rb.m_threw) - s << rb.m_exception << "\n"; - else - s << " values: " << assertString(rb.m_at) << "( " << rb.m_decomp << " )\n"; - } + + fulltext_log_assert_to_stream(s, rb); log_contexts(); } @@ -5331,7 +5702,7 @@ namespace { logTestStart(); - file_line_to_stream(s, mb.m_file, mb.m_line, " "); + file_line_to_stream(mb.m_file, mb.m_line, " "); s << getSuccessOrFailColor(false, mb.m_severity) << getSuccessOrFailString(mb.m_severity & assertType::is_warn, mb.m_severity, "MESSAGE") << ": "; @@ -5591,7 +5962,9 @@ void Context::parseArgs(int argc, const char* const* argv, bool withDefaults) { DOCTEST_PARSE_AS_BOOL_OR_FLAG("gnu-file-line", "gfl", gnu_file_line, !bool(DOCTEST_MSVC)); DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-path-filenames", "npf", no_path_in_filenames, false); DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-line-numbers", "nln", no_line_numbers, false); + DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-debug-output", "ndo", no_debug_output, false); DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-skipped-summary", "nss", no_skipped_summary, false); + DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-time-in-output", "ntio", no_time_in_output, false); // clang-format on if(withDefaults) { @@ -5647,6 +6020,7 @@ void Context::clearFilters() { // allows the user to override procedurally the int/bool options from the command line void Context::setOption(const char* option, int value) { setOption(option, toString(value).c_str()); + // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) } // allows the user to override procedurally the string options from the command line @@ -5722,7 +6096,7 @@ int Context::run() { p->reporters_currently_used.insert(p->reporters_currently_used.begin(), curr.second(*g_cs)); #ifdef DOCTEST_PLATFORM_WINDOWS - if(isDebuggerActive()) + if(isDebuggerActive() && p->no_debug_output == false) p->reporters_currently_used.push_back(new DebugOutputWindowReporter(*g_cs)); #endif // DOCTEST_PLATFORM_WINDOWS @@ -5778,9 +6152,9 @@ int Context::run() { if(tc.m_skip && !p->no_skip) skip_me = true; - if(!matchesAny(tc.m_file, p->filters[0], true, p->case_sensitive)) + if(!matchesAny(tc.m_file.c_str(), p->filters[0], true, p->case_sensitive)) skip_me = true; - if(matchesAny(tc.m_file, p->filters[1], false, p->case_sensitive)) + if(matchesAny(tc.m_file.c_str(), p->filters[1], false, p->case_sensitive)) skip_me = true; if(!matchesAny(tc.m_test_suite, p->filters[2], true, p->case_sensitive)) skip_me = true;