diff --git a/.clang-tidy b/.clang-tidy index a16136785..19c7dc642 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,3 +1,33 @@ +# Style or vendor rule sets that do not fit a header-only library: +# altera-*, fuchsia-*, llvmlibc-*, android-cloexec-fopen, boost-use-ranges, llvm-header-guard, +# llvm-include-order, llvm-prefer-static-over-anonymous-namespace, llvm-use-ranges, +# misc-use-anonymous-namespace, google-runtime-float, google-runtime-int, hicpp-signed-bitwise, +# bugprone-signed-bitwise, hicpp-explicit-conversions, google-explicit-constructor, +# cppcoreguidelines-explicit-constructor, misc-explicit-constructor (implicit conversions are the +# API), readability-trailing-comma, +# hicpp-avoid-goto / cppcoreguidelines-avoid-goto, hicpp-no-array-decay, +# hicpp-uppercase-literal-suffix, readability-uppercase-literal-suffix, modernize-use-trailing-return-type. +# Constructs the library needs on purpose (the library supports C++11 and legacy compilers): +# cppcoreguidelines-macro-usage, cppcoreguidelines-avoid-do-while (macros), +# cppcoreguidelines-pro-bounds-*, cppcoreguidelines-pro-type-reinterpret-cast, +# cppcoreguidelines-pro-type-union-access (parsers, serializers, basic_json's value union), +# cppcoreguidelines-avoid-const-or-ref-data-members, cppcoreguidelines-rvalue-reference-param-not-moved, +# cppcoreguidelines-avoid-non-const-global-variables, misc-non-private-member-variables-in-classes, +# misc-no-recursion (value walks are recursive, bounded by detail::recursion_depth_limit()), +# modernize-type-traits, modernize-use-constraints, modernize-use-ranges, modernize-use-std-numbers, +# modernize-use-designated-initializers, modernize-use-nodiscard, modernize-concat-nested-namespaces, +# readability-redundant-typename, readability-redundant-parentheses, +# readability-redundant-access-specifiers, readability-simplify-boolean-expr, +# readability-use-concise-preprocessor-directives, modernize-use-string-view, +# modernize-use-integer-sign-comparison (needs std::cmp_less; the flagged +# static_cast(end - pos) < n comparisons are deliberate) +# (all need a newer standard or compiler than C++11, or would change macro-generated or +# deliberately explicit code). +# Too noisy to be useful (numeric parsing/formatting code, doctest, tests): +# bugprone-easily-swappable-parameters, cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, +# readability-identifier-length, readability-function-cognitive-complexity, performance-enum-size, +# concurrency-mt-unsafe, misc-confusable-identifiers, misc-include-cleaner (run on its own for +# the consumer headers in cmake/ci.cmake). # portability-avoid-pragma-once: kept disabled on purpose. #pragma once is accepted # by every supported compiler, and tools/amalgamate/amalgamate.py strips it from # single_include, so there is nothing left to fix here. @@ -10,14 +40,14 @@ Checks: '*, -android-cloexec-fopen, -boost-use-ranges, -bugprone-easily-swappable-parameters, - -cert-err58-cpp, - -clang-analyzer-webkit.NoUncountedMemberChecker, + -bugprone-signed-bitwise, -concurrency-mt-unsafe, -cppcoreguidelines-avoid-const-or-ref-data-members, -cppcoreguidelines-avoid-do-while, -cppcoreguidelines-avoid-goto, -cppcoreguidelines-avoid-magic-numbers, -cppcoreguidelines-avoid-non-const-global-variables, + -cppcoreguidelines-explicit-constructor, -cppcoreguidelines-macro-usage, -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, @@ -26,19 +56,15 @@ Checks: '*, -cppcoreguidelines-pro-type-reinterpret-cast, -cppcoreguidelines-pro-type-union-access, -cppcoreguidelines-rvalue-reference-param-not-moved, - -cppcoreguidelines-virtual-class-destructor, -fuchsia-default-arguments-calls, -fuchsia-default-arguments-declarations, -fuchsia-overloaded-operator, -google-explicit-constructor, - -google-readability-function-size, -google-runtime-float, -google-runtime-int, -hicpp-avoid-goto, -hicpp-explicit-conversions, - -hicpp-function-size, -hicpp-no-array-decay, - -hicpp-no-assembler, -hicpp-signed-bitwise, -hicpp-uppercase-literal-suffix, -llvm-header-guard, @@ -48,6 +74,7 @@ Checks: '*, -llvmlibc-*, -misc-use-anonymous-namespace, -misc-confusable-identifiers, + -misc-explicit-constructor, -misc-include-cleaner, -misc-no-recursion, -misc-non-private-member-variables-in-classes, @@ -55,20 +82,22 @@ Checks: '*, -modernize-type-traits, -modernize-use-constraints, -modernize-use-designated-initializers, + -modernize-use-integer-sign-comparison, -modernize-use-nodiscard, -modernize-use-ranges, + -modernize-use-string-view, -modernize-use-std-numbers, -modernize-use-trailing-return-type, -performance-enum-size, -portability-avoid-pragma-once, -readability-function-cognitive-complexity, - -readability-function-size, -readability-identifier-length, -readability-magic-numbers, -readability-redundant-access-specifiers, -readability-redundant-parentheses, -readability-redundant-typename, -readability-simplify-boolean-expr, + -readability-trailing-comma, -readability-uppercase-literal-suffix, -readability-use-concise-preprocessor-directives' diff --git a/.github/workflows/flawfinder.yml b/.github/workflows/flawfinder.yml index e8cb9f71f..2aebc6306 100644 --- a/.github/workflows/flawfinder.yml +++ b/.github/workflows/flawfinder.yml @@ -43,7 +43,8 @@ jobs: - name: flawfinder_scan uses: david-a-wheeler/flawfinder@c4216b74cf2639ffa98503768bd6e4299b5440c9 # v2.0.20 with: - arguments: '--sarif ./' + # single_include/ is omitted: it is generated from include/ and would report every finding twice + arguments: '--sarif ./cmake ./docs ./include ./src ./tests ./tools' output: 'flawfinder_results.sarif' - name: Upload analysis results to GitHub Security tab diff --git a/README.md b/README.md index c38e7e12e..87c70fc96 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ The `json` class provides an API for manipulating a JSON value. To create a `jso ```cpp #include #include -using json = nlohmann::json; +using nlohmann::json; // ... @@ -164,7 +164,7 @@ If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`), this example beco import std; import nlohmann.json; -using json = nlohmann::json; +using nlohmann::json; // ... @@ -842,7 +842,7 @@ assert(p == p2); To make this work with one of your types, you only need to provide two functions: ```cpp -using json = nlohmann::json; +using nlohmann::json; namespace ns { void to_json(json& j, const person& p) { @@ -1257,7 +1257,7 @@ See the page [quality assurance](https://json.nlohmann.me/community/quality_assu #include // for convenience -using json = nlohmann::json; +using nlohmann::json; ``` to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). diff --git a/docs/mkdocs/docs/api/basic_json/get_ptr.md b/docs/mkdocs/docs/api/basic_json/get_ptr.md index 7c000f1ac..1a8d87e9d 100644 --- a/docs/mkdocs/docs/api/basic_json/get_ptr.md +++ b/docs/mkdocs/docs/api/basic_json/get_ptr.md @@ -45,7 +45,7 @@ Constant. #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_assert.md b/docs/mkdocs/docs/api/macros/json_assert.md index 5cd52a091..a8f198a7a 100644 --- a/docs/mkdocs/docs/api/macros/json_assert.md +++ b/docs/mkdocs/docs/api/macros/json_assert.md @@ -38,7 +38,7 @@ Therefore, assertions can be switched off by defining `NDEBUG`. ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -64,7 +64,7 @@ Therefore, assertions can be switched off by defining `NDEBUG`. #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_brace_init_copy_semantics.md b/docs/mkdocs/docs/api/macros/json_brace_init_copy_semantics.md index a038f8435..adab0769a 100644 --- a/docs/mkdocs/docs/api/macros/json_brace_init_copy_semantics.md +++ b/docs/mkdocs/docs/api/macros/json_brace_init_copy_semantics.md @@ -79,7 +79,7 @@ The default value is `0` (disabled — existing behavior is preserved). ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -98,7 +98,7 @@ The default value is `0` (disabled — existing behavior is preserved). #define JSON_BRACE_INIT_COPY_SEMANTICS 1 #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_delete_deprecated_functions.md b/docs/mkdocs/docs/api/macros/json_delete_deprecated_functions.md index 20f0ead0c..bae89d52a 100644 --- a/docs/mkdocs/docs/api/macros/json_delete_deprecated_functions.md +++ b/docs/mkdocs/docs/api/macros/json_delete_deprecated_functions.md @@ -54,7 +54,7 @@ The default value is `0` (disabled, the deprecated functions can still be called ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -72,7 +72,7 @@ The default value is `0` (disabled, the deprecated functions can still be called #define JSON_DELETE_DEPRECATED_FUNCTIONS 1 #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_disable_enum_serialization.md b/docs/mkdocs/docs/api/macros/json_disable_enum_serialization.md index 83c48515b..a0024d81a 100644 --- a/docs/mkdocs/docs/api/macros/json_disable_enum_serialization.md +++ b/docs/mkdocs/docs/api/macros/json_disable_enum_serialization.md @@ -39,7 +39,7 @@ The default value is `0`. #define JSON_DISABLE_ENUM_SERIALIZATION 1 #include - using json = nlohmann::json; + using nlohmann::json; enum class Choice { @@ -66,7 +66,7 @@ The default value is `0`. #define JSON_DISABLE_ENUM_SERIALIZATION 1 #include - using json = nlohmann::json; + using nlohmann::json; enum class Choice { @@ -99,7 +99,7 @@ The default value is `0`. #define JSON_DISABLE_ENUM_SERIALIZATION 1 #include - using json = nlohmann::json; + using nlohmann::json; enum class Choice { diff --git a/docs/mkdocs/docs/api/macros/json_disable_stream_bulk_scan.md b/docs/mkdocs/docs/api/macros/json_disable_stream_bulk_scan.md index 3da1ac635..6b45a0056 100644 --- a/docs/mkdocs/docs/api/macros/json_disable_stream_bulk_scan.md +++ b/docs/mkdocs/docs/api/macros/json_disable_stream_bulk_scan.md @@ -92,7 +92,7 @@ The default value is `0`: streams are scanned in bulk. #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -115,7 +115,7 @@ The default value is `0`: streams are scanned in bulk. #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_disable_tuple_reference_conversion.md b/docs/mkdocs/docs/api/macros/json_disable_tuple_reference_conversion.md index 0a2216b57..6d3aa003b 100644 --- a/docs/mkdocs/docs/api/macros/json_disable_tuple_reference_conversion.md +++ b/docs/mkdocs/docs/api/macros/json_disable_tuple_reference_conversion.md @@ -72,7 +72,7 @@ The default value is `0` (disabled — existing behavior is preserved). ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -89,7 +89,7 @@ The default value is `0` (disabled — existing behavior is preserved). #define JSON_DISABLE_TUPLE_REFERENCE_CONVERSION 1 #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_disable_unlocked_file_io.md b/docs/mkdocs/docs/api/macros/json_disable_unlocked_file_io.md index 6bb9d84a6..f0aa40b65 100644 --- a/docs/mkdocs/docs/api/macros/json_disable_unlocked_file_io.md +++ b/docs/mkdocs/docs/api/macros/json_disable_unlocked_file_io.md @@ -73,7 +73,7 @@ The default value is `0`: the file is locked once and read with the unlocked std #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -92,7 +92,7 @@ The default value is `0`: the file is locked once and read with the unlocked std #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_no_simd.md b/docs/mkdocs/docs/api/macros/json_no_simd.md index 96926c48b..9c92fcc1d 100644 --- a/docs/mkdocs/docs/api/macros/json_no_simd.md +++ b/docs/mkdocs/docs/api/macros/json_no_simd.md @@ -61,7 +61,7 @@ By default, `#!cpp JSON_NO_SIMD` is not defined, and vector instructions are use #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_precise_stream_position.md b/docs/mkdocs/docs/api/macros/json_precise_stream_position.md index 0d0114da3..f641a737a 100644 --- a/docs/mkdocs/docs/api/macros/json_precise_stream_position.md +++ b/docs/mkdocs/docs/api/macros/json_precise_stream_position.md @@ -94,7 +94,7 @@ The default value is `0` (disabled — existing behavior is preserved). #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -115,7 +115,7 @@ The default value is `0` (disabled — existing behavior is preserved). #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_strict_binary_utf8.md b/docs/mkdocs/docs/api/macros/json_strict_binary_utf8.md index 2369f5f4d..dadcb5fbe 100644 --- a/docs/mkdocs/docs/api/macros/json_strict_binary_utf8.md +++ b/docs/mkdocs/docs/api/macros/json_strict_binary_utf8.md @@ -64,7 +64,7 @@ The default value is `0` (disabled, the behavior of version 3.12.0 and earlier i ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -81,7 +81,7 @@ The default value is `0` (disabled, the behavior of version 3.12.0 and earlier i #define JSON_STRICT_BINARY_UTF8 1 #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_strict_nul_handling.md b/docs/mkdocs/docs/api/macros/json_strict_nul_handling.md index 69af3811d..806246053 100644 --- a/docs/mkdocs/docs/api/macros/json_strict_nul_handling.md +++ b/docs/mkdocs/docs/api/macros/json_strict_nul_handling.md @@ -92,7 +92,7 @@ The default value is `0` (disabled — existing behavior is preserved). ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -109,7 +109,7 @@ The default value is `0` (disabled — existing behavior is preserved). #define JSON_STRICT_NUL_HANDLING 1 #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/api/macros/json_use_objects_for_enum_keyed_maps.md b/docs/mkdocs/docs/api/macros/json_use_objects_for_enum_keyed_maps.md index ca4139c48..8fdf47982 100644 --- a/docs/mkdocs/docs/api/macros/json_use_objects_for_enum_keyed_maps.md +++ b/docs/mkdocs/docs/api/macros/json_use_objects_for_enum_keyed_maps.md @@ -77,7 +77,7 @@ The default value is `0` (disabled — existing behavior is preserved). #include #include - using json = nlohmann::json; + using nlohmann::json; enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED }; @@ -105,7 +105,7 @@ The default value is `0` (disabled — existing behavior is preserved). #include #include - using json = nlohmann::json; + using nlohmann::json; enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED }; diff --git a/docs/mkdocs/docs/community/quality_assurance.md b/docs/mkdocs/docs/community/quality_assurance.md index d55d32c16..6c8046964 100644 --- a/docs/mkdocs/docs/community/quality_assurance.md +++ b/docs/mkdocs/docs/community/quality_assurance.md @@ -188,10 +188,13 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa ``` - [x] The code is checked with the latest [Cppcheck](https://cppcheck.sourceforge.io) with all warnings enabled. -- [x] The code is checked with the latest [Clang Static Analyzer](https://clang-analyzer.llvm.org) with 89 enabled - rules. +- [x] The code is checked with the latest [Clang Static Analyzer](https://clang-analyzer.llvm.org) with 52 enabled + checkers. - [x] The code is checked with [Infer](https://fbinfer.com). - [x] The code is checked with [Codacy](https://app.codacy.com/gh/nlohmann/json/dashboard). +- [x] The code is scanned with [CodeQL](https://codeql.github.com), [Semgrep](https://semgrep.dev), and + [Flawfinder](https://dwheeler.com/flawfinder/) on every push and pull request and on a schedule; the findings are + reported to GitHub's code scanning. ## Dynamic analysis @@ -204,6 +207,8 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa - [x] The test suite is executed with [Valgrind](https://valgrind.org) (Memcheck) to detect memory leaks. - [x] The test suite is executed with [Sanitizers](https://github.com/google/sanitizers) (address sanitizer, undefined behavior sanitizer, integer overflow detection, nullability violations). +- [x] The fuzz targets are run with [CIFuzz](https://google.github.io/oss-fuzz/getting-started/continuous-integration/) + on every pull request to catch regressions before they are merged. ## Dependencies @@ -223,6 +228,8 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa No release is made while such an alert is open. - [x] Third-party code included in the repository for testing, such as [doctest](https://github.com/doctest/doctest), is updated manually. +- [x] The repository is evaluated with the [OpenSSF Scorecard](https://securityscorecards.dev) on a schedule and on + every push to the default branch, and the results are reported to GitHub's code scanning. ## Style check @@ -258,4 +265,21 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa - [x] The library is tested against relevant CMake versions: - CMake 3.5 (the earliest supported) - CMake 3.31.6 (the latest 3.x release) - - CMake 4.0.0 (a very recent release) + - CMake 4.0.0 (the first 4.x release) +!!! success "Requirement: Other build systems" + + The library can be used with build systems and package managers other than CMake. + +- [x] The library can be consumed with [Meson](https://json.nlohmann.me/integration/package_managers/) and the Swift + Package Manager; the installation with Meson and the resolution of `Package.swift` are tested in the CI. + +!!! success "Requirement: Reproducible and verifiable builds" + + The library can be built and tested without network access or a Git checkout, and its licensing is machine-readable. + +- [x] The test suite is executed from an exported source tree without the `.git` directory and with the test data + being available offline. +- [x] The test suite is executed without the tests that change installed files to check that builds are reproducible. +- [x] The repository is checked for compliance with the [REUSE specification](https://reuse.software) so that the + license and copyright of every file is machine-readable. +- [x] The documentation is built with warnings treated as errors, and its links are checked regularly. diff --git a/docs/mkdocs/docs/examples/README.cpp b/docs/mkdocs/docs/examples/README.cpp index 2d641e587..19d86a2b2 100644 --- a/docs/mkdocs/docs/examples/README.cpp +++ b/docs/mkdocs/docs/examples/README.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/accept__iterator_pair.cpp b/docs/mkdocs/docs/examples/accept__iterator_pair.cpp index dc7320de6..676ecb256 100644 --- a/docs/mkdocs/docs/examples/accept__iterator_pair.cpp +++ b/docs/mkdocs/docs/examples/accept__iterator_pair.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/accept__string.cpp b/docs/mkdocs/docs/examples/accept__string.cpp index 8eb3d9b75..dc97c9818 100644 --- a/docs/mkdocs/docs/examples/accept__string.cpp +++ b/docs/mkdocs/docs/examples/accept__string.cpp @@ -2,19 +2,19 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { // a valid JSON text - auto valid_text = R"( + const auto* valid_text = R"( { "numbers": [1, 2, 3] } )"; // an invalid JSON text - auto invalid_text = R"( + const auto* invalid_text = R"( { "strings": ["extra", "comma", ] } diff --git a/docs/mkdocs/docs/examples/accept__wide_string.cpp b/docs/mkdocs/docs/examples/accept__wide_string.cpp index 63dce67db..af25ae873 100644 --- a/docs/mkdocs/docs/examples/accept__wide_string.cpp +++ b/docs/mkdocs/docs/examples/accept__wide_string.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/array.cpp b/docs/mkdocs/docs/examples/array.cpp index 139b5ef10..b0ee3f932 100644 --- a/docs/mkdocs/docs/examples/array.cpp +++ b/docs/mkdocs/docs/examples/array.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/array_t.cpp b/docs/mkdocs/docs/examples/array_t.cpp index 0964857b7..c1196620e 100644 --- a/docs/mkdocs/docs/examples/array_t.cpp +++ b/docs/mkdocs/docs/examples/array_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same, json::array_t>::value << std::endl; + std::cout << std::boolalpha << std::is_same, json::array_t>::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/at__json_pointer.cpp b/docs/mkdocs/docs/examples/at__json_pointer.cpp index 15e91433d..23bb917c3 100644 --- a/docs/mkdocs/docs/examples/at__json_pointer.cpp +++ b/docs/mkdocs/docs/examples/at__json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/at__json_pointer_const.cpp b/docs/mkdocs/docs/examples/at__json_pointer_const.cpp index ab026e078..6e121b75d 100644 --- a/docs/mkdocs/docs/examples/at__json_pointer_const.cpp +++ b/docs/mkdocs/docs/examples/at__json_pointer_const.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/at__keytype.c++17.cpp b/docs/mkdocs/docs/examples/at__keytype.c++17.cpp index 032506acd..271158dd7 100644 --- a/docs/mkdocs/docs/examples/at__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/at__keytype.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/at__keytype_const.c++17.cpp b/docs/mkdocs/docs/examples/at__keytype_const.c++17.cpp index 712d456e6..948e46368 100644 --- a/docs/mkdocs/docs/examples/at__keytype_const.c++17.cpp +++ b/docs/mkdocs/docs/examples/at__keytype_const.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/at__object_t_key_type.cpp b/docs/mkdocs/docs/examples/at__object_t_key_type.cpp index e1f33ceca..6aaf1b4d0 100644 --- a/docs/mkdocs/docs/examples/at__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/at__object_t_key_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/at__object_t_key_type_const.cpp b/docs/mkdocs/docs/examples/at__object_t_key_type_const.cpp index b37bbd489..9cbf3c2c2 100644 --- a/docs/mkdocs/docs/examples/at__object_t_key_type_const.cpp +++ b/docs/mkdocs/docs/examples/at__object_t_key_type_const.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/at__size_type.cpp b/docs/mkdocs/docs/examples/at__size_type.cpp index 6527c6b16..ce6fdefeb 100644 --- a/docs/mkdocs/docs/examples/at__size_type.cpp +++ b/docs/mkdocs/docs/examples/at__size_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/at__size_type_const.cpp b/docs/mkdocs/docs/examples/at__size_type_const.cpp index 2080387a3..78d2d5f62 100644 --- a/docs/mkdocs/docs/examples/at__size_type_const.cpp +++ b/docs/mkdocs/docs/examples/at__size_type_const.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/back.cpp b/docs/mkdocs/docs/examples/back.cpp index 45342db13..6f1526cc4 100644 --- a/docs/mkdocs/docs/examples/back.cpp +++ b/docs/mkdocs/docs/examples/back.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__BasicJsonType.cpp b/docs/mkdocs/docs/examples/basic_json__BasicJsonType.cpp index 562d45b9f..9586832db 100644 --- a/docs/mkdocs/docs/examples/basic_json__BasicJsonType.cpp +++ b/docs/mkdocs/docs/examples/basic_json__BasicJsonType.cpp @@ -1,8 +1,8 @@ #include #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__CompatibleType.cpp b/docs/mkdocs/docs/examples/basic_json__CompatibleType.cpp index 130ef6aec..f1301b8d9 100644 --- a/docs/mkdocs/docs/examples/basic_json__CompatibleType.cpp +++ b/docs/mkdocs/docs/examples/basic_json__CompatibleType.cpp @@ -8,7 +8,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__InputIt_InputIt.cpp b/docs/mkdocs/docs/examples/basic_json__InputIt_InputIt.cpp index dec693c80..ece5a7fe4 100644 --- a/docs/mkdocs/docs/examples/basic_json__InputIt_InputIt.cpp +++ b/docs/mkdocs/docs/examples/basic_json__InputIt_InputIt.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__basic_json.cpp b/docs/mkdocs/docs/examples/basic_json__basic_json.cpp index 17136f437..481d43458 100644 --- a/docs/mkdocs/docs/examples/basic_json__basic_json.cpp +++ b/docs/mkdocs/docs/examples/basic_json__basic_json.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__copyassignment.cpp b/docs/mkdocs/docs/examples/basic_json__copyassignment.cpp index 2d8657400..e2b684e13 100644 --- a/docs/mkdocs/docs/examples/basic_json__copyassignment.cpp +++ b/docs/mkdocs/docs/examples/basic_json__copyassignment.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__list_init_t.cpp b/docs/mkdocs/docs/examples/basic_json__list_init_t.cpp index 78611e1a3..c678318dc 100644 --- a/docs/mkdocs/docs/examples/basic_json__list_init_t.cpp +++ b/docs/mkdocs/docs/examples/basic_json__list_init_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__moveconstructor.cpp b/docs/mkdocs/docs/examples/basic_json__moveconstructor.cpp index 48b68f61e..f8a84d091 100644 --- a/docs/mkdocs/docs/examples/basic_json__moveconstructor.cpp +++ b/docs/mkdocs/docs/examples/basic_json__moveconstructor.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__nullptr_t.cpp b/docs/mkdocs/docs/examples/basic_json__nullptr_t.cpp index 7a4366650..ed69b5707 100644 --- a/docs/mkdocs/docs/examples/basic_json__nullptr_t.cpp +++ b/docs/mkdocs/docs/examples/basic_json__nullptr_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__size_type_basic_json.cpp b/docs/mkdocs/docs/examples/basic_json__size_type_basic_json.cpp index 9ec767727..526ed5457 100644 --- a/docs/mkdocs/docs/examples/basic_json__size_type_basic_json.cpp +++ b/docs/mkdocs/docs/examples/basic_json__size_type_basic_json.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json__value_t.cpp b/docs/mkdocs/docs/examples/basic_json__value_t.cpp index c306731ac..b52b3a442 100644 --- a/docs/mkdocs/docs/examples/basic_json__value_t.cpp +++ b/docs/mkdocs/docs/examples/basic_json__value_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__accept.cpp b/docs/mkdocs/docs/examples/basic_json_document__accept.cpp index 675fa823f..e7273bab6 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__accept.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__accept.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__basic_json_document.cpp b/docs/mkdocs/docs/examples/basic_json_document__basic_json_document.cpp index 0c185f7b8..3d6741155 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__basic_json_document.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__basic_json_document.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__erase.cpp b/docs/mkdocs/docs/examples/basic_json_document__erase.cpp index 4b3197698..116eba8a9 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__erase.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__erase.cpp @@ -1,9 +1,9 @@ #include #include -using json = nlohmann::json; -using json_editable_document = nlohmann::json_editable_document; -using json_editable_view = nlohmann::json_editable_view; +using nlohmann::json; +using nlohmann::json_editable_document; +using nlohmann::json_editable_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__insert.cpp b/docs/mkdocs/docs/examples/basic_json_document__insert.cpp index 1c04bd6cc..714c4b98e 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__insert.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__insert.cpp @@ -1,9 +1,9 @@ #include #include -using json = nlohmann::json; -using json_editable_document = nlohmann::json_editable_document; -using json_editable_view = nlohmann::json_editable_view; +using nlohmann::json; +using nlohmann::json_editable_document; +using nlohmann::json_editable_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__is_discarded.cpp b/docs/mkdocs/docs/examples/basic_json_document__is_discarded.cpp index d64d37d23..62ce5b29a 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__is_discarded.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__is_discarded.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__load.cpp b/docs/mkdocs/docs/examples/basic_json_document__load.cpp index 52a085a10..03861deb6 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__load.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__load.cpp @@ -4,8 +4,8 @@ #include #include -using json = nlohmann::json; -using json_document = nlohmann::json_document; +using nlohmann::json; +using nlohmann::json_document; using image_check = json_document::image_check; int main() diff --git a/docs/mkdocs/docs/examples/basic_json_document__memory_usage.cpp b/docs/mkdocs/docs/examples/basic_json_document__memory_usage.cpp index efa235310..7e9e9136f 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__memory_usage.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__memory_usage.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__node_count.cpp b/docs/mkdocs/docs/examples/basic_json_document__node_count.cpp index d6ae5f89b..bb685bb51 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__node_count.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__node_count.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__owns_source.cpp b/docs/mkdocs/docs/examples/basic_json_document__owns_source.cpp index 8f442ea13..05bca7856 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__owns_source.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__owns_source.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__parse.cpp b/docs/mkdocs/docs/examples/basic_json_document__parse.cpp index 4eba7350d..b103bac85 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__parse.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__parse.cpp @@ -3,7 +3,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__parse_copy.cpp b/docs/mkdocs/docs/examples/basic_json_document__parse_copy.cpp index 16d3ce939..4770a4bfb 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__parse_copy.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__parse_copy.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; json_document parse_from_temporary_buffer() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__parse_iterator_pair.cpp b/docs/mkdocs/docs/examples/basic_json_document__parse_iterator_pair.cpp index 6d446bd67..ea0376254 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__parse_iterator_pair.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__parse_iterator_pair.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__push_back.cpp b/docs/mkdocs/docs/examples/basic_json_document__push_back.cpp index 16385ea99..e7a33a3e0 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__push_back.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__push_back.cpp @@ -1,8 +1,8 @@ #include #include -using json = nlohmann::json; -using json_editable_document = nlohmann::json_editable_document; +using nlohmann::json; +using nlohmann::json_editable_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__read.cpp b/docs/mkdocs/docs/examples/basic_json_document__read.cpp index 4858a7db7..7b9208da9 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__read.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__read.cpp @@ -3,7 +3,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__root.cpp b/docs/mkdocs/docs/examples/basic_json_document__root.cpp index 9789f2566..5b06ec6da 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__root.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__root.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__save.cpp b/docs/mkdocs/docs/examples/basic_json_document__save.cpp index 6a3cc978b..76eac5efe 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__save.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__save.cpp @@ -4,7 +4,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__set.cpp b/docs/mkdocs/docs/examples/basic_json_document__set.cpp index 7631a7b1b..ece572ee2 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__set.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__set.cpp @@ -1,9 +1,9 @@ #include #include -using json = nlohmann::json; -using json_editable_document = nlohmann::json_editable_document; -using json_editable_view = nlohmann::json_editable_view; +using nlohmann::json; +using nlohmann::json_editable_document; +using nlohmann::json_editable_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__shrink_to_fit.cpp b/docs/mkdocs/docs/examples/basic_json_document__shrink_to_fit.cpp index f2560078a..6ab9b6503 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__shrink_to_fit.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__shrink_to_fit.cpp @@ -3,7 +3,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_document__source.cpp b/docs/mkdocs/docs/examples/basic_json_document__source.cpp index 722cdffaf..12d40ee72 100644 --- a/docs/mkdocs/docs/examples/basic_json_document__source.cpp +++ b/docs/mkdocs/docs/examples/basic_json_document__source.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__at.cpp b/docs/mkdocs/docs/examples/basic_json_view__at.cpp index 1e984c55f..2cd390e36 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__at.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__at.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__at_json_pointer.cpp b/docs/mkdocs/docs/examples/basic_json_view__at_json_pointer.cpp index 47ed9419c..8f6088d84 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__at_json_pointer.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__at_json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; using json_pointer = nlohmann::json::json_pointer; int main() diff --git a/docs/mkdocs/docs/examples/basic_json_view__back.cpp b/docs/mkdocs/docs/examples/basic_json_view__back.cpp index 606cb8966..6fa64cbc0 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__back.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__back.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__begin.cpp b/docs/mkdocs/docs/examples/basic_json_view__begin.cpp index 5989ae404..71a119602 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__begin.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__begin.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp b/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp index 4e67a8041..70b532077 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp @@ -2,8 +2,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__cend.cpp b/docs/mkdocs/docs/examples/basic_json_view__cend.cpp index c72990673..d396f43bf 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__cend.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__cend.cpp @@ -2,8 +2,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__contains.cpp b/docs/mkdocs/docs/examples/basic_json_view__contains.cpp index e339ee79a..603bef166 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__contains.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__contains.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__contains_json_pointer.cpp b/docs/mkdocs/docs/examples/basic_json_view__contains_json_pointer.cpp index 260bac5eb..cb86f14bd 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__contains_json_pointer.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__contains_json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; using json_pointer = nlohmann::json::json_pointer; int main() diff --git a/docs/mkdocs/docs/examples/basic_json_view__count.cpp b/docs/mkdocs/docs/examples/basic_json_view__count.cpp index 75d82579f..414ff816f 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__count.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__count.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__dump.cpp b/docs/mkdocs/docs/examples/basic_json_view__dump.cpp index c9bd8198f..617e4307b 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__dump.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__dump.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__end.cpp b/docs/mkdocs/docs/examples/basic_json_view__end.cpp index 6808a6c95..cf5f6f421 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__end.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__end.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__find.cpp b/docs/mkdocs/docs/examples/basic_json_view__find.cpp index 06141b65a..47512fb27 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__find.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__find.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__front.cpp b/docs/mkdocs/docs/examples/basic_json_view__front.cpp index ed743f77e..079813c48 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__front.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__front.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__get.cpp b/docs/mkdocs/docs/examples/basic_json_view__get.cpp index 43e262035..e5ede47b9 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__get.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__get.cpp @@ -3,8 +3,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; // address has no direct conversion in get(), so get
() falls back // to materialize().get
() -- a real nlohmann::json value is built diff --git a/docs/mkdocs/docs/examples/basic_json_view__get_string.cpp b/docs/mkdocs/docs/examples/basic_json_view__get_string.cpp index 66ae1bbb3..1c459286b 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__get_string.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__get_string.cpp @@ -2,8 +2,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__get_to.cpp b/docs/mkdocs/docs/examples/basic_json_view__get_to.cpp index 748270759..ccab7e309 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__get_to.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__get_to.cpp @@ -2,8 +2,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__items.cpp b/docs/mkdocs/docs/examples/basic_json_view__items.cpp index 3f7779c19..aaaaafba5 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__items.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__items.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__materialize.cpp b/docs/mkdocs/docs/examples/basic_json_view__materialize.cpp index 38160b461..9d11cee95 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__materialize.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__materialize.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__number_format.cpp b/docs/mkdocs/docs/examples/basic_json_view__number_format.cpp index f67ea4e84..5ba989d40 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__number_format.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__number_format.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__number_token.cpp b/docs/mkdocs/docs/examples/basic_json_view__number_token.cpp index dec63371c..4ccd8d089 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__number_token.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__number_token.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__operator[].cpp b/docs/mkdocs/docs/examples/basic_json_view__operator[].cpp index 1919e06be..a454bf7c5 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__operator[].cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__operator[].cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__operator[]_json_pointer.cpp b/docs/mkdocs/docs/examples/basic_json_view__operator[]_json_pointer.cpp index 2ea8130a6..3010cb2b5 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__operator[]_json_pointer.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__operator[]_json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; using json_pointer = nlohmann::json::json_pointer; int main() diff --git a/docs/mkdocs/docs/examples/basic_json_view__operator_eq.cpp b/docs/mkdocs/docs/examples/basic_json_view__operator_eq.cpp index 846d40fc0..abea9086d 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__operator_eq.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__operator_eq.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json = nlohmann::json; +using nlohmann::json_document; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__operator_ltlt.cpp b/docs/mkdocs/docs/examples/basic_json_view__operator_ltlt.cpp index 97100d536..09afb9f6f 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__operator_ltlt.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__operator_ltlt.cpp @@ -2,8 +2,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__operator_ne.cpp b/docs/mkdocs/docs/examples/basic_json_view__operator_ne.cpp index faa1d61cf..24fe8927c 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__operator_ne.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__operator_ne.cpp @@ -1,9 +1,9 @@ #include #include -using json_document = nlohmann::json_document; -using ordered_json_document = nlohmann::ordered_json_document; -using json = nlohmann::json; +using nlohmann::json_document; +using nlohmann::ordered_json_document; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__size_empty.cpp b/docs/mkdocs/docs/examples/basic_json_view__size_empty.cpp index 36700ef9d..64a16c589 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__size_empty.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__size_empty.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__source_offset.cpp b/docs/mkdocs/docs/examples/basic_json_view__source_offset.cpp index 651964f0f..9ec340f1f 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__source_offset.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__source_offset.cpp @@ -2,7 +2,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp b/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp index 30661c0fd..7e7740877 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__type_predicates.cpp b/docs/mkdocs/docs/examples/basic_json_view__type_predicates.cpp index edc37ff52..210dbe230 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__type_predicates.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__type_predicates.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__value.cpp b/docs/mkdocs/docs/examples/basic_json_view__value.cpp index 1829730ef..252c80c57 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__value.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__value.cpp @@ -2,8 +2,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; int main() { diff --git a/docs/mkdocs/docs/examples/basic_json_view__value_json_pointer.cpp b/docs/mkdocs/docs/examples/basic_json_view__value_json_pointer.cpp index 605f389a7..e5b4448fa 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__value_json_pointer.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__value_json_pointer.cpp @@ -1,8 +1,8 @@ #include #include -using json_document = nlohmann::json_document; -using json_view = nlohmann::json_view; +using nlohmann::json_document; +using nlohmann::json_view; using json_pointer = nlohmann::json::json_pointer; int main() diff --git a/docs/mkdocs/docs/examples/begin.cpp b/docs/mkdocs/docs/examples/begin.cpp index 654835b02..6be31d569 100644 --- a/docs/mkdocs/docs/examples/begin.cpp +++ b/docs/mkdocs/docs/examples/begin.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/binary.cpp b/docs/mkdocs/docs/examples/binary.cpp index 617ce6096..6fff997bb 100644 --- a/docs/mkdocs/docs/examples/binary.cpp +++ b/docs/mkdocs/docs/examples/binary.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -12,5 +12,5 @@ int main() json j = json::binary(vec, 42); // output type and subtype - std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << std::endl; + std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << '\n'; } diff --git a/docs/mkdocs/docs/examples/binary_t.cpp b/docs/mkdocs/docs/examples/binary_t.cpp index bfaee5ca8..ed6196634 100644 --- a/docs/mkdocs/docs/examples/binary_t.cpp +++ b/docs/mkdocs/docs/examples/binary_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same>, json::binary_t>::value << std::endl; + std::cout << std::boolalpha << std::is_same>, json::binary_t>::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/boolean_t.cpp b/docs/mkdocs/docs/examples/boolean_t.cpp index 75b8c99f9..45d6aa385 100644 --- a/docs/mkdocs/docs/examples/boolean_t.cpp +++ b/docs/mkdocs/docs/examples/boolean_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same::value << std::endl; + std::cout << std::boolalpha << std::is_same::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__byte_container_with_subtype.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__byte_container_with_subtype.cpp index 1c10be5c2..bcb8d664a 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__byte_container_with_subtype.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__byte_container_with_subtype.cpp @@ -4,7 +4,7 @@ // define a byte container based on std::vector using byte_container_with_subtype = nlohmann::byte_container_with_subtype>; -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -19,5 +19,5 @@ int main() // (3) create container with subtype auto c3 = byte_container_with_subtype(bytes, 42); - std::cout << json(c1) << "\n" << json(c2) << "\n" << json(c3) << std::endl; + std::cout << json(c1) << "\n" << json(c2) << "\n" << json(c3) << '\n'; } diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__clear_subtype.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__clear_subtype.cpp index f9ce6842b..68a6767f7 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__clear_subtype.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__clear_subtype.cpp @@ -4,7 +4,7 @@ // define a byte container based on std::vector using byte_container_with_subtype = nlohmann::byte_container_with_subtype>; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__has_subtype.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__has_subtype.cpp index 61c21eaae..b393be891 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__has_subtype.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__has_subtype.cpp @@ -15,5 +15,5 @@ int main() auto c2 = byte_container_with_subtype(bytes, 42); std::cout << std::boolalpha << "c1.has_subtype() = " << c1.has_subtype() - << "\nc2.has_subtype() = " << c2.has_subtype() << std::endl; + << "\nc2.has_subtype() = " << c2.has_subtype() << '\n'; } diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__equal.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__equal.cpp index ef2a9cdcb..6b7f079a8 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__equal.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__equal.cpp @@ -19,5 +19,5 @@ int main() << "c1 == c2: " << (c1 == c2) << '\n' << "c1 == c3: " << (c1 == c3) << '\n' << "c3 == c4: " << (c3 == c4) << '\n' - << "c3 == c5: " << (c3 == c5) << std::endl; + << "c3 == c5: " << (c3 == c5) << '\n'; } diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__notequal.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__notequal.cpp index c7c104342..a3f6f9578 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__notequal.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__operator__notequal.cpp @@ -19,5 +19,5 @@ int main() << "c1 != c2: " << (c1 != c2) << '\n' << "c1 != c3: " << (c1 != c3) << '\n' << "c3 != c4: " << (c3 != c4) << '\n' - << "c3 != c5: " << (c3 != c5) << std::endl; + << "c3 != c5: " << (c3 != c5) << '\n'; } diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__set_subtype.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__set_subtype.cpp index b2694c54d..3b8123aec 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__set_subtype.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__set_subtype.cpp @@ -4,7 +4,7 @@ // define a byte container based on std::vector using byte_container_with_subtype = nlohmann::byte_container_with_subtype>; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/byte_container_with_subtype__subtype.cpp b/docs/mkdocs/docs/examples/byte_container_with_subtype__subtype.cpp index cd230ade1..b60e93e8b 100644 --- a/docs/mkdocs/docs/examples/byte_container_with_subtype__subtype.cpp +++ b/docs/mkdocs/docs/examples/byte_container_with_subtype__subtype.cpp @@ -15,7 +15,7 @@ int main() auto c2 = byte_container_with_subtype(bytes, 42); std::cout << "c1.subtype() = " << c1.subtype() - << "\nc2.subtype() = " << c2.subtype() << std::endl; + << "\nc2.subtype() = " << c2.subtype() << '\n'; // in case no subtype is set, return special value assert(c1.subtype() == static_cast(-1)); diff --git a/docs/mkdocs/docs/examples/cbegin.cpp b/docs/mkdocs/docs/examples/cbegin.cpp index bed2b3725..c0c66a36d 100644 --- a/docs/mkdocs/docs/examples/cbegin.cpp +++ b/docs/mkdocs/docs/examples/cbegin.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/cbor_tag_handler_t.cpp b/docs/mkdocs/docs/examples/cbor_tag_handler_t.cpp index 38d168ca8..78c03c282 100644 --- a/docs/mkdocs/docs/examples/cbor_tag_handler_t.cpp +++ b/docs/mkdocs/docs/examples/cbor_tag_handler_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,14 +15,14 @@ int main() } catch (const json::parse_error& e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << '\n'; } // cbor_tag_handler_t::ignore ignores the tag auto b_ignore_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::ignore); - std::cout << b_ignore_tag << std::endl; + std::cout << b_ignore_tag << '\n'; // cbor_tag_handler_t::store stores the tag as binary subtype auto b_store_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::store); - std::cout << b_store_tag << std::endl; + std::cout << b_store_tag << '\n'; } diff --git a/docs/mkdocs/docs/examples/cend.cpp b/docs/mkdocs/docs/examples/cend.cpp index 3050f5001..374bb3d17 100644 --- a/docs/mkdocs/docs/examples/cend.cpp +++ b/docs/mkdocs/docs/examples/cend.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/clear.cpp b/docs/mkdocs/docs/examples/clear.cpp index f081e7eda..b4a73dbcb 100644 --- a/docs/mkdocs/docs/examples/clear.cpp +++ b/docs/mkdocs/docs/examples/clear.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/comments.cpp b/docs/mkdocs/docs/examples/comments.cpp index fc8a0ac34..1e02a071d 100644 --- a/docs/mkdocs/docs/examples/comments.cpp +++ b/docs/mkdocs/docs/examples/comments.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -20,7 +20,7 @@ int main() } catch (json::exception& e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << '\n'; } json j = json::parse(s, diff --git a/docs/mkdocs/docs/examples/contains__json_pointer.cpp b/docs/mkdocs/docs/examples/contains__json_pointer.cpp index 985ad9ec5..1ace92a93 100644 --- a/docs/mkdocs/docs/examples/contains__json_pointer.cpp +++ b/docs/mkdocs/docs/examples/contains__json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -23,5 +23,5 @@ int main() // an array index with a leading '0' is not found << j.contains("/array/01"_json_pointer) << '\n' // an array index that is not a number is not found - << j.contains("/array/one"_json_pointer) << std::endl; + << j.contains("/array/one"_json_pointer) << '\n'; } diff --git a/docs/mkdocs/docs/examples/contains__keytype.c++17.cpp b/docs/mkdocs/docs/examples/contains__keytype.c++17.cpp index 43b62fab1..6b98b99b6 100644 --- a/docs/mkdocs/docs/examples/contains__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/contains__keytype.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -16,5 +16,5 @@ int main() std::cout << std::boolalpha << "j_object contains 'key': " << j_object.contains("key"sv) << '\n' << "j_object contains 'another': " << j_object.contains("another"sv) << '\n' << - "j_array contains 'key': " << j_array.contains("key"sv) << std::endl; + "j_array contains 'key': " << j_array.contains("key"sv) << '\n'; } diff --git a/docs/mkdocs/docs/examples/contains__object_t_key_type.cpp b/docs/mkdocs/docs/examples/contains__object_t_key_type.cpp index a8bc8143d..f72d81b6a 100644 --- a/docs/mkdocs/docs/examples/contains__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/contains__object_t_key_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -14,5 +14,5 @@ int main() std::cout << std::boolalpha << "j_object contains 'key': " << j_object.contains("key") << '\n' << "j_object contains 'another': " << j_object.contains("another") << '\n' << - "j_array contains 'key': " << j_array.contains("key") << std::endl; + "j_array contains 'key': " << j_array.contains("key") << '\n'; } diff --git a/docs/mkdocs/docs/examples/count__keytype.c++17.cpp b/docs/mkdocs/docs/examples/count__keytype.c++17.cpp index ec6de0607..0310e17e1 100644 --- a/docs/mkdocs/docs/examples/count__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/count__keytype.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/count__object_t_key_type.cpp b/docs/mkdocs/docs/examples/count__object_t_key_type.cpp index a8d54b9d1..9680f94c3 100644 --- a/docs/mkdocs/docs/examples/count__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/count__object_t_key_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/crbegin.cpp b/docs/mkdocs/docs/examples/crbegin.cpp index dc3209cf8..2ff1b41f8 100644 --- a/docs/mkdocs/docs/examples/crbegin.cpp +++ b/docs/mkdocs/docs/examples/crbegin.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/crend.cpp b/docs/mkdocs/docs/examples/crend.cpp index dff260931..298a69cb8 100644 --- a/docs/mkdocs/docs/examples/crend.cpp +++ b/docs/mkdocs/docs/examples/crend.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/custom_array_type.cpp b/docs/mkdocs/docs/examples/custom_array_type.cpp index 9765c51e8..fe60d575c 100644 --- a/docs/mkdocs/docs/examples/custom_array_type.cpp +++ b/docs/mkdocs/docs/examples/custom_array_type.cpp @@ -13,6 +13,6 @@ int main() j.push_back(2); j.push_back(3); - std::cout << j.dump() << std::endl; - std::cout << std::boolalpha << (custom_json::parse(j.dump()) == j) << std::endl; + std::cout << j.dump() << '\n'; + std::cout << std::boolalpha << (custom_json::parse(j.dump()) == j) << '\n'; } diff --git a/docs/mkdocs/docs/examples/custom_binary_type.cpp b/docs/mkdocs/docs/examples/custom_binary_type.cpp index 0a835d475..38356af6d 100644 --- a/docs/mkdocs/docs/examples/custom_binary_type.cpp +++ b/docs/mkdocs/docs/examples/custom_binary_type.cpp @@ -10,6 +10,6 @@ int main() { const auto j = custom_json::binary({0x01, 0x02, 0x03}); - std::cout << j.dump() << std::endl; - std::cout << std::boolalpha << (custom_json::from_cbor(custom_json::to_cbor(j)) == j) << std::endl; + std::cout << j.dump() << '\n'; + std::cout << std::boolalpha << (custom_json::from_cbor(custom_json::to_cbor(j)) == j) << '\n'; } diff --git a/docs/mkdocs/docs/examples/custom_object_type.cpp b/docs/mkdocs/docs/examples/custom_object_type.cpp index 9d4b43daf..d37363fc5 100644 --- a/docs/mkdocs/docs/examples/custom_object_type.cpp +++ b/docs/mkdocs/docs/examples/custom_object_type.cpp @@ -14,12 +14,12 @@ int main() j["happy"] = true; j["list"] = {1, 2, 3}; - std::cout << j.dump(2) << std::endl; - std::cout << std::boolalpha << (custom_json::parse(j.dump()) == j) << std::endl; + std::cout << j.dump(2) << '\n'; + std::cout << std::boolalpha << (custom_json::parse(j.dump()) == j) << '\n'; // custom_object_type has no key_compare member, so object_comparator_t // falls back to its default std::cout << std::boolalpha << std::is_same::value - << std::endl; + << '\n'; } diff --git a/docs/mkdocs/docs/examples/custom_string_type.cpp b/docs/mkdocs/docs/examples/custom_string_type.cpp index 2e71c9378..691848aca 100644 --- a/docs/mkdocs/docs/examples/custom_string_type.cpp +++ b/docs/mkdocs/docs/examples/custom_string_type.cpp @@ -13,6 +13,6 @@ int main() j["happy"] = true; j["list"] = {1, 2, 3}; - std::cout << j.dump(2) << std::endl; - std::cout << std::boolalpha << (custom_json::parse(j.dump()) == j) << std::endl; + std::cout << j.dump(2) << '\n'; + std::cout << std::boolalpha << (custom_json::parse(j.dump()) == j) << '\n'; } diff --git a/docs/mkdocs/docs/examples/default_object_comparator_t.cpp b/docs/mkdocs/docs/examples/default_object_comparator_t.cpp index 9f200fe6b..6cb4d4c22 100644 --- a/docs/mkdocs/docs/examples/default_object_comparator_t.cpp +++ b/docs/mkdocs/docs/examples/default_object_comparator_t.cpp @@ -1,11 +1,11 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { std::cout << std::boolalpha << "one < two : " << json::default_object_comparator_t{}("one", "two") << "\n" - << "three < four : " << json::default_object_comparator_t{}("three", "four") << std::endl; + << "three < four : " << json::default_object_comparator_t{}("three", "four") << '\n'; } diff --git a/docs/mkdocs/docs/examples/diagnostic_positions.cpp b/docs/mkdocs/docs/examples/diagnostic_positions.cpp index 259344cd0..5675a1297 100644 --- a/docs/mkdocs/docs/examples/diagnostic_positions.cpp +++ b/docs/mkdocs/docs/examples/diagnostic_positions.cpp @@ -3,7 +3,7 @@ #define JSON_DIAGNOSTIC_POSITIONS 1 #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/diagnostic_positions_exception.cpp b/docs/mkdocs/docs/examples/diagnostic_positions_exception.cpp index 085a25c19..599176364 100644 --- a/docs/mkdocs/docs/examples/diagnostic_positions_exception.cpp +++ b/docs/mkdocs/docs/examples/diagnostic_positions_exception.cpp @@ -3,7 +3,7 @@ #define JSON_DIAGNOSTIC_POSITIONS 1 #include -using json = nlohmann::json; +using nlohmann::json; /* Demonstration of type error exception with diagnostic positions support enabled */ int main() diff --git a/docs/mkdocs/docs/examples/diagnostics_extended.cpp b/docs/mkdocs/docs/examples/diagnostics_extended.cpp index 3b9f484b7..bac1a5363 100644 --- a/docs/mkdocs/docs/examples/diagnostics_extended.cpp +++ b/docs/mkdocs/docs/examples/diagnostics_extended.cpp @@ -3,7 +3,7 @@ # define JSON_DIAGNOSTICS 1 #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/diagnostics_extended_positions.cpp b/docs/mkdocs/docs/examples/diagnostics_extended_positions.cpp index 9a31ac356..22c8685a7 100644 --- a/docs/mkdocs/docs/examples/diagnostics_extended_positions.cpp +++ b/docs/mkdocs/docs/examples/diagnostics_extended_positions.cpp @@ -4,7 +4,7 @@ #define JSON_DIAGNOSTIC_POSITIONS 1 #include -using json = nlohmann::json; +using nlohmann::json; /* Demonstration of type error exception with diagnostic positions support enabled */ int main() diff --git a/docs/mkdocs/docs/examples/diagnostics_standard.cpp b/docs/mkdocs/docs/examples/diagnostics_standard.cpp index eae61a4a7..2c3537d2e 100644 --- a/docs/mkdocs/docs/examples/diagnostics_standard.cpp +++ b/docs/mkdocs/docs/examples/diagnostics_standard.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/diff.cpp b/docs/mkdocs/docs/examples/diff.cpp index ef01332a1..3b089e9a8 100644 --- a/docs/mkdocs/docs/examples/diff.cpp +++ b/docs/mkdocs/docs/examples/diff.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -33,5 +33,5 @@ int main() // output patch and roundtrip result std::cout << std::setw(4) << patch << "\n\n" - << std::setw(4) << patched_source << std::endl; + << std::setw(4) << patched_source << '\n'; } diff --git a/docs/mkdocs/docs/examples/dump.cpp b/docs/mkdocs/docs/examples/dump.cpp index 009c95fd7..c95381f9e 100644 --- a/docs/mkdocs/docs/examples/dump.cpp +++ b/docs/mkdocs/docs/examples/dump.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -33,11 +33,11 @@ int main() json j_invalid = "ä\xA9ü"; try { - std::cout << j_invalid.dump() << std::endl; + std::cout << j_invalid.dump() << '\n'; } catch (const json::type_error& e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << '\n'; } std::cout << "string with replaced invalid characters: " diff --git a/docs/mkdocs/docs/examples/emplace.cpp b/docs/mkdocs/docs/examples/emplace.cpp index a5314910f..93f665f39 100644 --- a/docs/mkdocs/docs/examples/emplace.cpp +++ b/docs/mkdocs/docs/examples/emplace.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/emplace_back.cpp b/docs/mkdocs/docs/examples/emplace_back.cpp index e979a945c..a9b055434 100644 --- a/docs/mkdocs/docs/examples/emplace_back.cpp +++ b/docs/mkdocs/docs/examples/emplace_back.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/empty.cpp b/docs/mkdocs/docs/examples/empty.cpp index 6ef6e40ed..7d92ebfed 100644 --- a/docs/mkdocs/docs/examples/empty.cpp +++ b/docs/mkdocs/docs/examples/empty.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/end.cpp b/docs/mkdocs/docs/examples/end.cpp index 47beedb77..01d4700bf 100644 --- a/docs/mkdocs/docs/examples/end.cpp +++ b/docs/mkdocs/docs/examples/end.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/erase__IteratorType.cpp b/docs/mkdocs/docs/examples/erase__IteratorType.cpp index f0d4ec6fb..1b1578ba9 100644 --- a/docs/mkdocs/docs/examples/erase__IteratorType.cpp +++ b/docs/mkdocs/docs/examples/erase__IteratorType.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/erase__IteratorType_IteratorType.cpp b/docs/mkdocs/docs/examples/erase__IteratorType_IteratorType.cpp index 392511ffe..f2b06d690 100644 --- a/docs/mkdocs/docs/examples/erase__IteratorType_IteratorType.cpp +++ b/docs/mkdocs/docs/examples/erase__IteratorType_IteratorType.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/erase__keytype.c++17.cpp b/docs/mkdocs/docs/examples/erase__keytype.c++17.cpp index c5e4bed5d..f1339941e 100644 --- a/docs/mkdocs/docs/examples/erase__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/erase__keytype.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/erase__object_t_key_type.cpp b/docs/mkdocs/docs/examples/erase__object_t_key_type.cpp index 2fd84c86e..140e77ef6 100644 --- a/docs/mkdocs/docs/examples/erase__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/erase__object_t_key_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/erase__size_type.cpp b/docs/mkdocs/docs/examples/erase__size_type.cpp index 810062387..b92fed88a 100644 --- a/docs/mkdocs/docs/examples/erase__size_type.cpp +++ b/docs/mkdocs/docs/examples/erase__size_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/error_handler_t.cpp b/docs/mkdocs/docs/examples/error_handler_t.cpp index bf035cea3..1b6945b47 100644 --- a/docs/mkdocs/docs/examples/error_handler_t.cpp +++ b/docs/mkdocs/docs/examples/error_handler_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -9,11 +9,11 @@ int main() json j_invalid = "ä\xA9ü"; try { - std::cout << j_invalid.dump() << std::endl; + std::cout << j_invalid.dump() << '\n'; } catch (const json::type_error& e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << '\n'; } std::cout << "string with replaced invalid characters: " diff --git a/docs/mkdocs/docs/examples/exception.cpp b/docs/mkdocs/docs/examples/exception.cpp index 3e5a23b3c..17ed0584b 100644 --- a/docs/mkdocs/docs/examples/exception.cpp +++ b/docs/mkdocs/docs/examples/exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,6 +15,6 @@ int main() { // output exception information std::cout << "message: " << e.what() << '\n' - << "exception id: " << e.id << std::endl; + << "exception id: " << e.id << '\n'; } } diff --git a/docs/mkdocs/docs/examples/find__keytype.c++17.cpp b/docs/mkdocs/docs/examples/find__keytype.c++17.cpp index da94cf0ad..f7efb5d6f 100644 --- a/docs/mkdocs/docs/examples/find__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/find__keytype.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/find__object_t_key_type.cpp b/docs/mkdocs/docs/examples/find__object_t_key_type.cpp index 685ba7763..e1e226cb4 100644 --- a/docs/mkdocs/docs/examples/find__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/find__object_t_key_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/flatten.cpp b/docs/mkdocs/docs/examples/flatten.cpp index 83f3ff6c3..a0ae57ec6 100644 --- a/docs/mkdocs/docs/examples/flatten.cpp +++ b/docs/mkdocs/docs/examples/flatten.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/flatten__empty.cpp b/docs/mkdocs/docs/examples/flatten__empty.cpp index 1dd52d551..b802c060d 100644 --- a/docs/mkdocs/docs/examples/flatten__empty.cpp +++ b/docs/mkdocs/docs/examples/flatten__empty.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/format_as.cpp b/docs/mkdocs/docs/examples/format_as.cpp index 0ea3a5ede..e392a6251 100644 --- a/docs/mkdocs/docs/examples/format_as.cpp +++ b/docs/mkdocs/docs/examples/format_as.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -12,5 +12,5 @@ int main() // fmt::format/fmt::print would find it auto j_str = format_as(j); - std::cout << j_str << std::endl; + std::cout << j_str << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_bjdata.cpp b/docs/mkdocs/docs/examples/from_bjdata.cpp index 961164c29..3de031c27 100644 --- a/docs/mkdocs/docs/examples/from_bjdata.cpp +++ b/docs/mkdocs/docs/examples/from_bjdata.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -16,5 +16,5 @@ int main() json j = json::from_bjdata(v); // print the deserialized JSON value - std::cout << std::setw(2) << j << std::endl; + std::cout << std::setw(2) << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_bon8.cpp b/docs/mkdocs/docs/examples/from_bon8.cpp index 83ab82060..320f5b3a0 100644 --- a/docs/mkdocs/docs/examples/from_bon8.cpp +++ b/docs/mkdocs/docs/examples/from_bon8.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -17,5 +17,5 @@ int main() json j = json::from_bon8(v); // print the deserialized JSON value - std::cout << std::setw(2) << j << std::endl; + std::cout << std::setw(2) << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_bson.cpp b/docs/mkdocs/docs/examples/from_bson.cpp index c9d9fdfa6..8d7d2f951 100644 --- a/docs/mkdocs/docs/examples/from_bson.cpp +++ b/docs/mkdocs/docs/examples/from_bson.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -17,5 +17,5 @@ int main() json j = json::from_bson(v); // print the deserialized JSON value - std::cout << std::setw(2) << j << std::endl; + std::cout << std::setw(2) << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_cbor.cpp b/docs/mkdocs/docs/examples/from_cbor.cpp index e685329ef..4ef881acb 100644 --- a/docs/mkdocs/docs/examples/from_cbor.cpp +++ b/docs/mkdocs/docs/examples/from_cbor.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -16,5 +16,5 @@ int main() json j = json::from_cbor(v); // print the deserialized JSON value - std::cout << std::setw(2) << j << std::endl; + std::cout << std::setw(2) << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_json__default_constructible.cpp b/docs/mkdocs/docs/examples/from_json__default_constructible.cpp index 17c0551c8..33c75f95a 100644 --- a/docs/mkdocs/docs/examples/from_json__default_constructible.cpp +++ b/docs/mkdocs/docs/examples/from_json__default_constructible.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -33,5 +33,5 @@ int main() auto p = j.get(); - std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl; + std::cout << p.name << " (" << p.age << ") lives in " << p.address << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_json__non_default_constructible.cpp b/docs/mkdocs/docs/examples/from_json__non_default_constructible.cpp index 6cb86153c..73eba7584 100644 --- a/docs/mkdocs/docs/examples/from_json__non_default_constructible.cpp +++ b/docs/mkdocs/docs/examples/from_json__non_default_constructible.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -49,5 +49,5 @@ int main() auto p = j.get(); - std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl; + std::cout << p.name << " (" << p.age << ") lives in " << p.address << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_msgpack.cpp b/docs/mkdocs/docs/examples/from_msgpack.cpp index 5c2183f1e..f2a1db51e 100644 --- a/docs/mkdocs/docs/examples/from_msgpack.cpp +++ b/docs/mkdocs/docs/examples/from_msgpack.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -16,5 +16,5 @@ int main() json j = json::from_msgpack(v); // print the deserialized JSON value - std::cout << std::setw(2) << j << std::endl; + std::cout << std::setw(2) << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/from_ubjson.cpp b/docs/mkdocs/docs/examples/from_ubjson.cpp index 1e85e4e36..9046ac406 100644 --- a/docs/mkdocs/docs/examples/from_ubjson.cpp +++ b/docs/mkdocs/docs/examples/from_ubjson.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -16,5 +16,5 @@ int main() json j = json::from_ubjson(v); // print the deserialized JSON value - std::cout << std::setw(2) << j << std::endl; + std::cout << std::setw(2) << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/front.cpp b/docs/mkdocs/docs/examples/front.cpp index a0f630689..4f53be276 100644 --- a/docs/mkdocs/docs/examples/front.cpp +++ b/docs/mkdocs/docs/examples/front.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/get__BasicJsonType.cpp b/docs/mkdocs/docs/examples/get__BasicJsonType.cpp index d63458c0b..fb70d402e 100644 --- a/docs/mkdocs/docs/examples/get__BasicJsonType.cpp +++ b/docs/mkdocs/docs/examples/get__BasicJsonType.cpp @@ -1,8 +1,8 @@ #include #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; int main() { diff --git a/docs/mkdocs/docs/examples/get__PointerType.cpp b/docs/mkdocs/docs/examples/get__PointerType.cpp index 2f32ed7af..aa7440482 100644 --- a/docs/mkdocs/docs/examples/get__PointerType.cpp +++ b/docs/mkdocs/docs/examples/get__PointerType.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -9,11 +9,11 @@ int main() json value = 17; // explicitly getting pointers - auto p1 = value.get(); - auto p2 = value.get(); - auto p3 = value.get(); - auto p4 = value.get(); - auto p5 = value.get(); + const auto* p1 = value.get(); + auto* p2 = value.get(); + auto* p3 = value.get(); + const auto* p4 = value.get(); + auto* p5 = value.get(); // print the pointees std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n'; diff --git a/docs/mkdocs/docs/examples/get__ValueType_const.cpp b/docs/mkdocs/docs/examples/get__ValueType_const.cpp index 7b0ca47bb..19d9b8d9f 100644 --- a/docs/mkdocs/docs/examples/get__ValueType_const.cpp +++ b/docs/mkdocs/docs/examples/get__ValueType_const.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -43,7 +43,7 @@ int main() } std::cout << "\n\n"; - for (auto i : v8) + for (const auto& i : v8) { std::cout << i.first << ": " << i.second << '\n'; } diff --git a/docs/mkdocs/docs/examples/get_allocator.cpp b/docs/mkdocs/docs/examples/get_allocator.cpp index 35079a10c..186b102a8 100644 --- a/docs/mkdocs/docs/examples/get_allocator.cpp +++ b/docs/mkdocs/docs/examples/get_allocator.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -11,7 +11,7 @@ int main() json* j = traits_t::allocate(alloc, 1); traits_t::construct(alloc, j, "Hello, world!"); - std::cout << *j << std::endl; + std::cout << *j << '\n'; traits_t::destroy(alloc, j); traits_t::deallocate(alloc, j, 1); diff --git a/docs/mkdocs/docs/examples/get_binary.cpp b/docs/mkdocs/docs/examples/get_binary.cpp index 617ce6096..6fff997bb 100644 --- a/docs/mkdocs/docs/examples/get_binary.cpp +++ b/docs/mkdocs/docs/examples/get_binary.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -12,5 +12,5 @@ int main() json j = json::binary(vec, 42); // output type and subtype - std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << std::endl; + std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << '\n'; } diff --git a/docs/mkdocs/docs/examples/get_ptr.cpp b/docs/mkdocs/docs/examples/get_ptr.cpp index 564ce0f4e..0826523b9 100644 --- a/docs/mkdocs/docs/examples/get_ptr.cpp +++ b/docs/mkdocs/docs/examples/get_ptr.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -9,11 +9,11 @@ int main() json value = 17; // explicitly getting pointers - auto p1 = value.get_ptr(); - auto p2 = value.get_ptr(); - auto p3 = value.get_ptr(); - auto p4 = value.get_ptr(); - auto p5 = value.get_ptr(); + const auto* p1 = value.get_ptr(); + auto* p2 = value.get_ptr(); + auto* p3 = value.get_ptr(); + const auto* p4 = value.get_ptr(); + auto* p5 = value.get_ptr(); // print the pointees std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n'; diff --git a/docs/mkdocs/docs/examples/get_ref.cpp b/docs/mkdocs/docs/examples/get_ref.cpp index 0183a6537..706b45708 100644 --- a/docs/mkdocs/docs/examples/get_ref.cpp +++ b/docs/mkdocs/docs/examples/get_ref.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/get_to.cpp b/docs/mkdocs/docs/examples/get_to.cpp index cdbe347c0..553c80c06 100644 --- a/docs/mkdocs/docs/examples/get_to.cpp +++ b/docs/mkdocs/docs/examples/get_to.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -52,7 +52,7 @@ int main() } std::cout << "\n\n"; - for (auto i : v8) + for (const auto& i : v8) { std::cout << i.first << ": " << i.second << '\n'; } diff --git a/docs/mkdocs/docs/examples/insert.cpp b/docs/mkdocs/docs/examples/insert.cpp index 4ee609876..de6b03d61 100644 --- a/docs/mkdocs/docs/examples/insert.cpp +++ b/docs/mkdocs/docs/examples/insert.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/insert__count.cpp b/docs/mkdocs/docs/examples/insert__count.cpp index ce33b93e4..43e1e3164 100644 --- a/docs/mkdocs/docs/examples/insert__count.cpp +++ b/docs/mkdocs/docs/examples/insert__count.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/insert__ilist.cpp b/docs/mkdocs/docs/examples/insert__ilist.cpp index a20766a14..9415b6887 100644 --- a/docs/mkdocs/docs/examples/insert__ilist.cpp +++ b/docs/mkdocs/docs/examples/insert__ilist.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/insert__range.cpp b/docs/mkdocs/docs/examples/insert__range.cpp index 92fe63b0d..69bf25069 100644 --- a/docs/mkdocs/docs/examples/insert__range.cpp +++ b/docs/mkdocs/docs/examples/insert__range.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/insert__range_object.cpp b/docs/mkdocs/docs/examples/insert__range_object.cpp index 97373d3b4..f2cc9627c 100644 --- a/docs/mkdocs/docs/examples/insert__range_object.cpp +++ b/docs/mkdocs/docs/examples/insert__range_object.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/invalid_iterator.cpp b/docs/mkdocs/docs/examples/invalid_iterator.cpp index ecde12e62..138fdbc7e 100644 --- a/docs/mkdocs/docs/examples/invalid_iterator.cpp +++ b/docs/mkdocs/docs/examples/invalid_iterator.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -16,6 +16,6 @@ int main() { // output exception information std::cout << "message: " << e.what() << '\n' - << "exception id: " << e.id << std::endl; + << "exception id: " << e.id << '\n'; } } diff --git a/docs/mkdocs/docs/examples/is_array.cpp b/docs/mkdocs/docs/examples/is_array.cpp index 8ecc45035..c1ef39b8e 100644 --- a/docs/mkdocs/docs/examples/is_array.cpp +++ b/docs/mkdocs/docs/examples/is_array.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_binary.cpp b/docs/mkdocs/docs/examples/is_binary.cpp index d7f049ec4..6fe4d5006 100644 --- a/docs/mkdocs/docs/examples/is_binary.cpp +++ b/docs/mkdocs/docs/examples/is_binary.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_boolean.cpp b/docs/mkdocs/docs/examples/is_boolean.cpp index 0b7981955..e48d1ef5c 100644 --- a/docs/mkdocs/docs/examples/is_boolean.cpp +++ b/docs/mkdocs/docs/examples/is_boolean.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_discarded.cpp b/docs/mkdocs/docs/examples/is_discarded.cpp index 09016655e..9bfcba039 100644 --- a/docs/mkdocs/docs/examples/is_discarded.cpp +++ b/docs/mkdocs/docs/examples/is_discarded.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_discarded__parse.cpp b/docs/mkdocs/docs/examples/is_discarded__parse.cpp index feaca2363..2286c15e6 100644 --- a/docs/mkdocs/docs/examples/is_discarded__parse.cpp +++ b/docs/mkdocs/docs/examples/is_discarded__parse.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_null.cpp b/docs/mkdocs/docs/examples/is_null.cpp index 8a8433260..891acb1be 100644 --- a/docs/mkdocs/docs/examples/is_null.cpp +++ b/docs/mkdocs/docs/examples/is_null.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_number.cpp b/docs/mkdocs/docs/examples/is_number.cpp index f107a048a..0384891c1 100644 --- a/docs/mkdocs/docs/examples/is_number.cpp +++ b/docs/mkdocs/docs/examples/is_number.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_number_float.cpp b/docs/mkdocs/docs/examples/is_number_float.cpp index bba2b446f..f3804cf9d 100644 --- a/docs/mkdocs/docs/examples/is_number_float.cpp +++ b/docs/mkdocs/docs/examples/is_number_float.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_number_integer.cpp b/docs/mkdocs/docs/examples/is_number_integer.cpp index 8d6a5ae16..141190b47 100644 --- a/docs/mkdocs/docs/examples/is_number_integer.cpp +++ b/docs/mkdocs/docs/examples/is_number_integer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_number_unsigned.cpp b/docs/mkdocs/docs/examples/is_number_unsigned.cpp index b52ac6b31..332aff2f0 100644 --- a/docs/mkdocs/docs/examples/is_number_unsigned.cpp +++ b/docs/mkdocs/docs/examples/is_number_unsigned.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_object.cpp b/docs/mkdocs/docs/examples/is_object.cpp index a0216fd21..fa0b16a32 100644 --- a/docs/mkdocs/docs/examples/is_object.cpp +++ b/docs/mkdocs/docs/examples/is_object.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_primitive.cpp b/docs/mkdocs/docs/examples/is_primitive.cpp index af3968e83..1aaa871d1 100644 --- a/docs/mkdocs/docs/examples/is_primitive.cpp +++ b/docs/mkdocs/docs/examples/is_primitive.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_string.cpp b/docs/mkdocs/docs/examples/is_string.cpp index c89f550b0..6aaae09dd 100644 --- a/docs/mkdocs/docs/examples/is_string.cpp +++ b/docs/mkdocs/docs/examples/is_string.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/is_structured.cpp b/docs/mkdocs/docs/examples/is_structured.cpp index 41947b1be..2b8939d2b 100644 --- a/docs/mkdocs/docs/examples/is_structured.cpp +++ b/docs/mkdocs/docs/examples/is_structured.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/items.cpp b/docs/mkdocs/docs/examples/items.cpp index 9cd2b51b1..5d3786c5f 100644 --- a/docs/mkdocs/docs/examples/items.cpp +++ b/docs/mkdocs/docs/examples/items.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -10,13 +10,13 @@ int main() json j_array = {1, 2, 4, 8, 16}; // example for an object - for (auto& x : j_object.items()) + for (const auto& x : j_object.items()) { std::cout << "key: " << x.key() << ", value: " << x.value() << '\n'; } // example for an array - for (auto& x : j_array.items()) + for (const auto& x : j_array.items()) { std::cout << "key: " << x.key() << ", value: " << x.value() << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_base_class_t.cpp b/docs/mkdocs/docs/examples/json_base_class_t.cpp index 1dafd417f..678e22dd4 100644 --- a/docs/mkdocs/docs/examples/json_base_class_t.cpp +++ b/docs/mkdocs/docs/examples/json_base_class_t.cpp @@ -24,7 +24,7 @@ void visitor_adaptor_with_metadata::visit(const Fnc& fnc) const template void visitor_adaptor_with_metadata::do_visit(const Ptr& ptr, const Fnc& fnc) const { - using value_t = nlohmann::detail::value_t; + using nlohmann::detail::value_t; const json& j = *static_cast(this); switch (j.type()) { diff --git a/docs/mkdocs/docs/examples/json_document.cpp b/docs/mkdocs/docs/examples/json_document.cpp index 01f2847e2..1613526ac 100644 --- a/docs/mkdocs/docs/examples/json_document.cpp +++ b/docs/mkdocs/docs/examples/json_document.cpp @@ -1,7 +1,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/json_editable_document.cpp b/docs/mkdocs/docs/examples/json_editable_document.cpp index bf08c0d9b..1ae760183 100644 --- a/docs/mkdocs/docs/examples/json_editable_document.cpp +++ b/docs/mkdocs/docs/examples/json_editable_document.cpp @@ -1,9 +1,9 @@ #include #include -using json = nlohmann::json; -using json_editable_document = nlohmann::json_editable_document; -using json_editable_view = nlohmann::json_editable_view; +using nlohmann::json; +using nlohmann::json_editable_document; +using nlohmann::json_editable_view; int main() { diff --git a/docs/mkdocs/docs/examples/json_lines.cpp b/docs/mkdocs/docs/examples/json_lines.cpp index 233c81a4a..a8da39d80 100644 --- a/docs/mkdocs/docs/examples/json_lines.cpp +++ b/docs/mkdocs/docs/examples/json_lines.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -17,6 +17,6 @@ int main() std::string line; while (std::getline(input, line)) { - std::cout << json::parse(line) << std::endl; + std::cout << json::parse(line) << '\n'; } } diff --git a/docs/mkdocs/docs/examples/json_pointer.cpp b/docs/mkdocs/docs/examples/json_pointer.cpp index 8705cf498..d8a43601b 100644 --- a/docs/mkdocs/docs/examples/json_pointer.cpp +++ b/docs/mkdocs/docs/examples/json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/json_pointer__back.cpp b/docs/mkdocs/docs/examples/json_pointer__back.cpp index dd3b210bf..04f663818 100644 --- a/docs/mkdocs/docs/examples/json_pointer__back.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__back.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -11,5 +11,5 @@ int main() // call empty() std::cout << "last reference token of \"" << ptr1 << "\" is \"" << ptr1.back() << "\"\n" - << "last reference token of \"" << ptr2 << "\" is \"" << ptr2.back() << "\"" << std::endl; + << "last reference token of \"" << ptr2 << "\" is \"" << ptr2.back() << "\"" << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__empty.cpp b/docs/mkdocs/docs/examples/json_pointer__empty.cpp index 57257e8b1..f59955533 100644 --- a/docs/mkdocs/docs/examples/json_pointer__empty.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__empty.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -16,5 +16,5 @@ int main() << "\"" << ptr0 << "\": " << ptr0.empty() << '\n' << "\"" << ptr1 << "\": " << ptr1.empty() << '\n' << "\"" << ptr2 << "\": " << ptr2.empty() << '\n' - << "\"" << ptr3 << "\": " << ptr3.empty() << std::endl; + << "\"" << ptr3 << "\": " << ptr3.empty() << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__front.cpp b/docs/mkdocs/docs/examples/json_pointer__front.cpp index 42c76ffec..6d0fd400d 100644 --- a/docs/mkdocs/docs/examples/json_pointer__front.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__front.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -11,5 +11,5 @@ int main() // call empty() std::cout << "first reference token of \"" << ptr1 << "\" is \"" << ptr1.front() << "\"\n" - << "first reference token of \"" << ptr2 << "\" is \"" << ptr2.front() << "\"" << std::endl; + << "first reference token of \"" << ptr2 << "\" is \"" << ptr2.front() << "\"" << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator__equal.cpp b/docs/mkdocs/docs/examples/json_pointer__operator__equal.cpp index dce6df03c..e59bc453c 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator__equal.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator__equal.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,5 +15,5 @@ int main() << "\"" << ptr0 << "\" == \"" << ptr0 << "\": " << (ptr0 == ptr0) << '\n' << "\"" << ptr0 << "\" == \"" << ptr1 << "\": " << (ptr0 == ptr1) << '\n' << "\"" << ptr1 << "\" == \"" << ptr2 << "\": " << (ptr1 == ptr2) << '\n' - << "\"" << ptr2 << "\" == \"" << ptr2 << "\": " << (ptr2 == ptr2) << std::endl; + << "\"" << ptr2 << "\" == \"" << ptr2 << "\": " << (ptr2 == ptr2) << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator__equal_stringtype.cpp b/docs/mkdocs/docs/examples/json_pointer__operator__equal_stringtype.cpp index af8ec5a29..28197c3db 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator__equal_stringtype.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator__equal_stringtype.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -20,14 +20,14 @@ int main() std::cout << std::boolalpha << "\"" << ptr0 << "\" == \"" << str0 << "\": " << (ptr0 == str0) << '\n' << "\"" << str0 << "\" == \"" << ptr1 << "\": " << (str0 == ptr1) << '\n' - << "\"" << ptr2 << "\" == \"" << str1 << "\": " << (ptr2 == str1) << std::endl; + << "\"" << ptr2 << "\" == \"" << str1 << "\": " << (ptr2 == str1) << '\n'; try { - std::cout << "\"" << str2 << "\" == \"" << ptr2 << "\": " << (str2 == ptr2) << std::endl; + std::cout << "\"" << str2 << "\" == \"" << ptr2 << "\": " << (str2 == ptr2) << '\n'; } catch (const json::parse_error& ex) { - std::cout << ex.what() << std::endl; + std::cout << ex.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator__notequal.cpp b/docs/mkdocs/docs/examples/json_pointer__operator__notequal.cpp index 9bbdd5310..624b4e053 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator__notequal.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator__notequal.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,5 +15,5 @@ int main() << "\"" << ptr0 << "\" != \"" << ptr0 << "\": " << (ptr0 != ptr0) << '\n' << "\"" << ptr0 << "\" != \"" << ptr1 << "\": " << (ptr0 != ptr1) << '\n' << "\"" << ptr1 << "\" != \"" << ptr2 << "\": " << (ptr1 != ptr2) << '\n' - << "\"" << ptr2 << "\" != \"" << ptr2 << "\": " << (ptr2 != ptr2) << std::endl; + << "\"" << ptr2 << "\" != \"" << ptr2 << "\": " << (ptr2 != ptr2) << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator__notequal_stringtype.cpp b/docs/mkdocs/docs/examples/json_pointer__operator__notequal_stringtype.cpp index b9b898728..abf92adc3 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator__notequal_stringtype.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator__notequal_stringtype.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -19,14 +19,14 @@ int main() std::cout << std::boolalpha << "\"" << ptr0 << "\" != \"" << str0 << "\": " << (ptr0 != str0) << '\n' << "\"" << str0 << "\" != \"" << ptr1 << "\": " << (str0 != ptr1) << '\n' - << "\"" << ptr2 << "\" != \"" << str1 << "\": " << (ptr2 != str1) << std::endl; + << "\"" << ptr2 << "\" != \"" << str1 << "\": " << (ptr2 != str1) << '\n'; try { - std::cout << "\"" << str2 << "\" != \"" << ptr2 << "\": " << (str2 != ptr2) << std::endl; + std::cout << "\"" << str2 << "\" != \"" << ptr2 << "\": " << (str2 != ptr2) << '\n'; } catch (const json::parse_error& ex) { - std::cout << ex.what() << std::endl; + std::cout << ex.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator_add.cpp b/docs/mkdocs/docs/examples/json_pointer__operator_add.cpp index 14bd74561..9526cc690 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator_add.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator_add.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -19,5 +19,5 @@ int main() // append an array index ptr /= 42; - std::cout << "\"" << ptr << "\"" << std::endl; + std::cout << "\"" << ptr << "\"" << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator_add_binary.cpp b/docs/mkdocs/docs/examples/json_pointer__operator_add_binary.cpp index d26a0d171..55941da25 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator_add_binary.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator_add_binary.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,5 +15,5 @@ int main() std::cout << "\"" << ptr / "fob" << "\"\n"; // append an array index - std::cout << "\"" << ptr / 42 << "\"" << std::endl; + std::cout << "\"" << ptr / 42 << "\"" << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__operator_spaceship.c++20.cpp b/docs/mkdocs/docs/examples/json_pointer__operator_spaceship.c++20.cpp index a2354da73..374c07dd6 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator_spaceship.c++20.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator_spaceship.c++20.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; const char* to_string(const std::strong_ordering& so) { diff --git a/docs/mkdocs/docs/examples/json_pointer__operator_string_t.cpp b/docs/mkdocs/docs/examples/json_pointer__operator_string_t.cpp index 56f213020..964797230 100644 --- a/docs/mkdocs/docs/examples/json_pointer__operator_string_t.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__operator_string_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,5 +15,5 @@ int main() s += "\n"; s += ptr2; - std::cout << s << std::endl; + std::cout << s << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__parent_pointer.cpp b/docs/mkdocs/docs/examples/json_pointer__parent_pointer.cpp index ef9df4534..f321050dd 100644 --- a/docs/mkdocs/docs/examples/json_pointer__parent_pointer.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__parent_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -14,5 +14,5 @@ int main() std::cout << std::boolalpha << "parent of \"" << ptr1 << "\" is \"" << ptr1.parent_pointer() << "\"\n" << "parent of \"" << ptr2 << "\" is \"" << ptr2.parent_pointer() << "\"\n" - << "parent of \"" << ptr3 << "\" is \"" << ptr3.parent_pointer() << "\"" << std::endl; + << "parent of \"" << ptr3 << "\" is \"" << ptr3.parent_pointer() << "\"" << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__pop_back.cpp b/docs/mkdocs/docs/examples/json_pointer__pop_back.cpp index fd077b7e9..ebd43b23d 100644 --- a/docs/mkdocs/docs/examples/json_pointer__pop_back.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__pop_back.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/json_pointer__pop_front.cpp b/docs/mkdocs/docs/examples/json_pointer__pop_front.cpp index 59403668c..dfadd63c6 100644 --- a/docs/mkdocs/docs/examples/json_pointer__pop_front.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__pop_front.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/json_pointer__push_back.cpp b/docs/mkdocs/docs/examples/json_pointer__push_back.cpp index e6b59a125..76b27ebc4 100644 --- a/docs/mkdocs/docs/examples/json_pointer__push_back.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__push_back.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/json_pointer__push_front.cpp b/docs/mkdocs/docs/examples/json_pointer__push_front.cpp index 7bcdccb45..723edb7c8 100644 --- a/docs/mkdocs/docs/examples/json_pointer__push_front.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__push_front.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/json_pointer__string_t.cpp b/docs/mkdocs/docs/examples/json_pointer__string_t.cpp index fbe0f179e..48e9d22aa 100644 --- a/docs/mkdocs/docs/examples/json_pointer__string_t.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__string_t.cpp @@ -1,13 +1,13 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { json::json_pointer::string_t s = "This is a string."; - std::cout << s << std::endl; + std::cout << s << '\n'; - std::cout << std::boolalpha << std::is_same::value << std::endl; + std::cout << std::boolalpha << std::is_same::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_pointer__to_string.cpp b/docs/mkdocs/docs/examples/json_pointer__to_string.cpp index 31d35a724..af52355a5 100644 --- a/docs/mkdocs/docs/examples/json_pointer__to_string.cpp +++ b/docs/mkdocs/docs/examples/json_pointer__to_string.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -30,5 +30,5 @@ int main() << "\"" << ptr9.to_string() << "\"\n" << "\"" << ptr10.to_string() << "\"\n" << "\"" << ptr11.to_string() << "\"\n" - << "\"" << ptr12.to_string() << "\"" << std::endl; + << "\"" << ptr12.to_string() << "\"" << '\n'; } diff --git a/docs/mkdocs/docs/examples/json_view_ownership.cpp b/docs/mkdocs/docs/examples/json_view_ownership.cpp index fa0e8262a..7fa2afdf3 100644 --- a/docs/mkdocs/docs/examples/json_view_ownership.cpp +++ b/docs/mkdocs/docs/examples/json_view_ownership.cpp @@ -3,7 +3,7 @@ #include #include -using json_document = nlohmann::json_document; +using nlohmann::json_document; int main() { diff --git a/docs/mkdocs/docs/examples/max_size.cpp b/docs/mkdocs/docs/examples/max_size.cpp index c2ffc5468..7477acab7 100644 --- a/docs/mkdocs/docs/examples/max_size.cpp +++ b/docs/mkdocs/docs/examples/max_size.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/merge_diff.cpp b/docs/mkdocs/docs/examples/merge_diff.cpp index 22ae9b087..906b5786f 100644 --- a/docs/mkdocs/docs/examples/merge_diff.cpp +++ b/docs/mkdocs/docs/examples/merge_diff.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -47,5 +47,5 @@ int main() // output patch and roundtrip result std::cout << std::setw(4) << patch << "\n\n" - << std::setw(4) << patched_source << std::endl; + << std::setw(4) << patched_source << '\n'; } diff --git a/docs/mkdocs/docs/examples/merge_patch.cpp b/docs/mkdocs/docs/examples/merge_patch.cpp index f3fee1ed1..aaed7d923 100644 --- a/docs/mkdocs/docs/examples/merge_patch.cpp +++ b/docs/mkdocs/docs/examples/merge_patch.cpp @@ -2,7 +2,7 @@ #include #include // for std::setw -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -37,5 +37,5 @@ int main() document.merge_patch(patch); // output original and patched document - std::cout << std::setw(4) << document << std::endl; + std::cout << std::setw(4) << document << '\n'; } diff --git a/docs/mkdocs/docs/examples/meta.cpp b/docs/mkdocs/docs/examples/meta.cpp index a051575ba..f0de003b4 100644 --- a/docs/mkdocs/docs/examples/meta.cpp +++ b/docs/mkdocs/docs/examples/meta.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/nlohmann_define_derived_type_intrusive_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_derived_type_intrusive_macro.cpp index 0dca78162..30f792b86 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_derived_type_intrusive_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_derived_type_intrusive_macro.cpp @@ -33,5 +33,5 @@ int main() B example(23, 42, 3.142, 1.777); json example_json = example; - std::cout << std::setw(4) << example_json << std::endl; + std::cout << std::setw(4) << example_json << '\n'; } \ No newline at end of file diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_explicit.cpp index ce13ff114..832f2cbdd 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -43,7 +43,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -57,6 +57,6 @@ int main() } catch (const json::exception& e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_macro.cpp index ab8657801..3a0392717 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -29,7 +29,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -43,6 +43,6 @@ int main() } catch (const json::exception& e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp index dd6a9a679..a61d57e93 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -35,5 +35,5 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_macro.cpp index 8213f9063..ca2dcaa22 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -29,5 +29,5 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_explicit.cpp index 2713444b6..a06cdff0c 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -44,7 +44,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -53,5 +53,5 @@ int main() // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; auto p3 = j3.get(); - std::cout << "roundtrip: " << json(p3) << std::endl; + std::cout << "roundtrip: " << json(p3) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_macro.cpp index 851a3582f..764c8dd81 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -29,7 +29,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -38,5 +38,5 @@ int main() // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; auto p3 = j3.get(); - std::cout << "roundtrip: " << json(p3) << std::endl; + std::cout << "roundtrip: " << json(p3) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_explicit.cpp index dfbdde4ac..d6089f165 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -36,7 +36,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -50,6 +50,6 @@ int main() } catch (const json::exception& e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_macro.cpp index cdf90dc2c..fea57ea24 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -22,7 +22,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -36,6 +36,6 @@ int main() } catch (const json::exception& e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp index ead75b74e..6f20b1067 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -28,5 +28,5 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_macro.cpp index 7aef23af6..978d14bde 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -22,5 +22,5 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp index 23384b70c..a6bcc624a 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -42,7 +42,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -51,5 +51,5 @@ int main() // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; auto p3 = j3.get(); - std::cout << "roundtrip: " << json(p3) << std::endl; + std::cout << "roundtrip: " << json(p3) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp index aa9bc53b4..d33d5355d 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -27,7 +27,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; @@ -36,5 +36,5 @@ int main() // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; auto p3 = j3.get(); - std::cout << "roundtrip: " << json(p3) << std::endl; + std::cout << "roundtrip: " << json(p3) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp index d92f13c42..f022cffd8 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -36,7 +36,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"json_address": "742 Evergreen Terrace", "json_age": 40, "json_name": "Homer Simpson"})"_json; @@ -50,6 +50,6 @@ int main() } catch (const json::exception& e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_macro.cpp b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_macro.cpp index 36114e724..bb3712486 100644 --- a/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_macro.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_names_macro.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; namespace ns @@ -22,7 +22,7 @@ int main() // serialization: person -> json json j = p; - std::cout << "serialization: " << j << std::endl; + std::cout << "serialization: " << j << '\n'; // deserialization: json -> person json j2 = R"({"json_address": "742 Evergreen Terrace", "json_age": 40, "json_name": "Homer Simpson"})"_json; @@ -36,6 +36,6 @@ int main() } catch (const json::exception& e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_namespace.cpp b/docs/mkdocs/docs/examples/nlohmann_json_namespace.cpp index 4bad91f4b..6cb07f584 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_namespace.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_namespace.cpp @@ -2,7 +2,7 @@ #include // possible use case: use NLOHMANN_JSON_NAMESPACE instead of nlohmann -using json = NLOHMANN_JSON_NAMESPACE::json; +using NLOHMANN_JSON_NAMESPACE::json; // macro needed to output the NLOHMANN_JSON_NAMESPACE as string literal #define Q(x) #x @@ -10,5 +10,5 @@ using json = NLOHMANN_JSON_NAMESPACE::json; int main() { - std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << std::endl; + std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_namespace_begin.c++17.cpp b/docs/mkdocs/docs/examples/nlohmann_json_namespace_begin.c++17.cpp index 9385d593d..924bcb239 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_namespace_begin.c++17.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_namespace_begin.c++17.cpp @@ -29,5 +29,5 @@ int main() NLOHMANN_JSON_NAMESPACE::json j; j.push_back(o1); j.push_back(o2); - std::cout << j << std::endl; + std::cout << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_namespace_no_version.cpp b/docs/mkdocs/docs/examples/nlohmann_json_namespace_no_version.cpp index 97948dd7e..9f1bd9ad6 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_namespace_no_version.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_namespace_no_version.cpp @@ -9,5 +9,5 @@ int main() { - std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << std::endl; + std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum.cpp b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum.cpp index f9e472c64..5a59e826a 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -39,7 +39,7 @@ int main() json j_stopped = ns::TS_STOPPED; json j_red = ns::Color::red; std::cout << "ns::TS_STOPPED -> " << j_stopped - << ", ns::Color::red -> " << j_red << std::endl; + << ", ns::Color::red -> " << j_red << '\n'; // deserialization json j_running = "running"; @@ -47,7 +47,7 @@ int main() auto running = j_running.get(); auto blue = j_blue.get(); std::cout << j_running << " -> " << running - << ", " << j_blue << " -> " << static_cast(blue) << std::endl; + << ", " << j_blue << " -> " << static_cast(blue) << '\n'; // deserializing undefined JSON value to enum // (where the first map entry above is the default) @@ -55,5 +55,5 @@ int main() auto invalid = j_pi.get(); auto unknown = j_pi.get(); std::cout << j_pi << " -> " << invalid << ", " - << j_pi << " -> " << static_cast(unknown) << std::endl; + << j_pi << " -> " << static_cast(unknown) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_2.cpp b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_2.cpp index fd27226ca..e7b0fa966 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_2.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_2.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -22,12 +22,12 @@ int main() { // serialization json j_red = ns::Color::red; - std::cout << static_cast(ns::Color::red) << " -> " << j_red << std::endl; + std::cout << static_cast(ns::Color::red) << " -> " << j_red << '\n'; // deserialization json j_rot = "rot"; auto rot = j_rot.get(); auto red = j_red.get(); - std::cout << j_rot << " -> " << static_cast(rot) << std::endl; - std::cout << j_red << " -> " << static_cast(red) << std::endl; + std::cout << j_rot << " -> " << static_cast(rot) << '\n'; + std::cout << j_red << " -> " << static_cast(red) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp index fa2572dec..1017a729f 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -39,7 +39,7 @@ int main() json j_stopped = ns::TS_STOPPED; json j_red = ns::Color::red; std::cout << "ns::TS_STOPPED -> " << j_stopped - << ", ns::Color::red -> " << j_red << std::endl; + << ", ns::Color::red -> " << j_red << '\n'; // deserialization json j_running = "running"; @@ -47,6 +47,6 @@ int main() auto running = j_running.get(); auto blue = j_blue.get(); std::cout << j_running << " -> " << running - << ", " << j_blue << " -> " << static_cast(blue) << std::endl; + << ", " << j_blue << " -> " << static_cast(blue) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_2.cpp b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_2.cpp index 7f766b7a9..263fe30a6 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_2.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_2.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -22,12 +22,12 @@ int main() { // serialization json j_red = ns::Color::red; - std::cout << static_cast(ns::Color::red) << " -> " << j_red << std::endl; + std::cout << static_cast(ns::Color::red) << " -> " << j_red << '\n'; // deserialization json j_rot = "rot"; auto rot = j_rot.get(); auto red = j_red.get(); - std::cout << j_rot << " -> " << static_cast(rot) << std::endl; - std::cout << j_red << " -> " << static_cast(red) << std::endl; + std::cout << j_rot << " -> " << static_cast(rot) << '\n'; + std::cout << j_red << " -> " << static_cast(red) << '\n'; } diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_err.cpp b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_err.cpp index aeb901122..4589edc8a 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_err.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict_err.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -34,7 +34,7 @@ int main() } catch (const json::exception e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } // invalid deserialization @@ -46,7 +46,7 @@ int main() } catch (const json::exception e) { - std::cout << "deserialization failed: " << e.what() << std::endl; + std::cout << "deserialization failed: " << e.what() << '\n'; } return 0; diff --git a/docs/mkdocs/docs/examples/nlohmann_json_version.cpp b/docs/mkdocs/docs/examples/nlohmann_json_version.cpp index ca5f53728..e83eb9227 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_version.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_version.cpp @@ -1,12 +1,12 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { std::cout << "JSON for Modern C++ version " << NLOHMANN_JSON_VERSION_MAJOR << "." << NLOHMANN_JSON_VERSION_MINOR << "." - << NLOHMANN_JSON_VERSION_PATCH << std::endl; + << NLOHMANN_JSON_VERSION_PATCH << '\n'; } diff --git a/docs/mkdocs/docs/examples/no_exceptions_access.cpp b/docs/mkdocs/docs/examples/no_exceptions_access.cpp new file mode 100644 index 000000000..2395ac21e --- /dev/null +++ b/docs/mkdocs/docs/examples/no_exceptions_access.cpp @@ -0,0 +1,43 @@ +#include +#include + +using json = nlohmann::json; + +int main() +{ + json j = json::parse(R"({"name": "Mary", "age": 42})"); + + // look up the key, then test the type: neither step throws + const auto keys = {"name", "age", "city"}; + for (const char* key : keys) + { + const auto it = j.find(key); + if (it == j.end()) + { + std::cout << key << ": missing\n"; + } + else if (it->is_string()) + { + std::cout << key << ": string \"" << it->get_ref() << "\"\n"; + } + else + { + std::cout << key << ": not a string, but " << it->type_name() << '\n'; + } + } + + // get_ptr returns a null pointer if the value has another type + const std::string* name = j["name"].get_ptr(); + const std::string* age = j["age"].get_ptr(); + std::cout << "get_ptr: name = " << (name != nullptr) << ", age = " << (age != nullptr) << '\n'; + + // value() is NOT exception-free: it throws if the key exists with a different type + try + { + std::cout << j.value("name", 0) << '\n'; + } + catch (const json::type_error& e) + { + std::cout << e.what() << '\n'; + } +} diff --git a/docs/mkdocs/docs/examples/no_exceptions_access.output b/docs/mkdocs/docs/examples/no_exceptions_access.output new file mode 100644 index 000000000..38b700296 --- /dev/null +++ b/docs/mkdocs/docs/examples/no_exceptions_access.output @@ -0,0 +1,5 @@ +name: string "Mary" +age: not a string, but number +city: missing +get_ptr: name = 1, age = 0 +[json.exception.type_error.302] type must be number, but is string diff --git a/docs/mkdocs/docs/examples/no_exceptions_convert.c++17.cpp b/docs/mkdocs/docs/examples/no_exceptions_convert.c++17.cpp new file mode 100644 index 000000000..a48dfdb06 --- /dev/null +++ b/docs/mkdocs/docs/examples/no_exceptions_convert.c++17.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +using json = nlohmann::json; + +struct person +{ + std::string name; + int age; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, age) + +// Compile this function with exceptions enabled and call it from code that does not use them. +std::optional to_person(const json& j) +{ + try + { + return j.get(); + } + catch (const json::exception&) + { + return std::nullopt; + } +} + +int main() +{ + const json good = {{"name", "Mary"}, {"age", 42}}; + const json bad = {{"name", "Mary"}, {"age", "forty-two"}}; + const json incomplete = {{"name", "Mary"}}; + + const auto inputs = {good, bad, incomplete}; + + for (const json& j : inputs) + { + if (const auto p = to_person(j)) + { + std::cout << "person: " << p->name << ", " << p->age << '\n'; + } + else + { + std::cout << "not a person: " << j << '\n'; + } + } +} diff --git a/docs/mkdocs/docs/examples/no_exceptions_convert.c++17.output b/docs/mkdocs/docs/examples/no_exceptions_convert.c++17.output new file mode 100644 index 000000000..1409aa105 --- /dev/null +++ b/docs/mkdocs/docs/examples/no_exceptions_convert.c++17.output @@ -0,0 +1,3 @@ +person: Mary, 42 +not a person: {"age":"forty-two","name":"Mary"} +not a person: {"name":"Mary"} diff --git a/docs/mkdocs/docs/examples/no_exceptions_parse.cpp b/docs/mkdocs/docs/examples/no_exceptions_parse.cpp new file mode 100644 index 000000000..d65d5c829 --- /dev/null +++ b/docs/mkdocs/docs/examples/no_exceptions_parse.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +using json = nlohmann::json; + +// None of the calls below throws, so this example also works with -fno-exceptions. +int main() +{ + const std::string invalid = R"({"name": "Mary", "age": )"; + + // parse() with allow_exceptions = false returns a discarded value + json j = json::parse(invalid, nullptr, false); + std::cout << std::boolalpha << "parse: discarded = " << j.is_discarded() << '\n'; + + // accept() only tells whether the input is valid JSON + std::cout << "accept: " << json::accept(invalid) << '\n'; + + // the binary formats have the same parameter + const std::vector truncated_cbor = {0x83, 0x01}; + json c = json::from_cbor(truncated_cbor, true, false); + std::cout << "from_cbor: discarded = " << c.is_discarded() << '\n'; + + // valid input is parsed as usual + json valid = json::parse(R"({"name": "Mary"})", nullptr, false); + std::cout << "valid input: " << valid << '\n'; +} diff --git a/docs/mkdocs/docs/examples/no_exceptions_parse.output b/docs/mkdocs/docs/examples/no_exceptions_parse.output new file mode 100644 index 000000000..d571e3cbb --- /dev/null +++ b/docs/mkdocs/docs/examples/no_exceptions_parse.output @@ -0,0 +1,4 @@ +parse: discarded = true +accept: false +from_cbor: discarded = true +valid input: {"name":"Mary"} diff --git a/docs/mkdocs/docs/examples/number_float_t.cpp b/docs/mkdocs/docs/examples/number_float_t.cpp index 21211dc51..4eb91369e 100644 --- a/docs/mkdocs/docs/examples/number_float_t.cpp +++ b/docs/mkdocs/docs/examples/number_float_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same::value << std::endl; + std::cout << std::boolalpha << std::is_same::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/number_integer_t.cpp b/docs/mkdocs/docs/examples/number_integer_t.cpp index 75ee57b63..db6954847 100644 --- a/docs/mkdocs/docs/examples/number_integer_t.cpp +++ b/docs/mkdocs/docs/examples/number_integer_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same::value << std::endl; + std::cout << std::boolalpha << std::is_same::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/number_unsigned_t.cpp b/docs/mkdocs/docs/examples/number_unsigned_t.cpp index ff3b86df8..b1a3d12b1 100644 --- a/docs/mkdocs/docs/examples/number_unsigned_t.cpp +++ b/docs/mkdocs/docs/examples/number_unsigned_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same::value << std::endl; + std::cout << std::boolalpha << std::is_same::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/object.cpp b/docs/mkdocs/docs/examples/object.cpp index ad167d4af..bed0d7124 100644 --- a/docs/mkdocs/docs/examples/object.cpp +++ b/docs/mkdocs/docs/examples/object.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/object_comparator_t.cpp b/docs/mkdocs/docs/examples/object_comparator_t.cpp index 6b82c7ca6..6526a09c1 100644 --- a/docs/mkdocs/docs/examples/object_comparator_t.cpp +++ b/docs/mkdocs/docs/examples/object_comparator_t.cpp @@ -1,11 +1,11 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { std::cout << std::boolalpha << "json::object_comparator_t(\"one\", \"two\") = " << json::object_comparator_t{}("one", "two") << "\n" - << "json::object_comparator_t(\"three\", \"four\") = " << json::object_comparator_t{}("three", "four") << std::endl; + << "json::object_comparator_t(\"three\", \"four\") = " << json::object_comparator_t{}("three", "four") << '\n'; } diff --git a/docs/mkdocs/docs/examples/object_t.cpp b/docs/mkdocs/docs/examples/object_t.cpp index 85cfa3e33..275898b76 100644 --- a/docs/mkdocs/docs/examples/object_t.cpp +++ b/docs/mkdocs/docs/examples/object_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same, json::object_t>::value << std::endl; + std::cout << std::boolalpha << std::is_same, json::object_t>::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/operator__ValueType.cpp b/docs/mkdocs/docs/examples/operator__ValueType.cpp index 7ab29ec71..0363c79cc 100644 --- a/docs/mkdocs/docs/examples/operator__ValueType.cpp +++ b/docs/mkdocs/docs/examples/operator__ValueType.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -43,7 +43,7 @@ int main() } std::cout << "\n\n"; - for (auto i : v8) + for (const auto& i : v8) { std::cout << i.first << ": " << i.second << '\n'; } diff --git a/docs/mkdocs/docs/examples/operator__equal.cpp b/docs/mkdocs/docs/examples/operator__equal.cpp index 1426f489a..2a9a89e54 100644 --- a/docs/mkdocs/docs/examples/operator__equal.cpp +++ b/docs/mkdocs/docs/examples/operator__equal.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__equal__nullptr_t.cpp b/docs/mkdocs/docs/examples/operator__equal__nullptr_t.cpp index dbb210372..7b1810a4a 100644 --- a/docs/mkdocs/docs/examples/operator__equal__nullptr_t.cpp +++ b/docs/mkdocs/docs/examples/operator__equal__nullptr_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__equal__specializations.cpp b/docs/mkdocs/docs/examples/operator__equal__specializations.cpp index 97d5ecead..a053af6df 100644 --- a/docs/mkdocs/docs/examples/operator__equal__specializations.cpp +++ b/docs/mkdocs/docs/examples/operator__equal__specializations.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -12,5 +12,5 @@ int main() nlohmann::ordered_json oj1 = {{"version", 1}, {"type", "integer"}}; nlohmann::ordered_json oj2 = {{"type", "integer"}, {"version", 1}}; - std::cout << std::boolalpha << (uj1 == uj2) << '\n' << (oj1 == oj2) << std::endl; + std::cout << std::boolalpha << (uj1 == uj2) << '\n' << (oj1 == oj2) << '\n'; } diff --git a/docs/mkdocs/docs/examples/operator__greater.cpp b/docs/mkdocs/docs/examples/operator__greater.cpp index 65bb9c049..878eba66f 100644 --- a/docs/mkdocs/docs/examples/operator__greater.cpp +++ b/docs/mkdocs/docs/examples/operator__greater.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__greaterequal.cpp b/docs/mkdocs/docs/examples/operator__greaterequal.cpp index f8659ee07..2d36c1d4e 100644 --- a/docs/mkdocs/docs/examples/operator__greaterequal.cpp +++ b/docs/mkdocs/docs/examples/operator__greaterequal.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__less.cpp b/docs/mkdocs/docs/examples/operator__less.cpp index 64209a22a..949982102 100644 --- a/docs/mkdocs/docs/examples/operator__less.cpp +++ b/docs/mkdocs/docs/examples/operator__less.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__lessequal.cpp b/docs/mkdocs/docs/examples/operator__lessequal.cpp index 543b9543f..e044e5544 100644 --- a/docs/mkdocs/docs/examples/operator__lessequal.cpp +++ b/docs/mkdocs/docs/examples/operator__lessequal.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__notequal.cpp b/docs/mkdocs/docs/examples/operator__notequal.cpp index 43e89509c..0f9f85869 100644 --- a/docs/mkdocs/docs/examples/operator__notequal.cpp +++ b/docs/mkdocs/docs/examples/operator__notequal.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__notequal__nullptr_t.cpp b/docs/mkdocs/docs/examples/operator__notequal__nullptr_t.cpp index dc7e6ac4c..30ef0d1bd 100644 --- a/docs/mkdocs/docs/examples/operator__notequal__nullptr_t.cpp +++ b/docs/mkdocs/docs/examples/operator__notequal__nullptr_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator__value_t.cpp b/docs/mkdocs/docs/examples/operator__value_t.cpp index d9aac7978..c2340c68a 100644 --- a/docs/mkdocs/docs/examples/operator__value_t.cpp +++ b/docs/mkdocs/docs/examples/operator__value_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_array__json_pointer.cpp b/docs/mkdocs/docs/examples/operator_array__json_pointer.cpp index 0fa207f02..388011f45 100644 --- a/docs/mkdocs/docs/examples/operator_array__json_pointer.cpp +++ b/docs/mkdocs/docs/examples/operator_array__json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/operator_array__json_pointer_const.cpp b/docs/mkdocs/docs/examples/operator_array__json_pointer_const.cpp index f40e2494a..1a327cea8 100644 --- a/docs/mkdocs/docs/examples/operator_array__json_pointer_const.cpp +++ b/docs/mkdocs/docs/examples/operator_array__json_pointer_const.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/operator_array__keytype.c++17.cpp b/docs/mkdocs/docs/examples/operator_array__keytype.c++17.cpp index 7f2b41dd8..69e5b340e 100644 --- a/docs/mkdocs/docs/examples/operator_array__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/operator_array__keytype.c++17.cpp @@ -4,7 +4,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_array__keytype_const.c++17.cpp b/docs/mkdocs/docs/examples/operator_array__keytype_const.c++17.cpp index 2cf94f40f..99343575a 100644 --- a/docs/mkdocs/docs/examples/operator_array__keytype_const.c++17.cpp +++ b/docs/mkdocs/docs/examples/operator_array__keytype_const.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_array__object_t_key_type.cpp b/docs/mkdocs/docs/examples/operator_array__object_t_key_type.cpp index f9b7f7318..209b08d61 100644 --- a/docs/mkdocs/docs/examples/operator_array__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/operator_array__object_t_key_type.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_array__object_t_key_type_const.cpp b/docs/mkdocs/docs/examples/operator_array__object_t_key_type_const.cpp index 1bdb34024..1f774cc6f 100644 --- a/docs/mkdocs/docs/examples/operator_array__object_t_key_type_const.cpp +++ b/docs/mkdocs/docs/examples/operator_array__object_t_key_type_const.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_array__size_type.cpp b/docs/mkdocs/docs/examples/operator_array__size_type.cpp index d6f3e153c..5a7f735d7 100644 --- a/docs/mkdocs/docs/examples/operator_array__size_type.cpp +++ b/docs/mkdocs/docs/examples/operator_array__size_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_array__size_type_const.cpp b/docs/mkdocs/docs/examples/operator_array__size_type_const.cpp index d56fa0a43..4a41c4529 100644 --- a/docs/mkdocs/docs/examples/operator_array__size_type_const.cpp +++ b/docs/mkdocs/docs/examples/operator_array__size_type_const.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_deserialize.cpp b/docs/mkdocs/docs/examples/operator_deserialize.cpp index 8e3d8bd83..b42f40428 100644 --- a/docs/mkdocs/docs/examples/operator_deserialize.cpp +++ b/docs/mkdocs/docs/examples/operator_deserialize.cpp @@ -3,7 +3,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_literal_json.cpp b/docs/mkdocs/docs/examples/operator_literal_json.cpp index 84ca6297d..6b8a7e18c 100644 --- a/docs/mkdocs/docs/examples/operator_literal_json.cpp +++ b/docs/mkdocs/docs/examples/operator_literal_json.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/operator_literal_json_pointer.cpp b/docs/mkdocs/docs/examples/operator_literal_json_pointer.cpp index aba93e88e..c7e5460b1 100644 --- a/docs/mkdocs/docs/examples/operator_literal_json_pointer.cpp +++ b/docs/mkdocs/docs/examples/operator_literal_json_pointer.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/operator_ltlt__basic_json.cpp b/docs/mkdocs/docs/examples/operator_ltlt__basic_json.cpp index 3bd4ad577..148d60276 100644 --- a/docs/mkdocs/docs/examples/operator_ltlt__basic_json.cpp +++ b/docs/mkdocs/docs/examples/operator_ltlt__basic_json.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/operator_ltlt__json_pointer.cpp b/docs/mkdocs/docs/examples/operator_ltlt__json_pointer.cpp index 044318fa5..156a07794 100644 --- a/docs/mkdocs/docs/examples/operator_ltlt__json_pointer.cpp +++ b/docs/mkdocs/docs/examples/operator_ltlt__json_pointer.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -9,5 +9,5 @@ int main() json::json_pointer ptr("/foo/bar/baz"); // write string representation to stream - std::cout << ptr << std::endl; + std::cout << ptr << '\n'; } diff --git a/docs/mkdocs/docs/examples/operator_spaceship__const_reference.c++20.cpp b/docs/mkdocs/docs/examples/operator_spaceship__const_reference.c++20.cpp index 9e7c9e9be..2c260adc8 100644 --- a/docs/mkdocs/docs/examples/operator_spaceship__const_reference.c++20.cpp +++ b/docs/mkdocs/docs/examples/operator_spaceship__const_reference.c++20.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; const char* to_string(const std::partial_ordering& po) { diff --git a/docs/mkdocs/docs/examples/operator_spaceship__scalartype.c++20.cpp b/docs/mkdocs/docs/examples/operator_spaceship__scalartype.c++20.cpp index ebb5b4349..8628dfb0b 100644 --- a/docs/mkdocs/docs/examples/operator_spaceship__scalartype.c++20.cpp +++ b/docs/mkdocs/docs/examples/operator_spaceship__scalartype.c++20.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; const char* to_string(const std::partial_ordering& po) { diff --git a/docs/mkdocs/docs/examples/ordered_json.cpp b/docs/mkdocs/docs/examples/ordered_json.cpp index effad530c..a427e635e 100644 --- a/docs/mkdocs/docs/examples/ordered_json.cpp +++ b/docs/mkdocs/docs/examples/ordered_json.cpp @@ -1,7 +1,7 @@ #include #include -using ordered_json = nlohmann::ordered_json; +using nlohmann::ordered_json; int main() { diff --git a/docs/mkdocs/docs/examples/ordered_json_document.cpp b/docs/mkdocs/docs/examples/ordered_json_document.cpp index 2cae413bf..389055115 100644 --- a/docs/mkdocs/docs/examples/ordered_json_document.cpp +++ b/docs/mkdocs/docs/examples/ordered_json_document.cpp @@ -1,7 +1,7 @@ #include #include -using ordered_json_document = nlohmann::ordered_json_document; +using nlohmann::ordered_json_document; int main() { diff --git a/docs/mkdocs/docs/examples/ordered_json_editable_document.cpp b/docs/mkdocs/docs/examples/ordered_json_editable_document.cpp index c6e7fa08e..9d7821066 100644 --- a/docs/mkdocs/docs/examples/ordered_json_editable_document.cpp +++ b/docs/mkdocs/docs/examples/ordered_json_editable_document.cpp @@ -1,7 +1,7 @@ #include #include -using ordered_json_editable_document = nlohmann::ordered_json_editable_document; +using nlohmann::ordered_json_editable_document; int main() { diff --git a/docs/mkdocs/docs/examples/ordered_map.cpp b/docs/mkdocs/docs/examples/ordered_map.cpp index dcc60cb52..7c507a564 100644 --- a/docs/mkdocs/docs/examples/ordered_map.cpp +++ b/docs/mkdocs/docs/examples/ordered_map.cpp @@ -10,7 +10,7 @@ void output(const char* prefix, const Map& m) { std::cout << element.first << ":" << element.second << ' '; } - std::cout << "}" << std::endl; + std::cout << "}" << '\n'; } int main() diff --git a/docs/mkdocs/docs/examples/other_error.cpp b/docs/mkdocs/docs/examples/other_error.cpp index 56aa8ae6b..2fa152b8b 100644 --- a/docs/mkdocs/docs/examples/other_error.cpp +++ b/docs/mkdocs/docs/examples/other_error.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -25,6 +25,6 @@ int main() { // output exception information std::cout << "message: " << e.what() << '\n' - << "exception id: " << e.id << std::endl; + << "exception id: " << e.id << '\n'; } } diff --git a/docs/mkdocs/docs/examples/out_of_range.cpp b/docs/mkdocs/docs/examples/out_of_range.cpp index 03282082d..cb87d1a33 100644 --- a/docs/mkdocs/docs/examples/out_of_range.cpp +++ b/docs/mkdocs/docs/examples/out_of_range.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,6 +15,6 @@ int main() { // output exception information std::cout << "message: " << e.what() << '\n' - << "exception id: " << e.id << std::endl; + << "exception id: " << e.id << '\n'; } } diff --git a/docs/mkdocs/docs/examples/parse__allow_exceptions.cpp b/docs/mkdocs/docs/examples/parse__allow_exceptions.cpp index f396c347e..6e9385a86 100644 --- a/docs/mkdocs/docs/examples/parse__allow_exceptions.cpp +++ b/docs/mkdocs/docs/examples/parse__allow_exceptions.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -19,7 +19,7 @@ int main() } catch (const json::parse_error& e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << '\n'; } // parse without exceptions @@ -27,10 +27,10 @@ int main() if (j.is_discarded()) { - std::cout << "the input is invalid JSON" << std::endl; + std::cout << "the input is invalid JSON" << '\n'; } else { - std::cout << "the input is valid JSON: " << j << std::endl; + std::cout << "the input is valid JSON: " << j << '\n'; } } diff --git a/docs/mkdocs/docs/examples/parse__array__parser_callback_t.cpp b/docs/mkdocs/docs/examples/parse__array__parser_callback_t.cpp index 63f0a0e4d..747df8599 100644 --- a/docs/mkdocs/docs/examples/parse__array__parser_callback_t.cpp +++ b/docs/mkdocs/docs/examples/parse__array__parser_callback_t.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/parse__contiguouscontainer__parser_callback_t.cpp b/docs/mkdocs/docs/examples/parse__contiguouscontainer__parser_callback_t.cpp index 6eb409bba..50eeee437 100644 --- a/docs/mkdocs/docs/examples/parse__contiguouscontainer__parser_callback_t.cpp +++ b/docs/mkdocs/docs/examples/parse__contiguouscontainer__parser_callback_t.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/parse__istream__parser_callback_t.cpp b/docs/mkdocs/docs/examples/parse__istream__parser_callback_t.cpp index 2ef14dab2..4a7b60873 100644 --- a/docs/mkdocs/docs/examples/parse__istream__parser_callback_t.cpp +++ b/docs/mkdocs/docs/examples/parse__istream__parser_callback_t.cpp @@ -3,12 +3,12 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { // a JSON text - auto text = R"( + const auto* text = R"( { "Image": { "Width": 800, diff --git a/docs/mkdocs/docs/examples/parse__iterator_pair.cpp b/docs/mkdocs/docs/examples/parse__iterator_pair.cpp index d0c30c1a5..751f29d17 100644 --- a/docs/mkdocs/docs/examples/parse__iterator_pair.cpp +++ b/docs/mkdocs/docs/examples/parse__iterator_pair.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/parse__pointers.cpp b/docs/mkdocs/docs/examples/parse__pointers.cpp index a5a16eea9..9152588c8 100644 --- a/docs/mkdocs/docs/examples/parse__pointers.cpp +++ b/docs/mkdocs/docs/examples/parse__pointers.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/parse__string__parser_callback_t.cpp b/docs/mkdocs/docs/examples/parse__string__parser_callback_t.cpp index 19c6c448a..24a284e62 100644 --- a/docs/mkdocs/docs/examples/parse__string__parser_callback_t.cpp +++ b/docs/mkdocs/docs/examples/parse__string__parser_callback_t.cpp @@ -2,12 +2,12 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { // a JSON text - auto text = R"( + const auto* text = R"( { "Image": { "Width": 800, diff --git a/docs/mkdocs/docs/examples/parse__wide_string.cpp b/docs/mkdocs/docs/examples/parse__wide_string.cpp index 93a599947..33b251d8b 100644 --- a/docs/mkdocs/docs/examples/parse__wide_string.cpp +++ b/docs/mkdocs/docs/examples/parse__wide_string.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/parse_error.cpp b/docs/mkdocs/docs/examples/parse_error.cpp index 2f73ac65f..73367dada 100644 --- a/docs/mkdocs/docs/examples/parse_error.cpp +++ b/docs/mkdocs/docs/examples/parse_error.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,6 +15,6 @@ int main() // output exception information std::cout << "message: " << e.what() << '\n' << "exception id: " << e.id << '\n' - << "byte position of error: " << e.byte << std::endl; + << "byte position of error: " << e.byte << '\n'; } } diff --git a/docs/mkdocs/docs/examples/parse_error_position.cpp b/docs/mkdocs/docs/examples/parse_error_position.cpp new file mode 100644 index 000000000..ed3c42e79 --- /dev/null +++ b/docs/mkdocs/docs/examples/parse_error_position.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using json = nlohmann::json; + +// line and column (both starting at 1) of a byte index in a text +std::pair line_column(const std::string& input, std::size_t byte) +{ + std::size_t line = 1; + std::size_t column = 1; + for (std::size_t i = 0; i + 1 < byte && i < input.size(); ++i) + { + if (input[i] == '\n') + { + ++line; + column = 1; + } + else + { + ++column; + } + } + return {line, column}; +} + +int main() +{ + const std::string input = "{\n \"a\": 1,\n \"b\": ]\n}"; + + try + { + const json j = json::parse(input); + std::cout << j << '\n'; + } + catch (const json::parse_error& e) + { + const auto position = line_column(input, e.byte); + std::cout << e.what() << '\n'; + std::cout << "byte " << e.byte << " = line " << position.first << ", column " << position.second << '\n'; + } +} diff --git a/docs/mkdocs/docs/examples/parse_error_position.output b/docs/mkdocs/docs/examples/parse_error_position.output new file mode 100644 index 000000000..77730d4db --- /dev/null +++ b/docs/mkdocs/docs/examples/parse_error_position.output @@ -0,0 +1,2 @@ +[json.exception.parse_error.101] parse error at line 3, column 8: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal +byte 20 = line 3, column 8 diff --git a/docs/mkdocs/docs/examples/parse_error_position_diagnostic.cpp b/docs/mkdocs/docs/examples/parse_error_position_diagnostic.cpp new file mode 100644 index 000000000..1630b2a59 --- /dev/null +++ b/docs/mkdocs/docs/examples/parse_error_position_diagnostic.cpp @@ -0,0 +1,46 @@ +#define JSON_DIAGNOSTIC_POSITIONS 1 +#include +#include +#include + +using json = nlohmann::json; + +// line and column (both starting at 1) of a byte index in a text +std::pair line_column(const std::string& input, std::size_t byte) +{ + std::size_t line = 1; + std::size_t column = 1; + for (std::size_t i = 0; i + 1 < byte && i < input.size(); ++i) + { + if (input[i] == '\n') + { + ++line; + column = 1; + } + else + { + ++column; + } + } + return {line, column}; +} + +int main() +{ + const std::string input = "{\n \"name\": \"Mary\",\n \"age\": \"forty-two\"\n}"; + const json j = json::parse(input); // the input is valid JSON ... + + try + { + const int age = j["age"]; // ... but the value has the wrong type + std::cout << age << '\n'; + } + catch (const json::exception& e) + { + std::cout << e.what() << '\n'; + + // start_pos() is a 0-based index into the input; the helper expects a 1-based byte index + const auto position = line_column(input, j["age"].start_pos() + 1); + std::cout << "the value starts at line " << position.first << ", column " << position.second << '\n'; + } +} diff --git a/docs/mkdocs/docs/examples/parse_error_position_diagnostic.output b/docs/mkdocs/docs/examples/parse_error_position_diagnostic.output new file mode 100644 index 000000000..2f2f0862d --- /dev/null +++ b/docs/mkdocs/docs/examples/parse_error_position_diagnostic.output @@ -0,0 +1,2 @@ +[json.exception.type_error.302] (bytes 29-40) type must be number, but is string +the value starts at line 3, column 10 diff --git a/docs/mkdocs/docs/examples/parse_event_t.cpp b/docs/mkdocs/docs/examples/parse_event_t.cpp index 3a56b22a9..ff1e2c557 100644 --- a/docs/mkdocs/docs/examples/parse_event_t.cpp +++ b/docs/mkdocs/docs/examples/parse_event_t.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; // translate a parse_event_t to a human-readable name std::string event_name(json::parse_event_t event) @@ -29,7 +29,7 @@ std::string event_name(json::parse_event_t event) int main() { // a small JSON text - auto text = R"({"pi": 3.141, "numbers": [1, 2]})"; + const auto* text = R"({"pi": 3.141, "numbers": [1, 2]})"; // parse the text and report every event together with its depth; // returning true keeps every value unchanged diff --git a/docs/mkdocs/docs/examples/parser_callback_t.cpp b/docs/mkdocs/docs/examples/parser_callback_t.cpp index 99c4298e8..16ce0f2f5 100644 --- a/docs/mkdocs/docs/examples/parser_callback_t.cpp +++ b/docs/mkdocs/docs/examples/parser_callback_t.cpp @@ -1,12 +1,12 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { // a JSON text with an array and a number inside an object - auto text = R"({"IDs": [116, 943], "Width": 800})"; + const auto* text = R"({"IDs": [116, 943], "Width": 800})"; // discard the array when the parser reads its opening bracket json j_array_start = json::parse(text, [](int /*depth*/, json::parse_event_t event, json& /*parsed*/) diff --git a/docs/mkdocs/docs/examples/partial_input_buffering.cpp b/docs/mkdocs/docs/examples/partial_input_buffering.cpp new file mode 100644 index 000000000..f8c0f49e0 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_buffering.cpp @@ -0,0 +1,47 @@ +#include +#include + +using json = nlohmann::json; + +std::string buffer; + +// call for every chunk; returns true and sets `value` once the buffer holds one complete JSON value +bool push(const std::string& chunk, json& value) +{ + buffer += chunk; + try + { + value = json::parse(buffer); + buffer.clear(); + return true; + } + catch (const json::parse_error& e) + { + if (e.byte == buffer.size() + 1) + { + return false; // the input ends prematurely: wait for more data + } + throw; // a real syntax error + } +} + +int main() +{ + json value; + const auto chunks = {"{\"na", "me\": \"Ma", "ry\", \"l", "ist\": [1, 2", ", 3]}"}; + for (const std::string chunk : chunks) + { + std::cout << "chunk " << chunk << " -> " << (push(chunk, value) ? "complete" : "incomplete") << '\n'; + } + std::cout << value << '\n'; + + // a syntax error in the middle is not mistaken for missing data + try + { + push("[1, ]", value); + } + catch (const json::parse_error& e) + { + std::cout << e.what() << '\n'; + } +} diff --git a/docs/mkdocs/docs/examples/partial_input_buffering.output b/docs/mkdocs/docs/examples/partial_input_buffering.output new file mode 100644 index 000000000..b1d5a6526 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_buffering.output @@ -0,0 +1,7 @@ +chunk {"na -> incomplete +chunk me": "Ma -> incomplete +chunk ry", "l -> incomplete +chunk ist": [1, 2 -> incomplete +chunk , 3]} -> complete +{"list":[1,2,3],"name":"Mary"} +[json.exception.parse_error.101] parse error at line 1, column 5: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal diff --git a/docs/mkdocs/docs/examples/partial_input_cbor.cpp b/docs/mkdocs/docs/examples/partial_input_cbor.cpp new file mode 100644 index 000000000..8e2497e26 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_cbor.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + // a CBOR value followed by other data + const std::vector cbor = json::to_cbor(json{1, 2, 3}); + std::string data(cbor.begin(), cbor.end()); + data += "TRAILING"; + + std::istringstream input(data); + json j = json::from_cbor(input, false); // strict = false: stop after the first value + + std::cout << "value: " << j << '\n'; + std::cout << "bytes used: " << input.tellg() << '\n'; + + std::string rest; + input >> rest; + std::cout << "remaining input: " << rest << '\n'; +} diff --git a/docs/mkdocs/docs/examples/partial_input_cbor.output b/docs/mkdocs/docs/examples/partial_input_cbor.output new file mode 100644 index 000000000..beaf841d1 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_cbor.output @@ -0,0 +1,3 @@ +value: [1,2,3] +bytes used: 4 +remaining input: TRAILING diff --git a/docs/mkdocs/docs/examples/partial_input_concatenated.cpp b/docs/mkdocs/docs/examples/partial_input_concatenated.cpp new file mode 100644 index 000000000..0f7079ee4 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_concatenated.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + std::istringstream input(R"({"a":1} [2,3] 4 "s")"); + + // check for the end of the stream before each read, because a read at the end throws + json j; + while (input >> std::ws && input.peek() != std::char_traits::eof()) + { + input >> j; + std::cout << j << '\n'; + } +} diff --git a/docs/mkdocs/docs/examples/partial_input_concatenated.output b/docs/mkdocs/docs/examples/partial_input_concatenated.output new file mode 100644 index 000000000..e51277135 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_concatenated.output @@ -0,0 +1,4 @@ +{"a":1} +[2,3] +4 +"s" diff --git a/docs/mkdocs/docs/examples/partial_input_discard_records.cpp b/docs/mkdocs/docs/examples/partial_input_discard_records.cpp new file mode 100644 index 000000000..3aebda1f7 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_discard_records.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using json = nlohmann::json; + +void handle(const json& record) +{ + std::cout << "handling " << record << '\n'; +} + +int main() +{ + std::istringstream file(R"([{"id": 1}, {"id": 2}, {"id": 3}])"); + + json result = json::parse(file, [](int depth, json::parse_event_t event, json & record) + { + if (depth == 1 && event == json::parse_event_t::object_end) + { + handle(record); + return false; // discard the record + } + return true; + }); + + std::cout << "result: " << result << '\n'; +} diff --git a/docs/mkdocs/docs/examples/partial_input_discard_records.output b/docs/mkdocs/docs/examples/partial_input_discard_records.output new file mode 100644 index 000000000..7eed01747 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_discard_records.output @@ -0,0 +1,4 @@ +handling {"id":1} +handling {"id":2} +handling {"id":3} +result: [] diff --git a/docs/mkdocs/docs/examples/partial_input_sax_stop.cpp b/docs/mkdocs/docs/examples/partial_input_sax_stop.cpp new file mode 100644 index 000000000..6dbeb0945 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_sax_stop.cpp @@ -0,0 +1,81 @@ +#include +#include + +using json = nlohmann::json; + +struct find_id : nlohmann::json_sax +{ + bool next_is_id = false; + json::number_unsigned_t id = 0; + + bool key(std::string& name) override + { + next_is_id = (name == "id"); + return true; + } + + bool number_unsigned(json::number_unsigned_t value) override + { + if (next_is_id) + { + id = value; + return false; // found: stop parsing + } + return true; + } + + bool null() override + { + return true; + } + bool boolean(bool /*val*/) override + { + return true; + } + bool number_integer(json::number_integer_t /*val*/) override + { + return true; + } + bool number_float(json::number_float_t /*val*/, const std::string& /*s*/) override + { + return true; + } + bool string(std::string& /*val*/) override + { + return true; + } + bool binary(json::binary_t& /*val*/) override + { + return true; + } + bool start_object(std::size_t /*elements*/) override + { + return true; + } + bool end_object() override + { + return true; + } + bool start_array(std::size_t /*elements*/) override + { + return true; + } + bool end_array() override + { + return true; + } + bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& /*ex*/) override + { + return false; + } +}; + +int main() +{ + find_id handler; + const bool completed = json::sax_parse(R"({"x": [1, 2, 3], "id": 42, "rest": [4, 5, 6]})", &handler); + + std::cout << std::boolalpha; + std::cout << "parse completed: " << completed << '\n'; + std::cout << "id: " << handler.id << '\n'; +} diff --git a/docs/mkdocs/docs/examples/partial_input_sax_stop.output b/docs/mkdocs/docs/examples/partial_input_sax_stop.output new file mode 100644 index 000000000..947e23ee9 --- /dev/null +++ b/docs/mkdocs/docs/examples/partial_input_sax_stop.output @@ -0,0 +1,2 @@ +parse completed: false +id: 42 diff --git a/docs/mkdocs/docs/examples/patch.cpp b/docs/mkdocs/docs/examples/patch.cpp index b7ecb8eee..53688a364 100644 --- a/docs/mkdocs/docs/examples/patch.cpp +++ b/docs/mkdocs/docs/examples/patch.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -29,5 +29,5 @@ int main() // output original and patched document std::cout << std::setw(4) << doc << "\n\n" - << std::setw(4) << patched_doc << std::endl; + << std::setw(4) << patched_doc << '\n'; } diff --git a/docs/mkdocs/docs/examples/patch__exception.cpp b/docs/mkdocs/docs/examples/patch__exception.cpp index 9e0a27cab..98b8bebc7 100644 --- a/docs/mkdocs/docs/examples/patch__exception.cpp +++ b/docs/mkdocs/docs/examples/patch__exception.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -32,5 +32,5 @@ int main() } // the original document is unchanged - std::cout << std::setw(4) << doc << std::endl; + std::cout << std::setw(4) << doc << '\n'; } diff --git a/docs/mkdocs/docs/examples/patch_inplace.cpp b/docs/mkdocs/docs/examples/patch_inplace.cpp index 061708a2d..82e837549 100644 --- a/docs/mkdocs/docs/examples/patch_inplace.cpp +++ b/docs/mkdocs/docs/examples/patch_inplace.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -25,11 +25,11 @@ int main() )"_json; // output original document - std::cout << "Before\n" << std::setw(4) << doc << std::endl; + std::cout << "Before\n" << std::setw(4) << doc << '\n'; // apply the patch doc.patch_inplace(patch); // output patched document - std::cout << "\nAfter\n" << std::setw(4) << doc << std::endl; + std::cout << "\nAfter\n" << std::setw(4) << doc << '\n'; } diff --git a/docs/mkdocs/docs/examples/patch_inplace__exception.cpp b/docs/mkdocs/docs/examples/patch_inplace__exception.cpp index 02ebe133a..bb89e08e6 100644 --- a/docs/mkdocs/docs/examples/patch_inplace__exception.cpp +++ b/docs/mkdocs/docs/examples/patch_inplace__exception.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -34,5 +34,5 @@ int main() } // the first operation has already been applied to doc - std::cout << std::setw(4) << doc << std::endl; + std::cout << std::setw(4) << doc << '\n'; } diff --git a/docs/mkdocs/docs/examples/push_back.cpp b/docs/mkdocs/docs/examples/push_back.cpp index bbddf4f51..3120d452b 100644 --- a/docs/mkdocs/docs/examples/push_back.cpp +++ b/docs/mkdocs/docs/examples/push_back.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/push_back__initializer_list.cpp b/docs/mkdocs/docs/examples/push_back__initializer_list.cpp index e96645f16..e0cf4e6de 100644 --- a/docs/mkdocs/docs/examples/push_back__initializer_list.cpp +++ b/docs/mkdocs/docs/examples/push_back__initializer_list.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/push_back__object_t__value.cpp b/docs/mkdocs/docs/examples/push_back__object_t__value.cpp index 5d694e938..b63e8f7e1 100644 --- a/docs/mkdocs/docs/examples/push_back__object_t__value.cpp +++ b/docs/mkdocs/docs/examples/push_back__object_t__value.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/rbegin.cpp b/docs/mkdocs/docs/examples/rbegin.cpp index 239f7a689..867a9c952 100644 --- a/docs/mkdocs/docs/examples/rbegin.cpp +++ b/docs/mkdocs/docs/examples/rbegin.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/reject_duplicate_keys.cpp b/docs/mkdocs/docs/examples/reject_duplicate_keys.cpp index 0e5e78925..3e8259763 100644 --- a/docs/mkdocs/docs/examples/reject_duplicate_keys.cpp +++ b/docs/mkdocs/docs/examples/reject_duplicate_keys.cpp @@ -5,7 +5,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; json parse_strict(const std::string& input) { diff --git a/docs/mkdocs/docs/examples/rend.cpp b/docs/mkdocs/docs/examples/rend.cpp index adadbbdc2..86d70c7f9 100644 --- a/docs/mkdocs/docs/examples/rend.cpp +++ b/docs/mkdocs/docs/examples/rend.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/sax_no_exception.cpp b/docs/mkdocs/docs/examples/sax_no_exception.cpp index 1eeff466e..580c0cb9d 100644 --- a/docs/mkdocs/docs/examples/sax_no_exception.cpp +++ b/docs/mkdocs/docs/examples/sax_no_exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; // a DOM parser that reports parse errors instead of throwing class sax_no_exception : public nlohmann::detail::json_sax_dom_parser @@ -18,7 +18,7 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser #include -using json = nlohmann::json; +using nlohmann::json; // a simple event consumer that collects string representations of the passed // values; note inheriting from json::json_sax_t is not required, but can @@ -95,7 +95,7 @@ class sax_event_consumer : public json::json_sax_t int main() { // a JSON text - auto text = R"( + const auto* text = R"( { "Image": { "Width": 800, @@ -127,5 +127,5 @@ int main() } // output the result of sax_parse - std::cout << "\nresult: " << std::boolalpha << result << std::endl; + std::cout << "\nresult: " << std::boolalpha << result << '\n'; } diff --git a/docs/mkdocs/docs/examples/sax_parse__binary.cpp b/docs/mkdocs/docs/examples/sax_parse__binary.cpp index 08bc85df6..300bbda66 100644 --- a/docs/mkdocs/docs/examples/sax_parse__binary.cpp +++ b/docs/mkdocs/docs/examples/sax_parse__binary.cpp @@ -3,7 +3,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; // a simple event consumer that collects string representations of the passed // values; note inheriting from json::json_sax_t is not required, but can @@ -110,5 +110,5 @@ int main() } // output the result of sax_parse - std::cout << "\nresult: " << std::boolalpha << result << std::endl; + std::cout << "\nresult: " << std::boolalpha << result << '\n'; } diff --git a/docs/mkdocs/docs/examples/size.cpp b/docs/mkdocs/docs/examples/size.cpp index 237548377..00ed8cd15 100644 --- a/docs/mkdocs/docs/examples/size.cpp +++ b/docs/mkdocs/docs/examples/size.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/std_formatter.c++20.cpp b/docs/mkdocs/docs/examples/std_formatter.c++20.cpp index d249415d7..3401ad5e6 100644 --- a/docs/mkdocs/docs/examples/std_formatter.c++20.cpp +++ b/docs/mkdocs/docs/examples/std_formatter.c++20.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/std_hash.cpp b/docs/mkdocs/docs/examples/std_hash.cpp index 184ddbbb3..0fcabdcf1 100644 --- a/docs/mkdocs/docs/examples/std_hash.cpp +++ b/docs/mkdocs/docs/examples/std_hash.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -16,5 +16,5 @@ int main() << "hash({}) = " << std::hash {}(json::object()) << '\n' << "hash([]) = " << std::hash {}(json::array()) << '\n' << "hash({\"hello\": \"world\"}) = " << std::hash {}("{\"hello\": \"world\"}"_json) - << std::endl; + << '\n'; } diff --git a/docs/mkdocs/docs/examples/std_swap.cpp b/docs/mkdocs/docs/examples/std_swap.cpp index 36ab3ce6b..f829c9a5d 100644 --- a/docs/mkdocs/docs/examples/std_swap.cpp +++ b/docs/mkdocs/docs/examples/std_swap.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,5 +15,5 @@ int main() // swap values std::swap(j1, j2); - std::cout << "j1 = " << j1 << " | j2 = " << j2 << std::endl; + std::cout << "j1 = " << j1 << " | j2 = " << j2 << '\n'; } diff --git a/docs/mkdocs/docs/examples/string_t.cpp b/docs/mkdocs/docs/examples/string_t.cpp index 77a9ea480..118c42d26 100644 --- a/docs/mkdocs/docs/examples/string_t.cpp +++ b/docs/mkdocs/docs/examples/string_t.cpp @@ -2,9 +2,9 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { - std::cout << std::boolalpha << std::is_same::value << std::endl; + std::cout << std::boolalpha << std::is_same::value << '\n'; } diff --git a/docs/mkdocs/docs/examples/swap__array_t.cpp b/docs/mkdocs/docs/examples/swap__array_t.cpp index 2119dd552..e551f1fcc 100644 --- a/docs/mkdocs/docs/examples/swap__array_t.cpp +++ b/docs/mkdocs/docs/examples/swap__array_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/swap__binary_t.cpp b/docs/mkdocs/docs/examples/swap__binary_t.cpp index 4b8fc3db7..a9c7b9d03 100644 --- a/docs/mkdocs/docs/examples/swap__binary_t.cpp +++ b/docs/mkdocs/docs/examples/swap__binary_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/swap__object_t.cpp b/docs/mkdocs/docs/examples/swap__object_t.cpp index 301b558d3..9aff7667e 100644 --- a/docs/mkdocs/docs/examples/swap__object_t.cpp +++ b/docs/mkdocs/docs/examples/swap__object_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/swap__reference.cpp b/docs/mkdocs/docs/examples/swap__reference.cpp index 34182ad24..d2ffbedf2 100644 --- a/docs/mkdocs/docs/examples/swap__reference.cpp +++ b/docs/mkdocs/docs/examples/swap__reference.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/swap__string_t.cpp b/docs/mkdocs/docs/examples/swap__string_t.cpp index b5d58316f..14d7552ef 100644 --- a/docs/mkdocs/docs/examples/swap__string_t.cpp +++ b/docs/mkdocs/docs/examples/swap__string_t.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/to_bjdata.cpp b/docs/mkdocs/docs/examples/to_bjdata.cpp index 9b7abac4e..ddebccb2f 100644 --- a/docs/mkdocs/docs/examples/to_bjdata.cpp +++ b/docs/mkdocs/docs/examples/to_bjdata.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; // function to print BJData's diagnostic format @@ -10,11 +10,11 @@ void print_byte(uint8_t byte) { if (32 < byte and byte < 128) { - std::cout << (char)byte; + std::cout << static_cast(byte); } else { - std::cout << (int)byte; + std::cout << static_cast(byte); } } @@ -31,7 +31,7 @@ int main() { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; // create an array of numbers json array = {1, 2, 3, 4, 5, 6, 7, 8}; @@ -48,17 +48,17 @@ int main() { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; for (auto& byte : v_array_size) { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; for (auto& byte : v_array_size_and_type) { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_bjdata__exception.cpp b/docs/mkdocs/docs/examples/to_bjdata__exception.cpp index 560f3343b..8e3dda347 100644 --- a/docs/mkdocs/docs/examples/to_bjdata__exception.cpp +++ b/docs/mkdocs/docs/examples/to_bjdata__exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/to_bon8.cpp b/docs/mkdocs/docs/examples/to_bon8.cpp index a40130903..2dc36fc6a 100644 --- a/docs/mkdocs/docs/examples/to_bon8.cpp +++ b/docs/mkdocs/docs/examples/to_bon8.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -16,7 +16,7 @@ int main() // print the vector content for (auto& byte : v) { - std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; + std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte) << " "; } - std::cout << std::endl; + std::cout << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_bon8__exception.cpp b/docs/mkdocs/docs/examples/to_bon8__exception.cpp index 48992413b..cefea2809 100644 --- a/docs/mkdocs/docs/examples/to_bon8__exception.cpp +++ b/docs/mkdocs/docs/examples/to_bon8__exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/to_bson.cpp b/docs/mkdocs/docs/examples/to_bson.cpp index 3484b0b76..bfbb3677b 100644 --- a/docs/mkdocs/docs/examples/to_bson.cpp +++ b/docs/mkdocs/docs/examples/to_bson.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -16,7 +16,7 @@ int main() // print the vector content for (auto& byte : v) { - std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; + std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte) << " "; } - std::cout << std::endl; + std::cout << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_bson__exception.cpp b/docs/mkdocs/docs/examples/to_bson__exception.cpp index fe12c202f..387bba3e8 100644 --- a/docs/mkdocs/docs/examples/to_bson__exception.cpp +++ b/docs/mkdocs/docs/examples/to_bson__exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/to_cbor.cpp b/docs/mkdocs/docs/examples/to_cbor.cpp index 3d5e04150..46a2cd042 100644 --- a/docs/mkdocs/docs/examples/to_cbor.cpp +++ b/docs/mkdocs/docs/examples/to_cbor.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -16,7 +16,7 @@ int main() // print the vector content for (auto& byte : v) { - std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; + std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte) << " "; } - std::cout << std::endl; + std::cout << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_json.cpp b/docs/mkdocs/docs/examples/to_json.cpp index 1f82a4de4..9ac74116a 100644 --- a/docs/mkdocs/docs/examples/to_json.cpp +++ b/docs/mkdocs/docs/examples/to_json.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; namespace ns { @@ -28,5 +28,5 @@ int main() json j = p; - std::cout << j << std::endl; + std::cout << j << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_msgpack.cpp b/docs/mkdocs/docs/examples/to_msgpack.cpp index b29ae8c7c..958a13767 100644 --- a/docs/mkdocs/docs/examples/to_msgpack.cpp +++ b/docs/mkdocs/docs/examples/to_msgpack.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() @@ -16,7 +16,7 @@ int main() // print the vector content for (auto& byte : v) { - std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; + std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte) << " "; } - std::cout << std::endl; + std::cout << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_msgpack__exception.cpp b/docs/mkdocs/docs/examples/to_msgpack__exception.cpp index fb20d0ad8..d932ffac4 100644 --- a/docs/mkdocs/docs/examples/to_msgpack__exception.cpp +++ b/docs/mkdocs/docs/examples/to_msgpack__exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/to_string.cpp b/docs/mkdocs/docs/examples/to_string.cpp index ee44283f3..2e9ec0d3b 100644 --- a/docs/mkdocs/docs/examples/to_string.cpp +++ b/docs/mkdocs/docs/examples/to_string.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using std::to_string; int main() @@ -16,5 +16,5 @@ int main() // serialize without indentation std::cout << j_str << "\n\n" - << i_str << std::endl; + << i_str << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_ubjson.cpp b/docs/mkdocs/docs/examples/to_ubjson.cpp index fd267a85a..e951378e2 100644 --- a/docs/mkdocs/docs/examples/to_ubjson.cpp +++ b/docs/mkdocs/docs/examples/to_ubjson.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; // function to print UBJSON's diagnostic format @@ -10,11 +10,11 @@ void print_byte(uint8_t byte) { if (32 < byte and byte < 128) { - std::cout << (char)byte; + std::cout << static_cast(byte); } else { - std::cout << (int)byte; + std::cout << static_cast(byte); } } @@ -31,7 +31,7 @@ int main() { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; // create an array of numbers json array = {1, 2, 3, 4, 5, 6, 7, 8}; @@ -48,17 +48,17 @@ int main() { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; for (auto& byte : v_array_size) { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; for (auto& byte : v_array_size_and_type) { print_byte(byte); } - std::cout << std::endl; + std::cout << '\n'; } diff --git a/docs/mkdocs/docs/examples/to_ubjson__exception.cpp b/docs/mkdocs/docs/examples/to_ubjson__exception.cpp index 8fe481d84..bfed7abfe 100644 --- a/docs/mkdocs/docs/examples/to_ubjson__exception.cpp +++ b/docs/mkdocs/docs/examples/to_ubjson__exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/trailing_commas.cpp b/docs/mkdocs/docs/examples/trailing_commas.cpp index 32bf6874e..4a2285e6b 100644 --- a/docs/mkdocs/docs/examples/trailing_commas.cpp +++ b/docs/mkdocs/docs/examples/trailing_commas.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -25,7 +25,7 @@ int main() } catch (json::exception& e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << '\n'; } json j = json::parse(s, diff --git a/docs/mkdocs/docs/examples/type.cpp b/docs/mkdocs/docs/examples/type.cpp index 68fba3a91..3b51b143f 100644 --- a/docs/mkdocs/docs/examples/type.cpp +++ b/docs/mkdocs/docs/examples/type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/type_error.cpp b/docs/mkdocs/docs/examples/type_error.cpp index f520f4013..44c3e0279 100644 --- a/docs/mkdocs/docs/examples/type_error.cpp +++ b/docs/mkdocs/docs/examples/type_error.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -15,6 +15,6 @@ int main() { // output exception information std::cout << "message: " << e.what() << '\n' - << "exception id: " << e.id << std::endl; + << "exception id: " << e.id << '\n'; } } diff --git a/docs/mkdocs/docs/examples/type_name.cpp b/docs/mkdocs/docs/examples/type_name.cpp index 32d3590cc..b3a46712f 100644 --- a/docs/mkdocs/docs/examples/type_name.cpp +++ b/docs/mkdocs/docs/examples/type_name.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/type_recipes_chrono.cpp b/docs/mkdocs/docs/examples/type_recipes_chrono.cpp new file mode 100644 index 000000000..a3bd357ff --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_chrono.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +// the name of a period; extend the list for the periods you use +template const char* unit_name(); +template <> const char* unit_name() +{ + return "ns"; +} +template <> const char* unit_name() +{ + return "us"; +} +template <> const char* unit_name() +{ + return "ms"; +} +template <> const char* unit_name>() +{ + return "s"; +} +template <> const char* unit_name>() +{ + return "min"; +} +template <> const char* unit_name>() +{ + return "h"; +} + +namespace nlohmann +{ +template +struct adl_serializer> +{ + static void to_json(json& j, const std::chrono::duration& d) + { + j = {{"unit", unit_name()}, {"value", d.count()}}; + } + + static void from_json(const json& j, std::chrono::duration& d) + { + if (j.at("unit").get() != unit_name()) + { + throw std::invalid_argument("unexpected unit " + j.at("unit").dump()); + } + d = std::chrono::duration(j.at("value").get()); + } +}; + +template +struct adl_serializer> +{ + static void to_json(json& j, const std::chrono::time_point& t) + { + j = t.time_since_epoch(); + } + + static void from_json(const json& j, std::chrono::time_point& t) + { + t = std::chrono::time_point(j.get()); + } +}; +} // namespace nlohmann + +int main() +{ + using namespace std::chrono; + + std::cout << std::boolalpha; + + // the unit is part of the JSON value + const json timeout = milliseconds(1500); + std::cout << "duration: " << timeout << '\n'; + std::cout << "as milliseconds: " << timeout.get().count() << '\n'; + + // reading with a different unit is rejected + try + { + timeout.get(); + } + catch (const std::invalid_argument& e) + { + std::cout << "error: " << e.what() << '\n'; + } + + // a system_clock time point is the time since the Unix epoch + const time_point t(milliseconds(1700000000123)); + const json stamp = t; + std::cout << "time point: " << stamp << '\n'; + + const auto back = stamp.get>(); + std::cout << "round trip: " << (back == t) << '\n'; +} diff --git a/docs/mkdocs/docs/examples/type_recipes_chrono.output b/docs/mkdocs/docs/examples/type_recipes_chrono.output new file mode 100644 index 000000000..6b40a2bc2 --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_chrono.output @@ -0,0 +1,5 @@ +duration: {"unit":"ms","value":1500} +as milliseconds: 1500 +error: unexpected unit "ms" +time point: {"unit":"ms","value":1700000000123} +round trip: true diff --git a/docs/mkdocs/docs/examples/type_recipes_class_hierarchy.c++14.cpp b/docs/mkdocs/docs/examples/type_recipes_class_hierarchy.c++14.cpp new file mode 100644 index 000000000..a7ac926f3 --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_class_hierarchy.c++14.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +struct shape +{ + virtual ~shape() = default; + virtual json serialize() const = 0; +}; + +struct circle : shape +{ + double radius; + + explicit circle(double r) : radius(r) {} + + json serialize() const override + { + return {{"type", "circle"}, {"radius", radius}}; + } +}; + +struct rectangle : shape +{ + double width; + double height; + + rectangle(double w, double h) : width(w), height(h) {} + + json serialize() const override + { + return {{"type", "rectangle"}, {"width", width}, {"height", height}}; + } +}; + +namespace nlohmann +{ +template <> +struct adl_serializer> +{ + static void to_json(json& j, const std::unique_ptr& p) + { + j = p ? p->serialize() : json(nullptr); + } + + static void from_json(const json& j, std::unique_ptr& p) + { + if (j.is_null()) + { + p.reset(); + return; + } + + const auto& type = j.at("type").get_ref(); + if (type == "circle") + { + p = std::make_unique(j.at("radius").get()); + } + else if (type == "rectangle") + { + p = std::make_unique(j.at("width").get(), j.at("height").get()); + } + else + { + throw std::invalid_argument("unknown shape type: " + type); + } + } +}; +} // namespace nlohmann + +int main() +{ + std::vector> shapes; + shapes.push_back(std::make_unique(1.5)); + shapes.push_back(std::make_unique(2, 3)); + + const json j = shapes; + std::cout << j << '\n'; + + // reading creates objects of the derived types + const auto back = j.get>>(); + std::cout << std::boolalpha; + std::cout << "first is a circle: " << (dynamic_cast(back[0].get()) != nullptr) << '\n'; + std::cout << "second is a rectangle: " << (dynamic_cast(back[1].get()) != nullptr) << '\n'; + + // an unknown type tag is reported + try + { + json{{"type", "star"}}.get>(); + } + catch (const std::invalid_argument& e) + { + std::cout << e.what() << '\n'; + } +} diff --git a/docs/mkdocs/docs/examples/type_recipes_class_hierarchy.c++14.output b/docs/mkdocs/docs/examples/type_recipes_class_hierarchy.c++14.output new file mode 100644 index 000000000..b04cea385 --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_class_hierarchy.c++14.output @@ -0,0 +1,4 @@ +[{"radius":1.5,"type":"circle"},{"height":3.0,"type":"rectangle","width":2.0}] +first is a circle: true +second is a rectangle: true +unknown shape type: star diff --git a/docs/mkdocs/docs/examples/type_recipes_smart_pointers.c++14.cpp b/docs/mkdocs/docs/examples/type_recipes_smart_pointers.c++14.cpp new file mode 100644 index 000000000..d06557df2 --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_smart_pointers.c++14.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +using json = nlohmann::json; + +namespace nlohmann +{ +template +struct adl_serializer> +{ + static void to_json(json& j, const std::shared_ptr& p) + { + if (p) + { + j = *p; + } + else + { + j = nullptr; + } + } + + static void from_json(const json& j, std::shared_ptr& p) + { + if (j.is_null()) + { + p = nullptr; + } + else + { + p = std::make_shared(j.get::type>()); + } + } +}; + +template +struct adl_serializer> +{ + static void to_json(json& j, const std::unique_ptr& p) + { + if (p) + { + j = *p; + } + else + { + j = nullptr; + } + } + + static void from_json(const json& j, std::unique_ptr& p) + { + if (j.is_null()) + { + p = nullptr; + } + else + { + p = std::make_unique(j.get()); + } + } +}; +} // namespace nlohmann + +struct point +{ + int x; + int y; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(point, x, y) + +int main() +{ + // an empty pointer is written as null + const std::vector> points = {std::make_shared(point{1, 2}), nullptr}; + const json j = points; + std::cout << j << '\n'; + + // reading allocates new objects + const auto back = j.get>>(); + std::cout << back[0]->x << ", " << back[0]->y << ", " << (back[1] == nullptr) << '\n'; + + // pointers to const and unique_ptr work as well + const auto const_ptr = j[0].get>(); + const auto unique = j[0].get>(); + std::cout << const_ptr->y << ", " << unique->x << '\n'; +} diff --git a/docs/mkdocs/docs/examples/type_recipes_smart_pointers.c++14.output b/docs/mkdocs/docs/examples/type_recipes_smart_pointers.c++14.output new file mode 100644 index 000000000..083b00774 --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_smart_pointers.c++14.output @@ -0,0 +1,3 @@ +[{"x":1,"y":2},null] +1, 2, 1 +2, 1 diff --git a/docs/mkdocs/docs/examples/type_recipes_variant.c++17.cpp b/docs/mkdocs/docs/examples/type_recipes_variant.c++17.cpp new file mode 100644 index 000000000..e774f8a2b --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_variant.c++17.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +namespace geo +{ +struct circ +{ + double radius; +}; + +struct rect +{ + double width; + double height; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(circ, radius) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(rect, width, height) + +using figure = std::variant; + +void to_json(json& j, const figure& f) +{ + std::visit([&j](const auto & alternative) + { + j = alternative; + }, f); + j["type"] = std::holds_alternative(f) ? "circ" : "rect"; +} + +void from_json(const json& j, figure& f) +{ + const auto& type = j.at("type").get_ref(); + if (type == "circ") + { + f = j.get(); + } + else if (type == "rect") + { + f = j.get(); + } + else + { + throw std::invalid_argument("unknown figure type: " + type); + } +} +} // namespace geo + +int main() +{ + const std::vector figures = {geo::circ{2}, geo::rect{3, 4}}; + + const json j = figures; + std::cout << j << '\n'; + + const auto back = j.get>(); + std::cout << "alternatives read back: " << back[0].index() << ", " << back[1].index() << '\n'; +} diff --git a/docs/mkdocs/docs/examples/type_recipes_variant.c++17.output b/docs/mkdocs/docs/examples/type_recipes_variant.c++17.output new file mode 100644 index 000000000..062554cb5 --- /dev/null +++ b/docs/mkdocs/docs/examples/type_recipes_variant.c++17.output @@ -0,0 +1,2 @@ +[{"radius":2.0,"type":"circ"},{"height":4.0,"type":"rect","width":3.0}] +alternatives read back: 0, 1 diff --git a/docs/mkdocs/docs/examples/unflatten.cpp b/docs/mkdocs/docs/examples/unflatten.cpp index 75fb02d03..ac665e2da 100644 --- a/docs/mkdocs/docs/examples/unflatten.cpp +++ b/docs/mkdocs/docs/examples/unflatten.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/update.cpp b/docs/mkdocs/docs/examples/update.cpp index ff94b67fa..413b12779 100644 --- a/docs/mkdocs/docs/examples/update.cpp +++ b/docs/mkdocs/docs/examples/update.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/update__range.cpp b/docs/mkdocs/docs/examples/update__range.cpp index 5b4385046..e38b5c19d 100644 --- a/docs/mkdocs/docs/examples/update__range.cpp +++ b/docs/mkdocs/docs/examples/update__range.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/value__exception.cpp b/docs/mkdocs/docs/examples/value__exception.cpp index 1d611d4c9..79cbc53b2 100644 --- a/docs/mkdocs/docs/examples/value__exception.cpp +++ b/docs/mkdocs/docs/examples/value__exception.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/value__json_ptr.cpp b/docs/mkdocs/docs/examples/value__json_ptr.cpp index d866ef076..8f8bb37e3 100644 --- a/docs/mkdocs/docs/examples/value__json_ptr.cpp +++ b/docs/mkdocs/docs/examples/value__json_ptr.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; using namespace nlohmann::literals; int main() diff --git a/docs/mkdocs/docs/examples/value__keytype.c++17.cpp b/docs/mkdocs/docs/examples/value__keytype.c++17.cpp index 1f6ff5c30..b1a034a55 100644 --- a/docs/mkdocs/docs/examples/value__keytype.c++17.cpp +++ b/docs/mkdocs/docs/examples/value__keytype.c++17.cpp @@ -3,7 +3,7 @@ #include using namespace std::string_view_literals; -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/value__object_t_key_type.cpp b/docs/mkdocs/docs/examples/value__object_t_key_type.cpp index 9488d30ef..df2e48f52 100644 --- a/docs/mkdocs/docs/examples/value__object_t_key_type.cpp +++ b/docs/mkdocs/docs/examples/value__object_t_key_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/examples/value__return_type.cpp b/docs/mkdocs/docs/examples/value__return_type.cpp index 7947e1d93..2818e2271 100644 --- a/docs/mkdocs/docs/examples/value__return_type.cpp +++ b/docs/mkdocs/docs/examples/value__return_type.cpp @@ -1,7 +1,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -9,6 +9,6 @@ int main() std::cout << "operator[]: " << j["uint64"] << '\n' << "default value (int): " << j.value("uint64", 0) << '\n' - << "default value (uint64_t): " << j.value("uint64", std::uint64_t(0)) << '\n' + << "default value (uint64_t): " << j.value("uint64", static_cast(0)) << '\n' << "explicit return value type: " << j.value("uint64", 0) << '\n'; } diff --git a/docs/mkdocs/docs/examples/with_t.cpp b/docs/mkdocs/docs/examples/with_t.cpp index 665efde3e..2be58f944 100644 --- a/docs/mkdocs/docs/examples/with_t.cpp +++ b/docs/mkdocs/docs/examples/with_t.cpp @@ -14,5 +14,5 @@ int main() j["b"] = 3; // keys are sorted, because std::map is used to store the object - std::cout << j.dump() << std::endl; + std::cout << j.dump() << '\n'; } diff --git a/docs/mkdocs/docs/features/arbitrary_types.md b/docs/mkdocs/docs/features/arbitrary_types.md index 0ca165902..9d918506f 100644 --- a/docs/mkdocs/docs/features/arbitrary_types.md +++ b/docs/mkdocs/docs/features/arbitrary_types.md @@ -54,7 +54,7 @@ assert(p == p2); To make this work with one of your types, you only need to provide two functions: ```cpp -using json = nlohmann::json; +using nlohmann::json; namespace ns { void to_json(json& j, const person& p) { @@ -409,6 +409,32 @@ namespace nlohmann { } ``` +## Why isn't my `to_json` or `from_json` found? + +The library calls `to_json` and `from_json` without qualification, so the compiler finds them by +[argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl) in the namespace of the type, or in the +namespaces of its template arguments. The usual reasons for a function that is "not found" are: + +- **The type is an alias of a standard type.** After `#!cpp using Hash = std::array;` the type + *is* `std::array`, which lives in namespace `std`. A `to_json(json&, const Hash&)` in your namespace + is never selected. Either give the type its own identity, for example `#!cpp struct Hash : std::array {};` + (the function is then found in `Hash`'s namespace), or specialize + [`adl_serializer`](#how-do-i-convert-third-party-types). +- **The function is in the wrong namespace.** It must be in the namespace of the type (a global type needs a global + function), not in the namespace of the code that calls the conversion. Specializations of `adl_serializer` belong in + namespace `nlohmann`. +- **It is declared too late.** A conversion can only use what has been declared when it is instantiated. If + `#!cpp json j = std::vector{...};` appears before the `adl_serializer` specialization, the + compiler reports an error such as "explicit specialization of `adl_serializer<...>` after instantiation". Put the + specialization in a small header that every file includes **before** it uses the type with `json`. +- **It is a template.** For `template struct box`, write `template void to_json(json&, const box&)`, + not an overload for one `T`. +- **It worked by accident.** Some versions found `to_json` overloads in `nlohmann::detail` through argument-dependent + lookup of the base class of `basic_json`. Do not rely on this; it is not part of the interface. + +For smart pointers, class hierarchies, `std::variant`, and `std::chrono`, see +[Recipes for common types](type_recipes.md). + ## Why can't I convert to/from `std::any`? `std::any` is intentionally excluded from `get()`/generic conversion support, so `get()` and @@ -505,6 +531,7 @@ struct bad_serializer ## See also +- [Recipes for common types](type_recipes.md) - smart pointers, class hierarchies, `std::variant`, and `std::chrono` - [Converting values](conversions.md) - the general overview of `get`/`get_to` and implicit conversions - [Specializing enum conversion](enum_conversion.md) - map enums to JSON strings instead of integers - [Supported macros](macros.md) - reference for `NLOHMANN_DEFINE_TYPE_*` and related macros diff --git a/docs/mkdocs/docs/features/assertions.md b/docs/mkdocs/docs/features/assertions.md index dadb0bd87..c551fef30 100644 --- a/docs/mkdocs/docs/features/assertions.md +++ b/docs/mkdocs/docs/features/assertions.md @@ -7,7 +7,9 @@ detect bugs in client code. ## Switch off runtime assertions Runtime assertions can be switched off by defining the preprocessor macro `NDEBUG` (see the -[documentation of assert](https://en.cppreference.com/w/cpp/error/assert)) which is the default for release builds. +[documentation of assert](https://en.cppreference.com/w/cpp/error/assert)) which is the default for release builds. Without `NDEBUG`, the assertions are compiled in and contain the name of the source +file (`__FILE__`); see [notes for builds and distribution](../integration/index.md#notes-for-builds-and-distribution) +if that path must not end up in your binaries. ## Change assertion behavior @@ -35,7 +37,7 @@ See also the documentation on [element access](element_access/index.md). ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -57,7 +59,7 @@ See also the documentation on [element access](element_access/index.md). ```cpp #include - using json = nlohmann::json; + using nlohmann::json; using namespace nlohmann::literals; int main() @@ -85,7 +87,7 @@ uninitialized iterator is undefined behavior and yields a runtime assertion. ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -112,7 +114,7 @@ behavior and yields a runtime assertion. ```cpp #include - using json = nlohmann::json; + using nlohmann::json; int main() { @@ -144,7 +146,7 @@ result in undefined behavior. Since version 3.12.0, this library checks for `nul #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/features/binary_formats/bjdata.md b/docs/mkdocs/docs/features/binary_formats/bjdata.md index 4591652eb..3a7a1798a 100644 --- a/docs/mkdocs/docs/features/binary_formats/bjdata.md +++ b/docs/mkdocs/docs/features/binary_formats/bjdata.md @@ -17,6 +17,39 @@ simple processing. - [BJData Specification](https://neurojson.org/bjdata/draft2) +## Byte layout + +BJData follows the structure of UBJSON: a one-byte, human-readable ASCII *type marker* followed by the payload of that type, with strings prefixed by a length that is itself an integer value. In contrast to UBJSON, numbers are stored in little-endian byte order, and additional markers cover unsigned 16/32/64-bit integers, half-precision floats, and bytes. Containers can be written unoptimized (opened with `[` or `{`, closed with `]` or `}`) or optimized with a declared element type (`$`) and/or count (`#`). BJData extends the optimized form: the count can be a list of dimensions, which stores an N-dimensional packed array as a flat sequence of values without any per-element markers. + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_bjdata` encodes it to 51 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{7Bobject start ({)
"id": 300,69 02 69 64 49 2C 01key: length 2 as int8 (i), then UTF-8 bytes; value: int16 (I), little-endian
"name": "json",69 04 6E 61 6D 65 53 69 04 6A 73 6F 6Ekey: length 4; value: string (S), length 4 as int8, UTF-8 bytes
"pi": 3.14,69 02 70 69 44 1F 85 EB 51 B8 1E 09 40key: length 2; value: float64 (D), little-endian
"tags": [69 04 74 61 67 73 5Bkey: length 4; value: array start ([)
"a",53 69 01 61string, length 1
"b"53 69 01 62string, length 1
]5Darray end (])
}7Dobject end (})
+ ## Serialization The library uses the following mapping from JSON values types to BJData types according to the BJData specification: @@ -190,41 +223,6 @@ The library uses the following mapping from JSON values types to BJData types ac --8<-- "examples/to_bjdata.output" ``` - - - - -### Byte layout - -Consider the following JSON value (shown formatted for readability): - -```json -{ - "id": 300, - "name": "json", - "pi": 3.14, - "tags": [ - "a", - "b" - ] -} -``` - -`to_bjdata` encodes it to 51 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. - -
- - - - - - - - - - -
JSONBytesMeaning
{7Bobject start ({)
"id": 300,69 02 69 64 49 2C 01key: length 2 as int8 (i), then UTF-8 bytes; value: int16 (I), little-endian
"name": "json",69 04 6E 61 6D 65 53 69 04 6A 73 6F 6Ekey: length 4; value: string (S), length 4 as int8, UTF-8 bytes
"pi": 3.14,69 02 70 69 44 1F 85 EB 51 B8 1E 09 40key: length 2; value: float64 (D), little-endian
"tags": [69 04 74 61 67 73 5Bkey: length 4; value: array start ([)
"a",53 69 01 61string, length 1
"b"53 69 01 62string, length 1
]5Darray end (])
}7Dobject end (})
- ## Deserialization The library maps BJData types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/bon8.md b/docs/mkdocs/docs/features/binary_formats/bon8.md index 555942def..b972dc035 100644 --- a/docs/mkdocs/docs/features/binary_formats/bon8.md +++ b/docs/mkdocs/docs/features/binary_formats/bon8.md @@ -10,6 +10,39 @@ values -1.0, 0.0, and 1.0 take a single byte, and arrays and objects with up to - [BON8 specification](https://github.com/hikoworks/hikogui/blob/main/docs/BON8.md) - [Reference implementation](https://github.com/hikoworks/hikogui/blob/main/src/hikogui/codec/BON8.hpp) in HikoGUI +## Byte layout + +BON8 exploits the fact that some byte values can never start a UTF-8 character. These bytes serve as *type markers*, so strings can be stored as plain UTF-8 without a length prefix: a string ends at the first byte that cannot continue it (often the marker of the next value). Small integers, `true`, `false`, `null`, and a few common floats are a single byte. Arrays and objects are introduced by a marker; those with up to four elements encode their size in the marker and need no terminator, while larger ones are closed by the end marker `0xFE`. + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_bon8` encodes it to 35 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{8Aobject with 4 members
"id": 300,69 64 C4 04key: UTF-8 bytes; the string ends at the next byte, which cannot continue a UTF-8 character; value: 2-byte positive integer (lead byte, then a byte below 0x80)
"name": "json",6E 61 6D 65 FF 6A 73 6F 6E FFkey: UTF-8 bytes; 0xFF terminates the string as another string follows; value: UTF-8 bytes; terminated as a string key follows
"pi": 3.14,70 69 8F 40 09 1E B8 51 EB 85 1Fkey: UTF-8 bytes; value: binary64 (IEEE 754, big-endian); its marker also ends "pi"
"tags": [74 61 67 73 82key: UTF-8 bytes; value: array with 2 elements; its marker also ends "tags"
"a",61 FFUTF-8 bytes; terminated as a string follows
"b"62 FFUTF-8 bytes; terminated as nothing follows
]no end marker: the element count was given up front
}no end marker: the member count was given up front
+ ## Serialization The library uses the following mapping from JSON values types to BON8 types according to the BON8 specification: @@ -105,41 +138,6 @@ serialized as 0x81 0x65 0xFF, but `[1,2,3,4,"e"]` as 0x85 0x91 0x92 0x93 0x94 0x --8<-- "examples/to_bon8.output" ``` - - - - -### Byte layout - -Consider the following JSON value (shown formatted for readability): - -```json -{ - "id": 300, - "name": "json", - "pi": 3.14, - "tags": [ - "a", - "b" - ] -} -``` - -`to_bon8` encodes it to 35 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. - -
- - - - - - - - - - -
JSONBytesMeaning
{8Aobject with 4 members
"id": 300,69 64 C4 04key: UTF-8 bytes; the string ends at the next byte, which cannot continue a UTF-8 character; value: 2-byte positive integer (lead byte, then a byte below 0x80)
"name": "json",6E 61 6D 65 FF 6A 73 6F 6E FFkey: UTF-8 bytes; 0xFF terminates the string as another string follows; value: UTF-8 bytes; terminated as a string key follows
"pi": 3.14,70 69 8F 40 09 1E B8 51 EB 85 1Fkey: UTF-8 bytes; value: binary64 (IEEE 754, big-endian); its marker also ends "pi"
"tags": [74 61 67 73 82key: UTF-8 bytes; value: array with 2 elements; its marker also ends "tags"
"a",61 FFUTF-8 bytes; terminated as a string follows
"b"62 FFUTF-8 bytes; terminated as nothing follows
]no end marker: the element count was given up front
}no end marker: the member count was given up front
- ## Deserialization The library maps BON8 types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/bson.md b/docs/mkdocs/docs/features/binary_formats/bson.md index 7a9f78d9a..9c82594dd 100644 --- a/docs/mkdocs/docs/features/binary_formats/bson.md +++ b/docs/mkdocs/docs/features/binary_formats/bson.md @@ -10,6 +10,39 @@ representation of data types that are not part of the JSON spec. For example, BS - [BSON Specification](http://bsonspec.org/spec.html) - the specification +## Byte layout + +A BSON document is a length-prefixed sequence of *elements*. The first four bytes hold the total size of the document (little-endian int32), followed by the elements, and a final `0x00` byte ends the document. Each element starts with a one-byte type, followed by its key as a NUL-terminated string and the value, whose encoding depends on the type. Arrays are documents whose keys are the decimal indices `"0"`, `"1"`, ...; nesting is expressed by embedding documents. Because every document announces its size, a reader can skip over it without parsing its content. The top level of a BSON value is always a document, which is why only JSON objects can be serialized. + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_bson` encodes it to 69 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{45 00 00 00document size: 69 bytes (int32, little-endian)
"id": 300,10 69 64 00 2C 01 00 00int32 element; key as NUL-terminated string; value (little-endian)
"name": "json",02 6E 61 6D 65 00 05 00 00 00 6A 73 6F 6E 00string element; key; length incl. NUL (int32); NUL-terminated UTF-8 bytes
"pi": 3.14,01 70 69 00 1F 85 EB 51 B8 1E 09 40double element; key; value (little-endian)
"tags": [04 74 61 67 73 00 17 00 00 00array element; key; embedded document of 23 bytes (keys are the indices)
"a",02 30 00 02 00 00 00 61 00string element; key "0"; length 2; a + NUL
"b"02 31 00 02 00 00 00 62 00string element; key "1"; length 2; b + NUL
]00end of the array document
}00end of the document
+ ## Serialization The library uses the following mapping from JSON values types to BSON types: @@ -60,42 +93,6 @@ The library uses the following mapping from JSON values types to BSON types: --8<-- "examples/to_bson.output" ``` - - - - - -### Byte layout - -Consider the following JSON value (shown formatted for readability): - -```json -{ - "id": 300, - "name": "json", - "pi": 3.14, - "tags": [ - "a", - "b" - ] -} -``` - -`to_bson` encodes it to 69 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. - -
- - - - - - - - - - -
JSONBytesMeaning
{45 00 00 00document size: 69 bytes (int32, little-endian)
"id": 300,10 69 64 00 2C 01 00 00int32 element; key as NUL-terminated string; value (little-endian)
"name": "json",02 6E 61 6D 65 00 05 00 00 00 6A 73 6F 6E 00string element; key; length incl. NUL (int32); NUL-terminated UTF-8 bytes
"pi": 3.14,01 70 69 00 1F 85 EB 51 B8 1E 09 40double element; key; value (little-endian)
"tags": [04 74 61 67 73 00 17 00 00 00array element; key; embedded document of 23 bytes (keys are the indices)
"a",02 30 00 02 00 00 00 61 00string element; key "0"; length 2; a + NUL
"b"02 31 00 02 00 00 00 62 00string element; key "1"; length 2; b + NUL
]00end of the array document
}00end of the document
- ## Deserialization The library maps BSON record types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/cbor.md b/docs/mkdocs/docs/features/binary_formats/cbor.md index 20f9230fc..8933630da 100644 --- a/docs/mkdocs/docs/features/binary_formats/cbor.md +++ b/docs/mkdocs/docs/features/binary_formats/cbor.md @@ -9,6 +9,39 @@ extremely small code sizes, fairly small message size, and extensibility without - [CBOR Playground](http://cbor.me) - an interactive webpage to translate between JSON and CBOR - [RFC 8949](https://www.rfc-editor.org/rfc/rfc8949.html) - the CBOR specification +## Byte layout + +CBOR encodes every data item as a *head* followed by optional content. The first byte of the head holds the *major type* in its upper three bits (unsigned integer, negative integer, byte string, text string, array, map, tag, or simple/float) and an *additional information* value in its lower five bits. Additional information below 24 is the argument itself (for example, a small integer or a short length); the values 24 to 27 announce that the argument follows in 1, 2, 4, or 8 big-endian bytes. Strings, arrays, and maps are prefixed with their length (or count of elements/pairs), and content follows directly, so a reader always knows how many items to expect without a terminator. + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_cbor` encodes it to 39 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{A4map with 4 key/value pairs
"id": 300,62 69 64 19 01 2Ckey: text string of length 2, then its UTF-8 bytes; value: unsigned integer, 2 bytes follow (big-endian)
"name": "json",64 6E 61 6D 65 64 6A 73 6F 6Ekey: text string of length 4; value: text string of length 4
"pi": 3.14,62 70 69 FB 40 09 1E B8 51 EB 85 1Fkey: text string of length 2; value: double-precision float (IEEE 754, big-endian)
"tags": [64 74 61 67 73 82key: text string of length 4; value: array with 2 elements
"a",61 61text string of length 1
"b"61 62text string of length 1
]no end marker: the element count was given up front
}no end marker: the member count was given up front
+ ## Serialization The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification @@ -110,41 +143,6 @@ see "binary" cells in the table above. --8<-- "examples/to_cbor.output" ``` - - - - -### Byte layout - -Consider the following JSON value (shown formatted for readability): - -```json -{ - "id": 300, - "name": "json", - "pi": 3.14, - "tags": [ - "a", - "b" - ] -} -``` - -`to_cbor` encodes it to 39 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. - -
- - - - - - - - - - -
JSONBytesMeaning
{A4map with 4 key/value pairs
"id": 300,62 69 64 19 01 2Ckey: text string of length 2, then its UTF-8 bytes; value: unsigned integer, 2 bytes follow (big-endian)
"name": "json",64 6E 61 6D 65 64 6A 73 6F 6Ekey: text string of length 4; value: text string of length 4
"pi": 3.14,62 70 69 FB 40 09 1E B8 51 EB 85 1Fkey: text string of length 2; value: double-precision float (IEEE 754, big-endian)
"tags": [64 74 61 67 73 82key: text string of length 4; value: array with 2 elements
"a",61 61text string of length 1
"b"61 62text string of length 1
]no end marker: the element count was given up front
}no end marker: the member count was given up front
- ## Deserialization The library maps CBOR types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/messagepack.md b/docs/mkdocs/docs/features/binary_formats/messagepack.md index 3bdd1a1be..90e4c295e 100644 --- a/docs/mkdocs/docs/features/binary_formats/messagepack.md +++ b/docs/mkdocs/docs/features/binary_formats/messagepack.md @@ -9,6 +9,39 @@ extra byte in addition to the strings themselves. - [MessagePack website](https://msgpack.org) - [MessagePack specification](https://github.com/msgpack/msgpack/blob/master/spec.md) +## Byte layout + +MessagePack encodes each value as a one-byte *format marker*, optionally followed by a length or the payload. The marker range is partitioned to keep common values small: small non-negative integers (0 to 127) and small negative integers (-32 to -1) are the marker byte itself; short strings, arrays, and maps (up to 31 or 15 elements) hide their length in the marker's lower bits. Larger values use dedicated markers that announce the width (8, 16, 32, or 64 bits) of the length or number that follows, in big-endian byte order. Like CBOR, containers are length-prefixed, so no terminator is needed. + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_msgpack` encodes it to 39 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{84map with 4 key/value pairs
"id": 300,A2 69 64 CD 01 2Ckey: fixstr of length 2, then its UTF-8 bytes; value: uint 16 (big-endian)
"name": "json",A4 6E 61 6D 65 A4 6A 73 6F 6Ekey: fixstr of length 4; value: fixstr of length 4
"pi": 3.14,A2 70 69 CB 40 09 1E B8 51 EB 85 1Fkey: fixstr of length 2; value: float 64 (IEEE 754, big-endian)
"tags": [A4 74 61 67 73 92key: fixstr of length 4; value: fixarray with 2 elements
"a",A1 61fixstr of length 1
"b"A1 62fixstr of length 1
]no end marker: the element count was given up front
}no end marker: the member count was given up front
+ ## Serialization The library uses the following mapping from JSON values types to MessagePack types according to the MessagePack @@ -91,41 +124,6 @@ specification: --8<-- "examples/to_msgpack.output" ``` - - - - -### Byte layout - -Consider the following JSON value (shown formatted for readability): - -```json -{ - "id": 300, - "name": "json", - "pi": 3.14, - "tags": [ - "a", - "b" - ] -} -``` - -`to_msgpack` encodes it to 39 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. - -
- - - - - - - - - - -
JSONBytesMeaning
{84map with 4 key/value pairs
"id": 300,A2 69 64 CD 01 2Ckey: fixstr of length 2, then its UTF-8 bytes; value: uint 16 (big-endian)
"name": "json",A4 6E 61 6D 65 A4 6A 73 6F 6Ekey: fixstr of length 4; value: fixstr of length 4
"pi": 3.14,A2 70 69 CB 40 09 1E B8 51 EB 85 1Fkey: fixstr of length 2; value: float 64 (IEEE 754, big-endian)
"tags": [A4 74 61 67 73 92key: fixstr of length 4; value: fixarray with 2 elements
"a",A1 61fixstr of length 1
"b"A1 62fixstr of length 1
]no end marker: the element count was given up front
}no end marker: the member count was given up front
- ## Deserialization The library maps MessagePack types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/ubjson.md b/docs/mkdocs/docs/features/binary_formats/ubjson.md index 6b7973627..ba12ee4f6 100644 --- a/docs/mkdocs/docs/features/binary_formats/ubjson.md +++ b/docs/mkdocs/docs/features/binary_formats/ubjson.md @@ -7,6 +7,39 @@ achieve the generality of JSON, combined with being much easier to process than - [UBJSON Website](http://ubjson.org) +## Byte layout + +UBJSON is built from just a one-byte, human-readable ASCII *type marker* (for example, `i` for int8, `S` for string, `[` for array) followed by the payload of that type. Strings and the like are prefixed with their length, which is itself a UBJSON integer value including its marker. Numbers are stored in big-endian byte order. Containers can be written in two ways: as an *unoptimized* container that is opened with `[` or `{` and closed with `]` or `}`, or as an *optimized* container that declares an element type (`$`) and/or a count (`#`) after the opening marker, which saves per-element markers and (for a count) the closing marker. + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_ubjson` encodes it to 51 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{7Bobject start ({)
"id": 300,69 02 69 64 49 01 2Ckey: length 2 as int8 (i), then UTF-8 bytes; value: int16 (I), big-endian
"name": "json",69 04 6E 61 6D 65 53 69 04 6A 73 6F 6Ekey: length 4; value: string (S), length 4 as int8, UTF-8 bytes
"pi": 3.14,69 02 70 69 44 40 09 1E B8 51 EB 85 1Fkey: length 2; value: float64 (D), big-endian
"tags": [69 04 74 61 67 73 5Bkey: length 4; value: array start ([)
"a",53 69 01 61string, length 1
"b"53 69 01 62string, length 1
]5Darray end (])
}7Dobject end (})
+ ## Serialization The library uses the following mapping from JSON values types to UBJSON types according to the UBJSON specification: @@ -103,41 +136,6 @@ The library uses the following mapping from JSON values types to UBJSON types ac --8<-- "examples/to_ubjson.output" ``` - - - - -### Byte layout - -Consider the following JSON value (shown formatted for readability): - -```json -{ - "id": 300, - "name": "json", - "pi": 3.14, - "tags": [ - "a", - "b" - ] -} -``` - -`to_ubjson` encodes it to 51 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. - -
- - - - - - - - - - -
JSONBytesMeaning
{7Bobject start ({)
"id": 300,69 02 69 64 49 01 2Ckey: length 2 as int8 (i), then UTF-8 bytes; value: int16 (I), big-endian
"name": "json",69 04 6E 61 6D 65 53 69 04 6A 73 6F 6Ekey: length 4; value: string (S), length 4 as int8, UTF-8 bytes
"pi": 3.14,69 02 70 69 44 40 09 1E B8 51 EB 85 1Fkey: length 2; value: float64 (D), big-endian
"tags": [69 04 74 61 67 73 5Bkey: length 4; value: array start ([)
"a",53 69 01 61string, length 1
"b"53 69 01 62string, length 1
]5Darray end (])
}7Dobject end (})
- ## Deserialization The library maps UBJSON types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/conversions.md b/docs/mkdocs/docs/features/conversions.md index a2d848da5..c0d383a5a 100644 --- a/docs/mkdocs/docs/features/conversions.md +++ b/docs/mkdocs/docs/features/conversions.md @@ -124,6 +124,29 @@ Implicit conversions are convenient but can be surprising (for example, in overl be disabled by defining [`JSON_USE_IMPLICIT_CONVERSIONS`](../api/macros/json_use_implicit_conversions.md) to `#!cpp 0`, which forces the explicit `get` form and can catch unintended conversions at compile time. +!!! warning "`auto` copies the JSON value; it does not convert it" + + The type of `#!cpp j["name"]` is `json`, not the C++ type of the value inside. `#!cpp auto` therefore deduces + `json`: + + ```cpp + json j = {{"name", "Mary"}, {"age", 42}}; + + auto name = j["name"]; // json: a copy of the value; prints as "Mary" (with quotes) + std::string s1 = j["name"]; // std::string: implicit conversion + auto s2 = j["name"].get(); // std::string: explicit conversion + auto& age = j["age"]; // json&: a reference, no copy + age = 43; // changes j + ``` + + This is also why `#!cpp std::cout << j["name"]` prints the quotes (see + [getting a string without quotes](#getting-values-out)), and why a `json` value cannot be passed to `printf`: the + argument is not an `int` or a `char*`, and a compiler either rejects passing it through `...` (Clang does) or + compiles it and prints garbage. Convert first: `#!cpp std::printf("%d", j["age"].get());`. + + Prefer `get()` over `auto` when you want the C++ value, and `auto&` or `const auto&` when you want to avoid a + copy of a large value. + !!! warning "Conversions do not range-check numbers" Just like C++ itself, the `get` family performs numeric conversions without range checks — retrieving a diff --git a/docs/mkdocs/docs/features/creating_values.md b/docs/mkdocs/docs/features/creating_values.md index 42a927b78..fd4c32cd8 100644 --- a/docs/mkdocs/docs/features/creating_values.md +++ b/docs/mkdocs/docs/features/creating_values.md @@ -61,6 +61,25 @@ with a string as the first element is treated as an object, everything else as a [FAQ](../home/faq.md#brace-initialization-yields-arrays) for details and the opt-in [`JSON_BRACE_INIT_COPY_SEMANTICS`](../api/macros/json_brace_init_copy_semantics.md) macro. +### What does this produce? + +The following table shows how some common spellings are interpreted. When in doubt, use `#!cpp json::array()`, +`#!cpp json::object()`, or the `#!cpp json(value)` form with parentheses. + +| code | result | note | +|-------------------------------------------------|------------------------------|----------------------------------------------| +| `#!cpp json j = {};` / `#!cpp json j{};` | `#!json null` | use `json::object()` or `json::array()` | +| `#!cpp json j = json::object();` | `#!json {}` | | +| `#!cpp json j = json::array();` | `#!json []` | | +| `#!cpp json j(true);` | `#!json true` | parentheses create the value itself | +| `#!cpp json j = {true};` | `#!json [true]` | braces create a one-element array (`#!cpp json j{true};` depends on the compiler, see the [FAQ](../home/faq.md#brace-initialization-yields-arrays)) | +| `#!cpp json j = {{"a", 1}};` | `#!json {"a":1}` | all elements are `[string, value]` pairs: object | +| `#!cpp json j = {{"a", 1}, {"b"}};` | `#!json [["a",1],["b"]]` | one element is not a pair: array | +| `#!cpp json j = {{"a", 1}, 2};` | `#!json [["a",1],2]` | a plain value among pairs: array | +| `#!cpp json j = {{"a", 1, 2}};` | `#!json [["a",1,2]]` | a three-element list is not a pair: array | +| `#!cpp json j = "{\"a\":1}";` | `#!json "{\"a\":1}"` | a string **value**; it is not parsed | +| `#!cpp json j = json::parse("{\"a\":1}");` | `#!json {"a":1}` | parse text with `parse` or `_json` | + ## Building incrementally A value can also be built up piece by piece. Accessing a non-existing object key or array index with diff --git a/docs/mkdocs/docs/features/element_access/index.md b/docs/mkdocs/docs/features/element_access/index.md index 17d0a56db..4ec625a20 100644 --- a/docs/mkdocs/docs/features/element_access/index.md +++ b/docs/mkdocs/docs/features/element_access/index.md @@ -22,6 +22,45 @@ flowchart TD F -->|"yes"| G["contains() / find()"] ``` +## Does this key exist? + +Use [`contains`](../../api/basic_json/contains.md) to test for a key; it does not modify the value and does not throw: + +```cpp +json j = {{"name", "Mary"}, {"address", {{"city", "Berlin"}}}}; + +j.contains("name"); // true +j.contains("age"); // false +j.contains("city"); // false: contains() only looks at the top level +j["address"].contains("city"); // true +j.contains(json::json_pointer("/address/city")); // true: a JSON pointer tests a nested path in one call +``` + +If the value is needed afterwards, [`find`](../../api/basic_json/find.md) tests and accesses with a single lookup: + +```cpp +if (auto it = j.find("name"); it != j.end()) +{ + std::cout << *it << '\n'; +} +``` + +`contains` also returns `true` for a key whose value is `null`; use `j.contains("k") && !j["k"].is_null()` if a +`null` should count as missing. To read a value with a fallback instead, see [`value`](default_value.md). + +!!! warning "`operator[]` inserts missing keys" + + Like `#!cpp std::map::operator[]`, the non-const [`operator[]`](unchecked_access.md) adds a `null` value if the key + is missing. Testing a key with it therefore *changes the object*: + + ```cpp + json j = {{"foo", 1}}; + bool absent = j["bar"].is_null(); // true, but j is now {"bar": null, "foo": 1} + ``` + + Use `contains`/`find` to test, and [`at`](checked_access.md) to access a value that must exist (it throws an + [`out_of_range`](../../home/exceptions.md#out-of-range) exception otherwise). + ## See also - [**operator[]**](../../api/basic_json/operator%5B%5D.md) - unchecked access diff --git a/docs/mkdocs/docs/features/modules.md b/docs/mkdocs/docs/features/modules.md index 28b538a2e..b87cb8950 100644 --- a/docs/mkdocs/docs/features/modules.md +++ b/docs/mkdocs/docs/features/modules.md @@ -15,7 +15,7 @@ When using modules rather than headers, the previous example for creating a `jso import std; import nlohmann.json; -using json = nlohmann::json; +using nlohmann::json; // ... diff --git a/docs/mkdocs/docs/features/no_exceptions.md b/docs/mkdocs/docs/features/no_exceptions.md new file mode 100644 index 000000000..cfb489876 --- /dev/null +++ b/docs/mkdocs/docs/features/no_exceptions.md @@ -0,0 +1,113 @@ +# Working Without Exceptions + +By default, the library reports errors by throwing exceptions (see [Exceptions](../home/exceptions.md)). This page +collects what to use when exceptions are unwanted or unavailable, for example in embedded systems, game engines, or +code compiled with `-fno-exceptions`. + +## Two different questions + +1. **Can my code avoid *catching* exceptions?** Yes. For parsing and for accessing values there are functions that + report failure through a return value. They work in a normal build and need no `#!cpp try`/`#!cpp catch`. +2. **Can the library be *compiled* without exceptions?** Yes, with `-fno-exceptions` or by defining + [`JSON_NOEXCEPTION`](../api/macros/json_noexception.md). Both have the same effect: every `#!cpp throw` in the + library becomes a call to `std::abort()`. The functions listed below never reach a `#!cpp throw` for the failures + they report, so they keep working in this configuration. Anything else that fails aborts the program. + +## Parsing + +| function | on invalid input | +|---------------------------------------------------------------------------------------------|-----------------------------------------------------| +| `#!cpp json::parse(input, nullptr, false)` | returns a *discarded* value; test with `is_discarded()` | +| [`accept`](../api/basic_json/accept.md) | returns `#!cpp false` | +| `#!cpp json::from_cbor(input, true, false)` (and the other `from_*` functions) | returns a *discarded* value | +| [`sax_parse`](../api/basic_json/sax_parse.md) with your own handler | returns `#!cpp false` after calling `parse_error` | + +??? example "Example: parse and validate without exceptions" + + ```cpp + --8<-- "examples/no_exceptions_parse.cpp" + ``` + + Output: + + ``` + --8<-- "examples/no_exceptions_parse.output" + ``` + +None of the calls in this example throws, so it also compiles and runs with `-fno-exceptions`. + +The discarded value carries no message. If you need the position or the reason, use the SAX interface; see +[Parsing and exceptions](parsing/parse_exceptions.md). + +## Accessing values + +The access functions are not equally safe. Only some of them never throw: + +| expression | if the key is missing or the type is wrong | +|-------------------------------------|-------------------------------------------------------------------------------| +| `#!cpp j.contains("k")` | returns `#!cpp false` for a missing key; never throws | +| `#!cpp j.find("k")` | returns `end()` for a missing key; never throws | +| `#!cpp j.is_string()`, ... | the `is_*` functions test the type; never throw | +| `#!cpp j.get_ptr()` | returns `#!cpp nullptr` for a different type; never throws | +| `#!cpp j.at("k")` | throws `out_of_range.403` for a missing key | +| `#!cpp j.get()` | throws `type_error.302` for a value that is not a number | +| `#!cpp j.value("k", 0)` | returns the default for a missing key, but **throws `type_error.302` if `k` exists with another type** | +| `#!cpp j["k"]` (non-const) | inserts `null` for a missing key | +| `#!cpp j["k"]` (const) | undefined behavior for a missing key | + +So reading a value that may be absent or of the wrong type without exceptions takes two steps: look up, then test the +type. + +??? example "Example: look up a key and test its type" + + ```cpp + --8<-- "examples/no_exceptions_access.cpp" + ``` + + Output: + + ``` + --8<-- "examples/no_exceptions_access.output" + ``` + +The last lines of the example show why `value` is not on the safe list: it returns the default only for a *missing* +key. + +For numbers, test with `is_number()` (or `is_number_integer()`, `is_number_unsigned()`, `is_number_float()`) before +calling `get()`, and see [number handling](types/number_handling.md) for range and precision. Whether a value +fits into the target type is not checked by `get`. + +## Converting to your own types + +`from_json` functions (including the ones generated by the +[`NLOHMANN_DEFINE_TYPE_*` macros](arbitrary_types.md#simplify-your-life-with-macros)) report problems by throwing. +There is no variant that returns an error code. Without exceptions you have two options: + +- Validate the JSON value yourself, as shown above, and fill your struct field by field. +- Convert in a function that is compiled **with** exceptions enabled and catches them at the boundary, returning + `std::optional` or an error code to the rest of your code: + +??? example "Example: convert to a struct and report failure as std::optional" + + ```cpp + --8<-- "examples/no_exceptions_convert.c++17.cpp" + ``` + + Output: + + ``` + --8<-- "examples/no_exceptions_convert.c++17.output" + ``` + +## Overriding what happens instead of an exception + +With exceptions off, you can replace the `abort()` by your own code, for example to log the message first. See +[Switch off exceptions](../home/exceptions.md#switch-off-exceptions) and +[`JSON_THROW_USER`](../api/macros/json_throw_user.md). The handler must not return normally. + +## See also + +- [Exceptions](../home/exceptions.md) - all exceptions and their ids +- [Parsing and exceptions](parsing/parse_exceptions.md) - the parsing alternatives in detail +- [Parsing untrusted input](parsing/untrusted_input.md) +- [`JSON_NOEXCEPTION`](../api/macros/json_noexception.md) diff --git a/docs/mkdocs/docs/features/object_order.md b/docs/mkdocs/docs/features/object_order.md index b90637711..b9997238e 100644 --- a/docs/mkdocs/docs/features/object_order.md +++ b/docs/mkdocs/docs/features/object_order.md @@ -12,7 +12,7 @@ The default type `nlohmann::json` uses a `std::map` to store JSON objects, and t #include #include - using json = nlohmann::json; + using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/features/parsing/index.md b/docs/mkdocs/docs/features/parsing/index.md index 08a571545..447b65ffc 100644 --- a/docs/mkdocs/docs/features/parsing/index.md +++ b/docs/mkdocs/docs/features/parsing/index.md @@ -36,7 +36,9 @@ json data = json::parse(f); The input must be encoded in UTF-8; other encodings are not supported. The exception is wide-character input, which is read as UTF-16 or UTF-32 -- depending on the character type -- and converted to UTF-8; see the [`parse`](../../api/basic_json/parse.md) page for an example. A single input may contain only one JSON value. -Inputs consisting of multiple values separated by newlines are handled by the [JSON Lines](json_lines.md) format. +Inputs consisting of multiple values separated by newlines are handled by the [JSON Lines](json_lines.md) format; input +that arrives in chunks or contains several values is covered in +[Large, streamed, and partial input](large_and_partial_input.md). By default, the library rejects comments and trailing commas. Both can be enabled with parameters of the `parse` function — see [comments](../comments.md) and [trailing commas](../trailing_commas.md). @@ -92,7 +94,7 @@ again without changing any result. ## SAX vs. DOM parsing -The library offers two parsing models: +The library offers two parsing models, and a third, read-only alternative: - **DOM parsing** (the default): the complete input is read and stored as an in-memory `basic_json` value that can be traversed and modified freely. This is what [`parse`](../../api/basic_json/parse.md) does, and it is the right choice @@ -102,6 +104,13 @@ The library offers two parsing models: inputs or when you only need to extract parts of the input. See the [SAX interface](sax_interface.md) for details and [`sax_parse`](../../api/basic_json/sax_parse.md) for the API. +- **Document and views** (`#!cpp `, added in version 3.13.0): [`json_document::parse`](../../api/basic_json_document/parse.md) + reads the same JSON grammar but builds a flat, read-only index of the values instead of a `basic_json` tree. Strings and + floating-point numbers are read from the text on demand, and only the parts you + [`materialize()`](../../api/basic_json_view/materialize.md) become `basic_json` values. This avoids most of the work when you + need only a few values from a large or frequently parsed text; see [Zero-copy JSON views](../json_view.md). It is read-only, so use + `parse` when you need to modify the value. + You can influence a DOM parse without switching to the SAX interface by passing a [parser callback](parser_callbacks.md), which is called during parsing and can, for example, discard parts of the input. @@ -139,8 +148,10 @@ options. - [`accept`](../../api/basic_json/accept.md) - check if the input is valid JSON - [`sax_parse`](../../api/basic_json/sax_parse.md) - generate SAX events - [JSON Lines](json_lines.md) - parse newline-delimited JSON +- [Large, streamed, and partial input](large_and_partial_input.md) - chunked, concatenated, and very large input - [parser callbacks](parser_callbacks.md) - influence the parsing by a callback function - [SAX interface](sax_interface.md) - implement a custom SAX handler +- [Zero-copy JSON views](../json_view.md) - parse into a read-only document and read it through views - [parsing and exceptions](parse_exceptions.md) - control error handling - [parsing untrusted input](untrusted_input.md) - what to consider when parsing input from untrusted sources - [Zero-copy JSON views](../json_view.md) - parse into a document and read it through a view diff --git a/docs/mkdocs/docs/features/parsing/large_and_partial_input.md b/docs/mkdocs/docs/features/parsing/large_and_partial_input.md new file mode 100644 index 000000000..d24b5fab1 --- /dev/null +++ b/docs/mkdocs/docs/features/parsing/large_and_partial_input.md @@ -0,0 +1,154 @@ +# Large, Streamed, and Partial Input + +[`parse`](../../api/basic_json/parse.md) reads exactly one complete JSON value and builds it in memory. It cannot be +paused and resumed, and it does not hand back a half-parsed value when the input ends early. This page shows what to do +when the input does not fit that model: it arrives in pieces, it contains several values, or it is too large to hold +as a `json` value. + +```mermaid +flowchart TD + A["my input is..."] --> B{"several JSON values in one stream?"} + B -->|"yes"| C["read them one at a time"] + A --> D{"arriving in chunks (socket, pipe)?"} + D -->|"yes"| E["frame the messages, or buffer until complete"] + A --> F{"too large to keep in memory?"} + F -->|"yes"| G["discard parts with a callback, or use SAX"] + A --> H{"only a small part is needed?"} + H -->|"yes"| I["stop early with SAX, or read through a view"] +``` + +## Several values in one stream + +For newline-delimited input, read line by line and parse each line, as described in [JSON Lines](json_lines.md). For +values that are simply concatenated, [`operator>>`](../../api/operator_gtgt.md) reads one value and leaves the stream +positioned after it. Check for the end of the stream before each read, because a read at the end throws: + +??? example "Example: read concatenated values" + + ```cpp + --8<-- "examples/partial_input_concatenated.cpp" + ``` + + Output: + + ``` + --8<-- "examples/partial_input_concatenated.output" + ``` + +The same works for binary formats. [`from_cbor`](../../api/basic_json/from_cbor.md) and its relatives accept +`#!cpp strict = false` to stop after the first value and leave the rest of the stream untouched; `tellg()` then reports +how many bytes the value took: + +??? example "Example: read one CBOR value and find out how many bytes it used" + + ```cpp + --8<-- "examples/partial_input_cbor.cpp" + ``` + + Output: + + ``` + --8<-- "examples/partial_input_cbor.output" + ``` + +## Input arriving in chunks + +A socket, pipe, or HTTP body hands over data in pieces of arbitrary size, and a piece can end in the middle of a +number, a string, or an object. The parser does not keep state between calls, so there are two ways to deal with this. + +**Frame the messages.** The most robust approach is a protocol that says where a value ends: one value per line +([JSON Lines](json_lines.md)), a length prefix, or a delimiter. Collect the bytes of one message and parse them when +the frame is complete. Prefer this whenever you control the protocol. + +**Buffer until the value is complete.** If the data has no framing, append every chunk to a buffer and try to parse +it. A parse error at the very end of the buffer means that the input ended prematurely, while an error anywhere else is +a real syntax error. The member `byte` of the [`parse_error`](../../home/exceptions.md#jsonexceptionparse_error101) +exception is one past the buffer size in the first case: + +??? example "Example: buffer chunks until the value is complete" + + ```cpp + --8<-- "examples/partial_input_buffering.cpp" + ``` + + Output: + + ``` + --8<-- "examples/partial_input_buffering.output" + ``` + +The example feeds a document in five chunks. The first four are reported as incomplete, and a syntax error is reported as +such, not as missing data. + +!!! warning "Limits of buffering" + + - The buffer must contain exactly one value. Data that belongs to the next value is a syntax error. Use framing if + values follow each other without a separator. + - A number at the end of the buffer is a complete value, even if more digits are still to come (`12` may be the + start of `123`). Buffering cannot detect this; use framing for top-level numbers. + - Every attempt parses the buffer from the beginning, so the total work grows quadratically with the size of the + value. Limit the size of the buffer for input you do not trust (see [untrusted input](untrusted_input.md)). + +## Input too large for memory + +The input does not have to become one huge `json` value. The two tools below process it piece by piece. + +### Handle and discard elements with a callback + +A [parser callback](parser_callbacks.md) is called for every finished value. If it returns `#!cpp false`, the value is +discarded and its memory is released at once. For a file that consists of one huge array (or one huge object) of +records, handle each record at depth 1 and discard it: + +??? example "Example: handle and discard each record of a large array" + + ```cpp + --8<-- "examples/partial_input_discard_records.cpp" + ``` + + Output: + + ``` + --8<-- "examples/partial_input_discard_records.output" + ``` + +Only one record is in memory at any time. The same recipe, with more detail, is in the +[parser callbacks](parser_callbacks.md#recipe-streaming-a-large-homogeneous-array) page. To skip entire members you do not +need, discard them when their key is read; see the [example](parser_callbacks.md) for `parse_event_t::key`. + +### Extract a few values and stop with SAX + +With the [SAX interface](sax_interface.md), no value is built at all, and **returning `#!cpp false` from any handler +function stops the parser**. [`sax_parse`](../../api/basic_json/sax_parse.md) then returns `#!cpp false`, and the rest +of the input is not read. The example finds the first `id` in a document without parsing the remainder: + +??? example "Example: stop parsing as soon as a value is found" + + ```cpp + --8<-- "examples/partial_input_sax_stop.cpp" + ``` + + Output: + + ``` + --8<-- "examples/partial_input_sax_stop.output" + ``` + +Note that `sax_parse` returns `#!cpp false` both when a handler stopped the parse and when the input is invalid, so +record the reason in the handler (here: `id`) if you need to tell the two apart. + +### Read parts of a large document with a view + +If the whole text is available and you only need to read some of it, a [`json_document`](../json_view.md) from +`` (added in version 3.13.0) avoids the `basic_json` tree altogether: it keeps the text, builds a flat index of the values, +and gives read-only [views](../../api/basic_json_view/index.md) of the parts you look at. Only what you +[`materialize()`](../../api/basic_json_view/materialize.md) becomes a `basic_json` value. See +[Zero-copy JSON views](../json_view.md) for details and for the lifetime rules. + +## See also + +- [Parsing](index.md) - overview of the parsing functions +- [JSON Lines](json_lines.md) - newline-delimited JSON +- [Parser callbacks](parser_callbacks.md) - discard values while parsing +- [SAX interface](sax_interface.md) - event-based parsing +- [Zero-copy JSON views](../json_view.md) - read parts of a large document without building a tree +- [Untrusted input](untrusted_input.md) - limits for input you do not control diff --git a/docs/mkdocs/docs/features/parsing/parse_exceptions.md b/docs/mkdocs/docs/features/parsing/parse_exceptions.md index f524b67d8..eb0ae5f58 100644 --- a/docs/mkdocs/docs/features/parsing/parse_exceptions.md +++ b/docs/mkdocs/docs/features/parsing/parse_exceptions.md @@ -21,6 +21,10 @@ catch (json::parse_error& ex) In case exceptions are undesired or not supported by the environment, there are different ways to proceed: +!!! tip + + [Working without exceptions](../no_exceptions.md) also covers accessing values and converting to your own types. + ## Switch off exceptions The [`parse()`](../../api/basic_json/parse.md) function accepts a `#!cpp bool` parameter `allow_exceptions` which @@ -81,3 +85,61 @@ The return value indicates whether the parsing should continue, so the function ``` --8<-- "examples/sax_no_exception.output" ``` + +## Where did the error occur? + +The message of a [`parse_error`](../../home/exceptions.md#jsonexceptionparse_error101) for JSON text contains the line +and column of the error: + +``` +[json.exception.parse_error.101] parse error at line 3, column 8: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal +``` + +The exception object itself only stores the **byte index** of the last read character in the member `byte` (the first +byte of the input has index 1). The same index is passed to `parse_error` in the [SAX interface](sax_interface.md). If +you need the line and column as numbers, for example to show them in an editor, compute them from the input: + +??? example "Example: compute line and column of a parse error" + + ```cpp + --8<-- "examples/parse_error_position.cpp" + ``` + + Output: + + ``` + --8<-- "examples/parse_error_position.output" + ``` + +Columns count bytes, so a line with multi-byte UTF-8 characters before the error may differ from the column your editor +shows. + +### Where is the value that caused a later error? + +A `parse_error` is raised while the text is read. Many mistakes show up later: the text is valid JSON, but a value has +the wrong type when you convert it (`type_error.302`), or a key is missing (`out_of_range.403`). The exception then says +what is wrong, but not where in the input the value was. Define +[`JSON_DIAGNOSTIC_POSITIONS`](../../api/macros/json_diagnostic_positions.md) before including the library to add the +byte range of the offending value to such messages (since version 3.12.0), and to query the range of any value with +[`start_pos()`](../../api/basic_json/start_pos.md) and [`end_pos()`](../../api/basic_json/end_pos.md): + +??? example "Example: locate a value with diagnostic positions" + + ```cpp + --8<-- "examples/parse_error_position_diagnostic.cpp" + ``` + + Output: + + ``` + --8<-- "examples/parse_error_position_diagnostic.output" + ``` + +Positions are only available for values created by [`parse`](../../api/basic_json/parse.md), they are not updated when a +value is changed, and the macro makes every value 16 bytes larger on 64-bit platforms, so use it where the cost is +acceptable, for example for configuration files. Note that `start_pos()` is a 0-based index into the input, whereas +`byte` of a `parse_error` starts at 1. + +For binary formats such as CBOR, there are no lines; `byte` is the position in the byte sequence. To find out how many +bytes a successfully parsed value used, see +[Several values in one stream](large_and_partial_input.md#several-values-in-one-stream). diff --git a/docs/mkdocs/docs/features/parsing/untrusted_input.md b/docs/mkdocs/docs/features/parsing/untrusted_input.md index 551f9770e..65f1bda25 100644 --- a/docs/mkdocs/docs/features/parsing/untrusted_input.md +++ b/docs/mkdocs/docs/features/parsing/untrusted_input.md @@ -26,9 +26,11 @@ several alternatives; see [Parsing and exceptions](parse_exceptions.md) for the [example](parse_exceptions.md#user-defined-sax-interface) that overrides it to print instead of throw. If exceptions are unavailable entirely (`-fno-exceptions`, or [`JSON_NOEXCEPTION`](../../api/macros/json_noexception.md) -defined), every `#!cpp throw` in the library becomes a call to `std::abort()` -- there is no way to recover from a -parse error of untrusted input in that configuration; see -[Switch off exceptions](../../home/exceptions.md#switch-off-exceptions) for the details and for overriding this with +defined), every `#!cpp throw` in the library becomes a call to `std::abort()`. The alternatives above do not throw, so +`parse()` with `allow_exceptions` set to `#!cpp false` (and likewise `accept()` and the `from_*` functions with +`allow_exceptions` set to `#!cpp false`) can still reject invalid input in that configuration; a plain `parse()` call +aborts on invalid input. See [Working without exceptions](../no_exceptions.md) and +[Switch off exceptions](../../home/exceptions.md#switch-off-exceptions) for the details and for overriding the abort with `JSON_THROW_USER`. ## Nesting depth diff --git a/docs/mkdocs/docs/features/performance.md b/docs/mkdocs/docs/features/performance.md index a15fbf38b..7ad7b187e 100644 --- a/docs/mkdocs/docs/features/performance.md +++ b/docs/mkdocs/docs/features/performance.md @@ -122,7 +122,9 @@ an adapter, since their template argument order does not match what `basic_json` [linear in its size](../api/basic_json/basic_json.md#complexity) for the copy constructor but [constant](../api/basic_json/basic_json.md#complexity) for the move constructor. The same applies to assigning a large `#!cpp std::string`, `#!cpp std::vector`, or other container into a value: pass it as `#!cpp std::move(x)` - rather than `x` whenever `x` is no longer needed afterwards. + rather than `x` whenever `x` is no longer needed afterwards. Since version 3.13.0, converting an rvalue container + (`#!cpp json j = std::move(vec);`) moves the elements instead of copying them; the source is then left in a valid but + unspecified state. - **Access without copying.** [`get()`](../api/basic_json/get.md) returns a copy of the stored value converted to `T`. When a reference or pointer to the value already stored inside the `basic_json` is enough, [`get_ref()`](../api/basic_json/get_ref.md) and [`get_ptr()`](../api/basic_json/get_ptr.md) access it directly: @@ -151,6 +153,29 @@ an adapter, since their template argument order does not match what `basic_json` ["reserving array capacity"](element_access/unchecked_access.md#performance-reserving-array-capacity) for the one-line recipe. +## Memory + +- **A value is small, its content is on the heap.** A `basic_json` is 16 bytes on a 64-bit platform (a type tag and a + union). Strings, arrays, objects, and binary values are allocated on the heap and owned by the value. Destroying the + value frees everything it owns. +- **Copies are deep.** After `#!cpp json b = a;`, changing `b` does not change `a`, and both own their memory. Copying a + large value therefore doubles its memory; see [Avoiding copies](#avoiding-copies). `#!cpp auto x = j["key"];` is a + copy, `#!cpp auto& x = j["key"];` is not. +- **A moved-from value is `null`.** After `#!cpp json c = std::move(a);` the value `a` is `#!json null` and owns nothing. +- **Reading does not require a tree.** If you only read the data, parse it into a [`json_document`](json_view.md) + (`#!cpp `, added in version 3.13.0) instead of a `basic_json`. It stores 16 bytes per value, does not copy floating-point + numbers or strings without escape sequences out of the text, and creates no `basic_json` value except for the parts + you [`materialize()`](../api/basic_json_view/materialize.md). A document that borrows the text needs the text to + outlive it; see [Zero-copy JSON views](json_view.md). +- **Containers keep their capacity.** `clear()` and `erase()` on an array remove the elements, but the underlying + `#!cpp std::vector` keeps its allocated capacity, like `#!cpp std::vector::clear` does. To give the memory back, + assign a new value (`#!cpp j = json::array();`) or destroy the value. +- **Freed memory does not always go back to the operating system.** The library returns memory to the allocator + (`#!cpp std::allocator` by default, or the [one you configured](types/template_parameters.md)). Whether the process + size shrinks then depends on the allocator and the platform, as for any C++ program. A process that parses a large + document and drops it is not leaking if its resident size stays high; run it under a tool such as + [Valgrind](https://valgrind.org) or the sanitizers to tell a leak from retained memory. + ## Serialization [`dump()`](../api/basic_json/dump.md) with the default `#!cpp indent = -1` selects "the most compact representation" diff --git a/docs/mkdocs/docs/features/type_recipes.md b/docs/mkdocs/docs/features/type_recipes.md new file mode 100644 index 000000000..314425dc6 --- /dev/null +++ b/docs/mkdocs/docs/features/type_recipes.md @@ -0,0 +1,124 @@ +# Recipes for Common Types + +The library converts the standard containers, strings, numbers, tuples, `std::optional`, and `std::filesystem::path` +(the last two since version 3.12.0, C++17) out of the box, and your own types once you write conversion functions or use +the macros (see [Arbitrary type conversions](arbitrary_types.md)). It deliberately does **not** convert smart pointers, +`std::variant`, `std::chrono` types, or class hierarchies, because there is more than one reasonable JSON +representation for each of them. This page shows a representation for each that you can copy and adapt. + +All recipes use a specialization of [`adl_serializer`](../api/adl_serializer/index.md), which you may provide for types +you do not own, or free `to_json`/`from_json` functions in the namespace of the type. Place them where they are +visible before any code that converts the type; see +[Why isn't my `to_json` found?](arbitrary_types.md#why-isnt-my-to_json-or-from_json-found). + +## Smart pointers + +A `std::shared_ptr` or `std::unique_ptr` is written as the value it points to, or as `#!json null` for an empty +pointer, and read back by allocating a new `T`: + +??? example "Example: serialize shared_ptr and unique_ptr" + + ```cpp + --8<-- "examples/type_recipes_smart_pointers.c++14.cpp" + ``` + + Output: + + ``` + --8<-- "examples/type_recipes_smart_pointers.c++14.output" + ``` + +With this, containers of smart pointers, pointers to `const`, and members of these types in your own structs convert as +usual. A pointer to a base class cannot be filled this way, because `T` would have +to be created as a derived type; see the next section. + +Reading creates a new object. Two `shared_ptr`s that pointed to the same object are written twice and read back as two +separate objects. + +## Class hierarchies + +!!! tip "Derived classes whose type is known at compile time" + + If you only need to serialize the data members of a derived class, and the code always knows the concrete type, you do + not need a type tag. The [`NLOHMANN_DEFINE_DERIVED_TYPE_*` macros](arbitrary_types.md#simplify-your-life-with-macros) + (since version 3.12.0) add the members of the derived class to those of its base class. This section is for the case + where the concrete type is only known at run time, such as a `std::unique_ptr` that holds a circle or a + rectangle. + +JSON has no notion of types, so a value in a hierarchy needs a **type tag** that says which derived class to create. +Write the tag in the object, and select the class from it when reading: + +??? example "Example: serialize a class hierarchy with a type tag" + + ```cpp + --8<-- "examples/type_recipes_class_hierarchy.c++14.cpp" + ``` + + Output: + + ``` + --8<-- "examples/type_recipes_class_hierarchy.c++14.output" + ``` + +A `#!cpp std::vector>` converts to an array of tagged objects and back, creating a `circle` +and a `rectangle`. The same works for `std::shared_ptr`. Remember the virtual destructor: without it, deleting a +derived object through the base pointer is undefined behavior. + +## `std::variant` + +A variant has the same problem: the alternative must be recorded. If the alternatives are your own types, write a type +tag next to their members, exactly as for a hierarchy: + +??? example "Example: serialize a std::variant with a type tag" + + ```cpp + --8<-- "examples/type_recipes_variant.c++17.cpp" + ``` + + Output: + + ``` + --8<-- "examples/type_recipes_variant.c++17.output" + ``` + +In the example, the functions are found +because `geo` is the namespace of the template arguments of `#!cpp std::variant`; for a variant of only +standard types (for example `#!cpp std::variant`) there is no such namespace, and you need an +`adl_serializer` specialization or a wrapper type of your own. + +If the alternatives are JSON-native (numbers, strings, ...), a variant such as `#!cpp std::variant` +can instead be written as the plain value, and read by testing the JSON type (`is_number_integer()`, `is_string()`) +to choose the alternative. + +## `std::chrono` + +A duration is written as an object with the keys `unit` and `value`, for example `{"unit": "ms", "value": 1500}`, so +the JSON value is self-describing. The serializer names the unit of the period (`ns`, `us`, `ms`, `s`, `min`, `h`; +extend the list for other periods), and the deserializer rejects a value whose unit differs from the requested +duration type instead of silently misreading it: + +??? example "Example: serialize durations and system_clock time points" + + ```cpp + --8<-- "examples/type_recipes_chrono.cpp" + ``` + + Output: + + ``` + --8<-- "examples/type_recipes_chrono.output" + ``` + +A `system_clock::time_point` with millisecond precision is written as the duration since the Unix epoch, that is, as an object with `unit` and `value`. +The time point serializer is limited to `system_clock`, +because the epoch of other clocks (for example `steady_clock`) has no meaning outside the running program. + +If you prefer a text representation such as ISO 8601 (`2023-11-14T22:13:20Z`), write the string with +`#!cpp std::format("{:%FT%TZ}", t)` and read it with `std::chrono::parse` (both C++20), or with a date library, in the +same two functions. + +## See also + +- [Arbitrary type conversions](arbitrary_types.md) - the conversion mechanism and macros +- [Why isn't my `to_json` found?](arbitrary_types.md#why-isnt-my-to_json-or-from_json-found) +- [Converting values](conversions.md) - `get`, implicit conversions, and `std::optional` diff --git a/docs/mkdocs/docs/home/exceptions.md b/docs/mkdocs/docs/home/exceptions.md index 00d4058dc..4b8f6edd9 100644 --- a/docs/mkdocs/docs/home/exceptions.md +++ b/docs/mkdocs/docs/home/exceptions.md @@ -39,6 +39,8 @@ classDiagram 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`](../api/macros/json_noexception.md). In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER` (overriding `#!cpp throw`), `JSON_TRY_USER` (overriding `#!cpp try`), and `JSON_CATCH_USER` (overriding `#!cpp catch`). +See [Working without exceptions](../features/no_exceptions.md) for the functions that report errors without throwing. + Note that [`JSON_THROW_USER`](../api/macros/json_throw_user.md) should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior. ??? example "Example: switch off exceptions and log errors before aborting" @@ -193,6 +195,34 @@ This error indicates a syntax error while deserializing a JSON text. The error m - Make sure the input is correctly read. Try to write the input to standard output to check if, for instance, the input file was successfully opened. - Paste the input to a JSON validator like or a tool like [jq](https://jqlang.github.io/jq/). +??? info "Parsing a file fails at line 1, column 1 (unexpected end of input)" + + The messages `attempting to parse an empty input` (since version 3.11.3) and `unexpected end of input` (also the message of + earlier versions for an empty input) at the very start of the input mean + that the parser did not see a single character. The library only reads from the stream it is given: it does not + open files and cannot tell a missing file from an empty one. The usual causes are: + + - **The file was not opened.** A relative path is resolved against the *working directory of the running program*, + not against the source file or the project. IDEs and build tools often start the program in a build directory + (for example `build/` or `cmake-build-debug/`), so `"example.json"` may need to be `"../example.json"`. Use an + absolute path to rule this out. + - **The file is empty**, or a previous run truncated it (for example when it was opened for writing). + - **The stream was already read.** A stream whose content has been consumed (by printing it with + `std::cout << file.rdbuf()`, or by an earlier `accept`/`parse`/`>>`) is at its end. Parse it once, or rewind it + with `seekg(0)` after clearing the state with `clear()`. + + Check the stream before parsing it: + + ```cpp + std::ifstream f("example.json"); + if (!f.is_open()) + { + std::cerr << "cannot open example.json\n"; + return 1; + } + json data = json::parse(f); + ``` + ### json.exception.parse_error.102 JSON uses the `\uxxxx` format to describe Unicode characters. Code points above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. diff --git a/docs/mkdocs/docs/home/faq.md b/docs/mkdocs/docs/home/faq.md index dc0f4158a..d52bdc006 100644 --- a/docs/mkdocs/docs/home/faq.md +++ b/docs/mkdocs/docs/home/faq.md @@ -58,6 +58,208 @@ json j{obj}; // -> {"key":"value"} (copy, not array) Without the macro (default behavior), `json j{obj}` creates `[{"key":"value"}]`. This opt-in macro fixes issue #5074 while preserving backwards compatibility for existing code. +## Getting started + +### Which file do I need? + +!!! question + + Do I have to clone the repository or build the library? + +No. The library is header-only: the single file [`json.hpp`](https://github.com/nlohmann/json/releases) from the +releases page is all you need, including the `_json` literals. Put it in a directory `nlohmann` on your include path. +Companion headers (`json_fwd.hpp`, `json_view.hpp`, `json_literals.hpp`) are only needed if you use them. See +[Integration](../integration/index.md#header-only). + +### Using CMake or a package manager + +!!! question + + How do I add the library with CMake, Conan, vcpkg, Homebrew, or another tool? + +See [CMake](../integration/cmake.md) for `find_package`, `add_subdirectory`, and `FetchContent`, and +[Package managers](../integration/package_managers.md) for the list of supported package managers. + +### The IDE cannot open the header + +!!! question + + The compiler finds `nlohmann/json.hpp`, but my IDE reports "cannot open source file". Why? + +The compiler's include path is set correctly, but the IDE's is not. Add the same directory to the include paths of the +IDE (or of its language server); the build itself is not affected. + +### Compile times + +!!! question + + Including the library slows down my build. What can I do? + +Include [`json_fwd.hpp`](../integration/compile_times.md#include-json_fwdhpp-in-headers) in headers and `json.hpp` only +in the source files that need it, instantiate `basic_json` once, and consider precompiled headers or +[C++20 modules](../features/modules.md). See [Compile times](../integration/compile_times.md). + +## Parsing + +### Parsing a file fails with "unexpected end of input" + +!!! question + + `json::parse(file)` throws `parse_error.101` at line 1, column 1 for a file that is valid JSON. Why? + +The parser did not see any character: the file was most likely not opened. A relative path is resolved against the +working directory of the running program, which is often the build directory of your IDE, not the source directory. +Check `is_open()` and try an absolute path. Other causes are an empty file and a stream that was already read. See +[`parse_error.101`](exceptions.md#jsonexceptionparse_error101). + +### Input that arrives in chunks + +!!! question + + My data comes from a socket in pieces. How do I parse it? + +The parser cannot be resumed. Frame the messages (one value per line, a length prefix) and parse each complete frame, or +buffer the chunks until the value is complete; a `parse_error` at the end of the buffer means the input ended +prematurely. See [Large, streamed, and partial input](../features/parsing/large_and_partial_input.md#input-arriving-in-chunks). + +### Several values in one stream + +!!! question + + How do I read concatenated JSON values or one value per line? + +Read newline-delimited input line by line ([JSON Lines](../features/parsing/json_lines.md)). For concatenated values, use +`operator>>` and check for the end of the stream before each read, as shown in +[Several values in one stream](../features/parsing/large_and_partial_input.md#several-values-in-one-stream). + +### Files that are too large for memory + +!!! question + + How do I process a huge JSON file? + +Handle and discard the records of a large array with a [parser callback](../features/parsing/parser_callbacks.md), stop +early with the [SAX interface](../features/parsing/sax_interface.md), or read parts of the text through a +[`json_document`](../features/json_view.md) (added in version 3.13.0). See +[Input too large for memory](../features/parsing/large_and_partial_input.md#input-too-large-for-memory). + +### Position of a parse error + +!!! question + + How do I get the line and column of a parse error? + +The message contains them. The exception stores the byte index in `byte`, from which you can compute both; see +[Where did the error occur?](../features/parsing/parse_exceptions.md#where-did-the-error-occur). To locate a value that +causes a *later* error, such as a `type_error`, use +[`JSON_DIAGNOSTIC_POSITIONS`](../features/parsing/parse_exceptions.md#where-is-the-value-that-caused-a-later-error). + +### Comments and trailing commas + +!!! question + + Can the parser read JSON with comments or trailing commas? + +Yes, when asked to: see [Comments](../features/comments.md) and [Trailing commas](../features/trailing_commas.md). Both +are rejected by default. + +## Accessing and creating values + +### Reading a value into a variable + +!!! question + + How do I get a C++ value out of a `json` value? + +Use [`get()`](../api/basic_json/get.md), for example `#!cpp int age = j["age"].get();`, or an implicit conversion. +`#!cpp auto x = j["age"];` is **not** a conversion: it copies the `json` value. See +[Converting values](../features/conversions.md). + +### Strings are printed with quotes + +!!! question + + `std::cout << j["name"]` prints `"Mary"` with quotes. How do I get the string? + +The value is a `json`, and printing it produces JSON text. Convert it first: `#!cpp j["name"].get()`. See +[Getting a string without quotes](../features/conversions.md#getting-values-out). + +### Testing whether a key exists + +!!! question + + How do I check if a key exists, without changing the object? + +Use `contains` or `find`. Do not test with the non-const `operator[]`: like `std::map::operator[]`, it inserts a `null` +value for a missing key. See [Does this key exist?](../features/element_access/index.md#does-this-key-exist). + +### Reading a value with a fallback + +!!! question + + How do I read a value that may be missing? + +Use [`value`](../features/element_access/default_value.md), which returns a default for a missing key. It still throws if +the key exists with a different type; see [Working without exceptions](../features/no_exceptions.md). + +### Iterating and nested values + +!!! question + + How do I loop over an array or object, or reach a nested value? + +See [Iterators](../features/iterators.md) and [`items()`](../api/basic_json/items.md) for loops, and +[JSON Pointer](../features/json_pointer.md) to address or test a nested value by path. + +### Unexpected arrays when creating values + +!!! question + + Why does `json j{x}` or `{{"a", 1}, {"b"}}` produce an array instead of what I expected? + +Braces create arrays and objects based on their content, and single-element braces create a one-element array. See +[Brace initialization yields arrays](#brace-initialization-yields-arrays) and the table in +[Creating values](../features/creating_values.md#what-does-this-produce). + +### Keeping the order of object keys + +!!! question + + The keys of my object come out sorted. How do I keep the insertion order? + +Use `ordered_json`, and call `ordered_json::parse` when reading. See [Object order](../features/object_order.md). + +## Converting your own types + +### Structs, enums, and optional fields + +!!! question + + How do I convert my own types to and from JSON? + +Write `to_json`/`from_json` functions or use the `NLOHMANN_DEFINE_TYPE_*` macros; see +[Arbitrary type conversions](../features/arbitrary_types.md). Enums can be mapped to strings +([Enum conversion](../features/enum_conversion.md)), and an empty `std::optional` can be omitted +([Omitting a field](../features/conversions.md#omitting-a-field-when-serializing-stdoptional)). + +### Smart pointers, variants, chrono, and class hierarchies + +!!! question + + Why can't I convert `std::shared_ptr`, `std::variant`, `std::chrono` types, or a pointer to a base class? + +There is more than one sensible JSON representation for them, so the library leaves the choice to you. +[Recipes for common types](../features/type_recipes.md) shows one for each. + +### My `to_json` is not found + +!!! question + + I wrote `to_json`/`from_json`, but the compiler does not find them. Why? + +Typical causes are a type alias of a standard type, a function in the wrong namespace, or a declaration that comes too +late. See [Why isn't my `to_json` found?](../features/arbitrary_types.md#why-isnt-my-to_json-or-from_json-found). + ## Limitations ### Relaxed parsing @@ -203,6 +405,36 @@ Not directly, but the companion project [json-schema-validator](https://github.c builds JSON Schema (draft 7; draft 4 in its older, now-superseded 1.x releases) validation on top of this library and is a common recommendation for this use case. +### Memory use + +!!! question + + Does the library leak memory? My process stays large after I free a `json` value. + +Destroying a value frees everything it owns. Freed memory is returned to the allocator, which does not necessarily give +it back to the operating system, so a high resident size alone is not a leak. Copies are deep, and `clear()` keeps the +capacity of an array. See [Memory](../features/performance.md#memory), and use Valgrind or the sanitizers to tell a leak +from retained memory. + +### Reproducible builds + +!!! question + + The header path ends up in my binary. How do I get path-independent builds? + +The runtime assertions embed `__FILE__`. Define `NDEBUG` (the default for release builds) or map the path prefix; see +[Notes for builds and distribution](../integration/index.md#notes-for-builds-and-distribution). + +### Slow debug builds + +!!! question + + Parsing a large file is very slow in my debug build. Can I compile the library with optimizations? + +The library is header-only, so there is no separate source file. Keep the code that uses `json` in its own source file and +compile that file with optimizations; see +[Notes for builds and distribution](../integration/index.md#notes-for-builds-and-distribution). + ## Exceptions ### Parsing without exceptions @@ -211,7 +443,8 @@ and is a common recommendation for this use case. Is it possible to indicate a parse error without throwing an exception? -Yes, see [Parsing and exceptions](../features/parsing/parse_exceptions.md). +Yes, see [Parsing and exceptions](../features/parsing/parse_exceptions.md) and +[Working without exceptions](../features/no_exceptions.md). ### Key name in exceptions diff --git a/docs/mkdocs/docs/index.md b/docs/mkdocs/docs/index.md index 07e60067d..f53b8dcab 100644 --- a/docs/mkdocs/docs/index.md +++ b/docs/mkdocs/docs/index.md @@ -20,7 +20,7 @@ Add the single header to your project and use the library like this: #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { @@ -31,6 +31,10 @@ int main() j["name"] = "Niels"; j["list"] = {1, 0, 2}; + // read a value into a C++ variable and use it + double pi = j["pi"].get(); + std::cout << "2 * pi = " << 2 * pi << '\n'; + // serialize with an indent of 4 spaces std::cout << j.dump(4) << '\n'; } diff --git a/docs/mkdocs/docs/integration/bazel/example.cpp b/docs/mkdocs/docs/integration/bazel/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/bazel/example.cpp +++ b/docs/mkdocs/docs/integration/bazel/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/build2/example.cpp b/docs/mkdocs/docs/integration/build2/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/build2/example.cpp +++ b/docs/mkdocs/docs/integration/build2/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/cget/example.cpp b/docs/mkdocs/docs/integration/cget/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/cget/example.cpp +++ b/docs/mkdocs/docs/integration/cget/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/conan/example.cpp b/docs/mkdocs/docs/integration/conan/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/conan/example.cpp +++ b/docs/mkdocs/docs/integration/conan/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/conda/example.cpp b/docs/mkdocs/docs/integration/conda/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/conda/example.cpp +++ b/docs/mkdocs/docs/integration/conda/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/cpm/example.cpp b/docs/mkdocs/docs/integration/cpm/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/cpm/example.cpp +++ b/docs/mkdocs/docs/integration/cpm/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/example.cpp b/docs/mkdocs/docs/integration/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/example.cpp +++ b/docs/mkdocs/docs/integration/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/homebrew/example.cpp b/docs/mkdocs/docs/integration/homebrew/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/homebrew/example.cpp +++ b/docs/mkdocs/docs/integration/homebrew/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/hunter/example.cpp b/docs/mkdocs/docs/integration/hunter/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/hunter/example.cpp +++ b/docs/mkdocs/docs/integration/hunter/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/index.md b/docs/mkdocs/docs/integration/index.md index f6f303142..6b461ff99 100644 --- a/docs/mkdocs/docs/integration/index.md +++ b/docs/mkdocs/docs/integration/index.md @@ -37,12 +37,27 @@ file in `single_include/nlohmann` or [released here](https://github.com/nlohmann #include // for convenience -using json = nlohmann::json; +using nlohmann::json; ``` to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). +!!! tip "Which file do I download?" + + For a project without a package manager, the library itself, including the `_json` literals, is **one file**: + [`json.hpp`](https://github.com/nlohmann/json/releases/latest/download/json.hpp) from the + [releases page](https://github.com/nlohmann/json/releases). You do not need to clone the repository, build + anything, or link a library. Put the file in a directory named `nlohmann` that is on your include path (for + example `third_party/nlohmann/json.hpp` with `-I third_party`), so that `#include ` works. + Download the companion headers listed below only if you use them: `json_view.hpp` for the + [zero-copy views](../features/json_view.md), `json_fwd.hpp` for forward declarations, and `json_literals.hpp` if + you define `JSON_NO_AUTOMATIC_UDLS`. Keep them in the same `nlohmann` directory. + + Do not copy only parts of the repository's `include/` directory: the headers in it include each other and must be + kept together. If your IDE reports "cannot open source file" while the compiler succeeds, the compiler's include + path is set correctly but the IDE's is not; add the same directory to the IDE's include paths. + You can further use file [`single_include/nlohmann/json_fwd.hpp`](https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json_fwd.hpp) for forward declarations (see [Compile times](compile_times.md)), and file @@ -55,6 +70,24 @@ types, additionally include [`single_include/nlohmann/json_view.hpp`](https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json_view.hpp); see [Zero-copy JSON views](../features/json_view.md). +## Notes for builds and distribution + +**Reproducible and path-independent builds.** The runtime assertions use `assert`, which embeds the name of the source +file (`__FILE__`) in the binary. Release builds do not contain them, because they define `NDEBUG`. If you compile with +assertions enabled and need binaries that do not depend on the build directory, either define `NDEBUG` or map the +prefix of the path with the compiler option `-ffile-prefix-map==` (GCC 8 and later, Clang 10 and later). See +[Runtime assertions](../features/assertions.md). + +**There is no separate source file to compile.** The library is header-only, so there is no `.cpp` that could be +built with different options than the code that includes it. It is also not available as a precompiled library. To +compile the library with optimizations in an otherwise unoptimized build (for example to parse large files in a debug +build, which is slow because of the unoptimized code and the checks of the debug standard library), keep the code that +uses `json` in a separate source file and set the options for that file only. [Compile times](compile_times.md) +shows how to [instantiate `basic_json` once](compile_times.md#instantiate-basic_json-once). + +**Debugger visualization.** The Natvis file works with the Visual Studio debugger, including the `cppvsdbg` engine in +Visual Studio Code, but not with debuggers that wrap LLDB, such as `codelldb`; see [Debugging](../home/debugging.md). + ## See also - [CMake](cmake.md) - integrate the library into a CMake project diff --git a/docs/mkdocs/docs/integration/macports/example.cpp b/docs/mkdocs/docs/integration/macports/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/macports/example.cpp +++ b/docs/mkdocs/docs/integration/macports/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/meson/example.cpp b/docs/mkdocs/docs/integration/meson/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/meson/example.cpp +++ b/docs/mkdocs/docs/integration/meson/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/msys2/example.cpp b/docs/mkdocs/docs/integration/msys2/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/msys2/example.cpp +++ b/docs/mkdocs/docs/integration/msys2/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/spack/example.cpp b/docs/mkdocs/docs/integration/spack/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/spack/example.cpp +++ b/docs/mkdocs/docs/integration/spack/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/swift/example.cpp b/docs/mkdocs/docs/integration/swift/example.cpp index ad0a06827..33ccbfef0 100644 --- a/docs/mkdocs/docs/integration/swift/example.cpp +++ b/docs/mkdocs/docs/integration/swift/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/vcpkg/example.cpp b/docs/mkdocs/docs/integration/vcpkg/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/vcpkg/example.cpp +++ b/docs/mkdocs/docs/integration/vcpkg/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/docs/integration/xmake/example.cpp b/docs/mkdocs/docs/integration/xmake/example.cpp index 1a7ac4de2..c51a502aa 100644 --- a/docs/mkdocs/docs/integration/xmake/example.cpp +++ b/docs/mkdocs/docs/integration/xmake/example.cpp @@ -2,7 +2,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; int main() { diff --git a/docs/mkdocs/includes/glossary.md b/docs/mkdocs/includes/glossary.md index 86fbe313e..b84567b14 100644 --- a/docs/mkdocs/includes/glossary.md +++ b/docs/mkdocs/includes/glossary.md @@ -1,5 +1,7 @@ +*[AArch64]: 64-bit execution state of the Arm architecture +*[ABI]: Application Binary Interface *[ADL]: Argument-dependent lookup *[API]: Application Programming Interface *[ASCII]: American Standard Code for Information Interchange @@ -10,28 +12,51 @@ *[CBOR]: Concise Binary Object Representation *[CC0]: Creative Commons Zero *[CI]: Continuous Integration +*[CPU]: Central Processing Unit +*[CUDA]: Compute Unified Device Architecture +*[CVE]: Common Vulnerabilities and Exposures *[DOM]: Document Object Model *[EOF]: End of File *[FAQ]: Frequently Asked Questions *[GCC]: GNU Compiler Collection +*[GDB]: GNU Debugger +*[GiB]: Gibibyte (2^30 bytes) *[HTTP]: Hypertext Transfer Protocol *[ICC]: Intel C++ Compiler +*[IDE]: Integrated Development Environment *[IEEE]: Institute of Electrical and Electronics Engineers *[ISO]: International Organization for Standardization +*[IWYU]: Include What You Use *[JSON]: JavaScript Object Notation +*[JSONC]: JSON with Comments +*[JWCC]: JSON With Commas and Comments +*[LLDB]: LLVM Debugger +*[LTS]: Long-Term Support +*[MinGW]: Minimalist GNU for Windows *[MIT]: Massachusetts Institute of Technology -*[MSVC]: Microsoft Visual C++ *[MsgPack]: MessagePack +*[MSVC]: Microsoft Visual C++ +*[NaN]: Not a Number *[NASA]: National Aeronautics and Space Administration *[NDK]: Native Development Kit -*[NaN]: Not a Number +*[NEON]: Arm SIMD instruction set extension +*[NUL]: Null character (U+0000, byte value 0) +*[ODR]: One Definition Rule +*[REUSE]: Software licensing specification by the Free Software Foundation Europe *[RFC]: Request for Comments *[RTTI]: Runtime Type Information *[SAX]: Simple API for XML *[SDK]: Software Development Kit *[SFINAE]: Substitution failure is not an error +*[SGX]: Software Guard Extensions *[SHA]: Secure Hash Algorithm +*[SIMD]: Single Instruction, Multiple Data *[SPDX]: Software Package Data Exchange +*[SSE2]: Streaming SIMD Extensions 2 +*[SSSE3]: Supplemental Streaming SIMD Extensions 3 *[STL]: Standard Template Library *[UBJSON]: Universal Binary JSON +*[UDL]: User-defined literal +*[UDLs]: User-defined literals +*[URL]: Uniform Resource Locator *[UTF]: Unicode Transformation Format diff --git a/docs/mkdocs/mkdocs.yml b/docs/mkdocs/mkdocs.yml index 469ae9260..7e1c23aa8 100644 --- a/docs/mkdocs/mkdocs.yml +++ b/docs/mkdocs/mkdocs.yml @@ -106,6 +106,7 @@ nav: - Features: - features/index.md - features/arbitrary_types.md + - features/type_recipes.md - Binary Formats: - features/binary_formats/index.md - features/binary_formats/bjdata.md @@ -130,11 +131,13 @@ nav: - features/modifying_values.md - features/modules.md - 'nlohmann Namespace': features/namespace.md + - features/no_exceptions.md - features/object_order.md - features/performance.md - Parsing: - features/parsing/index.md - features/parsing/json_lines.md + - features/parsing/large_and_partial_input.md - features/parsing/parse_exceptions.md - features/parsing/parser_callbacks.md - features/parsing/sax_interface.md diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 676b36290..04201efbe 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -653,13 +653,14 @@ class json_sax_dom_parser : public json_sax_dom_error_state using detect_emplace_new_key = decltype(std::declval().emplace_new_key(std::declval(), std::declval())); template using detect_hash = decltype(std::hash()(std::declval())); - /// (std::hash is only looked at for objects in a vector: the one of VS 2015 - /// fails to compile, instead of being undetectable, for a type it does not hash) - template - struct has_indexable_keys : std::false_type {}; - template - struct has_indexable_keys : is_detected {}; - static constexpr bool indexed_keys = has_indexable_keys::value, string_t>::value; + // std::hash is only named once the object can hold the keys in a vector: + // VS 2015 static-asserts on std::hash of a type without one instead of + // reporting a substitution failure + template::value> + struct detect_indexable_keys : std::false_type {}; + template + struct detect_indexable_keys : is_detected {}; + static constexpr bool indexed_keys = detect_indexable_keys::value; /// what a frame holds for objects that are kept in a vector: the key index /// of an object with many keys, and for an object that is collected on the @@ -781,7 +782,6 @@ class json_sax_dom_parser : public json_sax_dom_error_state { const std::size_t new_capacity = capacity * 2; std::unique_ptr new_heap(new frame[new_capacity]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - // (a loop, not std::copy: VS 2015 rejects std::copy to a raw pointer, C4996) for (std::size_t i = 0; i < count; ++i) { new_heap[i] = data[i]; @@ -803,7 +803,10 @@ class json_sax_dom_parser : public json_sax_dom_error_state { if (other.heap == nullptr) { - std::copy(other.local.begin(), other.local.begin() + static_cast(other.count), local.begin()); + for (std::size_t i = 0; i < other.count; ++i) + { + local[i] = other.local[i]; + } data = local.data(); capacity = local_capacity; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fc4d96ccb..1beba149b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15776,13 +15776,14 @@ class json_sax_dom_parser : public json_sax_dom_error_state using detect_emplace_new_key = decltype(std::declval().emplace_new_key(std::declval(), std::declval())); template using detect_hash = decltype(std::hash()(std::declval())); - /// (std::hash is only looked at for objects in a vector: the one of VS 2015 - /// fails to compile, instead of being undetectable, for a type it does not hash) - template - struct has_indexable_keys : std::false_type {}; - template - struct has_indexable_keys : is_detected {}; - static constexpr bool indexed_keys = has_indexable_keys::value, string_t>::value; + // std::hash is only named once the object can hold the keys in a vector: + // VS 2015 static-asserts on std::hash of a type without one instead of + // reporting a substitution failure + template::value> + struct detect_indexable_keys : std::false_type {}; + template + struct detect_indexable_keys : is_detected {}; + static constexpr bool indexed_keys = detect_indexable_keys::value; /// what a frame holds for objects that are kept in a vector: the key index /// of an object with many keys, and for an object that is collected on the @@ -15904,7 +15905,6 @@ class json_sax_dom_parser : public json_sax_dom_error_state { const std::size_t new_capacity = capacity * 2; std::unique_ptr new_heap(new frame[new_capacity]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - // (a loop, not std::copy: VS 2015 rejects std::copy to a raw pointer, C4996) for (std::size_t i = 0; i < count; ++i) { new_heap[i] = data[i]; @@ -15926,7 +15926,10 @@ class json_sax_dom_parser : public json_sax_dom_error_state { if (other.heap == nullptr) { - std::copy(other.local.begin(), other.local.begin() + static_cast(other.count), local.begin()); + for (std::size_t i = 0; i < other.count; ++i) + { + local[i] = other.local[i]; + } data = local.data(); capacity = local_capacity; } diff --git a/tests/benchmarks/src/benchmarks.cpp b/tests/benchmarks/src/benchmarks.cpp index 6e51d4778..0d9e06d21 100644 --- a/tests/benchmarks/src/benchmarks.cpp +++ b/tests/benchmarks/src/benchmarks.cpp @@ -21,7 +21,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; ////////////////////////////////////////////////////////////////////////////// // parse JSON from file diff --git a/tests/benchmarks/src/benchmarks_view.cpp b/tests/benchmarks/src/benchmarks_view.cpp index 475ed54a4..02920ffab 100644 --- a/tests/benchmarks/src/benchmarks_view.cpp +++ b/tests/benchmarks/src/benchmarks_view.cpp @@ -17,8 +17,8 @@ #include #include -using json = nlohmann::json; -using json_document = nlohmann::json_document; +using nlohmann::json; +using nlohmann::json_document; static std::string read_file(const char* filename) { diff --git a/tests/src/fuzzer-json_view_image.cpp b/tests/src/fuzzer-json_view_image.cpp index 8495468a4..d990600af 100644 --- a/tests/src/fuzzer-json_view_image.cpp +++ b/tests/src/fuzzer-json_view_image.cpp @@ -33,8 +33,8 @@ drivers. #error "the fuzzer drivers must be built without NDEBUG" #endif -using json = nlohmann::json; -using json_document = nlohmann::json_document; +using nlohmann::json; +using nlohmann::json_document; using image_check = json_document::image_check; // see http://llvm.org/docs/LibFuzzer.html diff --git a/tests/src/fuzzer-parse_json_view.cpp b/tests/src/fuzzer-parse_json_view.cpp index 9b9f5a02a..5e41d6c79 100644 --- a/tests/src/fuzzer-parse_json_view.cpp +++ b/tests/src/fuzzer-parse_json_view.cpp @@ -42,8 +42,8 @@ drivers. #error "the fuzzer drivers must be built without NDEBUG" #endif -using json = nlohmann::json; -using json_document = nlohmann::json_document; +using nlohmann::json; +using nlohmann::json_document; namespace { diff --git a/tests/src/unit-bjdata.cpp b/tests/src/unit-bjdata.cpp index 2f91462da..67e9e0a05 100644 --- a/tests/src/unit-bjdata.cpp +++ b/tests/src/unit-bjdata.cpp @@ -19,7 +19,7 @@ #include using nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::ordered_json; #include #include diff --git a/tests/src/unit-diagnostic-positions.cpp b/tests/src/unit-diagnostic-positions.cpp index f5c6bc648..9e0625115 100644 --- a/tests/src/unit-diagnostic-positions.cpp +++ b/tests/src/unit-diagnostic-positions.cpp @@ -14,7 +14,7 @@ #define JSON_DIAGNOSTIC_POSITIONS 1 #include -using json = nlohmann::json; +using nlohmann::json; TEST_CASE("Better diagnostics with positions") { diff --git a/tests/src/unit-disabled_exceptions.cpp b/tests/src/unit-disabled_exceptions.cpp index 40a37c339..0e36d622e 100644 --- a/tests/src/unit-disabled_exceptions.cpp +++ b/tests/src/unit-disabled_exceptions.cpp @@ -13,7 +13,7 @@ DOCTEST_GCC_SUPPRESS_WARNING_PUSH DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept") #include -using json = nlohmann::json; +using nlohmann::json; #ifdef JSON_TEST_NO_GLOBAL_UDLS using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) #endif diff --git a/tests/src/unit-element_access2.cpp b/tests/src/unit-element_access2.cpp index 7765c0601..9f4d67fe8 100644 --- a/tests/src/unit-element_access2.cpp +++ b/tests/src/unit-element_access2.cpp @@ -1948,7 +1948,7 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann #ifdef JSON_HAS_CPP_17 TEST_CASE("operator[] with user-defined std::string_view-convertible types") { - using json = nlohmann::json; + using nlohmann::json; class TestClass { @@ -2031,7 +2031,7 @@ TEST_CASE("keys convertible to std::string_view work with all lookup functions ( SECTION("nlohmann::json") { - using json = nlohmann::json; + using nlohmann::json; SECTION("ViewKey") { @@ -2072,7 +2072,7 @@ TEST_CASE("keys convertible to std::string_view work with all lookup functions ( SECTION("nlohmann::ordered_json") { - using ordered_json = nlohmann::ordered_json; + using nlohmann::ordered_json; SECTION("ViewKey") { diff --git a/tests/src/unit-explicit_instantiation.cpp b/tests/src/unit-explicit_instantiation.cpp index 71132a59f..ddb5e40f9 100644 --- a/tests/src/unit-explicit_instantiation.cpp +++ b/tests/src/unit-explicit_instantiation.cpp @@ -15,7 +15,7 @@ #include "doctest_compatibility.h" #include -using json = nlohmann::json; +using nlohmann::json; ///////////////////////////////////////////////////////////////////// // for #4825 - explicitly instantiating basic_json must compile; this diff --git a/tests/src/unit-format-as.cpp b/tests/src/unit-format-as.cpp index b275f505c..61821f9ac 100644 --- a/tests/src/unit-format-as.cpp +++ b/tests/src/unit-format-as.cpp @@ -9,8 +9,8 @@ #include "doctest_compatibility.h" #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; namespace { diff --git a/tests/src/unit-hash.cpp b/tests/src/unit-hash.cpp index 117bc0151..e544cff9b 100644 --- a/tests/src/unit-hash.cpp +++ b/tests/src/unit-hash.cpp @@ -9,8 +9,8 @@ #include "doctest_compatibility.h" #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; #include #include diff --git a/tests/src/unit-no_automatic_udls.cpp b/tests/src/unit-no_automatic_udls.cpp index 1a708da50..02f900077 100644 --- a/tests/src/unit-no_automatic_udls.cpp +++ b/tests/src/unit-no_automatic_udls.cpp @@ -19,7 +19,7 @@ #include #include -using json = nlohmann::json; +using nlohmann::json; // An argument type whose associated namespace is the library namespace, so // argument-dependent lookup of a literal operator called by its function name diff --git a/tests/src/unit-no_io_and_user_exceptions.cpp b/tests/src/unit-no_io_and_user_exceptions.cpp index 667d114e7..5a2b7da44 100644 --- a/tests/src/unit-no_io_and_user_exceptions.cpp +++ b/tests/src/unit-no_io_and_user_exceptions.cpp @@ -38,7 +38,7 @@ static int json_throw_user_call_count = 0; // NOLINT(cppcoreguidelines-avoid-non #include "doctest_compatibility.h" #include -using json = nlohmann::json; +using nlohmann::json; TEST_CASE("JSON_NO_IO") { diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index b532e75ae..c7566c799 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -33,8 +33,8 @@ #define JSON_TESTS_PRIVATE #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; #ifdef JSON_TEST_NO_GLOBAL_UDLS using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) #endif diff --git a/tests/src/unit-regression3.cpp b/tests/src/unit-regression3.cpp index b31cfd054..148ce2658 100644 --- a/tests/src/unit-regression3.cpp +++ b/tests/src/unit-regression3.cpp @@ -36,8 +36,8 @@ #define JSON_TESTS_PRIVATE #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; #ifdef JSON_TEST_NO_GLOBAL_UDLS using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) #endif diff --git a/tests/src/unit-std-format.cpp b/tests/src/unit-std-format.cpp index f2be8d5ec..9f32868e4 100644 --- a/tests/src/unit-std-format.cpp +++ b/tests/src/unit-std-format.cpp @@ -16,8 +16,8 @@ #include "doctest_compatibility.h" #include -using json = nlohmann::json; -using ordered_json = nlohmann::ordered_json; +using nlohmann::json; +using nlohmann::ordered_json; // JSON_HAS_CPP_20 (do not remove; see note at top of file) #if JSON_HAS_STD_FORMAT