├── .clang-format ├── .clang-tidy ├── .cmake └── taocpp-json-config.cmake.in ├── .codecov.yml ├── .github └── workflows │ ├── android.yml │ ├── clang-analyze.yml │ ├── clang-format.yml │ ├── clang-tidy.yml │ ├── code-coverage.yml │ ├── codeql-analysis.yml │ ├── linux.yml │ ├── macos.yml │ ├── sanitizer.yml │ └── windows.yml ├── .gitignore ├── .gitmodules ├── .pkg-config └── taocpp-json.pc.in ├── CMakeLists.txt ├── LICENSE ├── LICENSE.double-conversion ├── LICENSE.itoa ├── LICENSE.ryu ├── Makefile ├── README.md ├── contrib └── nlohmann │ ├── json.hpp │ └── test.cpp ├── doc ├── Advanced-Use-Cases.md ├── Batteries-Included.md ├── Binding-Traits.md ├── Changelog.md ├── Common-Use-Cases.md ├── Design-Decisions.md ├── Events-Interface.md ├── Getting-Started.md ├── Instance-Sharing.md ├── Interoperability.md ├── Overview.md ├── Overview.png ├── Parser-Interface.md ├── README.md ├── Scratchpad.md ├── Type-Traits.md ├── Types.png └── Value-Class.md ├── include └── tao │ ├── json.hpp │ └── json │ ├── basic_value.hpp │ ├── binary.hpp │ ├── binary_view.hpp │ ├── binding.hpp │ ├── binding │ ├── constant.hpp │ ├── element.hpp │ ├── element2.hpp │ ├── factory.hpp │ ├── for_nothing_value.hpp │ ├── for_unknown_key.hpp │ ├── inherit.hpp │ ├── internal │ │ ├── array.hpp │ │ ├── elementr.hpp │ │ ├── elementw.hpp │ │ ├── inherit.hpp │ │ ├── object.hpp │ │ └── type_key.hpp │ ├── member.hpp │ ├── member_kind.hpp │ └── versions.hpp │ ├── cbor.hpp │ ├── cbor │ ├── consume_file.hpp │ ├── consume_string.hpp │ ├── events │ │ ├── from_binary.hpp │ │ ├── from_file.hpp │ │ ├── from_input.hpp │ │ ├── from_string.hpp │ │ ├── to_stream.hpp │ │ └── to_string.hpp │ ├── from_binary.hpp │ ├── from_file.hpp │ ├── from_input.hpp │ ├── from_string.hpp │ ├── internal │ │ ├── grammar.hpp │ │ └── major.hpp │ ├── parts_parser.hpp │ ├── to_stream.hpp │ └── to_string.hpp │ ├── consume.hpp │ ├── consume_file.hpp │ ├── consume_string.hpp │ ├── contrib │ ├── array_traits.hpp │ ├── deque_traits.hpp │ ├── diff.hpp │ ├── from_nlohmann.hpp │ ├── get.hpp │ ├── internal │ │ ├── array_traits.hpp │ │ ├── indirect_traits.hpp │ │ ├── object_traits.hpp │ │ └── type_traits.hpp │ ├── list_traits.hpp │ ├── map_traits.hpp │ ├── multimap_traits.hpp │ ├── multiset_traits.hpp │ ├── pair_traits.hpp │ ├── patch.hpp │ ├── pointer_traits.hpp │ ├── position.hpp │ ├── reference.hpp │ ├── schema.hpp │ ├── set_traits.hpp │ ├── shared_ptr_traits.hpp │ ├── to_nlohmann.hpp │ ├── traits.hpp │ ├── tuple_traits.hpp │ ├── unique_ptr_traits.hpp │ ├── unordered_map_traits.hpp │ ├── unordered_set_traits.hpp │ ├── variant_traits.hpp │ ├── vector_bool_traits.hpp │ └── vector_traits.hpp │ ├── events.hpp │ ├── events │ ├── apply.hpp │ ├── binary_to_base64.hpp │ ├── binary_to_base64url.hpp │ ├── binary_to_exception.hpp │ ├── binary_to_hex.hpp │ ├── capture.hpp │ ├── compare.hpp │ ├── debug.hpp │ ├── discard.hpp │ ├── from_file.hpp │ ├── from_input.hpp │ ├── from_stream.hpp │ ├── from_string.hpp │ ├── from_value.hpp │ ├── hash.hpp │ ├── invalid_string_to_binary.hpp │ ├── invalid_string_to_exception.hpp │ ├── invalid_string_to_hex.hpp │ ├── key_camel_case_to_snake_case.hpp │ ├── key_snake_case_to_camel_case.hpp │ ├── limit_nesting_depth.hpp │ ├── limit_value_count.hpp │ ├── non_finite_to_exception.hpp │ ├── non_finite_to_null.hpp │ ├── non_finite_to_string.hpp │ ├── prefer_signed.hpp │ ├── prefer_unsigned.hpp │ ├── produce.hpp │ ├── ref.hpp │ ├── set_value.hpp │ ├── statistics.hpp │ ├── tee.hpp │ ├── to_pretty_stream.hpp │ ├── to_stream.hpp │ ├── to_string.hpp │ ├── to_value.hpp │ ├── transformer.hpp │ ├── validate_event_order.hpp │ ├── validate_keys.hpp │ ├── virtual_base.hpp │ ├── virtual_ref.hpp │ ├── virtual_wrap.hpp │ └── virtualize.hpp │ ├── external │ ├── double.hpp │ ├── itoa.hpp │ └── ryu.hpp │ ├── forward.hpp │ ├── from_file.hpp │ ├── from_input.hpp │ ├── from_stream.hpp │ ├── from_string.hpp │ ├── internal │ ├── action.hpp │ ├── base64.hpp │ ├── base64url.hpp │ ├── dependent_false.hpp │ ├── endian.hpp │ ├── endian_gcc.hpp │ ├── endian_win.hpp │ ├── errors.hpp │ ├── escape.hpp │ ├── format.hpp │ ├── grammar.hpp │ ├── hexdump.hpp │ ├── identity.hpp │ ├── number_state.hpp │ ├── number_traits.hpp │ ├── pair.hpp │ ├── parse_util.hpp │ ├── sha256.hpp │ ├── single.hpp │ ├── string_t.hpp │ ├── type_traits.hpp │ ├── unescape_action.hpp │ └── uri_fragment.hpp │ ├── jaxn.hpp │ ├── jaxn │ ├── consume_file.hpp │ ├── consume_string.hpp │ ├── events │ │ ├── from_file.hpp │ │ ├── from_input.hpp │ │ ├── from_stream.hpp │ │ ├── from_string.hpp │ │ ├── to_pretty_stream.hpp │ │ ├── to_stream.hpp │ │ └── to_string.hpp │ ├── from_file.hpp │ ├── from_input.hpp │ ├── from_stream.hpp │ ├── from_string.hpp │ ├── internal │ │ ├── action.hpp │ │ ├── bunescape_action.hpp │ │ ├── errors.hpp │ │ ├── grammar.hpp │ │ ├── integer.hpp │ │ └── unescape_action.hpp │ ├── is_identifier.hpp │ ├── parts_parser.hpp │ ├── to_stream.hpp │ └── to_string.hpp │ ├── message_extension.hpp │ ├── msgpack.hpp │ ├── msgpack │ ├── consume_file.hpp │ ├── consume_string.hpp │ ├── events │ │ ├── from_binary.hpp │ │ ├── from_file.hpp │ │ ├── from_input.hpp │ │ ├── from_string.hpp │ │ ├── to_stream.hpp │ │ └── to_string.hpp │ ├── from_binary.hpp │ ├── from_file.hpp │ ├── from_input.hpp │ ├── from_string.hpp │ ├── internal │ │ ├── format.hpp │ │ └── grammar.hpp │ ├── parts_parser.hpp │ ├── to_stream.hpp │ └── to_string.hpp │ ├── operators.hpp │ ├── parts_parser.hpp │ ├── pointer.hpp │ ├── produce.hpp │ ├── self_contained.hpp │ ├── span.hpp │ ├── stream.hpp │ ├── to_stream.hpp │ ├── to_string.hpp │ ├── traits.hpp │ ├── type.hpp │ ├── ubjson.hpp │ ├── ubjson │ ├── consume_file.hpp │ ├── consume_string.hpp │ ├── events │ │ ├── from_binary.hpp │ │ ├── from_file.hpp │ │ ├── from_input.hpp │ │ ├── from_string.hpp │ │ ├── to_stream.hpp │ │ └── to_string.hpp │ ├── from_binary.hpp │ ├── from_file.hpp │ ├── from_input.hpp │ ├── from_string.hpp │ ├── internal │ │ ├── grammar.hpp │ │ └── marker.hpp │ ├── parts_parser.hpp │ ├── to_stream.hpp │ └── to_string.hpp │ ├── utf8.hpp │ └── value.hpp ├── src ├── example │ └── json │ │ ├── CMakeLists.txt │ │ ├── cbor_to_jaxn.cpp │ │ ├── cbor_to_json.cpp │ │ ├── cbor_to_msgpack.cpp │ │ ├── cbor_to_pretty_jaxn.cpp │ │ ├── cbor_to_pretty_json.cpp │ │ ├── cbor_to_ubjson.cpp │ │ ├── jaxn_to_cbor.cpp │ │ ├── jaxn_to_cplusplus.cpp │ │ ├── jaxn_to_jaxn.cpp │ │ ├── jaxn_to_msgpack.cpp │ │ ├── jaxn_to_pretty_jaxn.cpp │ │ ├── jaxn_to_ubjson.cpp │ │ ├── json_to_cbor.cpp │ │ ├── json_to_json.cpp │ │ ├── json_to_msgpack.cpp │ │ ├── json_to_pretty_json.cpp │ │ ├── json_to_ubjson.cpp │ │ ├── msgpack_to_cbor.cpp │ │ ├── msgpack_to_jaxn.cpp │ │ ├── msgpack_to_json.cpp │ │ ├── msgpack_to_pretty_jaxn.cpp │ │ ├── msgpack_to_pretty_json.cpp │ │ ├── msgpack_to_ubjson.cpp │ │ ├── printf_doubles.cpp │ │ ├── ubjson_to_cbor.cpp │ │ ├── ubjson_to_jaxn.cpp │ │ ├── ubjson_to_json.cpp │ │ ├── ubjson_to_msgpack.cpp │ │ ├── ubjson_to_pretty_jaxn.cpp │ │ ├── ubjson_to_pretty_json.cpp │ │ ├── validate_event_order.cpp │ │ └── validate_integer.cpp ├── perf │ └── json │ │ ├── CMakeLists.txt │ │ ├── bench_mark.hpp │ │ ├── benchmark.cpp │ │ ├── parse_file.cpp │ │ ├── pretty_print_file.cpp │ │ ├── print_double.cpp │ │ ├── print_file.cpp │ │ ├── sizes.cpp │ │ └── syntax_only.cpp └── test │ └── json │ ├── CMakeLists.txt │ ├── big_list_of_naughty_strings.cpp │ ├── binding_array.cpp │ ├── binding_factory.cpp │ ├── binding_function.cpp │ ├── binding_object.cpp │ ├── binding_versions.cpp │ ├── cbor.cpp │ ├── cbor_parts_parser.cpp │ ├── contrib_diff.cpp │ ├── contrib_get.cpp │ ├── contrib_patch_add.cpp │ ├── contrib_patch_copy.cpp │ ├── contrib_patch_move.cpp │ ├── contrib_patch_remove.cpp │ ├── contrib_patch_replace.cpp │ ├── contrib_patch_test.cpp │ ├── contrib_position.cpp │ ├── contrib_reference.cpp │ ├── contrib_schema.cpp │ ├── contrib_traits.cpp │ ├── double.cpp │ ├── enable_implicit_constructor.cpp │ ├── escape.cpp │ ├── events_binary_to.cpp │ ├── events_compare.cpp │ ├── events_debug.cpp │ ├── events_hash.cpp │ ├── events_to_stream.cpp │ ├── events_to_string.cpp │ ├── include_json.cpp │ ├── integer.cpp │ ├── jaxn_ostream.cpp │ ├── jaxn_parse.cpp │ ├── jaxn_parts_parser.cpp │ ├── json_ostream.cpp │ ├── json_parse.cpp │ ├── json_parts_parser.cpp │ ├── json_pointer.cpp │ ├── key_camel_case_to_snake_case.cpp │ ├── key_snake_case_to_camel_case.cpp │ ├── literal.cpp │ ├── main.hpp │ ├── make_events.hpp │ ├── msgpack.cpp │ ├── object_construction.cpp │ ├── opaque_pointer.cpp │ ├── operators.cpp │ ├── optional.cpp │ ├── public_base.cpp │ ├── self_contained.cpp │ ├── sha256.cpp │ ├── temporary_parsing.cpp │ ├── test.hpp │ ├── test_events.hpp │ ├── test_types.hpp │ ├── test_unhex.hpp │ ├── type.cpp │ ├── ubjson.cpp │ ├── uri_fragment.cpp │ ├── validate_event_interfaces.cpp │ ├── validate_utf8.cpp │ ├── value_access.cpp │ ├── value_basics.cpp │ ├── value_create.cpp │ ├── value_ptr.cpp │ ├── value_subscript.cpp │ └── with_arguments.cpp └── tests ├── blns.json ├── canada.json ├── citm_catalog.json ├── draft4 ├── additionalItems.json ├── additionalProperties.json ├── allOf.json ├── anyOf.json ├── default.json ├── definitions.json ├── dependencies.json ├── enum.json ├── items.json ├── maxItems.json ├── maxLength.json ├── maxProperties.json ├── maximum.json ├── minItems.json ├── minLength.json ├── minProperties.json ├── minimum.json ├── multipleOf.json ├── not.json ├── oneOf.json ├── optional │ ├── bignum.json │ ├── format.json │ └── zeroTerminatedFloats.json ├── pattern.json ├── patternProperties.json ├── properties.json ├── ref.json ├── refRemote.json ├── required.json ├── type.json └── uniqueItems.json ├── taocpp ├── binary.jaxn ├── dateTime.json ├── make_events.cbor ├── number.json ├── position.json └── schema.json └── twitter.json /.cmake/taocpp-json-config.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | include(CMakeFindDependencyMacro) 4 | find_dependency(pegtl @TAOCPP_JSON_PEGTL_MIN_VERSION@ CONFIG) 5 | 6 | include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake") 7 | -------------------------------------------------------------------------------- /.codecov.yml: -------------------------------------------------------------------------------- 1 | ignore: 2 | - include/tao/json/external/* 3 | -------------------------------------------------------------------------------- /.github/workflows/android.yml: -------------------------------------------------------------------------------- 1 | name: Android 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | android: 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | platform: 19 | - android-26 20 | - android-27 21 | abi: 22 | - armeabi-v7a 23 | - arm64-v8a 24 | build_type: [Debug, Release] 25 | 26 | runs-on: ubuntu-latest 27 | 28 | steps: 29 | - name: Install Ninja 30 | run: sudo apt-get install ninja-build 31 | 32 | - uses: actions/checkout@v4 33 | with: 34 | submodules: recursive 35 | 36 | - run: cmake -H$GITHUB_WORKSPACE -B$GITHUB_WORKSPACE/build -GNinja -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake -DANDROID_ABI=${{ matrix.abi }} -DANDROID_PLATFORM=${{ matrix.platform }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} 37 | 38 | - run: cmake --build $GITHUB_WORKSPACE/build 39 | -------------------------------------------------------------------------------- /.github/workflows/clang-analyze.yml: -------------------------------------------------------------------------------- 1 | name: clang-analyze 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | clang-analyze: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | with: 20 | submodules: recursive 21 | 22 | - run: sudo apt-get install -yq clang-tools 23 | 24 | - run: scan-build cmake -E make_directory build 25 | 26 | - working-directory: build/ 27 | run: scan-build cmake $GITHUB_WORKSPACE 28 | 29 | - working-directory: build/ 30 | run: scan-build cmake --build . 31 | -------------------------------------------------------------------------------- /.github/workflows/clang-format.yml: -------------------------------------------------------------------------------- 1 | name: clang-format 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | clang-format: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | with: 20 | submodules: recursive 21 | 22 | - uses: DoozyX/clang-format-lint-action@v0.16.1 23 | with: 24 | source: './include ./src' 25 | exclude: './include/tao/json/external' 26 | extensions: 'hpp,cpp' 27 | clangFormatVersion: 16 28 | -------------------------------------------------------------------------------- /.github/workflows/clang-tidy.yml: -------------------------------------------------------------------------------- 1 | name: clang-tidy 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | clang-tidy: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | with: 20 | submodules: recursive 21 | 22 | - run: sudo apt-get install -yq clang-tidy 23 | 24 | - run: find include/ -name '*.hpp' | grep -vF include/tao/json/external/ | grep -vF endian_win.hpp | xargs -I '{}' clang-tidy --quiet '{}' -- --std=c++17 -Iinclude -Iexternal/PEGTL/include 25 | 26 | - run: find src/ -name '*.cpp' | xargs -I '{}' clang-tidy --quiet '{}' -- --std=c++17 -Iinclude -Iexternal/PEGTL/include 27 | -------------------------------------------------------------------------------- /.github/workflows/code-coverage.yml: -------------------------------------------------------------------------------- 1 | name: Code Coverage 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | code-coverage: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | with: 20 | submodules: recursive 21 | 22 | - run: cmake -E make_directory build 23 | 24 | - working-directory: build/ 25 | run: cmake $GITHUB_WORKSPACE -DCMAKE_CXX_FLAGS="-coverage" 26 | 27 | - working-directory: build/ 28 | run: cmake --build . 29 | 30 | - working-directory: build/ 31 | run: ctest --output-on-failure 32 | 33 | - uses: codecov/codecov-action@v5 34 | with: 35 | token: ${{ secrets.CODECOV_TOKEN }} -------------------------------------------------------------------------------- /.github/workflows/macos.yml: -------------------------------------------------------------------------------- 1 | name: macOS 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | xcode: 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | include: 19 | - xcode: 14 20 | build_type: Release 21 | os: macos-13 22 | - xcode: 15 23 | build_type: Release 24 | os: macos-14 25 | - xcode: 16 26 | build_type: Release 27 | os: macos-15 28 | runs-on: ${{ matrix.os }} 29 | 30 | steps: 31 | - name: Install Ninja 32 | run: brew install ninja 33 | 34 | - uses: actions/checkout@v4 35 | with: 36 | submodules: recursive 37 | 38 | - uses: maxim-lobanov/setup-xcode@v1 39 | with: 40 | xcode-version: ${{ matrix.xcode }} 41 | 42 | - run: cmake -E make_directory build 43 | 44 | - working-directory: build/ 45 | run: cmake $GITHUB_WORKSPACE -GNinja -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} 46 | 47 | - working-directory: build/ 48 | run: cmake --build . 49 | 50 | - working-directory: build/ 51 | run: ctest --output-on-failure 52 | -------------------------------------------------------------------------------- /.github/workflows/sanitizer.yml: -------------------------------------------------------------------------------- 1 | name: Sanitizer 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'README.md' 7 | - 'doc/**' 8 | pull_request: 9 | paths-ignore: 10 | - 'README.md' 11 | - 'doc/**' 12 | 13 | jobs: 14 | sanitizer: 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | cxx: [g++, clang++] 19 | sanitizer: [address, undefined] 20 | 21 | runs-on: ubuntu-latest 22 | 23 | env: 24 | CXX: ${{ matrix.cxx }} 25 | 26 | steps: 27 | - uses: actions/checkout@v3 28 | with: 29 | submodules: recursive 30 | 31 | - run: cmake -E make_directory build 32 | 33 | - working-directory: build/ 34 | run: cmake $GITHUB_WORKSPACE -DCMAKE_CXX_FLAGS="-fsanitize=${{ matrix.sanitizer }}" 35 | 36 | - working-directory: build/ 37 | run: cmake --build . 38 | 39 | - working-directory: build/ 40 | run: ctest --output-on-failure 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | build 3 | private 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "external/PEGTL"] 2 | path = external/PEGTL 3 | url = https://github.com/taocpp/PEGTL 4 | branch = 3.x 5 | -------------------------------------------------------------------------------- /.pkg-config/taocpp-json.pc.in: -------------------------------------------------------------------------------- 1 | includedir="@CMAKE_INSTALL_FULL_INCLUDEDIR@" 2 | 3 | Name: @PROJECT_NAME@ 4 | Description: taoJSON is a C++ header-only JSON library 5 | Version: @PROJECT_VERSION@ 6 | URL: https://github.com/taocpp/json 7 | Cflags: -I${includedir} 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE.itoa: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 James Edward Anhalt III - https://github.com/jeaiii/itoa 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /doc/Changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.0 4 | 5 | **Not yet released** 6 | 7 | ## Milestones 8 | 9 | Pre-1.0.0 milestones in rough reverse chronological order. 10 | 11 | * Change value from `union` to `std::variant`. 12 | * Change library to require and use C++17. 13 | * Direct parsing to arbitrary types. 14 | * Binding facilities and polymorphic factory. 15 | * Optional custom Base class for Values. 16 | * Support for Opaque Pointers in Values. 17 | * JAXN data model with binary data and non-finite doubles. 18 | * JSON Pointer and JSON Patch support. 19 | * Support for CBOR, UBJSON and MsgPack. 20 | * Support for Value Pointer in Values. 21 | * Events Interface for modularity and Transformers. 22 | * Type Traits for interoperation with C++ types. 23 | * 100% conformance score in [Native JSON Benchmark](https://github.com/miloyip/nativejson-benchmark). 24 | * Inclusion of double-conversion library. 25 | * Value Class with parsing and (pretty) printing. 26 | 27 | ## History 28 | 29 | Development of taoJSON started in October 2015 as an experiment to combine the [PEGTL](https://github.com/taocpp/PEGTL) JSON parser with a JSON value class based on a `union` that directly embeds standard containers for arrays, objects and strings. 30 | 31 | Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 32 | -------------------------------------------------------------------------------- /doc/Getting-Started.md: -------------------------------------------------------------------------------- 1 | # Getting Started 2 | 3 | A few short indications on how to use this library: 4 | 5 | * Requires Clang 7 or GCC 9 (or later) with `-std=c++17`, or Visual Studio 2019, or other compilers with sufficient C++17 support. 6 | * The library is header-only, to install and use simply copy the directory [`include/tao`](include/tao) to a convenient place and include the file [`include/tao/json.hpp`](include/tao/json.hpp). 7 | * The generic JSON value class is in [`include/tao/json/value.hpp`](include/tao/json/value.hpp). 8 | * To parse a JSON string representation to a JSON Value, use one of the functions in [`include/tao/json/from_string.hpp`](include/tao/json/from_string.hpp) or [`include/tao/json/from_file.hpp`](include/tao/json/from_file.hpp). 9 | * To produce a JSON string representation from a JSON Value, use one of the appropriate functions in [`include/tao/json/to_string.hpp`](include/tao/json/to_string.hpp) or [`include/tao/json/stream.hpp`](include/tao/json/stream.hpp). 10 | 11 | TODO: Hello-world-sized example. 12 | TODO: Which other pages to reference here? 13 | TODO: Which other concepts to reference here? 14 | 15 | The taocpp/json library is built around two main concepts, the [*Value class*](Value-Class.md) and the [*Events interface*](Events-Interface.md). 16 | 17 | See also the page on [common use cases](Common-Use-Cases.md) for how to do some frequently required things. 18 | 19 | Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 20 | -------------------------------------------------------------------------------- /doc/Overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taocpp/json/11a31e12eda35c1322f9cf6ebb4cca0653d579dc/doc/Overview.png -------------------------------------------------------------------------------- /doc/Scratchpad.md: -------------------------------------------------------------------------------- 1 | # Scratchpad 2 | 3 | TODO! 4 | 5 | ## JSON Pointer 6 | 7 | ###### `` 8 | 9 | ###### `` 10 | 11 | ###### `` 12 | 13 | ###### `` 14 | 15 | ## Still Need Description 16 | 17 | * `events::apply` 18 | * `events::basic_compare` 19 | * `events::compare` 20 | * `events::transformer` 21 | * `nlohmann::from_value` 22 | * `nlohmann::to_value` 23 | * TODO: Anything missing? 24 | 25 | Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 26 | -------------------------------------------------------------------------------- /doc/Types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taocpp/json/11a31e12eda35c1322f9cf6ebb4cca0653d579dc/doc/Types.png -------------------------------------------------------------------------------- /include/tao/json.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_HPP 5 | #define TAO_JSON_HPP 6 | 7 | // Value Class 8 | #include "json/value.hpp" 9 | 10 | // Value Reading 11 | #include "json/from_file.hpp" 12 | #include "json/from_input.hpp" 13 | #include "json/from_stream.hpp" 14 | #include "json/from_string.hpp" 15 | #include "json/parts_parser.hpp" 16 | 17 | // Value Writing 18 | #include "json/stream.hpp" // operator<< 19 | #include "json/to_stream.hpp" 20 | #include "json/to_string.hpp" 21 | 22 | // Value Support 23 | #include "json/operators.hpp" 24 | #include "json/self_contained.hpp" 25 | 26 | // Custom Types 27 | #include "json/binding.hpp" 28 | #include "json/consume.hpp" 29 | #include "json/consume_file.hpp" 30 | #include "json/consume_string.hpp" 31 | #include "json/produce.hpp" 32 | 33 | // Binary Literals 34 | #include "json/binary.hpp" 35 | 36 | // Other Formats 37 | #include "json/cbor.hpp" 38 | #include "json/jaxn.hpp" 39 | #include "json/msgpack.hpp" 40 | #include "json/ubjson.hpp" 41 | 42 | // Events Implementations 43 | #include "json/events.hpp" 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/tao/json/binary_view.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINARY_VIEW_HPP 5 | #define TAO_JSON_BINARY_VIEW_HPP 6 | 7 | #include "span.hpp" 8 | 9 | #include 10 | #include 11 | 12 | namespace tao 13 | { 14 | using binary_view = span< const std::byte >; 15 | 16 | template< typename T > 17 | [[nodiscard]] binary_view to_binary_view( const T* data, const std::size_t size ) noexcept 18 | { 19 | static_assert( sizeof( T ) == 1 ); 20 | return { reinterpret_cast< const std::byte* >( data ), size }; 21 | } 22 | 23 | template< typename T > 24 | [[nodiscard]] binary_view to_binary_view( const T& value ) noexcept 25 | { 26 | return tao::to_binary_view( std::data( value ), std::size( value ) ); 27 | } 28 | 29 | namespace internal 30 | { 31 | [[nodiscard]] inline bool binary_equal( const binary_view lhs, const binary_view rhs ) noexcept 32 | { 33 | return std::equal( lhs.begin(), lhs.end(), rhs.begin(), rhs.end() ); 34 | } 35 | 36 | [[nodiscard]] inline bool binary_less( const binary_view lhs, const binary_view rhs ) noexcept 37 | { 38 | return std::lexicographical_compare( lhs.begin(), lhs.end(), rhs.begin(), rhs.end() ); 39 | } 40 | 41 | } // namespace internal 42 | 43 | } // namespace tao 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/tao/json/binding/element2.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_ELEMENT2_HPP 5 | #define TAO_JSON_BINDING_ELEMENT2_HPP 6 | 7 | #include 8 | 9 | #include "internal/elementr.hpp" 10 | #include "internal/elementw.hpp" 11 | 12 | namespace tao::json::binding 13 | { 14 | namespace internal 15 | { 16 | template< typename GT, GT GET, typename ST, ST SET > 17 | struct element2 18 | : elementr< GT, GET >, 19 | elementw< ST, SET > 20 | { 21 | static_assert( std::is_same_v< typename elementr< GT, GET >::read_class_t, typename elementw< ST, SET >::write_class_t > ); 22 | static_assert( std::is_same_v< typename elementr< GT, GET >::read_value_t, typename elementw< ST, SET >::write_value_t > ); 23 | 24 | using class_t = typename elementr< GT, GET >::read_class_t; 25 | using value_t = typename elementr< GT, GET >::read_value_t; 26 | }; 27 | 28 | } // namespace internal 29 | 30 | template< auto GET, auto SET > 31 | using element2 = internal::element2< decltype( GET ), GET, decltype( SET ), SET >; 32 | 33 | } // namespace tao::json::binding 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/tao/json/binding/for_nothing_value.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_FOR_NOTHING_VALUE_HPP 5 | #define TAO_JSON_BINDING_FOR_NOTHING_VALUE_HPP 6 | 7 | namespace tao::json::binding 8 | { 9 | enum class for_nothing_value : bool 10 | { 11 | encode, 12 | suppress 13 | }; 14 | 15 | } // namespace tao::json::binding 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/tao/json/binding/for_unknown_key.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_FOR_UNKNOWN_KEY_HPP 5 | #define TAO_JSON_BINDING_FOR_UNKNOWN_KEY_HPP 6 | 7 | namespace tao::json::binding 8 | { 9 | enum class for_unknown_key : bool 10 | { 11 | fail, 12 | skip 13 | }; 14 | 15 | } // namespace tao::json::binding 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/tao/json/binding/inherit.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_INHERIT_HPP 5 | #define TAO_JSON_BINDING_INHERIT_HPP 6 | 7 | namespace tao::json::binding 8 | { 9 | template< typename T > 10 | struct inherit; 11 | 12 | } // namespace tao::json::binding 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/tao/json/binding/internal/inherit.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_INTERNAL_INHERIT_HPP 5 | #define TAO_JSON_BINDING_INTERNAL_INHERIT_HPP 6 | 7 | #include "../../internal/type_traits.hpp" 8 | 9 | #include "../inherit.hpp" 10 | 11 | namespace tao::json::binding::internal 12 | { 13 | template< typename T > 14 | struct inherit_elements_t 15 | { 16 | using list = json::internal::type_list< T >; 17 | }; 18 | 19 | template< typename T > 20 | struct inherit_elements_t< inherit< T > > 21 | { 22 | using list = typename T::elements; 23 | }; 24 | 25 | template< typename T > 26 | using inherit_elements = typename inherit_elements_t< T >::list; 27 | 28 | template< typename T > 29 | struct inherit_members_t 30 | { 31 | using list = json::internal::type_list< T >; 32 | }; 33 | 34 | template< typename T > 35 | struct inherit_members_t< inherit< T > > 36 | { 37 | using list = typename T::members; 38 | }; 39 | 40 | template< typename T > 41 | using inherit_members = typename inherit_members_t< T >::list; 42 | 43 | } // namespace tao::json::binding::internal 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/tao/json/binding/member.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_MEMBER_HPP 5 | #define TAO_JSON_BINDING_MEMBER_HPP 6 | 7 | #include 8 | 9 | #include "element.hpp" 10 | #include "element2.hpp" 11 | #include "member_kind.hpp" 12 | 13 | #include "internal/type_key.hpp" 14 | 15 | namespace tao::json::binding 16 | { 17 | template< member_kind R, typename K, auto P > 18 | struct member 19 | : element< P >, 20 | internal::type_key< K, typename binding::element< P >::value_t > 21 | { 22 | static constexpr member_kind kind = R; 23 | 24 | template< template< typename... > class Traits, typename C > 25 | [[nodiscard]] static bool is_nothing( const C& x ) 26 | { 27 | return json::internal::is_nothing< Traits >( binding::element< P >::read( x ) ); 28 | } 29 | }; 30 | 31 | template< member_kind R, typename K, auto G, auto S > 32 | struct member2 33 | : element2< G, S >, 34 | internal::type_key< K, typename binding::element2< G, S >::value_t > 35 | { 36 | static constexpr member_kind kind = R; 37 | 38 | template< template< typename... > class Traits, typename C > 39 | [[nodiscard]] static bool is_nothing( const C& x ) 40 | { 41 | return json::internal::is_nothing< Traits >( binding::element2< G, S >::read( x ) ); 42 | } 43 | }; 44 | 45 | } // namespace tao::json::binding 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /include/tao/json/binding/member_kind.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_BINDING_MEMBER_KIND_HPP 5 | #define TAO_JSON_BINDING_MEMBER_KIND_HPP 6 | 7 | namespace tao::json::binding 8 | { 9 | enum class member_kind : bool 10 | { 11 | optional, 12 | required 13 | }; 14 | 15 | } // namespace tao::json::binding 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/tao/json/cbor.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_HPP 5 | #define TAO_JSON_CBOR_HPP 6 | 7 | #include "../json.hpp" 8 | 9 | #include "cbor/consume_file.hpp" 10 | #include "cbor/consume_string.hpp" 11 | #include "cbor/from_binary.hpp" 12 | #include "cbor/from_file.hpp" 13 | #include "cbor/from_input.hpp" 14 | #include "cbor/from_string.hpp" 15 | #include "cbor/parts_parser.hpp" 16 | #include "cbor/to_stream.hpp" 17 | #include "cbor/to_string.hpp" 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/tao/json/cbor/consume_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_CONSUME_FILE_HPP 5 | #define TAO_JSON_CBOR_CONSUME_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../consume.hpp" 12 | #include "../forward.hpp" 13 | 14 | #include "parts_parser.hpp" 15 | 16 | namespace tao::json::cbor 17 | { 18 | template< typename T, template< typename... > class Traits = traits > 19 | [[nodiscard]] T consume_file( const std::filesystem::path& path ) 20 | { 21 | cbor::basic_parts_parser< utf8_mode::check, 8, pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 22 | return json::consume< T, Traits >( pp ); 23 | } 24 | 25 | template< template< typename... > class Traits = traits, typename T > 26 | void consume_file( const std::filesystem::path& path, T& t ) 27 | { 28 | cbor::basic_parts_parser< utf8_mode::check, 8, pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 29 | json::consume< Traits >( pp, t ); 30 | } 31 | 32 | } // namespace tao::json::cbor 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/tao/json/cbor/consume_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_CONSUME_STRING_HPP 5 | #define TAO_JSON_CBOR_CONSUME_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "parts_parser.hpp" 13 | 14 | namespace tao::json::cbor 15 | { 16 | template< typename T, template< typename... > class Traits = traits, typename F > 17 | [[nodiscard]] T consume_string( F&& string ) 18 | { 19 | cbor::basic_parts_parser< utf8_mode::check, 8, pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 20 | return json::consume< T, Traits >( pp ); 21 | } 22 | 23 | template< template< typename... > class Traits = traits, typename F, typename T > 24 | void consume_string( F&& string, T& t ) 25 | { 26 | cbor::basic_parts_parser< utf8_mode::check, 8, pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 27 | json::consume< Traits >( pp, t ); 28 | } 29 | 30 | } // namespace tao::json::cbor 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/cbor/events/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_EVENTS_FROM_FILE_HPP 5 | #define TAO_JSON_CBOR_EVENTS_FROM_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "../internal/grammar.hpp" 13 | 14 | namespace tao::json::cbor::events 15 | { 16 | // Events producer to parse a file containing a CBOR string representation. 17 | 18 | template< typename Consumer > 19 | void from_file( Consumer& consumer, const std::filesystem::path& path ) 20 | { 21 | pegtl::file_input< pegtl::tracking_mode::lazy > in( path ); 22 | pegtl::parse< internal::grammar >( in, consumer ); 23 | } 24 | 25 | } // namespace tao::json::cbor::events 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/cbor/events/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_EVENTS_FROM_INPUT_HPP 5 | #define TAO_JSON_CBOR_EVENTS_FROM_INPUT_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../internal/grammar.hpp" 12 | 13 | namespace tao::json::cbor::events 14 | { 15 | // Events producers that parse CBOR from a PEGTL input (or something compatible). 16 | 17 | template< typename Consumer, typename Input > 18 | void from_input( Consumer& consumer, Input&& in ) 19 | { 20 | pegtl::parse< cbor::internal::grammar >( std::forward< Input >( in ), consumer ); 21 | } 22 | 23 | template< typename Consumer, typename Input > 24 | void from_input_embedded( Consumer& consumer, Input&& in ) 25 | { 26 | pegtl::parse< cbor::internal::embedded >( std::forward< Input >( in ), consumer ); 27 | } 28 | 29 | template< typename Consumer, typename Outer, typename Input > 30 | void from_input_nested( Consumer& consumer, const Outer& oi, Input&& in ) 31 | { 32 | pegtl::parse_nested< cbor::internal::grammar >( oi, std::forward< Input >( in ), consumer ); 33 | } 34 | 35 | template< typename Consumer, typename Outer, typename Input > 36 | void from_input_embedded_nested( Consumer& consumer, const Outer& oi, Input&& in ) 37 | { 38 | pegtl::parse_nested< cbor::internal::embedded >( oi, std::forward< Input >( in ), consumer ); 39 | } 40 | 41 | } // namespace tao::json::cbor::events 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/cbor/events/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_EVENTS_TO_STRING_HPP 5 | #define TAO_JSON_CBOR_EVENTS_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | 13 | namespace tao::json::cbor::events 14 | { 15 | struct to_string 16 | : to_stream 17 | { 18 | std::ostringstream oss; 19 | 20 | to_string() 21 | : to_stream( oss ) 22 | {} 23 | 24 | [[nodiscard]] std::string value() const& 25 | { 26 | return oss.str(); 27 | } 28 | 29 | [[nodiscard]] std::string value() && 30 | { 31 | return std::move( oss ).str(); 32 | } 33 | }; 34 | 35 | } // namespace tao::json::cbor::events 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/tao/json/cbor/from_binary.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_FROM_BINARY_HPP 5 | #define TAO_JSON_CBOR_FROM_BINARY_HPP 6 | 7 | #include 8 | 9 | #include "../events/to_value.hpp" 10 | #include "../events/transformer.hpp" 11 | 12 | #include "events/from_binary.hpp" 13 | 14 | namespace tao::json::cbor 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_binary( Ts&&... ts ) 18 | { 19 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_binary( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_binary( Ts&&... ts ) 26 | { 27 | return basic_from_binary< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json::cbor 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/cbor/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_FROM_FILE_HPP 5 | #define TAO_JSON_CBOR_FROM_FILE_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_file.hpp" 14 | 15 | namespace tao::json::cbor 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers > 18 | [[nodiscard]] basic_value< Traits > basic_from_file( const std::filesystem::path& path ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | events::from_file( consumer, path ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers > 26 | [[nodiscard]] value from_file( const std::filesystem::path& path ) 27 | { 28 | return basic_from_file< traits, Transformers... >( path ); 29 | } 30 | 31 | } // namespace tao::json::cbor 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/cbor/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_FROM_INPUT_HPP 5 | #define TAO_JSON_CBOR_FROM_INPUT_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_input.hpp" 14 | 15 | namespace tao::json::cbor 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 18 | [[nodiscard]] basic_value< Traits > basic_from_input( Ts&&... ts ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | cbor::events::from_input( consumer, std::forward< Ts >( ts )... ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers, typename... Ts > 26 | [[nodiscard]] value from_input( Ts&&... ts ) 27 | { 28 | return basic_from_input< traits, Transformers... >( std::forward< Ts >( ts )... ); 29 | } 30 | 31 | } // namespace tao::json::cbor 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/cbor/from_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_FROM_STRING_HPP 5 | #define TAO_JSON_CBOR_FROM_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../events/to_value.hpp" 10 | #include "../events/transformer.hpp" 11 | 12 | #include "events/from_string.hpp" 13 | 14 | namespace tao::json::cbor 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_string( Ts&&... ts ) 18 | { 19 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_string( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_string( Ts&&... ts ) 26 | { 27 | return basic_from_string< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json::cbor 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/cbor/internal/major.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_INTERNAL_MAJOR_HPP 5 | #define TAO_JSON_CBOR_INTERNAL_MAJOR_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::cbor::internal 10 | { 11 | enum class major : std::uint8_t 12 | { 13 | UNSIGNED = 0, 14 | NEGATIVE = 0x20, 15 | BINARY = 0x40, 16 | STRING = 0x60, 17 | ARRAY = 0x80, 18 | OBJECT = 0xa0, 19 | TAG = 0xc0, 20 | OTHER = 0xe0 21 | }; 22 | 23 | static constexpr std::uint8_t major_mask = 0xe0; 24 | static constexpr std::uint8_t minor_mask = 0x1f; 25 | 26 | } // namespace tao::json::cbor::internal 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/tao/json/cbor/to_stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_TO_STREAM_HPP 5 | #define TAO_JSON_CBOR_TO_STREAM_HPP 6 | 7 | #include 8 | 9 | #include "../value.hpp" 10 | 11 | #include "../events/from_value.hpp" 12 | #include "../events/transformer.hpp" 13 | 14 | #include "events/to_stream.hpp" 15 | 16 | namespace tao::json::cbor 17 | { 18 | template< template< typename... > class... Transformers, template< typename... > class Traits > 19 | void to_stream( std::ostream& os, const basic_value< Traits >& v ) 20 | { 21 | json::events::transformer< events::to_stream, Transformers... > consumer( os ); 22 | json::events::from_value( consumer, v ); 23 | } 24 | 25 | } // namespace tao::json::cbor 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/cbor/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CBOR_TO_STRING_HPP 5 | #define TAO_JSON_CBOR_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../value.hpp" 11 | 12 | #include "../events/from_value.hpp" 13 | #include "../events/transformer.hpp" 14 | 15 | #include "events/to_string.hpp" 16 | 17 | namespace tao::json::cbor 18 | { 19 | template< template< typename... > class... Transformers, template< typename... > class Traits > 20 | [[nodiscard]] std::string to_string( const basic_value< Traits >& v ) 21 | { 22 | json::events::transformer< events::to_string, Transformers... > consumer; 23 | json::events::from_value( consumer, v ); 24 | return std::move( consumer ).value(); 25 | } 26 | 27 | } // namespace tao::json::cbor 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /include/tao/json/consume_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONSUME_FILE_HPP 5 | #define TAO_JSON_CONSUME_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "consume.hpp" 12 | #include "forward.hpp" 13 | #include "parts_parser.hpp" 14 | 15 | namespace tao::json 16 | { 17 | template< typename T, template< typename... > class Traits = traits > 18 | [[nodiscard]] T consume_file( const std::filesystem::path& path ) 19 | { 20 | basic_parts_parser< pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 21 | return consume< T, Traits >( pp ); 22 | } 23 | 24 | template< template< typename... > class Traits = traits, typename T > 25 | void consume_file( const std::filesystem::path& path, T& t ) 26 | { 27 | basic_parts_parser< pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 28 | consume< Traits >( pp, t ); 29 | } 30 | 31 | } // namespace tao::json 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/consume_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONSUME_STRING_HPP 5 | #define TAO_JSON_CONSUME_STRING_HPP 6 | 7 | #include 8 | 9 | #include "consume.hpp" 10 | #include "forward.hpp" 11 | #include "parts_parser.hpp" 12 | 13 | namespace tao::json 14 | { 15 | template< typename T, template< typename... > class Traits = traits, typename F > 16 | [[nodiscard]] T consume_string( F&& string ) 17 | { 18 | basic_parts_parser< pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 19 | return consume< T, Traits >( pp ); 20 | } 21 | 22 | template< template< typename... > class Traits = traits, typename F, typename T > 23 | void consume_string( F&& string, T& t ) 24 | { 25 | basic_parts_parser< pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 26 | consume< Traits >( pp, t ); 27 | } 28 | 29 | } // namespace tao::json 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/tao/json/contrib/array_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_ARRAY_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_ARRAY_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "internal/array_traits.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< typename T, std::size_t N > 17 | struct array_traits 18 | : internal::array_traits< std::array< T, N > > 19 | { 20 | template< template< typename... > class Traits, typename... With > 21 | static void to( const basic_value< Traits >& v, std::array< T, N >& r, With&... with ) 22 | { 23 | const auto& a = v.get_array(); 24 | for( std::size_t i = 0; i < N; ++i ) { 25 | a[ i ].to_with( r[ i ], with... ); 26 | } 27 | } 28 | 29 | template< template< typename... > class Traits, typename Producer > 30 | static void consume( Producer& parser, std::array< T, N >& r ) 31 | { 32 | auto s = parser.begin_array(); 33 | for( std::size_t i = 0; i < N; ++i ) { 34 | parser.element( s ); 35 | json::consume< Traits >( parser, r[ i ] ); 36 | } 37 | parser.end_array( s ); 38 | } 39 | }; 40 | 41 | } // namespace tao::json 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/contrib/deque_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_DEQUE_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_DEQUE_TRAITS_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../consume.hpp" 11 | #include "../forward.hpp" 12 | 13 | #include "internal/array_traits.hpp" 14 | 15 | namespace tao::json 16 | { 17 | template< typename T, typename... Ts > 18 | struct deque_traits 19 | : internal::array_traits< std::deque< T, Ts... > > 20 | { 21 | template< template< typename... > class Traits, typename... With > 22 | static void to( const basic_value< Traits >& v, std::deque< T, Ts... >& r, With&... with ) 23 | { 24 | const auto& a = v.get_array(); 25 | for( const auto& i : a ) { 26 | r.emplace_back( i.template as_with< T >( with... ) ); 27 | } 28 | } 29 | 30 | template< template< typename... > class Traits, typename Producer > 31 | static void consume( Producer& parser, std::vector< T, Ts... >& v ) 32 | { 33 | auto s = parser.begin_array(); 34 | while( parser.element_or_end_array( s ) ) { 35 | v.emplace_back( json::consume< T, Traits >( parser ) ); 36 | } 37 | } 38 | }; 39 | 40 | } // namespace tao::json 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/tao/json/contrib/list_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_LIST_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_LIST_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "internal/array_traits.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< typename T, typename... Ts > 17 | struct list_traits 18 | : internal::array_traits< std::list< T, Ts... > > 19 | { 20 | template< template< typename... > class Traits, typename... With > 21 | static void to( const basic_value< Traits >& v, std::list< T, Ts... >& r, With&... with ) 22 | { 23 | const auto& a = v.get_array(); 24 | for( const auto& i : a ) { 25 | r.emplace_back( i.template as_with< T >( with... ) ); 26 | } 27 | } 28 | 29 | template< template< typename... > class Traits, typename Producer > 30 | static void consume( Producer& parser, std::list< T, Ts... >& r ) 31 | { 32 | auto s = parser.begin_array(); 33 | while( parser.element_or_end_array( s ) ) { 34 | r.emplace_back( json::consume< T, Traits >( parser ) ); 35 | } 36 | } 37 | }; 38 | 39 | } // namespace tao::json 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/tao/json/contrib/map_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_MAP_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_MAP_TRAITS_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../consume.hpp" 11 | #include "../forward.hpp" 12 | 13 | #include "internal/object_traits.hpp" 14 | 15 | namespace tao::json 16 | { 17 | template< typename T, typename... Ts > 18 | struct map_traits 19 | : internal::object_traits< std::map< std::string, T, Ts... > > 20 | { 21 | template< template< typename... > class Traits, typename... With > 22 | static void to( const basic_value< Traits >& v, std::map< std::string, T, Ts... >& r, With&... with ) 23 | { 24 | const auto& o = v.get_object(); 25 | for( const auto& i : o ) { 26 | r.try_emplace( i.first, i.second.template as_with< T >( with... ) ); 27 | } 28 | } 29 | 30 | template< template< typename... > class Traits, typename Producer > 31 | static void consume( Producer& parser, std::map< std::string, T, Ts... >& v ) 32 | { 33 | auto s = parser.begin_object(); 34 | while( parser.member_or_end_object( s ) ) { 35 | auto k = parser.key(); 36 | v.try_emplace( std::move( k ), json::consume< T, Traits >( parser ) ); 37 | } 38 | } 39 | }; 40 | 41 | } // namespace tao::json 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/contrib/multimap_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_MULTIMAP_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_MULTIMAP_TRAITS_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../consume.hpp" 11 | #include "../forward.hpp" 12 | 13 | #include "internal/object_traits.hpp" 14 | 15 | namespace tao::json 16 | { 17 | template< typename T, typename... Ts > 18 | struct multimap_traits 19 | : internal::object_multi_traits< std::multimap< std::string, T, Ts... > > 20 | { 21 | template< template< typename... > class Traits, typename... With > 22 | static void to( const basic_value< Traits >& v, std::multimap< std::string, T, Ts... >& r, With&... with ) 23 | { 24 | const auto& o = v.get_object(); 25 | for( const auto& i : o ) { 26 | r.try_emplace( i.first, i.second.template as_with< T >( with... ) ); 27 | } 28 | } 29 | 30 | template< template< typename... > class Traits, typename Producer > 31 | static void consume( Producer& parser, std::multimap< std::string, T, Ts... >& v ) 32 | { 33 | auto s = parser.begin_object(); 34 | while( parser.member_or_end_object( s ) ) { 35 | auto k = parser.key(); 36 | v.try_emplace( std::move( k ), json::consume< T, Traits >( parser ) ); 37 | } 38 | } 39 | }; 40 | 41 | } // namespace tao::json 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/contrib/multiset_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_MULTISET_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_MULTISET_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "internal/array_traits.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< typename T, typename... Ts > 17 | struct multiset_traits 18 | : internal::array_traits< std::multiset< T, Ts... > > 19 | { 20 | template< template< typename... > class Traits, typename... With > 21 | static void to( const basic_value< Traits >& v, std::multiset< T, Ts... >& r, With&... with ) 22 | { 23 | const auto& a = v.get_array(); 24 | for( const auto& i : a ) { 25 | r.emplace( i.template as_with< T >( with... ) ); 26 | } 27 | } 28 | 29 | template< template< typename... > class Traits, typename Producer > 30 | static void consume( Producer& parser, std::multiset< T, Ts... >& r ) 31 | { 32 | auto s = parser.begin_array(); 33 | while( parser.element_or_end_array( s ) ) { 34 | r.emplace( json::consume< T, Traits >( parser ) ); 35 | } 36 | } 37 | }; 38 | 39 | } // namespace tao::json 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/tao/json/contrib/pair_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_PAIR_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_PAIR_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../binding.hpp" 10 | 11 | namespace tao::json 12 | { 13 | template< typename U, typename V > 14 | struct pair_traits 15 | : binding::array< TAO_JSON_BIND_ELEMENT( &std::pair< U, V >::first ), 16 | TAO_JSON_BIND_ELEMENT( &std::pair< U, V >::second ) > 17 | {}; 18 | 19 | } // namespace tao::json 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/tao/json/contrib/set_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_SET_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_SET_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "internal/array_traits.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< typename T, typename... Ts > 17 | struct set_traits 18 | : internal::array_traits< std::set< T, Ts... > > 19 | { 20 | template< template< typename... > class Traits, typename... With > 21 | static void to( const basic_value< Traits >& v, std::set< T, Ts... >& r, With&... with ) 22 | { 23 | const auto& a = v.get_array(); 24 | for( const auto& i : a ) { 25 | r.emplace( i.template as_with< T >( with... ) ); 26 | } 27 | } 28 | 29 | template< template< typename... > class Traits, typename Producer > 30 | static void consume( Producer& parser, std::set< T, Ts... >& r ) 31 | { 32 | auto s = parser.begin_array(); 33 | while( parser.element_or_end_array( s ) ) { 34 | r.emplace( json::consume< T, Traits >( parser ) ); 35 | } 36 | } 37 | }; 38 | 39 | } // namespace tao::json 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/tao/json/contrib/tuple_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_TUPLE_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_TUPLE_TRAITS_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../binding.hpp" 11 | 12 | namespace tao::json 13 | { 14 | namespace internal 15 | { 16 | template< std::size_t I, typename... Ts > 17 | struct getter 18 | { 19 | [[nodiscard]] static decltype( auto ) get( std::tuple< Ts... >& t ) noexcept 20 | { 21 | return std::get< I >( t ); 22 | } 23 | 24 | [[nodiscard]] static decltype( auto ) cget( const std::tuple< Ts... >& t ) noexcept 25 | { 26 | return std::get< I >( t ); 27 | } 28 | }; 29 | 30 | template< typename Indices, typename... Ts > 31 | struct tuple_traits; 32 | 33 | template< std::size_t... Is, typename... Ts > 34 | struct tuple_traits< std::index_sequence< Is... >, Ts... > 35 | { 36 | template< std::size_t I > 37 | using helper_t = binding::element2< &getter< I, Ts... >::cget, &getter< I, Ts... >::get >; 38 | 39 | using type = binding::array< helper_t< Is >... >; 40 | }; 41 | 42 | } // namespace internal 43 | 44 | template< typename... Ts > 45 | struct tuple_traits 46 | : internal::tuple_traits< std::index_sequence_for< Ts... >, Ts... >::type 47 | {}; 48 | 49 | } // namespace tao::json 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /include/tao/json/contrib/unordered_map_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_UNORDERED_MAP_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_UNORDERED_MAP_TRAITS_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../consume.hpp" 11 | #include "../forward.hpp" 12 | 13 | #include "internal/object_traits.hpp" 14 | 15 | namespace tao::json 16 | { 17 | template< typename T, typename... Ts > 18 | struct unordered_map_traits 19 | : internal::object_traits< std::unordered_map< std::string, T, Ts... > > 20 | { 21 | template< template< typename... > class Traits, typename... With > 22 | static void to( const basic_value< Traits >& v, std::unordered_map< std::string, T, Ts... >& r, With&... with ) 23 | { 24 | const auto& o = v.get_object(); 25 | for( const auto& i : o ) { 26 | r.try_emplace( i.first, i.second.template as_with< T >( with... ) ); 27 | } 28 | } 29 | 30 | template< template< typename... > class Traits, typename Producer > 31 | static void consume( Producer& parser, std::unordered_map< std::string, T, Ts... >& v ) 32 | { 33 | auto s = parser.begin_object(); 34 | while( parser.member_or_end_object( s ) ) { 35 | auto k = parser.key(); 36 | v.try_emplace( std::move( k ), json::consume< T, Traits >( parser ) ); 37 | } 38 | } 39 | }; 40 | 41 | } // namespace tao::json 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/contrib/unordered_set_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_UNORDERED_SET_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_UNORDERED_SET_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "internal/array_traits.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< typename T, typename... Ts > 17 | struct unordered_set_traits 18 | : internal::array_traits< std::unordered_set< T, Ts... > > 19 | { 20 | template< template< typename... > class Traits, typename... With > 21 | static void to( const basic_value< Traits >& v, std::unordered_set< T, Ts... >& r, With&... with ) 22 | { 23 | const auto& a = v.get_array(); 24 | for( const auto& i : a ) { 25 | r.emplace( i.template as_with< T >( with... ) ); 26 | } 27 | } 28 | 29 | template< template< typename... > class Traits, typename Producer > 30 | static void consume( Producer& parser, std::unordered_set< T, Ts... >& r ) 31 | { 32 | auto s = parser.begin_array(); 33 | while( parser.element_or_end_array( s ) ) { 34 | r.emplace( json::consume< T, Traits >( parser ) ); 35 | } 36 | } 37 | }; 38 | 39 | } // namespace tao::json 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/tao/json/contrib/vector_bool_traits.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_CONTRIB_VECTOR_BOOL_TRAITS_HPP 5 | #define TAO_JSON_CONTRIB_VECTOR_BOOL_TRAITS_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "vector_traits.hpp" 13 | 14 | namespace tao::json 15 | { 16 | struct vector_bool_traits 17 | : vector_traits< bool > 18 | { 19 | template< template< typename... > class Traits > 20 | static void assign( basic_value< Traits >& v, const std::vector< bool >& o ) 21 | { 22 | v.emplace_array(); 23 | v.get_array().reserve( o.size() ); 24 | for( const auto e : o ) { 25 | v.emplace_back( static_cast< bool >( e ) ); 26 | } 27 | } 28 | 29 | template< template< typename... > class Traits, typename Consumer > 30 | static void produce( Consumer& c, const std::vector< bool >& o ) 31 | { 32 | c.begin_array( o.size() ); 33 | for( const auto i : o ) { 34 | json::events::produce< Traits >( c, static_cast< bool >( i ) ); 35 | c.element(); 36 | } 37 | c.end_array( o.size() ); 38 | } 39 | 40 | // TODO: Check whether anything else needs "special-casing". 41 | }; 42 | 43 | } // namespace tao::json 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/tao/json/events/apply.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_APPLY_HPP 5 | #define TAO_JSON_EVENTS_APPLY_HPP 6 | 7 | #include "ref.hpp" 8 | #include "transformer.hpp" 9 | 10 | namespace tao::json::events 11 | { 12 | template< template< typename... > class... Transformer, typename Consumer > 13 | [[nodiscard]] transformer< ref< Consumer >, Transformer... > apply( Consumer& c ) noexcept( noexcept( transformer< ref< Consumer >, Transformer... >( c ) ) ) 14 | { 15 | return transformer< ref< Consumer >, Transformer... >( c ); 16 | } 17 | 18 | } // namespace tao::json::events 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /include/tao/json/events/binary_to_base64.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_BINARY_TO_BASE64_HPP 5 | #define TAO_JSON_EVENTS_BINARY_TO_BASE64_HPP 6 | 7 | #include "../binary_view.hpp" 8 | #include "../internal/base64.hpp" 9 | 10 | namespace tao::json::events 11 | { 12 | template< typename Consumer > 13 | struct binary_to_base64 14 | : Consumer 15 | { 16 | using Consumer::Consumer; 17 | 18 | void binary( const tao::binary_view v ) 19 | { 20 | Consumer::string( internal::base64( v ) ); 21 | } 22 | }; 23 | 24 | } // namespace tao::json::events 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/tao/json/events/binary_to_base64url.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_BINARY_TO_BASE64URL_HPP 5 | #define TAO_JSON_EVENTS_BINARY_TO_BASE64URL_HPP 6 | 7 | #include 8 | 9 | #include "../binary_view.hpp" 10 | #include "../internal/base64url.hpp" 11 | 12 | namespace tao::json::events 13 | { 14 | template< typename Consumer > 15 | struct binary_to_base64url 16 | : Consumer 17 | { 18 | using Consumer::Consumer; 19 | 20 | void binary( const tao::binary_view v ) 21 | { 22 | Consumer::string( internal::base64url( v ) ); 23 | } 24 | }; 25 | 26 | } // namespace tao::json::events 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/tao/json/events/binary_to_exception.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_BINARY_TO_EXCEPTION_HPP 5 | #define TAO_JSON_EVENTS_BINARY_TO_EXCEPTION_HPP 6 | 7 | #include 8 | 9 | #include "../binary_view.hpp" 10 | 11 | namespace tao::json::events 12 | { 13 | template< typename Consumer > 14 | struct binary_to_exception 15 | : Consumer 16 | { 17 | using Consumer::Consumer; 18 | 19 | void binary( const tao::binary_view /*unused*/ ) 20 | { 21 | throw std::runtime_error( "invalid binary data" ); 22 | } 23 | }; 24 | 25 | } // namespace tao::json::events 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/events/binary_to_hex.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_BINARY_TO_HEX_HPP 5 | #define TAO_JSON_EVENTS_BINARY_TO_HEX_HPP 6 | 7 | #include "../binary_view.hpp" 8 | #include "../internal/hexdump.hpp" 9 | 10 | namespace tao::json::events 11 | { 12 | template< typename Consumer > 13 | struct binary_to_hex 14 | : Consumer 15 | { 16 | using Consumer::Consumer; 17 | 18 | void binary( const tao::binary_view v ) 19 | { 20 | Consumer::string( internal::hexdump( v ) ); 21 | } 22 | }; 23 | 24 | } // namespace tao::json::events 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/tao/json/events/discard.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_DISCARD_HPP 5 | #define TAO_JSON_EVENTS_DISCARD_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "../binary_view.hpp" 12 | 13 | namespace tao::json::events 14 | { 15 | // Events consumer that discards events. 16 | 17 | struct discard 18 | { 19 | void null() noexcept {} 20 | 21 | void boolean( const bool /*unused*/ ) noexcept {} 22 | 23 | void number( const std::int64_t /*unused*/ ) noexcept {} 24 | void number( const std::uint64_t /*unused*/ ) noexcept {} 25 | void number( const double /*unused*/ ) noexcept {} 26 | 27 | void string( const std::string_view /*unused*/ ) noexcept {} 28 | 29 | void binary( const tao::binary_view /*unused*/ ) noexcept {} 30 | 31 | void begin_array( const std::size_t /*unused*/ = 0 ) noexcept {} 32 | void element() noexcept {} 33 | void end_array( const std::size_t /*unused*/ = 0 ) noexcept {} 34 | 35 | void begin_object( const std::size_t /*unused*/ = 0 ) noexcept {} 36 | void key( const std::string_view /*unused*/ ) noexcept {} 37 | void member() noexcept {} 38 | void end_object( const std::size_t /*unused*/ = 0 ) noexcept {} 39 | }; 40 | 41 | } // namespace tao::json::events 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/events/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_FROM_FILE_HPP 5 | #define TAO_JSON_EVENTS_FROM_FILE_HPP 6 | 7 | #include 8 | 9 | #include "../internal/action.hpp" 10 | #include "../internal/errors.hpp" 11 | #include "../internal/grammar.hpp" 12 | 13 | #include 14 | #include 15 | 16 | namespace tao::json::events 17 | { 18 | // Events producer to parse a file containing a JSON string representation. 19 | 20 | template< typename Consumer > 21 | void from_file( Consumer& consumer, const std::filesystem::path& path ) 22 | { 23 | pegtl::file_input< pegtl::tracking_mode::lazy > in( path ); 24 | pegtl::parse< internal::grammar, internal::action, internal::errors >( in, consumer ); 25 | } 26 | 27 | } // namespace tao::json::events 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /include/tao/json/events/from_stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_FROM_STREAM_HPP 5 | #define TAO_JSON_EVENTS_FROM_STREAM_HPP 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "../internal/action.hpp" 13 | #include "../internal/errors.hpp" 14 | #include "../internal/grammar.hpp" 15 | 16 | namespace tao::json::events 17 | { 18 | template< typename Consumer > 19 | void from_stream( Consumer& consumer, std::istream& stream, const char* source = nullptr, const std::size_t maximum_buffer_size = 4000 ) 20 | { 21 | pegtl::istream_input in( stream, maximum_buffer_size, source ? source : "tao::json::events::from_stream" ); 22 | pegtl::parse< internal::grammar, internal::action, internal::errors >( in, consumer ); 23 | } 24 | 25 | template< typename Consumer > 26 | void from_stream( Consumer& consumer, std::istream& stream, const std::string& source, const std::size_t maximum_buffer_size = 4000 ) 27 | { 28 | events::from_stream( consumer, stream, source.c_str(), maximum_buffer_size ); 29 | } 30 | 31 | } // namespace tao::json::events 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/events/invalid_string_to_binary.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_INVALID_STRING_TO_BINARY_HPP 5 | #define TAO_JSON_EVENTS_INVALID_STRING_TO_BINARY_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../binary_view.hpp" 11 | #include "../utf8.hpp" 12 | 13 | namespace tao::json::events 14 | { 15 | template< typename Consumer > 16 | struct invalid_string_to_binary 17 | : Consumer 18 | { 19 | using Consumer::Consumer; 20 | 21 | void string( const char* v ) 22 | { 23 | string( std::string_view( v ) ); 24 | } 25 | 26 | void string( std::string&& v ) 27 | { 28 | if( internal::validate_utf8_nothrow( v ) ) { 29 | Consumer::string( std::move( v ) ); 30 | } 31 | else { 32 | const auto* const data = reinterpret_cast< const std::byte* >( v.data() ); 33 | Consumer::binary( std::vector< std::byte >( data, data + v.size() ) ); 34 | } 35 | } 36 | 37 | void string( const std::string_view v ) 38 | { 39 | if( internal::validate_utf8_nothrow( v ) ) { 40 | Consumer::string( v ); 41 | } 42 | else { 43 | Consumer::binary( tao::binary_view( reinterpret_cast< const std::byte* >( v.data() ), v.size() ) ); 44 | } 45 | } 46 | }; 47 | 48 | } // namespace tao::json::events 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /include/tao/json/events/invalid_string_to_exception.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_INVALID_STRING_TO_EXCEPTION_HPP 5 | #define TAO_JSON_EVENTS_INVALID_STRING_TO_EXCEPTION_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../internal/hexdump.hpp" 11 | #include "../utf8.hpp" 12 | 13 | namespace tao::json::events 14 | { 15 | template< typename Consumer > 16 | struct invalid_string_to_exception 17 | : Consumer 18 | { 19 | using Consumer::Consumer; 20 | 21 | void string( const char* v ) 22 | { 23 | string( std::string_view( v ) ); 24 | } 25 | 26 | void string( std::string&& v ) 27 | { 28 | if( internal::validate_utf8_nothrow( v ) ) { 29 | Consumer::string( std::move( v ) ); 30 | } 31 | else { 32 | throw std::runtime_error( "invalid UTF8 string: $" + internal::hexdump( v ) ); 33 | } 34 | } 35 | 36 | void string( const std::string_view v ) 37 | { 38 | if( internal::validate_utf8_nothrow( v ) ) { 39 | Consumer::string( v ); 40 | } 41 | else { 42 | throw std::runtime_error( "invalid UTF8 string: $" + internal::hexdump( v ) ); 43 | } 44 | } 45 | }; 46 | 47 | } // namespace tao::json::events 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/tao/json/events/invalid_string_to_hex.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_INVALID_STRING_TO_HEX_HPP 5 | #define TAO_JSON_EVENTS_INVALID_STRING_TO_HEX_HPP 6 | 7 | #include 8 | 9 | #include "../internal/hexdump.hpp" 10 | #include "../utf8.hpp" 11 | 12 | namespace tao::json::events 13 | { 14 | template< typename Consumer > 15 | struct invalid_string_to_hex 16 | : Consumer 17 | { 18 | using Consumer::Consumer; 19 | 20 | void string( const char* v ) 21 | { 22 | string( std::string_view( v ) ); 23 | } 24 | 25 | void string( std::string&& v ) 26 | { 27 | if( internal::validate_utf8_nothrow( v ) ) { 28 | Consumer::string( std::move( v ) ); 29 | } 30 | else { 31 | Consumer::string( internal::hexdump( v ) ); 32 | } 33 | } 34 | 35 | void string( const std::string_view v ) 36 | { 37 | if( internal::validate_utf8_nothrow( v ) ) { 38 | Consumer::string( v ); 39 | } 40 | else { 41 | Consumer::string( internal::hexdump( v ) ); 42 | } 43 | } 44 | }; 45 | 46 | } // namespace tao::json::events 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/tao/json/events/limit_value_count.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_LIMIT_VALUE_COUNT_HPP 5 | #define TAO_JSON_EVENTS_LIMIT_VALUE_COUNT_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | namespace tao::json::events 13 | { 14 | template< typename Consumer, std::size_t Limit > 15 | class limit_value_count 16 | : public Consumer 17 | { 18 | private: 19 | std::size_t m_count = 1; // Top-level value is implied. 20 | 21 | void count_and_limit() 22 | { 23 | if( ++m_count > Limit ) { 24 | throw std::runtime_error( "value count limit exceeded" ); 25 | } 26 | } 27 | 28 | public: 29 | using Consumer::Consumer; 30 | 31 | void element() 32 | { 33 | count_and_limit(); 34 | Consumer::element(); 35 | } 36 | 37 | void member() 38 | { 39 | count_and_limit(); 40 | Consumer::member(); 41 | } 42 | }; 43 | 44 | } // namespace tao::json::events 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/tao/json/events/non_finite_to_exception.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_NON_FINITE_TO_EXCEPTION_HPP 5 | #define TAO_JSON_EVENTS_NON_FINITE_TO_EXCEPTION_HPP 6 | 7 | #include 8 | #include 9 | 10 | namespace tao::json::events 11 | { 12 | template< typename Consumer > 13 | struct non_finite_to_exception 14 | : Consumer 15 | { 16 | using Consumer::Consumer; 17 | 18 | using Consumer::number; 19 | 20 | void number( const double v ) 21 | { 22 | if( !std::isfinite( v ) ) { 23 | throw std::runtime_error( "invalid non-finite double value" ); 24 | } 25 | Consumer::number( v ); 26 | } 27 | }; 28 | 29 | } // namespace tao::json::events 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/tao/json/events/non_finite_to_null.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_NON_FINITE_TO_NULL_HPP 5 | #define TAO_JSON_EVENTS_NON_FINITE_TO_NULL_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::events 10 | { 11 | template< typename Consumer > 12 | struct non_finite_to_null 13 | : Consumer 14 | { 15 | using Consumer::Consumer; 16 | 17 | using Consumer::number; 18 | 19 | void number( const double v ) 20 | { 21 | if( !std::isfinite( v ) ) { 22 | Consumer::null(); 23 | } 24 | else { 25 | Consumer::number( v ); 26 | } 27 | } 28 | }; 29 | 30 | } // namespace tao::json::events 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/events/non_finite_to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_NON_FINITE_TO_STRING_HPP 5 | #define TAO_JSON_EVENTS_NON_FINITE_TO_STRING_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::events 10 | { 11 | template< typename Consumer > 12 | struct non_finite_to_string 13 | : Consumer 14 | { 15 | using Consumer::Consumer; 16 | 17 | using Consumer::number; 18 | 19 | void number( const double v ) 20 | { 21 | if( !std::isfinite( v ) ) { 22 | if( std::isnan( v ) ) { 23 | Consumer::string( "NaN" ); 24 | } 25 | else if( v > 0 ) { 26 | Consumer::string( "Infinity" ); 27 | } 28 | else { 29 | Consumer::string( "-Infinity" ); 30 | } 31 | } 32 | else { 33 | Consumer::number( v ); 34 | } 35 | } 36 | }; 37 | 38 | } // namespace tao::json::events 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/tao/json/events/prefer_signed.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_PREFER_SIGNED_HPP 5 | #define TAO_JSON_EVENTS_PREFER_SIGNED_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::events 10 | { 11 | template< typename Consumer > 12 | struct prefer_signed 13 | : Consumer 14 | { 15 | using Consumer::Consumer; 16 | 17 | using Consumer::number; 18 | 19 | void number( const std::uint64_t v ) 20 | { 21 | if( v <= 9223372036854775807ULL ) { 22 | Consumer::number( static_cast< std::int64_t >( v ) ); 23 | } 24 | else { 25 | Consumer::number( v ); 26 | } 27 | } 28 | }; 29 | 30 | } // namespace tao::json::events 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/events/prefer_unsigned.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_PREFER_UNSIGNED_HPP 5 | #define TAO_JSON_EVENTS_PREFER_UNSIGNED_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::events 10 | { 11 | template< typename Consumer > 12 | struct prefer_unsigned 13 | : Consumer 14 | { 15 | using Consumer::Consumer; 16 | 17 | using Consumer::number; 18 | 19 | void number( const std::int64_t v ) 20 | { 21 | if( v >= 0 ) { 22 | Consumer::number( static_cast< std::uint64_t >( v ) ); 23 | } 24 | else { 25 | Consumer::number( v ); 26 | } 27 | } 28 | }; 29 | 30 | } // namespace tao::json::events 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/events/produce.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_PRODUCE_HPP 5 | #define TAO_JSON_EVENTS_PRODUCE_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../forward.hpp" 11 | 12 | namespace tao::json::events 13 | { 14 | template< template< typename... > class Traits = traits, typename Consumer, typename T > 15 | void produce( Consumer& c, T&& t ) 16 | { 17 | Traits< std::decay_t< T > >::template produce< Traits >( c, std::forward< T >( t ) ); 18 | } 19 | 20 | template< template< typename... > class Traits = traits, typename Consumer, typename T > 21 | void produce( Consumer&& c, T&& t ) 22 | { 23 | produce< Traits >( c, std::forward< T >( t ) ); 24 | } 25 | 26 | } // namespace tao::json::events 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/tao/json/events/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_TO_STRING_HPP 5 | #define TAO_JSON_EVENTS_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | 13 | namespace tao::json::events 14 | { 15 | // Events consumer to build a JSON string representation. 16 | 17 | struct to_string 18 | : to_stream 19 | { 20 | std::ostringstream oss; 21 | 22 | to_string() 23 | : to_stream( oss ) 24 | {} 25 | 26 | [[nodiscard]] std::string value() const& 27 | { 28 | return oss.str(); 29 | } 30 | 31 | [[nodiscard]] std::string value() && 32 | { 33 | return std::move( oss ).str(); 34 | } 35 | }; 36 | 37 | } // namespace tao::json::events 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/tao/json/events/to_value.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_TO_VALUE_HPP 5 | #define TAO_JSON_EVENTS_TO_VALUE_HPP 6 | 7 | #include "set_value.hpp" 8 | 9 | #include "../value.hpp" 10 | 11 | namespace tao::json::events 12 | { 13 | // Events consumer to build a JSON Value. 14 | 15 | template< template< typename... > class Traits > 16 | struct to_basic_value 17 | : set_basic_value< Traits > 18 | { 19 | basic_value< Traits > value; 20 | 21 | to_basic_value() noexcept 22 | : set_basic_value< Traits >( value ) 23 | {} 24 | }; 25 | 26 | using to_value = to_basic_value< traits >; 27 | 28 | } // namespace tao::json::events 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /include/tao/json/events/validate_keys.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_VALIDATE_KEYS_HPP 5 | #define TAO_JSON_EVENTS_VALIDATE_KEYS_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | namespace tao::json::events 16 | { 17 | template< typename Consumer, typename Rule > 18 | struct validate_keys 19 | : Consumer 20 | { 21 | using Consumer::Consumer; 22 | 23 | void validate_key( const std::string_view v ) 24 | { 25 | pegtl::memory_input< pegtl::tracking_mode::lazy > in( v.data(), v.size(), "validate_key" ); 26 | if( !pegtl::parse< Rule >( in ) ) { 27 | throw std::runtime_error( "invalid key: " + std::string( v ) ); 28 | } 29 | } 30 | 31 | void key( const std::string_view v ) 32 | { 33 | validate_key( v ); 34 | Consumer::key( v ); 35 | } 36 | 37 | void key( const char* v ) 38 | { 39 | validate_key( v ); 40 | Consumer::key( v ); 41 | } 42 | 43 | void key( std::string&& v ) 44 | { 45 | validate_key( v ); 46 | Consumer::key( std::move( v ) ); 47 | } 48 | }; 49 | 50 | } // namespace tao::json::events 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/tao/json/events/virtualize.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_EVENTS_VIRTUALIZE_HPP 5 | #define TAO_JSON_EVENTS_VIRTUALIZE_HPP 6 | 7 | #include 8 | 9 | #include "virtual_base.hpp" 10 | #include "virtual_ref.hpp" 11 | 12 | namespace tao::json::events 13 | { 14 | template< typename Consumer > 15 | [[nodiscard]] decltype( auto ) virtualize( Consumer& c ) noexcept 16 | { 17 | if constexpr( std::is_base_of_v< virtual_base, Consumer > ) { 18 | return c; 19 | } 20 | else { 21 | return events::virtual_ref( c ); 22 | } 23 | } 24 | 25 | } // namespace tao::json::events 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/forward.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_FORWARD_HPP 5 | #define TAO_JSON_FORWARD_HPP 6 | 7 | namespace tao::json 8 | { 9 | namespace events 10 | { 11 | class virtual_base; 12 | 13 | } // namespace events 14 | 15 | template< typename T, typename = void > 16 | struct traits 17 | {}; 18 | 19 | template< template< typename... > class Traits > 20 | class basic_value; 21 | 22 | using value = basic_value< traits >; 23 | 24 | using producer_t = void( events::virtual_base&, const void* ); 25 | 26 | namespace internal 27 | { 28 | struct opaque_ptr_t 29 | { 30 | const void* data; 31 | producer_t* producer; 32 | }; 33 | 34 | template< template< typename... > class Traits > 35 | struct single; 36 | 37 | template< template< typename... > class Traits > 38 | struct pair; 39 | 40 | } // namespace internal 41 | 42 | } // namespace tao::json 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/tao/json/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_FROM_FILE_HPP 5 | #define TAO_JSON_FROM_FILE_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "events/from_file.hpp" 11 | #include "events/to_value.hpp" 12 | #include "events/transformer.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers > 17 | [[nodiscard]] basic_value< Traits > basic_from_file( const std::filesystem::path& path ) 18 | { 19 | events::transformer< events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_file( consumer, path ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers > 25 | [[nodiscard]] value from_file( const std::filesystem::path& path ) 26 | { 27 | return basic_from_file< traits, Transformers... >( path ); 28 | } 29 | 30 | } // namespace tao::json 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_FROM_INPUT_HPP 5 | #define TAO_JSON_FROM_INPUT_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "events/from_input.hpp" 11 | #include "events/to_value.hpp" 12 | #include "events/transformer.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_input( Ts&&... ts ) 18 | { 19 | events::transformer< events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_input( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_input( Ts&&... ts ) 26 | { 27 | return basic_from_input< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/from_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_FROM_STRING_HPP 5 | #define TAO_JSON_FROM_STRING_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "events/from_string.hpp" 11 | #include "events/to_value.hpp" 12 | #include "events/transformer.hpp" 13 | 14 | namespace tao::json 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_string( Ts&&... ts ) 18 | { 19 | events::transformer< events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_string( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_string( Ts&&... ts ) 26 | { 27 | return basic_from_string< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | inline namespace literals 31 | { 32 | [[nodiscard]] inline value operator""_json( const char* data, const std::size_t size ) 33 | { 34 | return json::from_string( data, size, "literal" ); 35 | } 36 | 37 | } // namespace literals 38 | 39 | } // namespace tao::json 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/tao/json/internal/base64.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_BASE64_HPP 5 | #define TAO_JSON_INTERNAL_BASE64_HPP 6 | 7 | #include 8 | #include 9 | 10 | namespace tao::json::internal 11 | { 12 | template< typename T > 13 | [[nodiscard]] std::string base64( const T& v ) 14 | { 15 | static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 16 | 17 | std::string s; 18 | s.reserve( ( v.size() + 2 ) / 3 * 4 ); 19 | 20 | unsigned cycle = 0; 21 | unsigned encode = 0; 22 | for( const auto c : v ) { 23 | encode <<= 8; 24 | encode += static_cast< unsigned char >( c ); 25 | s += table[ ( encode >> ( ++cycle * 2 ) ) & 0x3f ]; 26 | if( cycle == 3 ) { 27 | cycle = 0; 28 | s += table[ encode & 0x3f ]; 29 | } 30 | } 31 | 32 | switch( cycle ) { 33 | case 0: 34 | break; 35 | 36 | case 1: 37 | s += table[ ( encode << 4 ) & 0x3f ]; 38 | s += "=="; 39 | break; 40 | 41 | case 2: 42 | s += table[ ( encode << 2 ) & 0x3f ]; 43 | s += '='; 44 | break; 45 | 46 | default: 47 | throw std::logic_error( "code should be unreachable" ); // LCOV_EXCL_LINE 48 | } 49 | 50 | return s; 51 | } 52 | 53 | } // namespace tao::json::internal 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/tao/json/internal/base64url.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_BASE64URL_HPP 5 | #define TAO_JSON_INTERNAL_BASE64URL_HPP 6 | 7 | #include 8 | #include 9 | 10 | namespace tao::json::internal 11 | { 12 | template< typename T > 13 | [[nodiscard]] std::string base64url( const T& v ) 14 | { 15 | static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 16 | 17 | std::string s; 18 | s.reserve( ( v.size() + 2 ) / 3 * 4 ); 19 | 20 | unsigned cycle = 0; 21 | unsigned encode = 0; 22 | for( const auto c : v ) { 23 | encode <<= 8; 24 | encode += static_cast< unsigned char >( c ); 25 | s += table[ ( encode >> ( ++cycle * 2 ) ) & 0x3f ]; 26 | if( cycle == 3 ) { 27 | cycle = 0; 28 | s += table[ encode & 0x3f ]; 29 | } 30 | } 31 | 32 | switch( cycle ) { 33 | case 0: 34 | break; 35 | 36 | case 1: 37 | s += table[ ( encode << 4 ) & 0x3f ]; 38 | break; 39 | 40 | case 2: 41 | s += table[ ( encode << 2 ) & 0x3f ]; 42 | break; 43 | 44 | default: 45 | throw std::logic_error( "code should be unreachable" ); // LCOV_EXCL_LINE 46 | } 47 | 48 | return s; 49 | } 50 | 51 | } // namespace tao::json::internal 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/tao/json/internal/dependent_false.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_DEPENDENT_FALSE_HPP 5 | #define TAO_JSON_INTERNAL_DEPENDENT_FALSE_HPP 6 | 7 | namespace tao::json::internal 8 | { 9 | template< typename... > 10 | inline constexpr bool dependent_false = false; 11 | 12 | } // namespace tao::json::internal 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/tao/json/internal/endian.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_ENDIAN_HPP 5 | #define TAO_JSON_INTERNAL_ENDIAN_HPP 6 | 7 | #include 8 | #include 9 | 10 | #if defined( _WIN32 ) && !defined( __MINGW32__ ) 11 | #include "endian_win.hpp" 12 | #else 13 | #include "endian_gcc.hpp" 14 | #endif 15 | 16 | namespace tao::json::internal 17 | { 18 | template< typename N > 19 | [[nodiscard]] N h_to_be( const N n ) noexcept 20 | { 21 | return N( to_and_from_be< sizeof( N ) >::convert( n ) ); 22 | } 23 | 24 | template< typename N > 25 | [[nodiscard]] N be_to_h( const N n ) noexcept 26 | { 27 | return h_to_be( n ); 28 | } 29 | 30 | template< typename N > 31 | [[nodiscard]] N be_to_h( const void* p ) noexcept 32 | { 33 | N n; 34 | std::memcpy( &n, p, sizeof( n ) ); 35 | return internal::be_to_h( n ); 36 | } 37 | 38 | template< typename N > 39 | [[nodiscard]] N h_to_le( const N n ) noexcept 40 | { 41 | return N( to_and_from_le< sizeof( N ) >::convert( n ) ); 42 | } 43 | 44 | template< typename N > 45 | [[nodiscard]] N le_to_h( const N n ) noexcept 46 | { 47 | return h_to_le( n ); 48 | } 49 | 50 | template< typename N > 51 | [[nodiscard]] N le_to_h( const void* p ) noexcept 52 | { 53 | N n; 54 | std::memcpy( &n, p, sizeof( n ) ); 55 | return internal::le_to_h( n ); 56 | } 57 | 58 | } // namespace tao::json::internal 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/tao/json/internal/format.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_FORMAT_HPP 5 | #define TAO_JSON_INTERNAL_FORMAT_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "escape.hpp" 14 | 15 | #include "../forward.hpp" 16 | #include "../message_extension.hpp" 17 | #include "../type.hpp" 18 | 19 | namespace tao::json::internal 20 | { 21 | inline void to_stream( std::ostream& os, const bool v ) 22 | { 23 | os << ( v ? "true" : "false" ); 24 | } 25 | 26 | inline void to_stream( std::ostream& os, const type t ) 27 | { 28 | os << to_string( t ); 29 | } 30 | 31 | template< typename T > 32 | void to_stream( std::ostream& os, const T& t ) 33 | { 34 | os << t; 35 | } 36 | 37 | template< std::size_t N > 38 | void to_stream( std::ostream& os, const char ( &t )[ N ] ) 39 | { 40 | os.write( t, N - 1 ); 41 | } 42 | 43 | template< typename... Ts > 44 | void format_to( std::ostream& oss, const Ts&... ts ) 45 | { 46 | ( internal::to_stream( oss, ts ), ... ); 47 | } 48 | 49 | template< typename... Ts > 50 | [[nodiscard]] std::string format( const Ts&... ts ) 51 | { 52 | std::ostringstream oss; 53 | format_to( oss, ts... ); 54 | return std::move( oss ).str(); 55 | } 56 | 57 | } // namespace tao::json::internal 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /include/tao/json/internal/hexdump.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_HEXDUMP_HPP 5 | #define TAO_JSON_INTERNAL_HEXDUMP_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | namespace tao::json::internal 12 | { 13 | template< typename T > 14 | void hexdump( std::ostream& os, const T& v ) 15 | { 16 | static const char h[] = "0123456789ABCDEF"; 17 | for( const auto b : v ) { 18 | os.put( h[ static_cast< unsigned char >( b ) >> 4 ] ); 19 | os.put( h[ static_cast< unsigned char >( b ) & 0xF ] ); 20 | } 21 | } 22 | 23 | template< typename T > 24 | [[nodiscard]] std::string hexdump( const T& v ) 25 | { 26 | std::ostringstream oss; 27 | internal::hexdump( oss, v ); 28 | return std::move( oss ).str(); 29 | } 30 | 31 | } // namespace tao::json::internal 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/internal/identity.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_IDENTITY_HPP 5 | #define TAO_JSON_INTERNAL_IDENTITY_HPP 6 | 7 | #include 8 | 9 | namespace tao::internal 10 | { 11 | template< typename T > 12 | struct identity 13 | { 14 | using type = T; 15 | }; 16 | 17 | template< typename T > 18 | using identity_t = typename identity< T >::type; 19 | 20 | } // namespace tao::internal 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /include/tao/json/internal/pair.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_PAIR_HPP 5 | #define TAO_JSON_INTERNAL_PAIR_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "../forward.hpp" 12 | 13 | namespace tao::json::internal 14 | { 15 | template< template< typename... > class Traits > 16 | struct pair 17 | { 18 | mutable std::string key; 19 | mutable basic_value< Traits > value; 20 | 21 | template< typename U > 22 | pair( U&& v ) // NOLINT(bugprone-forwarding-reference-overload) 23 | : key( Traits< std::decay_t< U > >::template default_key< Traits >::as_string() ), 24 | value( std::forward< U >( v ) ) 25 | {} 26 | 27 | template< typename U > 28 | pair( U&& k, basic_value< Traits >&& v ) 29 | : key( std::forward< U >( k ) ), 30 | value( std::move( v ) ) 31 | {} 32 | 33 | template< typename U > 34 | pair( U&& k, const basic_value< Traits >& v ) 35 | : key( std::forward< U >( k ) ), 36 | value( v ) 37 | {} 38 | }; 39 | 40 | } // namespace tao::json::internal 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/tao/json/internal/single.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_SINGLE_HPP 5 | #define TAO_JSON_INTERNAL_SINGLE_HPP 6 | 7 | #include 8 | 9 | #include "../forward.hpp" 10 | 11 | #include "pair.hpp" 12 | 13 | namespace tao::json::internal 14 | { 15 | template< template< typename... > class Traits > 16 | struct single 17 | { 18 | mutable basic_value< Traits > value; 19 | 20 | template< typename U > 21 | single( U&& v ) // NOLINT(bugprone-forwarding-reference-overload) 22 | : value( std::forward< U >( v ) ) 23 | {} 24 | 25 | single( std::initializer_list< pair< Traits > >&& l ) 26 | : value( std::move( l ) ) 27 | {} 28 | 29 | single( const std::initializer_list< pair< Traits > >& l ) 30 | : value( l ) 31 | {} 32 | 33 | single( std::initializer_list< pair< Traits > >& l ) 34 | : value( l ) 35 | {} 36 | }; 37 | 38 | } // namespace tao::json::internal 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/tao/json/internal/string_t.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_STRING_T_HPP 5 | #define TAO_JSON_INTERNAL_STRING_T_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | namespace tao::json::internal 13 | { 14 | template< char... Cs > 15 | struct string_t 16 | : pegtl::string< Cs... > 17 | { 18 | static constexpr const char value[] = { Cs..., 0 }; 19 | 20 | [[nodiscard]] static constexpr std::string_view as_string_view() noexcept 21 | { 22 | return std::string_view( value, sizeof...( Cs ) ); 23 | } 24 | 25 | [[nodiscard]] static std::string as_string() 26 | { 27 | return std::string( value, sizeof...( Cs ) ); 28 | } 29 | }; 30 | 31 | } // namespace tao::json::internal 32 | 33 | #define TAO_JSON_STRING_T( x ) TAO_PEGTL_INTERNAL_STRING( tao::json::internal::string_t, x ) 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/tao/json/internal/unescape_action.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INTERNAL_UNESCAPE_ACTION_HPP 5 | #define TAO_JSON_INTERNAL_UNESCAPE_ACTION_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "grammar.hpp" 11 | 12 | namespace tao::json::internal 13 | { 14 | // clang-format off 15 | template< typename Rule > struct unescape_action : pegtl::nothing< Rule > {}; 16 | 17 | template<> struct unescape_action< rules::escaped_unicode > : pegtl::unescape::unescape_j {}; 18 | template<> struct unescape_action< rules::escaped_char > : pegtl::unescape::unescape_c< rules::escaped_char, '"', '\\', '/', '\b', '\f', '\n', '\r', '\t' > {}; 19 | template<> struct unescape_action< rules::unescaped > : pegtl::unescape::append_all {}; 20 | // clang-format on 21 | 22 | } // namespace tao::json::internal 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /include/tao/json/jaxn.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_HPP 5 | #define TAO_JSON_JAXN_HPP 6 | 7 | #include "../json.hpp" 8 | 9 | #include "jaxn/consume_file.hpp" 10 | #include "jaxn/consume_string.hpp" 11 | #include "jaxn/from_file.hpp" 12 | #include "jaxn/from_input.hpp" 13 | #include "jaxn/from_stream.hpp" 14 | #include "jaxn/from_string.hpp" 15 | #include "jaxn/parts_parser.hpp" 16 | #include "jaxn/to_stream.hpp" 17 | #include "jaxn/to_string.hpp" 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/consume_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_CONSUME_FILE_HPP 5 | #define TAO_JSON_JAXN_CONSUME_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../consume.hpp" 12 | #include "../forward.hpp" 13 | 14 | #include "parts_parser.hpp" 15 | 16 | namespace tao::json::jaxn 17 | { 18 | template< typename T, template< typename... > class Traits = traits > 19 | [[nodiscard]] T consume_file( const std::filesystem::path& path ) 20 | { 21 | jaxn::basic_parts_parser< pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 22 | return json::consume< T, Traits >( pp ); 23 | } 24 | 25 | template< template< typename... > class Traits = traits, typename T > 26 | void consume_file( const std::filesystem::path& path, T& t ) 27 | { 28 | jaxn::basic_parts_parser< pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 29 | json::consume< Traits >( pp, t ); 30 | } 31 | 32 | } // namespace tao::json::jaxn 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/consume_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_CONSUME_STRING_HPP 5 | #define TAO_JSON_JAXN_CONSUME_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "parts_parser.hpp" 13 | 14 | namespace tao::json::jaxn 15 | { 16 | template< typename T, template< typename... > class Traits = traits, typename F > 17 | [[nodiscard]] T consume_string( F&& string ) 18 | { 19 | jaxn::basic_parts_parser< pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 20 | return json::consume< T, Traits >( pp ); 21 | } 22 | 23 | template< template< typename... > class Traits = traits, typename F, typename T > 24 | void consume_string( F&& string, T& t ) 25 | { 26 | jaxn::basic_parts_parser< pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 27 | json::consume< Traits >( pp, t ); 28 | } 29 | 30 | } // namespace tao::json::jaxn 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/events/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_EVENTS_FROM_FILE_HPP 5 | #define TAO_JSON_JAXN_EVENTS_FROM_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "../internal/action.hpp" 13 | #include "../internal/errors.hpp" 14 | #include "../internal/grammar.hpp" 15 | 16 | namespace tao::json::jaxn::events 17 | { 18 | // Events producer to parse a file containing a JAXN string representation. 19 | 20 | template< typename Consumer > 21 | void from_file( Consumer& consumer, const std::filesystem::path& path ) 22 | { 23 | pegtl::file_input< pegtl::tracking_mode::lazy > in( path ); 24 | pegtl::parse< internal::grammar, internal::action, internal::errors >( in, consumer ); 25 | } 26 | 27 | } // namespace tao::json::jaxn::events 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/events/from_stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_EVENTS_FROM_STREAM_HPP 5 | #define TAO_JSON_JAXN_EVENTS_FROM_STREAM_HPP 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "../internal/action.hpp" 13 | #include "../internal/errors.hpp" 14 | #include "../internal/grammar.hpp" 15 | 16 | namespace tao::json::jaxn::events 17 | { 18 | template< typename Consumer > 19 | void from_stream( Consumer& consumer, std::istream& stream, const char* source = nullptr, const std::size_t maximum_buffer_size = 4000 ) 20 | { 21 | pegtl::istream_input in( stream, maximum_buffer_size, source ? source : "tao::json::events::jaxn::from_stream" ); 22 | pegtl::parse< internal::grammar, internal::action, internal::errors >( in, consumer ); 23 | } 24 | 25 | template< typename Consumer > 26 | void from_stream( Consumer& consumer, std::istream& stream, const std::string& source, const std::size_t maximum_buffer_size = 4000 ) 27 | { 28 | from_stream( consumer, stream, source.c_str(), maximum_buffer_size ); 29 | } 30 | 31 | } // namespace tao::json::jaxn::events 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/events/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_EVENTS_TO_STRING_HPP 5 | #define TAO_JSON_JAXN_EVENTS_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | 13 | namespace tao::json::jaxn::events 14 | { 15 | // Events consumer to build a JAXN string representation. 16 | 17 | struct to_string 18 | : to_stream 19 | { 20 | std::ostringstream oss; 21 | 22 | to_string() 23 | : to_stream( oss ) 24 | {} 25 | 26 | [[nodiscard]] std::string value() const& 27 | { 28 | return oss.str(); 29 | } 30 | 31 | [[nodiscard]] std::string value() && 32 | { 33 | return std::move( oss ).str(); 34 | } 35 | }; 36 | 37 | } // namespace tao::json::jaxn::events 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_FROM_FILE_HPP 5 | #define TAO_JSON_JAXN_FROM_FILE_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_file.hpp" 14 | 15 | namespace tao::json::jaxn 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers > 18 | [[nodiscard]] basic_value< Traits > basic_from_file( const std::filesystem::path& path ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | events::from_file( consumer, path ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers > 26 | [[nodiscard]] value from_file( const std::filesystem::path& path ) 27 | { 28 | return basic_from_file< traits, Transformers... >( path ); 29 | } 30 | 31 | } // namespace tao::json::jaxn 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_FROM_INPUT_HPP 5 | #define TAO_JSON_JAXN_FROM_INPUT_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_input.hpp" 14 | 15 | namespace tao::json::jaxn 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 18 | [[nodiscard]] basic_value< Traits > basic_from_input( Ts&&... ts ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | jaxn::events::from_input( consumer, std::forward< Ts >( ts )... ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers, typename... Ts > 26 | [[nodiscard]] value from_input( Ts&&... ts ) 27 | { 28 | return basic_from_input< traits, Transformers... >( std::forward< Ts >( ts )... ); 29 | } 30 | 31 | } // namespace tao::json::jaxn 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/from_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_FROM_STRING_HPP 5 | #define TAO_JSON_JAXN_FROM_STRING_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_string.hpp" 14 | 15 | namespace tao::json::jaxn 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 18 | [[nodiscard]] basic_value< Traits > basic_from_string( Ts&&... ts ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | events::from_string( consumer, std::forward< Ts >( ts )... ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers, typename... Ts > 26 | [[nodiscard]] value from_string( Ts&&... ts ) 27 | { 28 | return basic_from_string< traits, Transformers... >( std::forward< Ts >( ts )... ); 29 | } 30 | 31 | inline namespace literals 32 | { 33 | [[nodiscard]] inline value operator""_jaxn( const char* data, const std::size_t size ) 34 | { 35 | return jaxn::from_string( data, size, "literal" ); 36 | } 37 | 38 | } // namespace literals 39 | 40 | } // namespace tao::json::jaxn 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/internal/unescape_action.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_INTERNAL_UNESCAPE_ACTION_HPP 5 | #define TAO_JSON_JAXN_INTERNAL_UNESCAPE_ACTION_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "grammar.hpp" 11 | 12 | namespace tao::json::jaxn::internal 13 | { 14 | // clang-format off 15 | template< typename Rule > struct unescape_action : pegtl::nothing< Rule > {}; 16 | 17 | template<> struct unescape_action< rules::escaped_char > : pegtl::unescape::unescape_c< rules::escaped_char, '"', '\'', '\\', '/', '\b', '\f', '\n', '\r', '\t', '\v', '\0' > {}; 18 | template<> struct unescape_action< rules::escaped_unicode > : pegtl::unescape::unescape_j {}; 19 | template<> struct unescape_action< rules::escaped_unicode_code_point_content > : pegtl::unescape::unescape_u {}; 20 | 21 | template<> struct unescape_action< rules::identifier > : pegtl::unescape::append_all {}; // For parts_parser. 22 | template< char D > struct unescape_action< rules::unescaped< D > > : pegtl::unescape::append_all {}; 23 | template< char D > struct unescape_action< rules::mchars< D > > : pegtl::unescape::append_all {}; 24 | // clang-format on 25 | 26 | } // namespace tao::json::jaxn::internal 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/is_identifier.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_IS_IDENTIFIER_HPP 5 | #define TAO_JSON_JAXN_IS_IDENTIFIER_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | namespace tao::json::jaxn 12 | { 13 | [[nodiscard]] inline bool is_identifier( const std::string_view v ) noexcept 14 | { 15 | if( v.empty() || ( std::isdigit( v[ 0 ] ) != 0 ) ) { 16 | return false; 17 | } 18 | return std::all_of( v.begin(), v.end(), []( const char c ) { return ( std::isalnum( c ) != 0 ) || ( c == '_' ); } ); 19 | } 20 | 21 | } // namespace tao::json::jaxn 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/to_stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_TO_STREAM_HPP 5 | #define TAO_JSON_JAXN_TO_STREAM_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../value.hpp" 11 | 12 | #include "../events/from_value.hpp" 13 | #include "../events/transformer.hpp" 14 | 15 | #include "events/to_pretty_stream.hpp" 16 | #include "events/to_stream.hpp" 17 | 18 | namespace tao::json::jaxn 19 | { 20 | template< template< typename... > class... Transformers, template< typename... > class Traits > 21 | void to_stream( std::ostream& os, const basic_value< Traits >& v ) 22 | { 23 | json::events::transformer< events::to_stream, Transformers... > consumer( os ); 24 | json::events::from_value( consumer, v ); 25 | } 26 | 27 | template< template< typename... > class... Transformers, template< typename... > class Traits > 28 | void to_stream( std::ostream& os, const basic_value< Traits >& v, const std::size_t indent ) 29 | { 30 | json::events::transformer< events::to_pretty_stream, Transformers... > consumer( os, indent ); 31 | json::events::from_value( consumer, v ); 32 | } 33 | 34 | } // namespace tao::json::jaxn 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/tao/json/jaxn/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_JAXN_TO_STRING_HPP 5 | #define TAO_JSON_JAXN_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../value.hpp" 11 | 12 | #include "to_stream.hpp" 13 | 14 | namespace tao::json::jaxn 15 | { 16 | template< template< typename... > class... Transformers, template< typename... > class Traits > 17 | [[nodiscard]] std::string to_string( const basic_value< Traits >& v ) 18 | { 19 | std::ostringstream oss; 20 | jaxn::to_stream< Transformers... >( oss, v ); 21 | return std::move( oss ).str(); 22 | } 23 | 24 | template< template< typename... > class... Transformers, template< typename... > class Traits > 25 | [[nodiscard]] std::string to_string( const basic_value< Traits >& v, const unsigned indent ) 26 | { 27 | std::ostringstream oss; 28 | jaxn::to_stream< Transformers... >( oss, v, indent ); 29 | return std::move( oss ).str(); 30 | } 31 | 32 | } // namespace tao::json::jaxn 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/tao/json/message_extension.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MESSAGE_EXTENSION_HPP 5 | #define TAO_JSON_MESSAGE_EXTENSION_HPP 6 | 7 | #include 8 | #include 9 | 10 | namespace tao::json 11 | { 12 | template< typename T, typename = void > 13 | struct message_extension 14 | { 15 | const T& m_v; 16 | 17 | explicit message_extension( const T& v ) noexcept 18 | : m_v( v ) 19 | {} 20 | 21 | friend std::ostream& operator<<( std::ostream& os, const message_extension& /*unused*/ ) noexcept 22 | { 23 | return os; 24 | } 25 | }; 26 | 27 | template< typename T > 28 | struct message_extension< T, decltype( (void)std::declval< const T& >().public_base().append_message_extension( std::declval< std::ostream& >() ) ) > 29 | { 30 | const T& m_v; 31 | 32 | explicit message_extension( const T& v ) noexcept 33 | : m_v( v ) 34 | {} 35 | 36 | friend std::ostream& operator<<( std::ostream& os, const message_extension& v ) 37 | { 38 | os << ' '; 39 | v.m_v.public_base().append_message_extension( os ); 40 | return os; 41 | } 42 | }; 43 | 44 | template< typename T > 45 | message_extension( const T& ) -> message_extension< T >; 46 | 47 | } // namespace tao::json 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/tao/json/msgpack.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_HPP 5 | #define TAO_JSON_MSGPACK_HPP 6 | 7 | #include "../json.hpp" 8 | 9 | #include "msgpack/consume_file.hpp" 10 | #include "msgpack/consume_string.hpp" 11 | #include "msgpack/from_binary.hpp" 12 | #include "msgpack/from_file.hpp" 13 | #include "msgpack/from_input.hpp" 14 | #include "msgpack/from_string.hpp" 15 | #include "msgpack/parts_parser.hpp" 16 | #include "msgpack/to_stream.hpp" 17 | #include "msgpack/to_string.hpp" 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/consume_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_CONSUME_FILE_HPP 5 | #define TAO_JSON_MSGPACK_CONSUME_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../consume.hpp" 12 | #include "../forward.hpp" 13 | 14 | #include "parts_parser.hpp" 15 | 16 | namespace tao::json::msgpack 17 | { 18 | template< typename T, template< typename... > class Traits = traits > 19 | [[nodiscard]] T consume_file( const std::filesystem::path& path ) 20 | { 21 | msgpack::basic_parts_parser< utf8_mode::check, pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 22 | return json::consume< T, Traits >( pp ); 23 | } 24 | 25 | template< template< typename... > class Traits = traits, typename T > 26 | void consume_file( const std::filesystem::path& path, T& t ) 27 | { 28 | msgpack::basic_parts_parser< utf8_mode::check, pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 29 | json::consume< Traits >( pp, t ); 30 | } 31 | 32 | } // namespace tao::json::msgpack 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/consume_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_CONSUME_STRING_HPP 5 | #define TAO_JSON_MSGPACK_CONSUME_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "parts_parser.hpp" 13 | 14 | namespace tao::json::msgpack 15 | { 16 | template< typename T, template< typename... > class Traits = traits, typename F > 17 | [[nodiscard]] T consume_string( F&& string ) 18 | { 19 | msgpack::basic_parts_parser< utf8_mode::check, pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 20 | return json::consume< T, Traits >( pp ); 21 | } 22 | 23 | template< template< typename... > class Traits = traits, typename F, typename T > 24 | void consume_string( F&& string, T& t ) 25 | { 26 | msgpack::basic_parts_parser< utf8_mode::check, pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 27 | json::consume< Traits >( pp, t ); 28 | } 29 | 30 | } // namespace tao::json::msgpack 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/events/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_EVENTS_FROM_FILE_HPP 5 | #define TAO_JSON_MSGPACK_EVENTS_FROM_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "../internal/grammar.hpp" 13 | 14 | namespace tao::json::msgpack::events 15 | { 16 | // Events producer to parse a file containing a MSGPACK string representation. 17 | 18 | template< typename Consumer > 19 | void from_file( Consumer& consumer, const std::filesystem::path& path ) 20 | { 21 | pegtl::file_input< pegtl::tracking_mode::lazy > in( path ); 22 | pegtl::parse< internal::grammar >( in, consumer ); 23 | } 24 | 25 | } // namespace tao::json::msgpack::events 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/events/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_EVENTS_FROM_INPUT_HPP 5 | #define TAO_JSON_MSGPACK_EVENTS_FROM_INPUT_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../internal/grammar.hpp" 12 | 13 | namespace tao::json::msgpack::events 14 | { 15 | // Events producers that parse MSGPACK from a PEGTL input (or something compatible). 16 | 17 | template< typename Consumer, typename Input > 18 | void from_input( Consumer& consumer, Input&& in ) 19 | { 20 | pegtl::parse< msgpack::internal::grammar >( std::forward< Input >( in ), consumer ); 21 | } 22 | 23 | template< typename Consumer, typename Input > 24 | void from_input_embedded( Consumer& consumer, Input&& in ) 25 | { 26 | pegtl::parse< msgpack::internal::embedded >( std::forward< Input >( in ), consumer ); 27 | } 28 | 29 | template< typename Consumer, typename Outer, typename Input > 30 | void from_input_nested( Consumer& consumer, const Outer& oi, Input&& in ) 31 | { 32 | pegtl::parse_nested< msgpack::internal::grammar >( oi, std::forward< Input >( in ), consumer ); 33 | } 34 | 35 | template< typename Consumer, typename Outer, typename Input > 36 | void from_input_embedded_nested( Consumer& consumer, const Outer& oi, Input&& in ) 37 | { 38 | pegtl::parse_nested< msgpack::internal::embedded >( oi, std::forward< Input >( in ), consumer ); 39 | } 40 | 41 | } // namespace tao::json::msgpack::events 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/events/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_EVENTS_TO_STRING_HPP 5 | #define TAO_JSON_MSGPACK_EVENTS_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | 13 | namespace tao::json::msgpack::events 14 | { 15 | struct to_string 16 | : to_stream 17 | { 18 | std::ostringstream oss; 19 | 20 | to_string() 21 | : to_stream( oss ) 22 | {} 23 | 24 | [[nodiscard]] std::string value() const& 25 | { 26 | return oss.str(); 27 | } 28 | 29 | [[nodiscard]] std::string value() && 30 | { 31 | return std::move( oss ).str(); 32 | } 33 | }; 34 | 35 | } // namespace tao::json::msgpack::events 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/from_binary.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_FROM_BINARY_HPP 5 | #define TAO_JSON_MSGPACK_FROM_BINARY_HPP 6 | 7 | #include 8 | 9 | #include "../events/to_value.hpp" 10 | #include "../events/transformer.hpp" 11 | 12 | #include "events/from_binary.hpp" 13 | 14 | namespace tao::json::msgpack 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_binary( Ts&&... ts ) 18 | { 19 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_binary( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_binary( Ts&&... ts ) 26 | { 27 | return basic_from_binary< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json::msgpack 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_FROM_FILE_HPP 5 | #define TAO_JSON_MSGPACK_FROM_FILE_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_file.hpp" 14 | 15 | namespace tao::json::msgpack 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers > 18 | [[nodiscard]] basic_value< Traits > basic_from_file( const std::filesystem::path& path ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | events::from_file( consumer, path ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers > 26 | [[nodiscard]] value from_file( const std::filesystem::path& path ) 27 | { 28 | return basic_from_file< traits, Transformers... >( path ); 29 | } 30 | 31 | } // namespace tao::json::msgpack 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_FROM_INPUT_HPP 5 | #define TAO_JSON_MSGPACK_FROM_INPUT_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_input.hpp" 14 | 15 | namespace tao::json::msgpack 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 18 | [[nodiscard]] basic_value< Traits > basic_from_input( Ts&&... ts ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | msgpack::events::from_input( consumer, std::forward< Ts >( ts )... ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers, typename... Ts > 26 | [[nodiscard]] value from_input( Ts&&... ts ) 27 | { 28 | return basic_from_input< traits, Transformers... >( std::forward< Ts >( ts )... ); 29 | } 30 | 31 | } // namespace tao::json::msgpack 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/from_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_FROM_STRING_HPP 5 | #define TAO_JSON_MSGPACK_FROM_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../events/to_value.hpp" 10 | #include "../events/transformer.hpp" 11 | 12 | #include "events/from_string.hpp" 13 | 14 | namespace tao::json::msgpack 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_string( Ts&&... ts ) 18 | { 19 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_string( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_string( Ts&&... ts ) 26 | { 27 | return basic_from_string< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json::msgpack 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/internal/format.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_INTERNAL_FORMAT_HPP 5 | #define TAO_JSON_MSGPACK_INTERNAL_FORMAT_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::msgpack::internal 10 | { 11 | enum class format : std::uint8_t 12 | { 13 | POSITIVE_MAX = 0x7f, 14 | FIXMAP_MIN = 0x80, 15 | FIXMAP_MAX = 0x8f, 16 | FIXARRAY_MIN = 0x90, 17 | FIXARRAY_MAX = 0x9f, 18 | FIXSTR_MIN = 0xa0, 19 | FIXSTR_MAX = 0xbf, 20 | NIL = 0xc0, 21 | UNUSED = 0xc1, 22 | BOOL_FALSE = 0xc2, 23 | BOOL_TRUE = 0xc3, 24 | BIN8 = 0xc4, 25 | BIN16 = 0xc5, 26 | BIN32 = 0xc6, 27 | EXT8 = 0xc7, 28 | EXT16 = 0xc8, 29 | EXT32 = 0xc9, 30 | FLOAT32 = 0xca, 31 | FLOAT64 = 0xcb, 32 | UINT8 = 0xcc, 33 | UINT16 = 0xcd, 34 | UINT32 = 0xce, 35 | UINT64 = 0xcf, 36 | INT8 = 0xd0, 37 | INT16 = 0xd1, 38 | INT32 = 0xd2, 39 | INT64 = 0xd3, 40 | FIXEXT1 = 0xd4, 41 | FIXEXT2 = 0xd5, 42 | FIXEXT4 = 0xd6, 43 | FIXEXT8 = 0xd7, 44 | FIXEXT16 = 0xd8, 45 | STR8 = 0xd9, 46 | STR16 = 0xda, 47 | STR32 = 0xdb, 48 | ARRAY16 = 0xdc, 49 | ARRAY32 = 0xdd, 50 | MAP16 = 0xde, 51 | MAP32 = 0xdf, 52 | NEGATIVE_MIN = 0xe0 53 | }; 54 | 55 | } // namespace tao::json::msgpack::internal 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/to_stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_TO_STREAM_HPP 5 | #define TAO_JSON_MSGPACK_TO_STREAM_HPP 6 | 7 | #include 8 | 9 | #include "../value.hpp" 10 | 11 | #include "../events/from_value.hpp" 12 | #include "../events/transformer.hpp" 13 | 14 | #include "events/to_stream.hpp" 15 | 16 | namespace tao::json::msgpack 17 | { 18 | template< template< typename... > class... Transformers, template< typename... > class Traits > 19 | void to_stream( std::ostream& os, const basic_value< Traits >& v ) 20 | { 21 | json::events::transformer< events::to_stream, Transformers... > consumer( os ); 22 | json::events::from_value( consumer, v ); 23 | } 24 | 25 | } // namespace tao::json::msgpack 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/msgpack/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_MSGPACK_TO_STRING_HPP 5 | #define TAO_JSON_MSGPACK_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../value.hpp" 11 | 12 | #include "../events/from_value.hpp" 13 | #include "../events/transformer.hpp" 14 | 15 | #include "events/to_string.hpp" 16 | 17 | namespace tao::json::msgpack 18 | { 19 | template< template< typename... > class... Transformers, template< typename... > class Traits > 20 | [[nodiscard]] std::string to_string( const basic_value< Traits >& v ) 21 | { 22 | json::events::transformer< events::to_string, Transformers... > consumer; 23 | json::events::from_value( consumer, v ); 24 | return std::move( consumer ).value(); 25 | } 26 | 27 | } // namespace tao::json::msgpack 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /include/tao/json/stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_STREAM_HPP 5 | #define TAO_JSON_STREAM_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | #include "value.hpp" 13 | 14 | #include "internal/format.hpp" 15 | 16 | namespace tao::json 17 | { 18 | // Use ostream << std::setw( n ) for pretty-printing with indent n. 19 | 20 | template< template< typename... > class Traits > 21 | std::ostream& operator<<( std::ostream& o, const basic_value< Traits >& v ) 22 | { 23 | const auto w = o.width( 0 ); 24 | if( w > 0 ) { 25 | if( w >= 256 ) { 26 | throw std::runtime_error( internal::format( "indentation ", w, " larger than 255" ) ); 27 | } 28 | json::to_stream( o, v, static_cast< std::size_t >( w ) ); 29 | } 30 | else { 31 | json::to_stream( o, v ); 32 | } 33 | return o; 34 | } 35 | 36 | } // namespace tao::json 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /include/tao/json/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_TO_STRING_HPP 5 | #define TAO_JSON_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | 13 | namespace tao::json 14 | { 15 | template< template< typename... > class... Transformers, typename... Ts > 16 | [[nodiscard]] std::string to_string( Ts&&... ts ) 17 | { 18 | std::ostringstream oss; 19 | json::to_stream< Transformers... >( oss, std::forward< Ts >( ts )... ); 20 | return std::move( oss ).str(); 21 | } 22 | 23 | } // namespace tao::json 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /include/tao/json/ubjson.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_HPP 5 | #define TAO_JSON_UBJSON_HPP 6 | 7 | #include "../json.hpp" 8 | 9 | #include "ubjson/consume_file.hpp" 10 | #include "ubjson/consume_string.hpp" 11 | #include "ubjson/from_binary.hpp" 12 | #include "ubjson/from_file.hpp" 13 | #include "ubjson/from_input.hpp" 14 | #include "ubjson/from_string.hpp" 15 | #include "ubjson/parts_parser.hpp" 16 | #include "ubjson/to_stream.hpp" 17 | #include "ubjson/to_string.hpp" 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/consume_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_CONSUME_FILE_HPP 5 | #define TAO_JSON_UBJSON_CONSUME_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../consume.hpp" 12 | #include "../forward.hpp" 13 | 14 | #include "parts_parser.hpp" 15 | 16 | namespace tao::json::ubjson 17 | { 18 | template< typename T, template< typename... > class Traits = traits > 19 | [[nodiscard]] T consume_file( const std::filesystem::path& path ) 20 | { 21 | ubjson::basic_parts_parser< 1 << 24, utf8_mode::check, pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 22 | return json::consume< T, Traits >( pp ); 23 | } 24 | 25 | template< template< typename... > class Traits = traits, typename T > 26 | void consume_file( const std::filesystem::path& path, T& t ) 27 | { 28 | ubjson::basic_parts_parser< 1 << 24, utf8_mode::check, pegtl::file_input< pegtl::tracking_mode::lazy > > pp( path ); 29 | json::consume< Traits >( pp, t ); 30 | } 31 | 32 | } // namespace tao::json::ubjson 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/consume_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_CONSUME_STRING_HPP 5 | #define TAO_JSON_UBJSON_CONSUME_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../consume.hpp" 10 | #include "../forward.hpp" 11 | 12 | #include "parts_parser.hpp" 13 | 14 | namespace tao::json::ubjson 15 | { 16 | template< typename T, template< typename... > class Traits = traits, typename F > 17 | [[nodiscard]] T consume_string( F&& string ) 18 | { 19 | ubjson::basic_parts_parser< 1 << 24, utf8_mode::check, pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 20 | return json::consume< T, Traits >( pp ); 21 | } 22 | 23 | template< template< typename... > class Traits = traits, typename F, typename T > 24 | void consume_string( F&& string, T& t ) 25 | { 26 | ubjson::basic_parts_parser< 1 << 24, utf8_mode::check, pegtl::memory_input< pegtl::tracking_mode::lazy, pegtl::eol::lf_crlf, const char* > > pp( string, __FUNCTION__ ); 27 | json::consume< Traits >( pp, t ); 28 | } 29 | 30 | } // namespace tao::json::ubjson 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/events/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_EVENTS_FROM_FILE_HPP 5 | #define TAO_JSON_UBJSON_EVENTS_FROM_FILE_HPP 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "../internal/grammar.hpp" 13 | 14 | namespace tao::json::ubjson::events 15 | { 16 | // Events producer to parse a file containing a UBJSON string representation. 17 | 18 | template< typename Consumer > 19 | void from_file( Consumer& consumer, const std::filesystem::path& path ) 20 | { 21 | pegtl::file_input< pegtl::tracking_mode::lazy > in( path ); 22 | pegtl::parse< internal::grammar >( in, consumer ); 23 | } 24 | 25 | } // namespace tao::json::ubjson::events 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/events/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_EVENTS_FROM_INPUT_HPP 5 | #define TAO_JSON_UBJSON_EVENTS_FROM_INPUT_HPP 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "../internal/grammar.hpp" 12 | 13 | namespace tao::json::ubjson::events 14 | { 15 | // Events producers that parse UBJSON from a PEGTL input (or something compatible). 16 | 17 | template< typename Consumer, typename Input > 18 | void from_input( Consumer& consumer, Input&& in ) 19 | { 20 | pegtl::parse< ubjson::internal::grammar >( std::forward< Input >( in ), consumer ); 21 | } 22 | 23 | template< typename Consumer, typename Input > 24 | void from_input_embedded( Consumer& consumer, Input&& in ) 25 | { 26 | pegtl::parse< ubjson::internal::embedded >( std::forward< Input >( in ), consumer ); 27 | } 28 | 29 | template< typename Consumer, typename Outer, typename Input > 30 | void from_input_nested( Consumer& consumer, const Outer& oi, Input&& in ) 31 | { 32 | pegtl::parse_nested< ubjson::internal::grammar >( oi, std::forward< Input >( in ), consumer ); 33 | } 34 | 35 | template< typename Consumer, typename Outer, typename Input > 36 | void from_input_embedded_nested( Consumer& consumer, const Outer& oi, Input&& in ) 37 | { 38 | pegtl::parse_nested< ubjson::internal::embedded >( oi, std::forward< Input >( in ), consumer ); 39 | } 40 | 41 | } // namespace tao::json::ubjson::events 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/events/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_EVENTS_TO_STRING_HPP 5 | #define TAO_JSON_UBJSON_EVENTS_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "to_stream.hpp" 12 | 13 | namespace tao::json::ubjson::events 14 | { 15 | struct to_string 16 | : to_stream 17 | { 18 | std::ostringstream oss; 19 | 20 | to_string() 21 | : to_stream( oss ) 22 | {} 23 | 24 | [[nodiscard]] std::string value() const& 25 | { 26 | return oss.str(); 27 | } 28 | 29 | [[nodiscard]] std::string value() && 30 | { 31 | return std::move( oss ).str(); 32 | } 33 | }; 34 | 35 | } // namespace tao::json::ubjson::events 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/from_binary.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_FROM_BINARY_HPP 5 | #define TAO_JSON_UBJSON_FROM_BINARY_HPP 6 | 7 | #include 8 | 9 | #include "../events/to_value.hpp" 10 | #include "../events/transformer.hpp" 11 | 12 | #include "events/from_binary.hpp" 13 | 14 | namespace tao::json::ubjson 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_binary( Ts&&... ts ) 18 | { 19 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_binary( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_binary( Ts&&... ts ) 26 | { 27 | return basic_from_binary< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json::ubjson 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/from_file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_FROM_FILE_HPP 5 | #define TAO_JSON_UBJSON_FROM_FILE_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_file.hpp" 14 | 15 | namespace tao::json::ubjson 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers > 18 | [[nodiscard]] basic_value< Traits > basic_from_file( const std::filesystem::path& path ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | events::from_file( consumer, path ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers > 26 | [[nodiscard]] value from_file( const std::filesystem::path& path ) 27 | { 28 | return basic_from_file< traits, Transformers... >( path ); 29 | } 30 | 31 | } // namespace tao::json::ubjson 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/from_input.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_FROM_INPUT_HPP 5 | #define TAO_JSON_UBJSON_FROM_INPUT_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../events/to_value.hpp" 11 | #include "../events/transformer.hpp" 12 | 13 | #include "events/from_input.hpp" 14 | 15 | namespace tao::json::ubjson 16 | { 17 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 18 | [[nodiscard]] basic_value< Traits > basic_from_input( Ts&&... ts ) 19 | { 20 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 21 | ubjson::events::from_input( consumer, std::forward< Ts >( ts )... ); 22 | return std::move( consumer.value ); 23 | } 24 | 25 | template< template< typename... > class... Transformers, typename... Ts > 26 | [[nodiscard]] value from_input( Ts&&... ts ) 27 | { 28 | return basic_from_input< traits, Transformers... >( std::forward< Ts >( ts )... ); 29 | } 30 | 31 | } // namespace tao::json::ubjson 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/from_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_FROM_STRING_HPP 5 | #define TAO_JSON_UBJSON_FROM_STRING_HPP 6 | 7 | #include 8 | 9 | #include "../events/to_value.hpp" 10 | #include "../events/transformer.hpp" 11 | 12 | #include "events/from_string.hpp" 13 | 14 | namespace tao::json::ubjson 15 | { 16 | template< template< typename... > class Traits, template< typename... > class... Transformers, typename... Ts > 17 | [[nodiscard]] basic_value< Traits > basic_from_string( Ts&&... ts ) 18 | { 19 | json::events::transformer< json::events::to_basic_value< Traits >, Transformers... > consumer; 20 | events::from_string( consumer, std::forward< Ts >( ts )... ); 21 | return std::move( consumer.value ); 22 | } 23 | 24 | template< template< typename... > class... Transformers, typename... Ts > 25 | [[nodiscard]] value from_string( Ts&&... ts ) 26 | { 27 | return basic_from_string< traits, Transformers... >( std::forward< Ts >( ts )... ); 28 | } 29 | 30 | } // namespace tao::json::ubjson 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/internal/marker.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_INTERNAL_MARKER_HPP 5 | #define TAO_JSON_UBJSON_INTERNAL_MARKER_HPP 6 | 7 | #include 8 | 9 | namespace tao::json::ubjson::internal 10 | { 11 | enum class marker : char 12 | { 13 | NULL_ = 'Z', 14 | 15 | NO_OP = 'N', 16 | 17 | TRUE_ = 'T', 18 | FALSE_ = 'F', 19 | 20 | INT8 = 'i', 21 | UINT8 = 'U', 22 | INT16 = 'I', 23 | INT32 = 'l', 24 | INT64 = 'L', 25 | 26 | FLOAT32 = 'd', 27 | FLOAT64 = 'D', 28 | 29 | HIGH_PRECISION = 'H', 30 | 31 | CHAR = 'C', 32 | STRING = 'S', 33 | 34 | BEGIN_ARRAY = '[', 35 | END_ARRAY = ']', 36 | 37 | BEGIN_OBJECT = '{', 38 | END_OBJECT = '}', 39 | 40 | CONTAINER_SIZE = '#', 41 | CONTAINER_TYPE = '$' 42 | }; 43 | 44 | } // namespace tao::json::ubjson::internal 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/to_stream.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_TO_STREAM_HPP 5 | #define TAO_JSON_UBJSON_TO_STREAM_HPP 6 | 7 | #include 8 | 9 | #include "../value.hpp" 10 | 11 | #include "../events/from_value.hpp" 12 | #include "../events/non_finite_to_null.hpp" 13 | #include "../events/transformer.hpp" 14 | 15 | #include "events/to_stream.hpp" 16 | 17 | namespace tao::json::ubjson 18 | { 19 | template< template< typename... > class... Transformers, template< typename... > class Traits > 20 | void to_stream( std::ostream& os, const basic_value< Traits >& v ) 21 | { 22 | json::events::transformer< events::to_stream, Transformers..., json::events::non_finite_to_null > consumer( os ); 23 | json::events::from_value( consumer, v ); 24 | } 25 | 26 | } // namespace tao::json::ubjson 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/tao/json/ubjson/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_UBJSON_TO_STRING_HPP 5 | #define TAO_JSON_UBJSON_TO_STRING_HPP 6 | 7 | #include 8 | #include 9 | 10 | #include "../value.hpp" 11 | 12 | #include "../events/from_value.hpp" 13 | #include "../events/non_finite_to_null.hpp" 14 | #include "../events/transformer.hpp" 15 | 16 | #include "events/to_string.hpp" 17 | 18 | namespace tao::json::ubjson 19 | { 20 | template< template< typename... > class... Transformers, template< typename... > class Traits > 21 | [[nodiscard]] std::string to_string( const basic_value< Traits >& v ) 22 | { 23 | json::events::transformer< events::to_string, Transformers..., json::events::non_finite_to_null > consumer; 24 | json::events::from_value( consumer, v ); 25 | return std::move( consumer ).value(); 26 | } 27 | 28 | } // namespace tao::json::ubjson 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /include/tao/json/value.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_VALUE_HPP 5 | #define TAO_JSON_VALUE_HPP 6 | 7 | #include "basic_value.hpp" 8 | #include "forward.hpp" 9 | #include "operators.hpp" 10 | #include "traits.hpp" 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/example/json/cbor_to_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.cbor" << std::endl; 12 | std::cerr << " parses the cbor file and writes it to stdout as jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_stream consumer( std::cout ); 16 | tao::json::cbor::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/cbor_to_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.cbor" << std::endl; 12 | std::cerr << " parses the cbor file and writes it to stdout as json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_stream consumer( std::cout ); 16 | tao::json::cbor::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/cbor_to_msgpack.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.cbor" << std::endl; 12 | std::cerr << " parses the cbor file and writes it to stdout as msgpack" << std::endl; 13 | return 1; 14 | } 15 | tao::json::msgpack::events::to_stream consumer( std::cout ); 16 | tao::json::cbor::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/cbor_to_pretty_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.cbor" << std::endl; 12 | std::cerr << " parses the cbor file and writes it to stdout as pretty jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::cbor::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/cbor_to_pretty_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.cbor" << std::endl; 12 | std::cerr << " parses the cbor file and writes it to stdout as pretty json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::cbor::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/cbor_to_ubjson.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.cbor" << std::endl; 12 | std::cerr << " parses the cbor file and writes it to stdout as ubjson" << std::endl; 13 | return 1; 14 | } 15 | tao::json::ubjson::events::to_stream consumer( std::cout ); 16 | tao::json::cbor::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/jaxn_to_cbor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.jaxn" << std::endl; 12 | std::cerr << " parses the jaxn file and writes it to stdout as cbor" << std::endl; 13 | return 1; 14 | } 15 | tao::json::cbor::events::to_stream consumer( std::cout ); 16 | tao::json::jaxn::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/jaxn_to_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.jaxn" << std::endl; 12 | std::cerr << " parses the jaxn file and writes it to stdout as jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_stream consumer( std::cout ); 16 | tao::json::jaxn::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/jaxn_to_msgpack.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.jaxn" << std::endl; 12 | std::cerr << " parses the jaxn file and writes it to stdout as msgpack" << std::endl; 13 | return 1; 14 | } 15 | tao::json::msgpack::events::to_stream consumer( std::cout ); 16 | tao::json::jaxn::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/jaxn_to_pretty_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.jaxn" << std::endl; 12 | std::cerr << " parses the jaxn file and writes it to stdout as pretty jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::jaxn::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/jaxn_to_ubjson.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.jaxn" << std::endl; 12 | std::cerr << " parses the jaxn file and writes it to stdout as ubjson" << std::endl; 13 | return 1; 14 | } 15 | tao::json::ubjson::events::to_stream consumer( std::cout ); 16 | tao::json::jaxn::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/json_to_cbor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.json" << std::endl; 12 | std::cerr << " parses the json file and writes it to stdout as cbor" << std::endl; 13 | return 1; 14 | } 15 | tao::json::cbor::events::to_stream consumer( std::cout ); 16 | tao::json::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/json_to_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.json" << std::endl; 12 | std::cerr << " parses the json file and writes it to stdout as json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_stream consumer( std::cout ); 16 | tao::json::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/json_to_msgpack.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.json" << std::endl; 12 | std::cerr << " parses the json file and writes it to stdout as msgpack" << std::endl; 13 | return 1; 14 | } 15 | tao::json::msgpack::events::to_stream consumer( std::cout ); 16 | tao::json::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/json_to_pretty_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.json" << std::endl; 12 | std::cerr << " parses the json file and writes it to stdout as pretty json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/json_to_ubjson.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.json" << std::endl; 12 | std::cerr << " parses the json file and writes it to stdout as ubjson" << std::endl; 13 | return 1; 14 | } 15 | tao::json::ubjson::events::to_stream consumer( std::cout ); 16 | tao::json::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/msgpack_to_cbor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.msgpack" << std::endl; 12 | std::cerr << " parses the msgpack file and writes it to stdout as cbor" << std::endl; 13 | return 1; 14 | } 15 | tao::json::cbor::events::to_stream consumer( std::cout ); 16 | tao::json::msgpack::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/msgpack_to_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.msgpack" << std::endl; 12 | std::cerr << " parses the msgpack file and writes it to stdout as jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_stream consumer( std::cout ); 16 | tao::json::msgpack::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/msgpack_to_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.msgpack" << std::endl; 12 | std::cerr << " parses the msgpack file and writes it to stdout as json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_stream consumer( std::cout ); 16 | tao::json::msgpack::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/msgpack_to_pretty_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.msgpack" << std::endl; 12 | std::cerr << " parses the msgpack file and writes it to stdout as pretty jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::msgpack::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/msgpack_to_pretty_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.msgpack" << std::endl; 12 | std::cerr << " parses the msgpack file and writes it to stdout as pretty json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::msgpack::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/msgpack_to_ubjson.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.msgpack" << std::endl; 12 | std::cerr << " parses the msgpack file and writes it to stdout as ubjson" << std::endl; 13 | return 1; 14 | } 15 | tao::json::ubjson::events::to_stream consumer( std::cout ); 16 | tao::json::msgpack::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/ubjson_to_cbor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.ubjson" << std::endl; 12 | std::cerr << " parses the ubjson file and writes it to stdout as cbor" << std::endl; 13 | return 1; 14 | } 15 | tao::json::cbor::events::to_stream consumer( std::cout ); 16 | tao::json::ubjson::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/ubjson_to_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.ubjson" << std::endl; 12 | std::cerr << " parses the ubjson file and writes it to stdout as jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_stream consumer( std::cout ); 16 | tao::json::ubjson::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/ubjson_to_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.ubjson" << std::endl; 12 | std::cerr << " parses the ubjson file and writes it to stdout as json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_stream consumer( std::cout ); 16 | tao::json::ubjson::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/ubjson_to_msgpack.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.ubjson" << std::endl; 12 | std::cerr << " parses the ubjson file and writes it to stdout as msgpack" << std::endl; 13 | return 1; 14 | } 15 | tao::json::msgpack::events::to_stream consumer( std::cout ); 16 | tao::json::ubjson::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/ubjson_to_pretty_jaxn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.ubjson" << std::endl; 12 | std::cerr << " parses the ubjson file and writes it to stdout as pretty jaxn" << std::endl; 13 | return 1; 14 | } 15 | tao::json::jaxn::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::ubjson::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/ubjson_to_pretty_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2 ) { 11 | std::cerr << "usage: " << argv[ 0 ] << " file.ubjson" << std::endl; 12 | std::cerr << " parses the ubjson file and writes it to stdout as pretty json" << std::endl; 13 | return 1; 14 | } 15 | tao::json::events::to_pretty_stream consumer( std::cout, 3 ); 16 | tao::json::ubjson::events::from_file( consumer, argv[ 1 ] ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/example/json/validate_event_order.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape) 17 | { 18 | for( int i = 1; i < argc; ++i ) { 19 | tao::json::events::validate_event_order c1; 20 | tao::json::events::from_file( c1, argv[ i ] ); 21 | tao::json::events::to_value c2; 22 | tao::json::events::from_file( c2, argv[ i ] ); 23 | tao::json::events::validate_event_order c3; 24 | tao::json::events::from_value( c3, c2.value ); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/perf/json/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.8...3.19) 2 | 3 | set(perfsources 4 | benchmark.cpp 5 | parse_file.cpp 6 | pretty_print_file.cpp 7 | print_double.cpp 8 | print_file.cpp 9 | sizes.cpp 10 | syntax_only.cpp 11 | ) 12 | 13 | # file(GLOB ...) is used to validate the above list of perf_sources 14 | file(GLOB glob_perf_sources RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.cpp) 15 | 16 | foreach(perfsourcefile ${perfsources}) 17 | if(${perfsourcefile} IN_LIST glob_perf_sources) 18 | list(REMOVE_ITEM glob_perf_sources ${perfsourcefile}) 19 | else() 20 | message(SEND_ERROR "File ${perfsourcefile} is missing from src/perf/json") 21 | endif() 22 | get_filename_component(exename ${perfsourcefile} NAME_WE) 23 | set(exename "tao-json-perf-${exename}") 24 | add_executable(${exename} ${perfsourcefile}) 25 | target_link_libraries(${exename} PRIVATE taocpp::json) 26 | set_target_properties(${exename} PROPERTIES 27 | CXX_STANDARD 11 28 | CXX_STANDARD_REQUIRED ON 29 | CXX_EXTENSIONS OFF 30 | ) 31 | if(MSVC) 32 | target_compile_options(${exename} PRIVATE /W4 /WX /utf-8) 33 | else() 34 | target_compile_options(${exename} PRIVATE -pedantic -Wall -Wextra -Wshadow -Werror) 35 | endif() 36 | endforeach() 37 | 38 | if(glob_perf_sources) 39 | foreach(ignored_source_file ${glob_perf_sources}) 40 | message(SEND_ERROR "File ${ignored_source_file} in src/perf/json is ignored") 41 | endforeach() 42 | endif() 43 | -------------------------------------------------------------------------------- /src/perf/json/benchmark.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include "bench_mark.hpp" 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | for( int i = 1; i < argc; ++i ) { 11 | tao::json::events::to_value consumer; 12 | tao::json::events::from_file( consumer, argv[ i ] ); 13 | tao::json::events::to_value consumer2; 14 | tao::json::events::from_file( consumer2, argv[ i ] ); 15 | 16 | tao::bench::mark( "parse", argv[ i ], [ & ]() { 17 | tao::json::events::to_value tmp; 18 | tao::json::events::from_file( tmp, argv[ i ] ); 19 | } ); 20 | 21 | tao::bench::mark( "compare", argv[ i ], [ & ]() { 22 | (void)( consumer.value == consumer2.value ); 23 | } ); 24 | 25 | tao::bench::mark( "stringify", argv[ i ], [ & ]() { 26 | (void)tao::json::to_string( consumer.value ); 27 | } ); 28 | 29 | tao::bench::mark( "prettify", argv[ i ], [ & ]() { 30 | (void)tao::json::to_string( consumer.value, 4 ); 31 | } ); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /src/perf/json/parse_file.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include "bench_mark.hpp" 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | for( int i = 1; i < argc; ++i ) { 11 | tao::bench::mark( "json", argv[ i ], [ & ]() { 12 | tao::json::events::to_value consumer; 13 | tao::json::events::from_file( consumer, argv[ i ] ); 14 | } ); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /src/perf/json/pretty_print_file.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include "bench_mark.hpp" 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | for( int i = 1; i < argc; ++i ) { 11 | tao::json::events::to_value consumer; 12 | tao::json::events::from_file( consumer, argv[ i ] ); 13 | 14 | tao::bench::mark( "json", argv[ i ], [ & ]() { 15 | (void)tao::json::to_string( consumer.value, 4 ); 16 | } ); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /src/perf/json/print_file.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include "bench_mark.hpp" 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | for( int i = 1; i < argc; ++i ) { 11 | tao::json::events::to_value consumer; 12 | tao::json::events::from_file( consumer, argv[ i ] ); 13 | 14 | tao::bench::mark( "json", argv[ i ], [ & ]() { 15 | (void)tao::json::to_string( consumer.value ); 16 | } ); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /src/perf/json/sizes.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | 6 | #include 7 | 8 | #define PRINT_SIZE( ... ) \ 9 | std::cout << #__VA_ARGS__ << " size " << sizeof( __VA_ARGS__ ) << " align " << alignof( __VA_ARGS__ ) << std::endl; 10 | 11 | int main( int /*unused*/, char** /*unused*/ ) 12 | { 13 | PRINT_SIZE( tao::json::value ); 14 | PRINT_SIZE( std::vector< tao::json::value > ); 15 | PRINT_SIZE( std::string ); 16 | PRINT_SIZE( std::string_view ); 17 | PRINT_SIZE( tao::binary ); 18 | PRINT_SIZE( tao::binary_view ); 19 | PRINT_SIZE( std::vector< tao::json::value > ); 20 | PRINT_SIZE( std::map< std::string, tao::json::value > ); 21 | PRINT_SIZE( tao::json::token ); 22 | PRINT_SIZE( tao::json::pointer ); 23 | PRINT_SIZE( tao::json::internal::opaque_ptr_t ); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/perf/json/syntax_only.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include 5 | #include 6 | 7 | #include "bench_mark.hpp" 8 | 9 | namespace pegtl = tao::pegtl; 10 | 11 | // clang-format off 12 | 13 | int main( int argc, char** argv ) 14 | { 15 | for( int i = 1; i < argc; ++i ) { 16 | const auto r = tao::bench::mark( "pegtl", argv[ i ], [&]() { 17 | pegtl::file_input< pegtl::tracking_mode::lazy > in( argv[ i ] ); 18 | pegtl::parse< pegtl::must< pegtl::json::text, pegtl::eof > >( in ); 19 | } ); 20 | tao::bench::mark( "json", argv[ i ], [&]() { 21 | tao::json::events::discard consumer; 22 | tao::json::events::from_file( consumer, argv[ i ] ); 23 | }, 24 | r ); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/test/json/big_list_of_naughty_strings.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | [[nodiscard]] std::string get_file_contents( const char* filename ) 15 | { 16 | std::ifstream in( filename, std::ios::in | std::ios::binary ); 17 | if( !in.fail() ) { 18 | std::string contents; 19 | in.seekg( 0, std::ios::end ); 20 | contents.resize( static_cast< std::string::size_type >( in.tellg() ) ); 21 | in.seekg( 0, std::ios::beg ); 22 | in.read( contents.data(), static_cast< std::streamsize >( contents.size() ) ); 23 | in.close(); 24 | return contents; 25 | } 26 | throw std::runtime_error( "unable to read input file" ); 27 | } 28 | 29 | namespace tao::json 30 | { 31 | void unit_test() 32 | { 33 | const auto v = from_file( "tests/blns.json" ); 34 | TEST_ASSERT( v.get_array().size() == 494 ); 35 | const auto s = to_string( v, 2 ) + '\n'; 36 | #if !defined( _WIN32 ) 37 | TEST_ASSERT( s == get_file_contents( "tests/blns.json" ) ); 38 | #endif 39 | const auto v2 = from_string( s ); 40 | TEST_ASSERT( v2 == v ); 41 | } 42 | 43 | } // namespace tao::json 44 | 45 | #include "main.hpp" 46 | -------------------------------------------------------------------------------- /src/test/json/cbor_parts_parser.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | #include "test_unhex.hpp" 6 | 7 | #include 8 | 9 | namespace tao::json 10 | { 11 | void unit_test() 12 | { 13 | { 14 | cbor::parts_parser p( "", __FUNCTION__ ); 15 | TEST_ASSERT( p.empty() ); 16 | } 17 | { 18 | cbor::parts_parser p( test_unhex( "f6" ), __FUNCTION__ ); 19 | TEST_ASSERT( p.null() == true ); 20 | TEST_ASSERT( p.empty() ); 21 | } 22 | { 23 | cbor::parts_parser p( test_unhex( "f4" ), __FUNCTION__ ); 24 | TEST_ASSERT( p.boolean() == false ); 25 | TEST_ASSERT( p.empty() ); 26 | } 27 | { 28 | cbor::parts_parser p( test_unhex( "f5" ), __FUNCTION__ ); 29 | TEST_ASSERT( p.boolean() == true ); 30 | TEST_ASSERT( p.empty() ); 31 | } 32 | } 33 | 34 | } // namespace tao::json 35 | 36 | #include "main.hpp" 37 | -------------------------------------------------------------------------------- /src/test/json/contrib_diff.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | namespace tao::json 12 | { 13 | void unit_test() 14 | { 15 | const value source = { 16 | { "foo", 42 }, 17 | { "blue", 3.14 }, 18 | { "bar", value::array( { 1, 2, 3, 4, 5 } ) }, 19 | { "baz", 2 } 20 | }; 21 | 22 | const value destination = { 23 | { "bar", value::array( { 1, 2, 7 } ) }, 24 | { "foo", 42 }, 25 | { "yellow", empty_object }, 26 | { "baz", "hello" } 27 | }; 28 | 29 | const auto d = diff( source, destination ); 30 | 31 | // clang-format off 32 | TEST_ASSERT( d == value::array( { { { "op", "replace" }, { "path", "/bar/2" }, { "value", 7 } }, 33 | { { "op", "remove" }, { "path", "/bar/4" } }, 34 | { { "op", "remove" }, { "path", "/bar/3" } }, 35 | { { "op", "replace" }, { "path", "/baz" }, { "value", "hello" } }, 36 | { { "op", "remove" }, { "path", "/blue" } }, 37 | { { "op", "add" }, { "path", "/yellow" }, { "value", empty_object } } } ) ); 38 | // clang-format on 39 | } 40 | 41 | } // namespace tao::json 42 | 43 | #include "main.hpp" 44 | -------------------------------------------------------------------------------- /src/test/json/contrib_position.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | 8 | namespace tao::json 9 | { 10 | void test_json() 11 | { 12 | const std::string f = "tests/taocpp/position.json"; 13 | 14 | auto v = from_file_with_position( f ); 15 | 16 | TEST_ASSERT( v.source() == f ); 17 | TEST_ASSERT( v.line() == 1 ); 18 | TEST_ASSERT( v.column() == 1 ); 19 | 20 | TEST_ASSERT( v.at( 0 ).source() == f ); 21 | TEST_ASSERT( v.at( 0 ).line() == 2 ); 22 | TEST_ASSERT( v.at( 0 ).column() == 9 ); 23 | 24 | TEST_ASSERT( v.at( 1 ).source() == f ); 25 | TEST_ASSERT( v.at( 1 ).line() == 3 ); 26 | TEST_ASSERT( v.at( 1 ).column() == 9 ); 27 | 28 | TEST_ASSERT( v.at( 2 ).source() == f ); 29 | TEST_ASSERT( v.at( 2 ).line() == 4 ); 30 | TEST_ASSERT( v.at( 2 ).column() == 9 ); 31 | 32 | TEST_ASSERT( v.at( 3 ).source() == f ); 33 | TEST_ASSERT( v.at( 3 ).line() == 5 ); 34 | TEST_ASSERT( v.at( 3 ).column() == 9 ); 35 | 36 | TEST_ASSERT( v.at( 3 ).at( "hello" ).source() == f ); 37 | TEST_ASSERT( v.at( 3 ).at( "hello" ).line() == 6 ); 38 | TEST_ASSERT( v.at( 3 ).at( "hello" ).column() == 27 ); 39 | } 40 | 41 | void unit_test() 42 | { 43 | test_json(); 44 | } 45 | 46 | } // namespace tao::json 47 | 48 | #include "main.hpp" 49 | -------------------------------------------------------------------------------- /src/test/json/contrib_reference.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace tao::json 11 | { 12 | void test( const std::string& s1, const std::string& s2 ) 13 | { 14 | auto v1 = from_string( s1 ); 15 | const auto v2 = from_string( s2 ); 16 | resolve_references( v1 ); 17 | TEST_ASSERT( v1 == v2 ); 18 | } 19 | 20 | void test_throws( const value& v ) 21 | { 22 | value v2( v ); 23 | TEST_THROWS( resolve_references( v2 ) ); 24 | } 25 | 26 | void unit_test() 27 | { 28 | { 29 | value e; 30 | resolve_references( e ); 31 | } 32 | 33 | test( "{\"foo\":1,\"bar\":2}", "{\"foo\":1,\"bar\":2}" ); 34 | test( "{\"foo\":1,\"bar\":{\"$ref\":\"#/foo\"}}", "{\"foo\":1,\"bar\":1}" ); 35 | test( "{\"foo\":\"#/foo\",\"bar\":{\"$ref\":{\"$ref\":\"#/foo\"}}}", "{\"foo\":\"#/foo\",\"bar\":\"#/foo\"}" ); 36 | 37 | test_throws( { { "$ref", "#" } } ); 38 | test_throws( { { "$ref", "#/foo" } } ); 39 | test_throws( { { "foo", 0 }, { "bar", { { "$ref", "#/foo/bar" } } } } ); 40 | } 41 | 42 | } // namespace tao::json 43 | 44 | #include "main.hpp" 45 | -------------------------------------------------------------------------------- /src/test/json/enable_implicit_constructor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | #include "test_types.hpp" 6 | 7 | #include 8 | 9 | #include 10 | 11 | namespace tao::json 12 | { 13 | template< typename T > 14 | struct my_traits 15 | : public traits< T > 16 | {}; 17 | 18 | template<> 19 | struct my_traits< void > 20 | { 21 | template< typename > 22 | using public_base = internal::empty_base; 23 | 24 | static constexpr const bool enable_implicit_constructor = false; 25 | }; 26 | 27 | [[nodiscard]] auto force( const basic_value< my_traits >& v ) 28 | { 29 | return v; 30 | } 31 | 32 | void unit_test() 33 | { 34 | assert_null( force( null ) ); 35 | assert_string( force( empty_string ), "" ); 36 | assert_binary( force( empty_binary ), binary() ); 37 | assert_array( force( empty_array ), 0 ); 38 | assert_object( force( empty_object ), 0 ); 39 | 40 | basic_value< my_traits > v = null; 41 | assert_null( v ); 42 | v = empty_string; 43 | assert_string( v, "" ); 44 | v = empty_binary; 45 | assert_binary( v, binary() ); 46 | v = empty_array; 47 | assert_array( v, 0 ); 48 | v = empty_object; 49 | assert_object( v, 0 ); 50 | } 51 | 52 | } // namespace tao::json 53 | 54 | #include "main.hpp" 55 | -------------------------------------------------------------------------------- /src/test/json/escape.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | namespace tao::json 10 | { 11 | void roundtrip( const std::string& v ) 12 | { 13 | TEST_ASSERT( to_string( from_string( v ) ) == v ); 14 | } 15 | 16 | void unit_test() 17 | { 18 | roundtrip( "\" \\u0000 \"" ); 19 | roundtrip( "\" \\b \"" ); 20 | roundtrip( "\" \\f \"" ); 21 | roundtrip( "\" \\n \"" ); 22 | roundtrip( "\" \\r \"" ); 23 | roundtrip( "\" \\t \"" ); 24 | roundtrip( "\" \\\\ \"" ); 25 | roundtrip( "\" \\\" \"" ); 26 | roundtrip( "\" \\u007f \"" ); 27 | roundtrip( "\" \\b.\\f \"" ); 28 | roundtrip( "\" \\b\\f\\n\\r\\t\\\\\\\" \"" ); 29 | roundtrip( "\" \\bx\\fxz\\nzx\\ryx\\txy\\\\yz\\\" \"" ); 30 | 31 | const std::string s1 = "\" \\b\\f\\n\\r\\t\\\\\\\" \""; 32 | TEST_ASSERT( from_string( s1 ).get_string() == " \b\f\n\r\t\\\" " ); 33 | TEST_ASSERT( to_string( from_string( s1 ) ) == s1 ); 34 | 35 | const std::string s2 = "\" \\u007f \""; 36 | TEST_ASSERT( from_string( s2 ).get_string() == " \x7f " ); 37 | TEST_ASSERT( to_string( from_string( s2 ) ) == s2 ); 38 | } 39 | 40 | } // namespace tao::json 41 | 42 | #include "main.hpp" 43 | -------------------------------------------------------------------------------- /src/test/json/events_debug.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | namespace tao::json 10 | { 11 | void test( const std::string& v ) 12 | { 13 | events::debug consumer( std::cout ); 14 | events::from_string( consumer, v ); 15 | } 16 | 17 | void unit_test() 18 | { 19 | test( "[null,true,false,-42,43.0,\"foo\",[1,2,3],{\"a\":\"b\",\"c\":\"d\"}]" ); 20 | } 21 | 22 | } // namespace tao::json 23 | 24 | #include "main.hpp" 25 | -------------------------------------------------------------------------------- /src/test/json/events_to_stream.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | namespace tao::json 12 | { 13 | void test( const std::string& v ) 14 | { 15 | std::ostringstream oss; 16 | events::to_stream consumer( oss ); 17 | events::from_string( consumer, v ); 18 | TEST_ASSERT( oss.str() == v ); 19 | } 20 | 21 | void unit_test() 22 | { 23 | test( "[null,true,false,42,43.0,\"foo\",[1,2,3],{\"a\":\"b\",\"c\":\"d\"}]" ); 24 | } 25 | 26 | } // namespace tao::json 27 | 28 | #include "main.hpp" 29 | -------------------------------------------------------------------------------- /src/test/json/events_to_string.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | namespace tao::json 10 | { 11 | void test( const std::string& v ) 12 | { 13 | events::to_string consumer; 14 | events::from_string( consumer, v ); 15 | TEST_ASSERT( consumer.value() == v ); 16 | } 17 | 18 | void unit_test() 19 | { 20 | test( "[null,true,false,42,43.0,\"foo\",[1,2,3],{\"a\":\"b\",\"c\":\"d\"}]" ); 21 | } 22 | 23 | } // namespace tao::json 24 | 25 | #include "main.hpp" 26 | -------------------------------------------------------------------------------- /src/test/json/include_json.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | 8 | namespace tao::json 9 | { 10 | void unit_test() {} 11 | 12 | } // namespace tao::json 13 | 14 | #include "main.hpp" 15 | -------------------------------------------------------------------------------- /src/test/json/key_camel_case_to_snake_case.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | namespace tao::json 12 | { 13 | void test( const value& v, const std::string& s ) 14 | { 15 | TEST_ASSERT( to_string< events::key_camel_case_to_snake_case >( v ) == s ); 16 | } 17 | 18 | void unit_test() 19 | { 20 | test( { { "fooBar", 42 } }, "{\"foo_bar\":42}" ); 21 | test( { { "foo2Bar", 42 } }, "{\"foo2_bar\":42}" ); 22 | test( { { "fooHTTPBar", 42 } }, "{\"foo_http_bar\":42}" ); 23 | test( { { "HTTPBar", 42 } }, "{\"http_bar\":42}" ); 24 | 25 | test( { { "fooBar", "fooBar" } }, "{\"foo_bar\":\"fooBar\"}" ); 26 | 27 | test( { { "foo_", 42 } }, "{\"foo_\":42}" ); 28 | test( { { "foo_bar", 42 } }, "{\"foo_bar\":42}" ); 29 | test( { { "_foo", 42 } }, "{\"_foo\":42}" ); 30 | 31 | test( { { "Foo", 42 } }, "{\"foo\":42}" ); 32 | test( { { "_Foo", 42 } }, "{\"_foo\":42}" ); 33 | test( { { "foo_Bar", 42 } }, "{\"foo_bar\":42}" ); 34 | } 35 | 36 | } // namespace tao::json 37 | 38 | #include "main.hpp" 39 | -------------------------------------------------------------------------------- /src/test/json/key_snake_case_to_camel_case.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | namespace tao::json 12 | { 13 | void test( const value& v, const std::string& s ) 14 | { 15 | TEST_ASSERT( to_string< events::key_snake_case_to_camel_case >( v ) == s ); 16 | } 17 | 18 | void unit_test() 19 | { 20 | test( { { "foo_bar", 42 } }, "{\"fooBar\":42}" ); 21 | 22 | test( { { "foo_bar", "foo_bar" } }, "{\"fooBar\":\"foo_bar\"}" ); 23 | 24 | test( { { "foo_", 42 } }, "{\"foo_\":42}" ); 25 | test( { { "foo__bar", 42 } }, "{\"foo_Bar\":42}" ); 26 | test( { { "_foo", 42 } }, "{\"Foo\":42}" ); 27 | test( { { "_Foo", 42 } }, "{\"_Foo\":42}" ); 28 | test( { { "foo_Bar", 42 } }, "{\"foo_Bar\":42}" ); 29 | } 30 | 31 | } // namespace tao::json 32 | 33 | #include "main.hpp" 34 | -------------------------------------------------------------------------------- /src/test/json/literal.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | namespace tao::json 10 | { 11 | void unit_test() 12 | { 13 | TEST_ASSERT( "42"_json.get_unsigned() == 42 ); 14 | } 15 | 16 | } // namespace tao::json 17 | 18 | #include "main.hpp" 19 | -------------------------------------------------------------------------------- /src/test/json/main.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_INCLUDE_SRC_TEST_MAIN_HPP 5 | #define TAO_JSON_INCLUDE_SRC_TEST_MAIN_HPP 6 | 7 | #include 8 | #include 9 | 10 | int main( int /*unused*/, char** argv ) 11 | { 12 | tao::json::unit_test(); 13 | 14 | if( tao::json::failed != 0 ) { 15 | std::cerr << "json: unit test " << argv[ 0 ] << " failed " << tao::json::failed << std::endl; 16 | } 17 | return ( tao::json::failed == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE; 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /src/test/json/sha256.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | 8 | namespace tao::json 9 | { 10 | static char hex[] = "0123456789ABCDEF"; 11 | 12 | [[nodiscard]] std::string to_hex( const std::string& s ) 13 | { 14 | std::string result; 15 | result.reserve( s.size() * 2 ); 16 | for( unsigned char c : s ) { 17 | result += hex[ c >> 4 ]; 18 | result += hex[ c & 15 ]; 19 | } 20 | return result; 21 | } 22 | 23 | void unit_test() 24 | { 25 | // two test vectors from RFC 6234 should be enough 26 | 27 | internal::sha256 d; 28 | d.feed( "abc" ); 29 | TEST_ASSERT( to_hex( d.get() ) == "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD" ); 30 | 31 | d.reset(); 32 | d.feed( "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" ); 33 | TEST_ASSERT( to_hex( d.get() ) == "248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1" ); 34 | } 35 | 36 | } // namespace tao::json 37 | 38 | #include "main.hpp" 39 | -------------------------------------------------------------------------------- /src/test/json/test_unhex.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #ifndef TAO_JSON_SRC_TEST_JSON_UNHEX_HPP 5 | #define TAO_JSON_SRC_TEST_JSON_UNHEX_HPP 6 | 7 | #include 8 | #include 9 | 10 | namespace tao::json 11 | { 12 | [[nodiscard]] inline char test_unhex( const char c ) 13 | { 14 | if( ( '0' <= c ) && ( c <= '9' ) ) { 15 | return static_cast< char >( c - '0' ); 16 | } 17 | if( ( 'a' <= c ) && ( c <= 'f' ) ) { 18 | return static_cast< char >( c - 'a' + 10 ); 19 | } 20 | if( ( 'A' <= c ) && ( c <= 'F' ) ) { 21 | return static_cast< char >( c - 'A' + 10 ); 22 | } 23 | // LCOV_EXCL_START 24 | assert( false ); 25 | return 0; 26 | // LCOV_EXCL_STOP 27 | } 28 | 29 | [[nodiscard]] inline std::string test_unhex( const std::string& data ) 30 | { 31 | assert( !( data.size() & 1 ) ); 32 | std::string result; 33 | result.reserve( data.size() / 2 ); 34 | for( std::string::size_type i = 0; i < data.size(); i += 2 ) { 35 | result += static_cast< char >( ( test_unhex( data[ i ] ) << 4 ) + test_unhex( data[ i + 1 ] ) ); 36 | } 37 | return result; 38 | } 39 | 40 | } // namespace tao::json 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/test/json/type.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | 8 | namespace tao::json 9 | { 10 | void unit_test() 11 | { 12 | TEST_ASSERT( to_string( type::UNINITIALIZED ) == std::string( "uninitialized" ) ); 13 | 14 | TEST_ASSERT( to_string( type::NULL_ ) == std::string( "null" ) ); 15 | TEST_ASSERT( to_string( type::BOOLEAN ) == std::string( "boolean" ) ); 16 | TEST_ASSERT( to_string( type::SIGNED ) == std::string( "signed" ) ); 17 | TEST_ASSERT( to_string( type::UNSIGNED ) == std::string( "unsigned" ) ); 18 | TEST_ASSERT( to_string( type::DOUBLE ) == std::string( "double" ) ); 19 | 20 | TEST_ASSERT( to_string( type::STRING ) == std::string( "string" ) ); 21 | TEST_ASSERT( to_string( type::STRING_VIEW ) == std::string( "string_view" ) ); 22 | TEST_ASSERT( to_string( type::BINARY ) == std::string( "binary" ) ); 23 | TEST_ASSERT( to_string( type::BINARY_VIEW ) == std::string( "binary_view" ) ); 24 | TEST_ASSERT( to_string( type::ARRAY ) == std::string( "array" ) ); 25 | TEST_ASSERT( to_string( type::OBJECT ) == std::string( "object" ) ); 26 | 27 | TEST_ASSERT( to_string( type::VALUE_PTR ) == std::string( "value_ptr" ) ); 28 | TEST_ASSERT( to_string( type::OPAQUE_PTR ) == std::string( "opaque_ptr" ) ); 29 | 30 | TEST_ASSERT( to_string( type( 42 ) ) == std::string( "unknown" ) ); 31 | } 32 | 33 | } // namespace tao::json 34 | 35 | #include "main.hpp" 36 | -------------------------------------------------------------------------------- /src/test/json/validate_utf8.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace tao::json 14 | { 15 | void test( const value& v, const std::string& b, const std::string& h ) 16 | { 17 | TEST_ASSERT( jaxn::to_string< events::invalid_string_to_binary >( v ) == b ); 18 | TEST_ASSERT( jaxn::to_string< events::invalid_string_to_hex >( v ) == h ); 19 | TEST_THROWS( to_string< events::invalid_string_to_exception >( v ) ); 20 | } 21 | 22 | void unit_test() 23 | { 24 | test( "\x80", "$80", "\"80\"" ); 25 | test( "\xC0", "$C0", "\"C0\"" ); 26 | test( "\xC0\x80", "$C080", "\"C080\"" ); 27 | 28 | test( "A\xC0" 29 | "B\x80" 30 | "C", 31 | "$41C0428043", 32 | "\"41C0428043\"" ); 33 | } 34 | 35 | } // namespace tao::json 36 | 37 | #include "main.hpp" 38 | -------------------------------------------------------------------------------- /src/test/json/value_ptr.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-2023 Dr. Colin Hirsch and Daniel Frey 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ 3 | 4 | #include "test.hpp" 5 | 6 | #include 7 | #include 8 | 9 | namespace tao::json 10 | { 11 | void unit_test() 12 | { 13 | value v{ { "foo", 1 } }; 14 | const value cv{ { "fuu", 2 } }; 15 | 16 | const value v2{ { "bar", &v } }; // Store a non-owning const pointer to v. 17 | const value cv2{ { "baz", &cv } }; // Store a non-owning const pointer to cv. 18 | 19 | TEST_ASSERT( to_string( v ) == "{\"foo\":1}" ); 20 | TEST_ASSERT( to_string( cv ) == "{\"fuu\":2}" ); 21 | 22 | TEST_ASSERT( to_string( v2 ) == "{\"bar\":{\"foo\":1}}" ); 23 | 24 | TEST_ASSERT( v2.at( "bar" ).is_value_ptr() ); 25 | 26 | TEST_ASSERT( to_string( cv2 ) == "{\"baz\":{\"fuu\":2}}" ); 27 | 28 | TEST_ASSERT( cv2.at( "baz" ).is_value_ptr() ); 29 | } 30 | 31 | } // namespace tao::json 32 | 33 | #include "main.hpp" 34 | -------------------------------------------------------------------------------- /tests/draft4/default.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "invalid type for default", 4 | "schema": { 5 | "properties": { 6 | "foo": { 7 | "type": "integer", 8 | "default": [] 9 | } 10 | } 11 | }, 12 | "tests": [ 13 | { 14 | "description": "valid when property is specified", 15 | "data": {"foo": 13}, 16 | "valid": true 17 | }, 18 | { 19 | "description": "still valid when the invalid default is used", 20 | "data": {}, 21 | "valid": true 22 | } 23 | ] 24 | }, 25 | { 26 | "description": "invalid string value for default", 27 | "schema": { 28 | "properties": { 29 | "bar": { 30 | "type": "string", 31 | "minLength": 4, 32 | "default": "bad" 33 | } 34 | } 35 | }, 36 | "tests": [ 37 | { 38 | "description": "valid when property is specified", 39 | "data": {"bar": "good"}, 40 | "valid": true 41 | }, 42 | { 43 | "description": "still valid when the invalid default is used", 44 | "data": {}, 45 | "valid": true 46 | } 47 | ] 48 | } 49 | ] 50 | -------------------------------------------------------------------------------- /tests/draft4/definitions.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "valid definition", 4 | "schema": { 5 | "definitions": { 6 | "foo": { "type": "integer" } 7 | } 8 | }, 9 | "tests": [] 10 | }, 11 | { 12 | "description": "invalid definition", 13 | "schema": { 14 | "definitions": { 15 | "foo": { "type": 1 } 16 | } 17 | }, 18 | "valid": false 19 | } 20 | ] 21 | -------------------------------------------------------------------------------- /tests/draft4/items.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "a schema given for items", 4 | "schema": { 5 | "items": {"type": "integer"} 6 | }, 7 | "tests": [ 8 | { 9 | "description": "valid items", 10 | "data": [ 1, 2, 3 ], 11 | "valid": true 12 | }, 13 | { 14 | "description": "wrong type of items", 15 | "data": [1, "x"], 16 | "valid": false 17 | }, 18 | { 19 | "description": "ignores non-arrays", 20 | "data": {"foo" : "bar"}, 21 | "valid": true 22 | } 23 | ] 24 | }, 25 | { 26 | "description": "an array of schemas for items", 27 | "schema": { 28 | "items": [ 29 | {"type": "integer"}, 30 | {"type": "string"} 31 | ] 32 | }, 33 | "tests": [ 34 | { 35 | "description": "correct types", 36 | "data": [ 1, "foo" ], 37 | "valid": true 38 | }, 39 | { 40 | "description": "wrong types", 41 | "data": [ "foo", 1 ], 42 | "valid": false 43 | } 44 | ] 45 | } 46 | ] 47 | -------------------------------------------------------------------------------- /tests/draft4/maxItems.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "maxItems validation", 4 | "schema": {"maxItems": 2}, 5 | "tests": [ 6 | { 7 | "description": "shorter is valid", 8 | "data": [1], 9 | "valid": true 10 | }, 11 | { 12 | "description": "exact length is valid", 13 | "data": [1, 2], 14 | "valid": true 15 | }, 16 | { 17 | "description": "too long is invalid", 18 | "data": [1, 2, 3], 19 | "valid": false 20 | }, 21 | { 22 | "description": "ignores non-arrays", 23 | "data": "foobar", 24 | "valid": true 25 | } 26 | ] 27 | } 28 | ] 29 | -------------------------------------------------------------------------------- /tests/draft4/maxLength.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "maxLength validation", 4 | "schema": {"maxLength": 2}, 5 | "tests": [ 6 | { 7 | "description": "shorter is valid", 8 | "data": "f", 9 | "valid": true 10 | }, 11 | { 12 | "description": "exact length is valid", 13 | "data": "fo", 14 | "valid": true 15 | }, 16 | { 17 | "description": "exact length for unicode strings is valid", 18 | "data": "ää", 19 | "valid": true 20 | }, 21 | { 22 | "description": "too long is invalid", 23 | "data": "foo", 24 | "valid": false 25 | }, 26 | { 27 | "description": "ignores non-strings", 28 | "data": 100, 29 | "valid": true 30 | }, 31 | { 32 | "description": "two supplementary Unicode code points is long enough", 33 | "data": "\uD83D\uDCA9\uD83D\uDCA9", 34 | "valid": true 35 | } 36 | ] 37 | } 38 | ] 39 | -------------------------------------------------------------------------------- /tests/draft4/maxProperties.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "maxProperties validation", 4 | "schema": {"maxProperties": 2}, 5 | "tests": [ 6 | { 7 | "description": "shorter is valid", 8 | "data": {"foo": 1}, 9 | "valid": true 10 | }, 11 | { 12 | "description": "exact length is valid", 13 | "data": {"foo": 1, "bar": 2}, 14 | "valid": true 15 | }, 16 | { 17 | "description": "too long is invalid", 18 | "data": {"foo": 1, "bar": 2, "baz": 3}, 19 | "valid": false 20 | }, 21 | { 22 | "description": "ignores non-objects", 23 | "data": "foobar", 24 | "valid": true 25 | } 26 | ] 27 | } 28 | ] 29 | -------------------------------------------------------------------------------- /tests/draft4/maximum.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "maximum validation", 4 | "schema": {"maximum": 3.0}, 5 | "tests": [ 6 | { 7 | "description": "below the maximum is valid", 8 | "data": 2.6, 9 | "valid": true 10 | }, 11 | { 12 | "description": "above the maximum is invalid", 13 | "data": 3.5, 14 | "valid": false 15 | }, 16 | { 17 | "description": "ignores non-numbers", 18 | "data": "x", 19 | "valid": true 20 | } 21 | ] 22 | }, 23 | { 24 | "description": "exclusiveMaximum validation", 25 | "schema": { 26 | "maximum": 3.0, 27 | "exclusiveMaximum": true 28 | }, 29 | "tests": [ 30 | { 31 | "description": "below the maximum is still valid", 32 | "data": 2.2, 33 | "valid": true 34 | }, 35 | { 36 | "description": "boundary point is invalid", 37 | "data": 3.0, 38 | "valid": false 39 | } 40 | ] 41 | } 42 | ] 43 | -------------------------------------------------------------------------------- /tests/draft4/minItems.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "minItems validation", 4 | "schema": {"minItems": 1}, 5 | "tests": [ 6 | { 7 | "description": "longer is valid", 8 | "data": [1, 2], 9 | "valid": true 10 | }, 11 | { 12 | "description": "exact length is valid", 13 | "data": [1], 14 | "valid": true 15 | }, 16 | { 17 | "description": "too short is invalid", 18 | "data": [], 19 | "valid": false 20 | }, 21 | { 22 | "description": "ignores non-arrays", 23 | "data": "", 24 | "valid": true 25 | } 26 | ] 27 | } 28 | ] 29 | -------------------------------------------------------------------------------- /tests/draft4/minLength.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "minLength validation", 4 | "schema": {"minLength": 2}, 5 | "tests": [ 6 | { 7 | "description": "longer is valid", 8 | "data": "foo", 9 | "valid": true 10 | }, 11 | { 12 | "description": "exact length is valid", 13 | "data": "fo", 14 | "valid": true 15 | }, 16 | { 17 | "description": "too short is invalid", 18 | "data": "f", 19 | "valid": false 20 | }, 21 | { 22 | "description": "ignores non-strings", 23 | "data": 1, 24 | "valid": true 25 | }, 26 | { 27 | "description": "one supplementary Unicode code point is not long enough", 28 | "data": "\uD83D\uDCA9", 29 | "valid": false 30 | } 31 | ] 32 | } 33 | ] 34 | -------------------------------------------------------------------------------- /tests/draft4/minProperties.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "minProperties validation", 4 | "schema": {"minProperties": 1}, 5 | "tests": [ 6 | { 7 | "description": "longer is valid", 8 | "data": {"foo": 1, "bar": 2}, 9 | "valid": true 10 | }, 11 | { 12 | "description": "exact length is valid", 13 | "data": {"foo": 1}, 14 | "valid": true 15 | }, 16 | { 17 | "description": "too short is invalid", 18 | "data": {}, 19 | "valid": false 20 | }, 21 | { 22 | "description": "ignores non-objects", 23 | "data": "", 24 | "valid": true 25 | } 26 | ] 27 | } 28 | ] 29 | -------------------------------------------------------------------------------- /tests/draft4/minimum.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "minimum validation", 4 | "schema": {"minimum": 1.1}, 5 | "tests": [ 6 | { 7 | "description": "above the minimum is valid", 8 | "data": 2.6, 9 | "valid": true 10 | }, 11 | { 12 | "description": "below the minimum is invalid", 13 | "data": 0.6, 14 | "valid": false 15 | }, 16 | { 17 | "description": "ignores non-numbers", 18 | "data": "x", 19 | "valid": true 20 | } 21 | ] 22 | }, 23 | { 24 | "description": "exclusiveMinimum validation", 25 | "schema": { 26 | "minimum": 1.1, 27 | "exclusiveMinimum": true 28 | }, 29 | "tests": [ 30 | { 31 | "description": "above the minimum is still valid", 32 | "data": 1.2, 33 | "valid": true 34 | }, 35 | { 36 | "description": "boundary point is invalid", 37 | "data": 1.1, 38 | "valid": false 39 | } 40 | ] 41 | } 42 | ] 43 | -------------------------------------------------------------------------------- /tests/draft4/optional/zeroTerminatedFloats.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "some languages do not distinguish between different types of numeric value", 4 | "schema": { 5 | "type": "integer" 6 | }, 7 | "tests": [ 8 | { 9 | "description": "a float is not an integer even without fractional part", 10 | "data": 1.0, 11 | "valid": false 12 | } 13 | ] 14 | } 15 | ] 16 | -------------------------------------------------------------------------------- /tests/draft4/pattern.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "pattern validation", 4 | "schema": {"pattern": "^a*$"}, 5 | "tests": [ 6 | { 7 | "description": "a matching pattern is valid", 8 | "data": "aaa", 9 | "valid": true 10 | }, 11 | { 12 | "description": "a non-matching pattern is invalid", 13 | "data": "abc", 14 | "valid": false 15 | }, 16 | { 17 | "description": "ignores non-strings", 18 | "data": true, 19 | "valid": true 20 | } 21 | ] 22 | }, 23 | { 24 | "description": "pattern is not anchored", 25 | "schema": {"pattern": "a+"}, 26 | "tests": [ 27 | { 28 | "description": "matches a substring", 29 | "data": "xxaayy", 30 | "valid": true 31 | } 32 | ] 33 | } 34 | ] 35 | -------------------------------------------------------------------------------- /tests/draft4/required.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description": "required validation", 4 | "schema": { 5 | "properties": { 6 | "foo": {}, 7 | "bar": {} 8 | }, 9 | "required": ["foo"] 10 | }, 11 | "tests": [ 12 | { 13 | "description": "present required property is valid", 14 | "data": {"foo": 1}, 15 | "valid": true 16 | }, 17 | { 18 | "description": "non-present required property is invalid", 19 | "data": {"bar": 1}, 20 | "valid": false 21 | }, 22 | { 23 | "description": "ignores non-objects", 24 | "data": 12, 25 | "valid": true 26 | } 27 | ] 28 | }, 29 | { 30 | "description": "required default validation", 31 | "schema": { 32 | "properties": { 33 | "foo": {} 34 | } 35 | }, 36 | "tests": [ 37 | { 38 | "description": "not required by default", 39 | "data": {}, 40 | "valid": true 41 | } 42 | ] 43 | } 44 | ] 45 | -------------------------------------------------------------------------------- /tests/taocpp/binary.jaxn: -------------------------------------------------------------------------------- 1 | { 2 | foo : $42.de.ad.be.ef, 3 | bar : $"binary string" 4 | } 5 | -------------------------------------------------------------------------------- /tests/taocpp/make_events.cbor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taocpp/json/11a31e12eda35c1322f9cf6ebb4cca0653d579dc/tests/taocpp/make_events.cbor -------------------------------------------------------------------------------- /tests/taocpp/position.json: -------------------------------------------------------------------------------- 1 | [ 2 | 11, 3 | null, 4 | 33.491938, 5 | { 6 | "hello" : "world" 7 | } 8 | ] 9 | --------------------------------------------------------------------------------