├── .clang-format ├── .editorconfig ├── .github └── workflows │ ├── ci_macos.yml │ ├── ci_ubuntu.yml │ ├── ci_windows.yml │ └── doxygen.yaml ├── .gitignore ├── CMakeLists.txt ├── Doxyfile ├── LICENSE ├── LICENSE_Dragonbox ├── cmake ├── daw-json-linkConfig.cmake.in └── test_project │ ├── CMakeLists.txt │ └── test.cpp ├── docs ├── cookbook │ ├── .editorconfig │ ├── aliases.md │ ├── array.md │ ├── automated_code_generation.md │ ├── class.md │ ├── class_from_array.md │ ├── dates.md │ ├── enums.md │ ├── exact_class_mappings.md │ ├── graphs.md │ ├── inserting_extracting_raw_json.md │ ├── json_apply.md │ ├── json_lines.md │ ├── json_nullable.md │ ├── json_schema.md │ ├── json_value.md │ ├── key_values.md │ ├── mapping_deduction.md │ ├── mapping_overview.md │ ├── member_options.md │ ├── nullable_value_concept.md │ ├── numbers.md │ ├── output_options.md │ ├── parser_policies.md │ ├── parsing_individual_members.md │ ├── readme.md │ ├── strings.md │ ├── unknown_types_and_raw_parsing.md │ └── variant.md ├── images │ ├── json_link_logo.png │ ├── json_link_logo_128.png │ ├── json_link_logo_55.png │ ├── json_link_logo_64.png │ ├── json_link_logo_wide.png │ ├── kostya_bench_chart.png │ ├── kostya_bench_chart_2021_02_26.png │ ├── kostya_bench_chart_2021_04_03.png │ └── parse_flow.jpg ├── kostya_benchmark_results.md ├── mainpage.md └── theme │ ├── doxygen-style.css │ └── img │ ├── closed-folder.png │ ├── document.png │ ├── off_sync.png │ ├── on_sync.png │ └── opened-folder.png ├── extern └── CMakeLists.txt ├── include └── daw │ ├── daw_allocator_construct.h │ ├── daw_tuple_forward.h │ ├── json │ ├── concepts │ │ ├── daw_container_traits.h │ │ ├── daw_container_traits_fwd.h │ │ ├── daw_nullable_value.h │ │ ├── daw_nullable_value_fwd.h │ │ ├── daw_writable_output.h │ │ ├── daw_writable_output_basics.h │ │ ├── daw_writable_output_cstdio.h │ │ ├── daw_writable_output_fwd.h │ │ ├── daw_writable_output_ostream.h │ │ ├── impl │ │ │ └── daw_writable_output_details.h │ │ └── readme.md │ ├── daw_from_json.h │ ├── daw_from_json_fwd.h │ ├── daw_json_apply.h │ ├── daw_json_boost_pfr_mapping.h │ ├── daw_json_data_contract.h │ ├── daw_json_default_constuctor_fwd.h │ ├── daw_json_event_parser.h │ ├── daw_json_exception.h │ ├── daw_json_find_path.h │ ├── daw_json_iostream.h │ ├── daw_json_iterator.h │ ├── daw_json_lines_iterator.h │ ├── daw_json_link.h │ ├── daw_json_link_describe.h │ ├── daw_json_link_types.h │ ├── daw_json_parse_options.h │ ├── daw_json_reflection.h │ ├── daw_json_schema.h │ ├── daw_json_serialize_options.h │ ├── daw_json_switches.h │ ├── daw_json_value_state.h │ ├── daw_to_json.h │ ├── daw_to_json_fwd.h │ └── impl │ │ ├── daw_count_digits.h │ │ ├── daw_fp_fallback.h │ │ ├── daw_json_allocator_wrapper.h │ │ ├── daw_json_alternate_mapping.h │ │ ├── daw_json_arrow_proxy.h │ │ ├── daw_json_assert.h │ │ ├── daw_json_container_appender.h │ │ ├── daw_json_default_constuctor.h │ │ ├── daw_json_enums.h │ │ ├── daw_json_exec_modes.h │ │ ├── daw_json_find_result.h │ │ ├── daw_json_link_types_aggregate.h │ │ ├── daw_json_link_types_fwd.h │ │ ├── daw_json_link_types_iso8601.h │ │ ├── daw_json_location_info.h │ │ ├── daw_json_name.h │ │ ├── daw_json_option_bits.h │ │ ├── daw_json_parse_array_iterator.h │ │ ├── daw_json_parse_class.h │ │ ├── daw_json_parse_common.h │ │ ├── daw_json_parse_digit.h │ │ ├── daw_json_parse_iso8601_utils.h │ │ ├── daw_json_parse_kv_array_iterator.h │ │ ├── daw_json_parse_kv_class_iterator.h │ │ ├── daw_json_parse_name.h │ │ ├── daw_json_parse_options_impl.h │ │ ├── daw_json_parse_policy.h │ │ ├── daw_json_parse_policy_cpp_comments.h │ │ ├── daw_json_parse_policy_hash_comments.h │ │ ├── daw_json_parse_policy_no_comments.h │ │ ├── daw_json_parse_policy_policy_details.h │ │ ├── daw_json_parse_real.h │ │ ├── daw_json_parse_real_power10.h │ │ ├── daw_json_parse_std_string.h │ │ ├── daw_json_parse_string_need_slow.h │ │ ├── daw_json_parse_string_quote.h │ │ ├── daw_json_parse_unsigned_int.h │ │ ├── daw_json_parse_value.h │ │ ├── daw_json_parse_value_fwd.h │ │ ├── daw_json_req_helper.h │ │ ├── daw_json_serialize_impl.h │ │ ├── daw_json_serialize_options_impl.h │ │ ├── daw_json_serialize_policy.h │ │ ├── daw_json_serialize_policy_details.h │ │ ├── daw_json_skip.h │ │ ├── daw_json_string_util.h │ │ ├── daw_json_traits.h │ │ ├── daw_json_type_options.h │ │ ├── daw_json_value.h │ │ ├── daw_json_value_fwd.h │ │ ├── daw_murmur3.h │ │ ├── daw_not_const_ex_functions.h │ │ ├── power_of_five_128_table.h │ │ ├── to_daw_json_string.h │ │ └── version.h │ └── third_party │ └── dragonbox │ ├── dragonbox.h │ ├── dragonbox_to_chars.h │ └── dragonbox_to_decimal.h ├── json_bench_results.json ├── readme.md └── tests ├── CMakeLists.txt ├── cmake ├── git_properties.cmake └── test_compiler_options.cmake ├── extern └── CMakeLists.txt ├── include ├── apache_builds.h ├── apache_builds_json.h ├── bench_result.h ├── citm_test.h ├── citm_test_alloc.h ├── citm_test_json.h ├── citm_test_json_alloc.h ├── daw_json_benchmark.h ├── defines.h ├── fixed_alloc.h ├── geojson.h ├── geojson_alloc.h ├── geojson_json.h ├── gsoc.h ├── island.h ├── tweet │ ├── tweet.h │ ├── twitter_timeline.h │ └── twitter_user.h ├── twitter_test.h ├── twitter_test2.h ├── twitter_test2_json.h ├── twitter_test_alloc.h ├── twitter_test_alloc_json.h ├── twitter_test_json.h ├── twitter_test_pmr.h └── twitter_test_pmr_json.h └── src ├── amazon_cellphones_test.cpp ├── apache_builds_cpp_comments_test.cpp ├── apache_builds_hash_comments_test.cpp ├── apache_builds_test.cpp ├── base_child_class_test.cpp ├── canada_output_test.cpp ├── canada_test.cpp ├── canada_test_alloc.cpp ├── canada_test_basic.cpp ├── carray_test.cpp ├── citm_test.cpp ├── citm_test_alloc.cpp ├── citm_test_basic.cpp ├── city_test.cpp ├── cookbook_aliases1_test.cpp ├── cookbook_array1_test.cpp ├── cookbook_array2_test.cpp ├── cookbook_array3_test.cpp ├── cookbook_class1_test.cpp ├── cookbook_class2_test.cpp ├── cookbook_class3_test.cpp ├── cookbook_class_alternate1_test.cpp ├── cookbook_class_from_array1_test.cpp ├── cookbook_class_from_array2_test.cpp ├── cookbook_dates1_test.cpp ├── cookbook_dates2_test.cpp ├── cookbook_dates3_test.cpp ├── cookbook_dates4_test.cpp ├── cookbook_enums1_test.cpp ├── cookbook_enums2_test.cpp ├── cookbook_escaped_strings1_test.cpp ├── cookbook_graphs1_test.cpp ├── cookbook_inserting_extracting_raw_json1_test.cpp ├── cookbook_kv1_test.cpp ├── cookbook_kv2_test.cpp ├── cookbook_kv3_test.cpp ├── cookbook_kv4_test.cpp ├── cookbook_numbers1_test.cpp ├── cookbook_numbers2_test.cpp ├── cookbook_numbers3_test.cpp ├── cookbook_optional_values1_test.cpp ├── cookbook_output_flags1_test.cpp ├── cookbook_parsing_individual_members1_test.cpp ├── cookbook_parsing_individual_members2_test.cpp ├── cookbook_parsing_individual_members3_test.cpp ├── cookbook_tuple1_test.cpp ├── cookbook_unknown_types_and_raw_parsing1_test.cpp ├── cookbook_unknown_types_and_raw_parsing2_test.cpp ├── cookbook_unknown_types_and_raw_parsing3_test.cpp ├── cookbook_variant1_test.cpp ├── cookbook_variant2_test.cpp ├── cookbook_variant3_test.cpp ├── cookbook_variant4_test.cpp ├── cookbook_variant5_test.cpp ├── coords_test.cpp ├── coords_test2.cpp ├── daw_json_fuzzing.cpp ├── daw_json_fuzzing_gsoc.cpp ├── daw_json_fuzzing_replay.cpp ├── daw_json_fuzzing_twitter.cpp ├── daw_json_fuzzing_twitter_replay.cpp ├── daw_json_iostream_test.cpp ├── daw_json_link_describe_test.cpp ├── daw_json_link_reflection_test.cpp ├── daw_json_link_test.cpp ├── daw_json_minify.cpp ├── daw_json_minify_full.cpp ├── daw_json_roundtrip.cpp ├── daw_json_schema_test.cpp ├── daw_murmur3_test.cpp ├── error_handling_bench_test.cpp ├── find_tweet_test.cpp ├── float_array_basic_test.cpp ├── float_array_test.cpp ├── full_unicode_roundtrip_test.cpp ├── gsoc_test.cpp ├── int_array_basic_test.cpp ├── int_array_test.cpp ├── int_ptr_test.cpp ├── int_sanity_test.cpp ├── issue_334_test.cpp ├── issue_336_test.cpp ├── issue_357_test.cpp ├── issue_359_test.cpp ├── issue_361_test.cpp ├── issue_370_exact_class_mapping_test.cpp ├── issue_373_empty_string_nullable_test.cpp ├── issue_389_test.cpp ├── issue_398_test.cpp ├── issue_399_test.cpp ├── issue_402_test.cpp ├── issue_406_test.cpp ├── issue_409_test.cpp ├── issue_413_test.cpp ├── issue_418_test.cpp ├── issue_421_test.cpp ├── issue_428_test.cpp ├── issue_433_test.cpp ├── issue_439_test.cpp ├── issue_462_test.cpp ├── json_bench_viewer.cpp ├── json_benchmark.cpp ├── json_lines_bench_test.cpp ├── json_lines_test.cpp ├── json_member_test.cpp ├── kostya_bench.cpp ├── kostya_bench2.cpp ├── kostya_bench3.cpp ├── kv_map_test.cpp ├── make_full24.cpp ├── multi_tu_p0_test.cpp ├── multi_tu_p1_test.cpp ├── nativejson_bench.cpp ├── nativejson_bench2.cpp ├── nativejson_bench_alloc.cpp ├── nativejson_bench_basic.cpp ├── nativejson_bench_basic2.cpp ├── nativejson_bench_nanobench.cpp ├── nativejson_roundtrip.cpp ├── no_move_or_copy_cls_test.cpp ├── numbers_test.cpp ├── optional_tagged_variant_test.cpp ├── optional_variant_test.cpp ├── out_of_order_test.cpp ├── should_fail_001.cpp ├── simple_test.cpp ├── small_test.cpp ├── stream_output_test.cpp ├── strings_escaped_test.cpp ├── strings_test.cpp ├── test_array_of_ordered.cpp ├── test_dawjsonlink.cpp ├── test_details_parse_options.cpp ├── test_details_parse_real.cpp ├── test_details_parse_value_array.cpp ├── test_details_parse_value_class.cpp ├── test_details_parse_value_custom.cpp ├── test_details_parse_value_iso8601.cpp ├── test_details_parse_value_null.cpp ├── test_details_parse_value_real.cpp ├── test_details_parse_value_signed.cpp ├── test_details_parse_value_unsigned.cpp ├── test_details_skip_array.cpp ├── test_details_skip_class.cpp ├── test_details_skip_number.cpp ├── test_details_skip_string.cpp ├── test_json_array.cpp ├── test_json_checked_number.cpp ├── test_json_date.cpp ├── test_json_intrusive_variant.cpp ├── test_json_iterator.cpp ├── test_json_key_value_array.cpp ├── test_json_raw.cpp ├── test_json_sized_array.cpp ├── test_json_tuple.cpp ├── test_stateful_json_value.cpp ├── trailing_commas.cpp ├── twitter_output_test.cpp ├── twitter_test.cpp ├── twitter_test2.cpp ├── twitter_test_alloc.cpp ├── twitter_test_basic.cpp ├── twitter_test_basic2.cpp ├── twitter_test_pmr.cpp ├── twitter_timeline_test.cpp ├── utf16_string_test.cpp └── variant_on_members.cpp /.clang-format: -------------------------------------------------------------------------------- 1 | AccessModifierOffset: -2 2 | AlignAfterOpenBracket: "true" 3 | AlignEscapedNewlines: Left 4 | AlignOperands: "true" 5 | AlignTrailingComments: true 6 | AllowAllParametersOfDeclarationOnNextLine: true 7 | AllowShortBlocksOnASingleLine: Always 8 | AllowShortCaseLabelsOnASingleLine: false 9 | AllowShortFunctionsOnASingleLine: Empty 10 | AllowShortIfStatementsOnASingleLine: "false" 11 | AllowShortLoopsOnASingleLine: false 12 | AlwaysBreakAfterDefinitionReturnType: None 13 | AlwaysBreakBeforeMultilineStrings: true 14 | AlwaysBreakTemplateDeclarations: Yes 15 | BasedOnStyle: LLVM 16 | BinPackArguments: true 17 | BinPackParameters: true 18 | BreakBeforeBinaryOperators: None 19 | BreakBeforeBraces: Attach 20 | BreakBeforeTernaryOperators: true 21 | BreakConstructorInitializersBeforeComma: true 22 | ColumnLimit: 80 23 | CommentPragmas: '^ IWYU pragma:' 24 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 25 | ConstructorInitializerIndentWidth: 2 26 | ContinuationIndentWidth: 2 27 | Cpp11BracedListStyle: true 28 | DerivePointerAlignment: false 29 | DisableFormat: false 30 | ExperimentalAutoDetectBinPacking: false 31 | IndentCaseLabels: false 32 | IndentRequiresClause: false 33 | IndentWidth: 2 34 | IndentWrappedFunctionNames: false 35 | KeepEmptyLinesAtTheStartOfBlocks: true 36 | MaxEmptyLinesToKeep: 1 37 | NamespaceIndentation: All 38 | PenaltyBreakBeforeFirstCallParameter: 19 39 | PenaltyBreakComment: 300 40 | PenaltyBreakFirstLessLess: 80 41 | PenaltyBreakString: 1000 42 | PenaltyExcessCharacter: 1000000 43 | PenaltyReturnTypeOnItsOwnLine: 60 44 | PointerAlignment: Right 45 | RequiresClausePosition: OwnLine 46 | SpaceAfterCStyleCast: false 47 | SpaceAfterTemplateKeyword: false 48 | SpaceBeforeAssignmentOperators: true 49 | SpaceBeforeParens: Never 50 | SpaceInEmptyParentheses: true 51 | SpacesBeforeTrailingComments: 1 52 | SpacesInAngles: false 53 | SpacesInCStyleCastParentheses: false 54 | SpacesInContainerLiterals: true 55 | SpacesInParentheses: true 56 | SpacesInSquareBrackets: false 57 | Standard: c++17 58 | TabWidth: 2 59 | UseTab: ForIndentation 60 | AllowShortLambdasOnASingleLine: Empty 61 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root=true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = tab 6 | indent_size = 2 7 | trim_trailing_whitespace = true 8 | max_line_length = 100 9 | -------------------------------------------------------------------------------- /.github/workflows/ci_macos.yml: -------------------------------------------------------------------------------- 1 | name: MacOS 2 | 3 | on: 4 | schedule: 5 | - cron: '0 5 * * *' 6 | push: 7 | branches: [ release, develop ] 8 | pull_request: 9 | push: 10 | branches-ignore: develop 11 | pull_request: 12 | branches-ignore: develop 13 | 14 | jobs: 15 | CI_Tests: 16 | permissions: 17 | actions: none 18 | checks: none 19 | contents: none 20 | deployments: none 21 | issues: none 22 | packages: none 23 | pull-requests: none 24 | repository-projects: none 25 | security-events: none 26 | statuses: none 27 | strategy: 28 | fail-fast: false 29 | matrix: 30 | cpp_version: [ 17, 20 ] 31 | build_type: [ Debug, Release ] 32 | os: [ macos-latest, macos-14 ] 33 | toolset: [ clang++ ] 34 | runs-on: ${{ matrix.os }} 35 | name: "${{ matrix.os }} ${{ matrix.toolset }} ${{ matrix.cpp_version }} ${{ matrix.build_type }}" 36 | steps: 37 | - uses: actions/checkout@v1 38 | - name: Create Build 39 | run: | 40 | mkdir build && \ 41 | brew install ninja 42 | - name: Build Dependencies 43 | env: 44 | CXX: ${{ matrix.toolset }} 45 | run: cmake -GNinja -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DDAW_NUM_RUNS=1 -DDAW_ENABLE_TESTING=ON -DDAW_WERROR=ON -DDAW_JSON_USE_SANITIZERS=ON -DDAW_NO_FLATTEN=ON -DCMAKE_CXX_STANDARD=${{ matrix.cpp_version }} -Bbuild/ . 46 | - name: Build 47 | run: cmake --build build/ --target ci_tests -- -k 0 48 | continue-on-error: true 49 | - name: Test 50 | run: | 51 | ulimit -c unlimited 52 | ulimit -c 53 | ctest -C ${{ matrix.build_type }} -j2 -VV --timeout 1200 --test-dir build/ 54 | - name: Archive any crashes as an artifact 55 | uses: actions/upload-artifact@v4 56 | if: always( ) 57 | with: 58 | name: crashes 59 | path: | 60 | crash-* 61 | leak-* 62 | timeout-* 63 | /cores/** 64 | if-no-files-found: ignore 65 | -------------------------------------------------------------------------------- /.github/workflows/doxygen.yaml: -------------------------------------------------------------------------------- 1 | name: Doxygen 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - release 8 | 9 | jobs: 10 | doxygen: 11 | env: 12 | DOXYGEN_OUTPUT_DIRECTORY: ${{ github.workspace }}/docs 13 | 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Install doxygen and graphviz 18 | run: sudo apt-get install -y doxygen graphviz 19 | - name: Build 🏗️ doxygen site 20 | run: doxygen 21 | - name: Deploy 🚀 GitHub Pages Site 22 | uses: JamesIves/github-pages-deploy-action@v4.2.5 23 | with: 24 | branch: gh-pages 25 | folder: ${{ env.DOXYGEN_OUTPUT_DIRECTORY }}/html 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | !**/packages/build/ 2 | $tf/ 3 | **/.DS_Store 4 | **/packages/* 5 | *.aps 6 | *.azurePubxml 7 | *.bak 8 | *.bim.layout 9 | *.bim_*.settings 10 | *.build.csdef 11 | *.Cache 12 | *.cache 13 | *.cachefile 14 | *.cd 15 | *.dbmdl 16 | *.dbproj.schemaview 17 | *.dotCover 18 | *.DotSettings.user 19 | *.exe 20 | *.gpState 21 | *.ide/ 22 | *.ilk 23 | *.ldf 24 | *.lib 25 | *.log 26 | *.mdf 27 | *.meta 28 | *.mm.* 29 | *.ncb 30 | *.npp 31 | *.nupkg 32 | *.obj 33 | *.opensdf 34 | *.pch 35 | *.pdb 36 | *.pfx 37 | *.pgc 38 | *.pgd 39 | *.pidb 40 | *.psess 41 | *.publishproj 42 | *.publishsettings 43 | *.pubxml 44 | *.rdl.data 45 | *.rsp 46 | *.sbr 47 | *.scc 48 | *.sdf 49 | *.sln.docstates 50 | *.sln.ide/ 51 | *.suo 52 | *.svclog 53 | *.swp 54 | *.tlb 55 | *.tlh 56 | *.tli 57 | *.tmp 58 | *.tmp_proj 59 | *.user 60 | *.userosscache 61 | *.vcxproj.filters 62 | *.VisualState.xml 63 | *.vsp 64 | *.vspscc 65 | *.vspx 66 | *.vssscc 67 | *.[Pp]ublish.xml 68 | *.[Rr]e[Ss]harper 69 | *_i.c 70 | *_i.h 71 | *_p.c 72 | *~ 73 | .*crunch*.local.xml 74 | .builds 75 | .idea/ 76 | .JustCode 77 | .sass-cache/ 78 | .vs/ 79 | a.out 80 | AppPackages/ 81 | AutoTest.Net/ 82 | Backup*/ 83 | bld/ 84 | bower_components/ 85 | build/ 86 | ClientBin/ 87 | cmake-build-*/ 88 | cmake-build/ 89 | test_data/ 90 | CMakeFiles/ 91 | CMakeSettings.json 92 | csx/ 93 | dlldata.c 94 | DocProject/buildhelp/ 95 | DocProject/Help/*.hhc 96 | DocProject/Help/*.hhk 97 | DocProject/Help/*.hhp 98 | DocProject/Help/*.HxC 99 | DocProject/Help/*.HxT 100 | DocProject/Help/html 101 | DocProject/Help/Html2 102 | FakesAssemblies/ 103 | Generated_Code/ 104 | ipch/ 105 | nbproject/* 106 | node_modules/ 107 | obj/ 108 | out/ 109 | publish/ 110 | sql/ 111 | TestResult.xml 112 | Thumbs.db 113 | UpgradeLog*.htm 114 | UpgradeLog*.XML 115 | x64/ 116 | x86/ 117 | [Bb]in 118 | [Bb]in/ 119 | [Bb]uild[Ll]og.* 120 | [Dd]ebug*/ 121 | [Dd]ebug/ 122 | [Dd]ebugPS/ 123 | [Dd]ebugPublic/ 124 | [Ee]xpress/ 125 | [Oo]bj/ 126 | [Rr]elease*/ 127 | [Rr]elease/ 128 | [Rr]eleasePS/ 129 | [Rr]eleases/ 130 | [Ss]tyle[Cc]op.* 131 | [Tt]est[Rr]esult* 132 | [Tt]est[Rr]esult*/ 133 | _Chutzpah* 134 | _NCrunch_* 135 | _ReSharper*/ 136 | _TeamCity* 137 | _UpgradeReport_Files/ 138 | ~$* 139 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /LICENSE_Dragonbox: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /cmake/daw-json-linkConfig.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | include(CMakeFindDependencyMacro) 4 | find_dependency( daw-header-libraries ) 5 | find_dependency( daw-utf-range ) 6 | 7 | include("${CMAKE_CURRENT_LIST_DIR}/daw-json-linkTargets.cmake") 8 | 9 | check_required_components( daw-json-link ) 10 | -------------------------------------------------------------------------------- /cmake/test_project/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Darrell Wright 2 | # 3 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | # file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | # 6 | # Official repository: https://github.com/beached/header_libraries 7 | # 8 | 9 | cmake_minimum_required(VERSION 3.14) 10 | 11 | project("install-test" 12 | VERSION "1.0.0" 13 | DESCRIPTION "Tests that find_package works" 14 | HOMEPAGE_URL "https://github.com/beached/header_libraries" 15 | LANGUAGES C CXX 16 | ) 17 | 18 | set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested.") 19 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 20 | set(CMAKE_CXX_EXTENSIONS OFF) 21 | 22 | find_package( daw-header-libraries REQUIRED ) 23 | find_package( daw-utf-range REQUIRED ) 24 | find_package( daw-json-link REQUIRED ) 25 | 26 | enable_testing() 27 | 28 | add_executable( ${PROJECT_NAME} test.cpp ) 29 | target_compile_options( ${PROJECT_NAME} INTERFACE $<$:/permissive->) 30 | target_link_libraries( ${PROJECT_NAME} PRIVATE daw::daw-header-libraries daw::daw-utf-range daw::daw-json-link ) 31 | add_test( ${PROJECT_NAME}_test ${PROJECT_NAME} ) 32 | -------------------------------------------------------------------------------- /cmake/test_project/test.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace daw::cookbook_class1 { 12 | struct MyClass1 { 13 | std::string member_0; 14 | int member_1; 15 | bool member_2; 16 | }; 17 | } // namespace daw::cookbook_class1 18 | 19 | namespace daw::json { 20 | template<> 21 | struct json_data_contract { 22 | static constexpr char const member0[] = "member0"; 23 | static constexpr char const member1[] = "member1"; 24 | static constexpr char const member2[] = "member2"; 25 | using type = 26 | json_member_list, json_number, 27 | json_bool>; 28 | static inline auto 29 | to_json_data( daw::cookbook_class1::MyClass1 const &value ) { 30 | return std::forward_as_tuple( value.member_0, value.member_1, 31 | value.member_2 ); 32 | } 33 | }; 34 | } // namespace daw::json 35 | 36 | int main( ) { 37 | std::string_view data = R"( 38 | { 39 | "member0": "this is a test", 40 | "member1": 314159, 41 | "member2": true 42 | } 43 | )"; 44 | auto const cls = daw::json::from_json( 45 | std::string_view( data.data( ), data.size( ) ) ); 46 | 47 | assert( cls.member_0 == "this is a test" ); 48 | assert( cls.member_1 == 314159 ); 49 | assert( cls.member_2 == true ); 50 | std::string const str = daw::json::to_json( cls ); 51 | puts( str.c_str( ) ); 52 | 53 | auto const cls2 = daw::json::from_json( 54 | std::string_view( str.data( ), str.size( ) ) ); 55 | (void)cls2; 56 | puts( "All good" ); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /docs/cookbook/.editorconfig: -------------------------------------------------------------------------------- 1 | root=true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = tab 6 | indent_size = 2 7 | trim_trailing_whitespace = true 8 | max_line_length = 100 9 | -------------------------------------------------------------------------------- /docs/cookbook/aliases.md: -------------------------------------------------------------------------------- 1 | ### json_type_alias 2 | 3 | It is sometimes desirable to map a class to be parsed like that of another type of mapping or another class. This, can 4 | allow parsing the json data as a number and constructing a class. There are two parts, first the type alias describes what parser to use that will allow construction of the type. The `to_json_data` static function will return a value of the type that was parsed to. 5 | 6 | ```cpp 7 | // A basic type that wraps an integer 8 | class MyType { 9 | int value; 10 | 11 | public: 12 | MyType( int i ) : value( i ) {} 13 | 14 | int const& get() const { return value; } 15 | }; 16 | 17 | namespace daw::json { 18 | template <> 19 | struct json_data_contract { 20 | // Tell JSON Link that MyType will be constructible from and parsed as if it was an int. This could also be json_number_no_name 21 | using type = json_type_alias; 22 | 23 | // Extract the int so that it can be serialized as if it was the type being serialized 24 | static inline int to_json_data(MyType const& v) { 25 | return v.get(); 26 | } 27 | }; 28 | } // namespace daw::json 29 | ``` 30 | 31 | Another example is we want to store a GNU MP Rational type `mpq_class::mpq_class` as a string and use it's constructor to 32 | parse the data. 33 | 34 | ```cpp 35 | namespace daw::json { 36 | template<> 37 | struct json_data_contract { 38 | using type = json_type_alias; 39 | 40 | static inline auto to_json_data( mpq_class::mpq_class const & v ) { 41 | return return v.get_str( ); 42 | } 43 | }; 44 | } 45 | ``` 46 | 47 | The data for this mapping could look like "12345/56789" 48 | 49 | The 50 | file [cookbook_aliases1_test.cpp](../../tests/src/cookbook_aliases1_test.cpp) 51 | shows an example share a type `MyClass` has a single member of type `std::string` that is serialized and deserialized as 52 | a string. -------------------------------------------------------------------------------- /docs/cookbook/class_from_array.md: -------------------------------------------------------------------------------- 1 | ### Classes from array's 2 | 3 | Sometimes the members of a class are encoded as a JSON array. GeoJSON is an example of this. 4 | 5 | ```json 6 | [ 7 | 34.3434, 8 | 134.3434 9 | ] 10 | ``` 11 | 12 | The above is the encoding of a Point in GeoJSON. 13 | Too see a working example using this code, refer to [cookbook_class_from_array1_test.cpp](../../tests/src/cookbook_class_from_array1_test.cpp) 14 | 15 | Below is a `Point` structure with two double members. The mapping uses a `json_ordered_member_list` and can accept unnamed member items. Below describes a mapping to an array's first and second elements. Any subsequent members are ignored. 16 | 17 | ```c++ 18 | struct Point { 19 | double x; 20 | double y; 21 | }; 22 | 23 | namespace daw::json { 24 | template<> 25 | struct json_data_contract { 26 | using type = json_ordered_member_list; 27 | 28 | static constexpr auto to_json_data( Point const &p ) { 29 | return std::forward_as_tuple( Point.x, Point.y ); 30 | } 31 | }; 32 | } 33 | ``` 34 | 35 | Sometimes the members to extract are not contiguous and a specific index into the array is requried. For this a member can be wrapped in a `ordered_json_member` type. 36 | 37 | ```json 38 | [ 39 | 34.3434, 40 | "a", 41 | 34343, 42 | { 43 | "a": 1244, 44 | "b": [ 45 | 1, 46 | 2, 47 | 3, 48 | 4 49 | ] 50 | }, 51 | [ 52 | "a", 53 | 5, 54 | "4" 55 | ], 56 | 134.3434, 57 | "a" 58 | ] 59 | ``` 60 | 61 | With the previous `Point` struct, the mapping here will be to parse the first element as a double, and the sixth element as a double. 62 | 63 | Too see a working example using this code, refer to [cookbook_class_from_array2_test.cpp](../../tests/src/cookbook_class_from_array2_test.cpp) 64 | 65 | ```c++ 66 | struct Point { 67 | double x; 68 | double y; 69 | }; 70 | 71 | namespace daw::json { 72 | template<> 73 | struct json_data_contract { 74 | using type = json_ordered_member_list>>; 75 | 76 | static constexpr auto to_json_data( Point const &p ) { 77 | return std::forward_as_tuple( Point.x, Point.y ); 78 | } 79 | }; 80 | } 81 | ``` 82 | -------------------------------------------------------------------------------- /docs/cookbook/exact_class_mappings.md: -------------------------------------------------------------------------------- 1 | By default unmapped members in a JSON document are skipped. There are a few ways to turn this into an error. 2 | 3 | # Global parser option 4 | 5 | One can change the default for all classes as a parser option. For example with `from_json` it would loook similar to: 6 | 7 | ```c++ 8 | daw::json::from_json( 9 | json_doc1, 10 | daw::json::options::parse_flags< 11 | daw::json::options::UseExactMappingsByDefault::yes> 12 | ); 13 | ``` 14 | 15 | When the value is set to `yes`, you can exclude a class from this by adding a type alias to it's class mapping called `ignore_unknown_members`. For example: 16 | 17 | ```c++ 18 | namespace daw::json { 19 | template<> 20 | struct json_data_contract { 21 | using ignore_unknown_members = void; 22 | 23 | using type = json_member_list>; 24 | 25 | //... 26 | }; 27 | } 28 | ``` 29 | 30 | # Enabling per class 31 | 32 | To enable errors for unknown members per class, add a type alias to the mapping called `exact_class_mapping` 33 | 34 | ```c++ 35 | namespace daw::json { 36 | template<> 37 | struct json_data_contract { 38 | using exact_class_mapping = void; 39 | 40 | using type = json_member_list>; 41 | //... 42 | }; 43 | } 44 | ``` -------------------------------------------------------------------------------- /docs/cookbook/inserting_extracting_raw_json.md: -------------------------------------------------------------------------------- 1 | To parse/serialize raw JSON data, such as data from other libraries, one can use the `json_raw` mapping type. The type requirement for mapping to `json_raw` is constructable with a `char const *` and a `std::size_t`, along with supporting `std::begin`/`std::end`. 2 | 3 | ```c++ 4 | struct Foo { 5 | std::string bar; 6 | std::string raw_json; 7 | }; 8 | 9 | namespace daw::json { 10 | template<> 11 | struct json_data_contract { 12 | using type = json_member_list< 13 | json_link<"bar", std::string>, 14 | json_raw<"raw_json", std::string> 15 | >; 16 | 17 | static auto to_json_data( Foo const & v ) { 18 | return std::forward_as_tuple( v.bar, v.raw_json ); 19 | } 20 | }; 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /docs/cookbook/json_apply.md: -------------------------------------------------------------------------------- 1 | # json_apply 2 | 3 | `json_apply` parses the document to parameter types specified in signature, then passes them to 4 | the Callable for evaluation. This is similar to `std::apply` but using a JSON documents instead of tuples. 5 | 6 | ## Uses 7 | 8 | `json_apply` is useful in IPC/RPC situations where the data is (de)serialized from/to JSON, such as JSON RPC or IPC with web views. 9 | 10 | ## Example 11 | 12 | ```c++ 13 | struct NumberX { 14 | int x; 15 | }; 16 | // ... 17 | auto x = daw::json::json_apply( 18 | R"json({"x":10})json", 19 | []( NumberX nx ) { 20 | return nx.x; 21 | } ); 22 | assert( x == 10 ); 23 | ``` 24 | -------------------------------------------------------------------------------- /docs/cookbook/json_lines.md: -------------------------------------------------------------------------------- 1 | # JSON Lines Support 2 | 3 | [JSON Lines](https://jsonlines.org/) is in the form of an iterator `daw::json::json_lines_iterator` and a range `daw::json::json_lines_range` like object. They allow iteration over each lines in the JSON Lines file. 4 | 5 | ## Parsing JSON Lines 6 | 7 | A working example can be seen at [json_lines_test.cpp](../../tests/src/json_lines_test.cpp) 8 | 9 | ```json lines 10 | { 11 | "a": 1, 12 | "b": false 13 | } 14 | { 15 | "a": 2, 16 | "b": true 17 | } 18 | ``` 19 | 20 | The code to iterate over these elements can look like 21 | 22 | ```cpp 23 | struct Element { 24 | int a; 25 | bool b; 26 | }; 27 | 28 | namespace daw::json { 29 | template<> 30 | struct json_data_contract { 31 | static constexpr char const a[] = "a"; 32 | static constexpr char const b[] = "b"; 33 | using type = json_member_list, json_link>; 34 | }; 35 | } // namespace daw::json 36 | 37 | int main( ) { 38 | auto jl_range = daw::json::json_lines_range( json_lines_doc ); 39 | for( Element e : jl_range ) { 40 | std::cout << e.a << ", " << e.b << '\n'; 41 | } 42 | } 43 | ``` 44 | 45 | ## Serializing to JSON Lines 46 | 47 | Staring with the `Element` type in the previous example, one can output to a JSON Line document as follows. 48 | 49 | ```cpp 50 | std::vector elements = get_element_vector( ); 51 | auto result = std::string( ); 52 | for( auto const & e : elements ) { 53 | daw::json::to_json( e, result ); 54 | result += '\n'; 55 | } 56 | ``` -------------------------------------------------------------------------------- /docs/cookbook/json_nullable.md: -------------------------------------------------------------------------------- 1 | # json_nullable types 2 | 3 | nullable types are supported by using the `json_nullable` mapping type or appending the type name with `_null` to use the convenience wrapper. The requirement of the underlying type is that it will be default constructable for null values or support the [Nullable Concept](nullable_value_concept.md). 4 | 5 | Take the following JSON 6 | 7 | ```json 8 | [ 9 | { 10 | "member0": 5, 11 | "member1": "hello" 12 | }, 13 | { 14 | "member1": "world", 15 | "member2": true 16 | } 17 | ] 18 | ``` 19 | 20 | The JSON document is an array of an object with two members, an optional int, a string, and an optional bool. 21 | 22 | To see a working example using this code, refer to [cookbook_optional_values1_test.cpp](../../tests/src/cookbook_optional_values1_test.cpp). 23 | 24 | ```c++ 25 | struct MyOptionalStuff1 { 26 | std::optional member0; 27 | std::string member1; 28 | std::unique_ptr member2; 29 | }; 30 | 31 | namespace daw::json { 32 | template<> 33 | struct json_data_contract { 34 | using type = json_member_list< 35 | json_number_null<"member0", std::optional>, 36 | json_string<"member1">, 37 | json_bool_null< 38 | "member2", 39 | std::unique_ptr, 40 | LiteralAsStringOpt::NoEscapedDblQuote, 41 | UniquePtrConstructor 42 | > 43 | >; 44 | 45 | static inline auto to_json_data( MyOptionalStuff1 const &value ) { 46 | return std::forward_as_tuple( value.member0, value.member1, value.member2 ); 47 | } 48 | }; 49 | } 50 | ``` 51 | -------------------------------------------------------------------------------- /docs/cookbook/json_schema.md: -------------------------------------------------------------------------------- 1 | # Exporting JSON Schema 2 | 3 | One can export the JSON Schema to validate the mapped types. `#include ` and 4 | call `daw::json::to_json_schema( "identifier", "title" );` to generate a `std::string`. There is an output 5 | iterator version too. 6 | Refer 7 | to [daw_json_schema_test.cpp](../../tests/src/daw_json_schema_test.cpp) 8 | for a working example. -------------------------------------------------------------------------------- /docs/cookbook/json_value.md: -------------------------------------------------------------------------------- 1 | # Json Value 2 | 3 | The `json_value` type is a lazy pull parser. It provides lookup and extraction mechanisms for when the regular mappings cannot work or doing data exploration. One such example is the [variant_on_members.cpp](../../tests/src/variant_on_members.cpp) where the structure of the member is used to determine which parser to use. For types that use a tag or member to discriminate this isn't necessary as there are `json_variant_...` mappings for that. The use of `json_value` is not encouraged except when in conjunction with the mappings such as the `json_raw` mapping when existing mappings do not work, or data discovery. 4 | 5 | 6 | The general query interface to `json_value` is via `operator[]`. This can be used with a JSON Path to extract a new `json_value`. e.g 7 | 8 | ```cpp 9 | auto type_jv = jv["value.type"]; 10 | assert( type_kv ); 11 | auto type = as( type_jv ); 12 | ``` 13 | 14 | The example extracts the "type" member of "value" and returns a new `json_value`. If it is not found, the `operator bool` will evaluate to false. It is also false if the "type" member had a value of `null`. If one wants to know if it was found or not, `has_more( )` will return false as the return is a default constructed `json_value`. Following that, one can use the `as` function to parse the value. `from_json` will, also, work here. 15 | 16 | There are several ways to parse from a `json_value`. The most succinct is the `T as( json_value )` free function, but there is an `json_value::as( ) const` member function too. The `from_json` interface used for parsing from string data works too. Finally, one can cast to their desired type. 17 | 18 | One can also check the type of the current value via several members such as `is_class`, `is_null`, `is_array`, `is_number`, `is_string`, and `is_bool`. There is also `type( )` member that returns an enum of the basic JSON types that can be switched over. 19 | 20 | The `json_value` type is a range and one can iterate over classes or arrays. The value_type is a `json_pair` type that has a `name` and a `value` member. The name is an `optional`. When iterating over an array, the name will be `nullopt`. The value member is a `json_value`. The `json_pair`, also, fulfills the tuple protocol and can get used with structured bindings. 21 | 22 | ```cpp 23 | for( auto [name,value]: my_jv_of_class ) { 24 | std::cout << "name:" << *name << " value: " << value.get_string_view( ) << '\n'; 25 | } 26 | ``` 27 | 28 | The following is equivalent 29 | 30 | ```cpp 31 | auto jv = other_jv[5]["a"]["b"][2]; 32 | ``` 33 | 34 | ```cpp 35 | auto jv = other_jv["[5].a.b[2]"]; 36 | ``` -------------------------------------------------------------------------------- /docs/cookbook/mapping_deduction.md: -------------------------------------------------------------------------------- 1 | Many types can be deduced and not explicitly mapped to a JSON type(string, number, bool, object, array) 2 | This allows one to use them directly in places that don't require a member name(e.g. json_array's element type) or with 3 | the `json_link` mapping type. The order of deduction is as follows 4 | 5 | * types with existing json_data_contract's specialized for them 6 | * Well known types 7 | 8 | | Type | Mapped To | Notes | 9 | |-----------------------|------------------|---------------------------| 10 | | std:string_view |json_string_raw | | 11 | | std::string |json_string | | 12 | | bool |json_bool | | 13 | | Integer |json_number | Uses std::numeric_limits. json_number optimized for Signed/Unsigned integers| 14 | | Enum |json_number | Used std::is_enum and std::underlying_type| 15 | | Floating point |json_number | Uses std::numeric_limits| 16 | | Associative Container |json_key_value_map| Has begin()/end()/key_type/mapped_type and constructable with two iterators| 17 | | readable values | | The value_type in the readable mapping of T with a json_null wrapped around T's deduced mapping. See the readable value cookbook item | 18 | | Containers |json_array | Excluding associative containers. Uses value_type as the type for each element| 19 | 20 | * Containers - map to json_array with the element type as the detected type of the value_type. Must have the methods 21 | begin(), end(), type alias value_type, and can be constructed with two iterators. Same Iterator requirements as std:: 22 | vector's two iterator constructor. 23 | 24 | 25 | -------------------------------------------------------------------------------- /docs/cookbook/nullable_value_concept.md: -------------------------------------------------------------------------------- 1 | # Readable Values 2 | 3 | A nullable value is also called and option type or readable type. This includes std::optional, std::unique_ptr, std::shared_ptr, and pointers. In order to use them without a custom Constructor type for each mapping, or in deduced situations (`json_link<...>`) they must be mapped via the `daw::nullable_value_traits` type mapping. 4 | 5 | A specialization must have the following, assuming T is the specialized type(e.g `std::optional`): 6 | * `value_type` type alias to the inner type. e.g ( `std::optional` -> `U` ) 7 | * `is_nullable` A variable that must be `static constexpr bool is_nullable = true` for nullable types. If false it will mark the type `T` as not nullable 8 | * `static value_type const & read( T const & )` A function that reads the value from the nullable type and returns a const ref to it. A `value_type` return will work too 9 | * `T operator( )( daw::construct_nullable_with_empty_t ) const` A member function that takes no args and returns an empty value. 10 | * `T operator( )( daw::construct_nullable_with_value_t, Args... ) const` A member function that when passed appropriate args constructs a value. It should be SFINAE'able and constrain on valid arguments. 11 | * `static bool has_value( T const & )` returns if the value is present or not. -------------------------------------------------------------------------------- /docs/cookbook/parsing_individual_members.md: -------------------------------------------------------------------------------- 1 | # Parsing Individual Members 2 | 3 | Parsing single members from the JSON data is common. This can be to discriminate on a single member or when only a single member is needed. 4 | 5 | ```json 6 | { 7 | "member0": 55, 8 | "member1": [ 9 | 1, 10 | 2, 11 | 3, 12 | 4, 13 | 5 14 | ], 15 | "member2": { 16 | "a": 123.4, 17 | "b": "found me" 18 | } 19 | } 20 | ``` 21 | 22 | With the previous JSON data, the next example will extract the member `"member2.b"`, the `"b"` member of the `"member2"` member of the root object. 23 | 24 | To see a working example using this code, refer to [cookbook_parsing_individual_members1_test.cpp](../../tests/src/cookbook_parsing_individual_members1_test.cpp). 25 | 26 | ```c++ 27 | std::string b_value = daw::json::from_json( json_data, "member2.b" ); 28 | ``` 29 | 30 | `b_value` should have a value of `"found me"` 31 | 32 | Below shows when member is known to be an array and full control of the resulting collection is needed. 33 | 34 | Too see a working example using this code, refer to [cookbook_parsing_individual_members2_test.cpp](../../tests/src/cookbook_parsing_individual_members2_test.cpp). 35 | 36 | ```c++ 37 | std::vector values = daw::json::from_json_array( json_data, "member1" ); 38 | ``` 39 | 40 | ## Specifying array indexes 41 | 42 | In the member path array indexes are specified inside square brackets `[` and `]`. Below the 3rd item of `"member1"` will be extracted 43 | 44 | Too see a working example using this code, refer to [cookbook_parsing_individual_members3_test.cpp](../../tests/src/cookbook_parsing_individual_members3_test.cpp). 45 | 46 | ```c++ 47 | int third_value = daw::json::from_json( json_data, "member1[2]" ); 48 | ``` 49 | -------------------------------------------------------------------------------- /docs/cookbook/readme.md: -------------------------------------------------------------------------------- 1 | # CookBook 2 | 3 | This folder contains examples of various JSON constructs and how to create a C++ class/contract to parse them 4 | 5 | * [Aliases](aliases.md) 6 | * [Arrays](array.md) 7 | * [Automatic Code Generation](automated_code_generation.md) 8 | * [Classes from Array/JSON Tuples](class_from_array.md) 9 | * [Classes](class.md) 10 | * [Dates](dates.md) 11 | * [Enums](enums.md) 12 | * [Graphs](graphs.md) 13 | * [Inserting and Extracting Raw JSON](inserting_extracting_raw_json.md) 14 | * [JSON Lines/NDJSON](json_lines.md) 15 | * [JSON Schema Output](json_schema.md) 16 | * [Key Values](key_values.md) - Map and Dictionary like things 17 | * [Mapping Overview](mapping_overview.md) An overview of the mapping types using in `from_json` or `json_data_constract` 18 | * [Mapping Deduction](mapping_deduction.md) 19 | * [Member Options](member_options.md) - Options for the parse mappings 20 | * [Numbers](numbers.md) 21 | * [Nullable Concept](nullable_value_concept.md) - Trait for mapping Option/Nullable types used in deduction 22 | * [Nullable JSON Values](json_nullable.md) 23 | * [Output Options](output_options.md) - Options for serialization 24 | * [Parser Options](parser_policies.md) - Options for parsing 25 | * [Parsing Individual Members](parsing_individual_members.md) 26 | * [Strings](strings.md) 27 | * [Unknown JSON and Raw Parsing](unknown_types_and_raw_parsing.md) - Browsing the JSON Document and delaying of parsing of specified members 28 | * [Variant](variant.md) 29 | -------------------------------------------------------------------------------- /docs/images/json_link_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/json_link_logo.png -------------------------------------------------------------------------------- /docs/images/json_link_logo_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/json_link_logo_128.png -------------------------------------------------------------------------------- /docs/images/json_link_logo_55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/json_link_logo_55.png -------------------------------------------------------------------------------- /docs/images/json_link_logo_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/json_link_logo_64.png -------------------------------------------------------------------------------- /docs/images/json_link_logo_wide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/json_link_logo_wide.png -------------------------------------------------------------------------------- /docs/images/kostya_bench_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/kostya_bench_chart.png -------------------------------------------------------------------------------- /docs/images/kostya_bench_chart_2021_02_26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/kostya_bench_chart_2021_02_26.png -------------------------------------------------------------------------------- /docs/images/kostya_bench_chart_2021_04_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/kostya_bench_chart_2021_04_03.png -------------------------------------------------------------------------------- /docs/images/parse_flow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/images/parse_flow.jpg -------------------------------------------------------------------------------- /docs/mainpage.md: -------------------------------------------------------------------------------- 1 | ![logo image](images/json_link_logo_128.png) 2 | 3 | ##Fast JSON serialization and parsing in C++ 4 | 5 | * [Cookbook Documentation](pages.html) 6 | * [Project Page](https://github.com/beached/daw_json_link) 7 | -------------------------------------------------------------------------------- /docs/theme/img/closed-folder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/theme/img/closed-folder.png -------------------------------------------------------------------------------- /docs/theme/img/document.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/theme/img/document.png -------------------------------------------------------------------------------- /docs/theme/img/off_sync.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/theme/img/off_sync.png -------------------------------------------------------------------------------- /docs/theme/img/on_sync.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/theme/img/on_sync.png -------------------------------------------------------------------------------- /docs/theme/img/opened-folder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beached/daw_json_link/af2d6dc9ed797f316b7199c77a48808c85b98717/docs/theme/img/opened-folder.png -------------------------------------------------------------------------------- /extern/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Darrell Wright 2 | # 3 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | # file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | # 6 | # Official repository: https://github.com/beached/daw_json_link 7 | # 8 | 9 | # This prevents tests running on dependencies 10 | option( DAW_ENABLE_TESTING "Build unit tests" OFF ) 11 | if( DAW_ENABLE_TESTING ) 12 | set( DAW_ENABLE_TESTING OFF ) 13 | set( DAW_EXTERN_USE_LATEST ON ) 14 | endif() 15 | 16 | include( FetchContent ) 17 | 18 | if( DAW_EXTERN_USE_LATEST ) 19 | # Use the latest header_libraries and not release 20 | FetchContent_Declare( 21 | daw_header_libraries 22 | GIT_REPOSITORY https://github.com/beached/header_libraries.git 23 | GIT_TAG v2 24 | ) 25 | message( STATUS "Using latest header_libraries" ) 26 | else() 27 | FetchContent_Declare( 28 | daw_header_libraries 29 | GIT_REPOSITORY https://github.com/beached/header_libraries.git 30 | GIT_SHALLOW 1 31 | GIT_TAG master 32 | ) 33 | endif() 34 | 35 | FetchContent_Declare( 36 | daw_utf_range 37 | GIT_REPOSITORY https://github.com/beached/utf_range.git 38 | GIT_SHALLOW 1 39 | GIT_TAG v2.2.5 40 | ) 41 | 42 | FetchContent_MakeAvailable( daw_header_libraries daw_utf_range ) 43 | 44 | -------------------------------------------------------------------------------- /include/daw/daw_allocator_construct.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | namespace daw { 18 | template 20 | DAW_ATTRIB_INLINE constexpr auto try_alloc_construct( Allocator &&allocator, 21 | Args &&...args ) { 22 | using alloc_t = typename std::allocator_traits< 23 | Allocator>::template rebind_alloc; 24 | if constexpr( std::is_invocable_v ) { 25 | auto alloc = static_cast( allocator ); 26 | return Constructor{ }( DAW_FWD( args )..., alloc ); 27 | } else if constexpr( std::is_invocable_v ) { 29 | auto alloc = static_cast( allocator ); 30 | return Constructor{ }( std::allocator_arg, alloc, DAW_FWD( args )... ); 31 | } else { 32 | static_assert( std::is_invocable_v, 33 | "Unable to construct value with the supplied arguments" ); 34 | return Constructor{ }( DAW_FWD( args )... ); 35 | } 36 | } 37 | } // namespace daw 38 | -------------------------------------------------------------------------------- /include/daw/daw_tuple_forward.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | namespace daw { 17 | template 18 | using remove_rvalue_ref_t = daw::conditional_t, 19 | daw::remove_cvref_t, T>; 20 | /// Forwards arguments but stores rvalues. This allows use to prevent 21 | /// dangling ref's 22 | template 23 | constexpr auto forward_nonrvalue_as_tuple( Ts &&...values ) { 24 | using tuple_t = std::tuple...>; 25 | return tuple_t{ DAW_FWD( values )... }; 26 | } 27 | } // namespace daw 28 | -------------------------------------------------------------------------------- /include/daw/json/concepts/daw_container_traits.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include "daw_container_traits_fwd.h" 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::json { 23 | inline namespace DAW_JSON_VER { 24 | namespace concepts { 25 | namespace container_detect { 26 | DAW_JSON_MAKE_REQ_TRAIT( 27 | is_container_v, (void)( std::begin( std::declval( ) ) ), 28 | (void)( std::end( std::declval( ) ) ), 29 | (void)( std::declval( ) ), 30 | (void)( std::declval( ).insert( 31 | std::end( std::declval( ) ), 32 | std::declval( ) ) ) ); 33 | } // namespace container_detect 34 | 35 | template 36 | DAW_JSON_REQUIRES( container_detect::is_container_v ) 37 | struct container_traits )> : std::true_type {}; 39 | 40 | template 41 | struct container_traits> : std::true_type {}; 42 | 43 | /// @brief Is the type deduced or specialized as a container 44 | template 45 | inline constexpr bool is_container_v = container_traits::value; 46 | } // namespace concepts 47 | } // namespace DAW_JSON_VER 48 | } // namespace daw::json 49 | -------------------------------------------------------------------------------- /include/daw/json/concepts/daw_container_traits_fwd.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | namespace daw::json { 21 | inline namespace DAW_JSON_VER { 22 | namespace concepts { 23 | /// @brief Concept to help deduce container types. 24 | template 25 | struct container_traits : std::false_type {}; 26 | } // namespace concepts 27 | } // namespace DAW_JSON_VER 28 | } // namespace daw::json 29 | -------------------------------------------------------------------------------- /include/daw/json/concepts/daw_writable_output.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "daw_writable_output_basics.h" 12 | 13 | #if not defined( DAW_JSON_NO_DEFAULT_OSTREAM_WRITER ) 14 | #include "daw_writable_output_ostream.h" 15 | #endif 16 | 17 | #if not defined( DAW_JSON_NO_DEFAULT_CSTDIO_WRITER ) 18 | #include "daw_writable_output_cstdio.h" 19 | #endif 20 | -------------------------------------------------------------------------------- /include/daw/json/concepts/daw_writable_output_cstdio.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include "daw_writable_output_fwd.h" 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | namespace daw::json { 23 | inline namespace DAW_JSON_VER { 24 | namespace concepts { 25 | /// @brief Specialization for FILE * streams 26 | template<> 27 | struct writable_output_trait : std::true_type { 28 | 29 | template 30 | static inline void write( std::FILE *fp, StringViews const &...svs ) { 31 | static_assert( sizeof...( StringViews ) > 0 ); 32 | constexpr auto writer = []( std::FILE *f, 33 | auto sv ) DAW_JSON_CPP23_STATIC_CALL_OP { 34 | if( sv.empty( ) ) { 35 | return 0; 36 | } 37 | auto ret = std::fwrite( sv.data( ), 1, sv.size( ), f ); 38 | daw_json_ensure( ret == sv.size( ), 39 | daw::json::ErrorReason::OutputError ); 40 | return 1; 41 | }; 42 | ( (void)writer( fp, svs ), ... ); 43 | } 44 | 45 | static inline void put( std::FILE *f, char c ) { 46 | auto ret = std::fputc( c, f ); 47 | daw_json_ensure( ret == c, daw::json::ErrorReason::OutputError ); 48 | } 49 | }; 50 | } // namespace concepts 51 | } // namespace DAW_JSON_VER 52 | } // namespace daw::json -------------------------------------------------------------------------------- /include/daw/json/concepts/daw_writable_output_fwd.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include 14 | 15 | namespace daw::json { 16 | inline namespace DAW_JSON_VER { 17 | namespace concepts { 18 | /// @brief Writable output models write/putc methods to allow efficient 19 | /// output to buffers/FILE streams/ostreams/and containers with less 20 | /// allocation/size checks Specializations must have static T write( T, 21 | /// StringViews... ), static T put( T, char ), and static bool value. 22 | /// StringViews work will have a .size( ) and .data( ) member function, 23 | /// and have a character element type 24 | template 25 | struct writable_output_trait : std::false_type {}; 26 | 27 | template 28 | inline constexpr bool is_writable_output_type_v = 29 | writable_output_trait::value; 30 | } // namespace concepts 31 | 32 | template 33 | constexpr WritableOutput &write_output( WritableOutput &out, 34 | StringViews const &...svs ) { 35 | concepts::writable_output_trait::write( out, svs... ); 36 | return out; 37 | } 38 | 39 | template 40 | constexpr WritableOutput &put_output( WritableOutput &out, CharT c ) { 41 | concepts::writable_output_trait::put( out, c ); 42 | return out; 43 | } 44 | } // namespace DAW_JSON_VER 45 | } // namespace daw::json 46 | -------------------------------------------------------------------------------- /include/daw/json/concepts/daw_writable_output_ostream.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include "daw_writable_output_fwd.h" 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | 21 | namespace daw::json { 22 | inline namespace DAW_JSON_VER { 23 | namespace concepts { 24 | /// @brief Specialization for ostream & 25 | template 26 | DAW_JSON_REQUIRES( std::is_base_of_v ) 27 | struct writable_output_trait )> : std::true_type { 29 | 30 | template 31 | static inline void write( std::ostream &os, 32 | StringViews const &...svs ) { 33 | static_assert( sizeof...( StringViews ) > 0 ); 34 | constexpr auto writer = []( std::ostream &o, 35 | auto sv ) DAW_JSON_CPP23_STATIC_CALL_OP { 36 | if( sv.empty( ) ) { 37 | return 0; 38 | } 39 | o.write( sv.data( ), static_cast( sv.size( ) ) ); 40 | daw_json_ensure( static_cast( o ), 41 | daw::json::ErrorReason::OutputError ); 42 | return 1; 43 | }; 44 | ( (void)writer( os, svs ), ... ); 45 | } 46 | 47 | static inline void put( std::ostream &os, char c ) { 48 | os.put( c ); 49 | daw_json_ensure( static_cast( os ), 50 | daw::json::ErrorReason::OutputError ); 51 | } 52 | }; 53 | } // namespace concepts 54 | } // namespace DAW_JSON_VER 55 | } // namespace daw::json 56 | -------------------------------------------------------------------------------- /include/daw/json/concepts/readme.md: -------------------------------------------------------------------------------- 1 | # Deduction concepts 2 | 3 | These files are used in constraints, errors, and type deduction. They provide a way of telling the system that some types are able to fullfill these concepts, via specialization. 4 | 5 | ## Current Concepts 6 | 7 | * Writable Output Types - `writable_output_traits` - A type that can be serialized via write and put. 8 | * Readable Value - `nullable_value_traits` - Option/Nullable/Readable like types that can be constructed, tested for whether they container a value, and read from. -------------------------------------------------------------------------------- /include/daw/json/daw_json_boost_pfr_mapping.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "daw/daw_tuple_forward.h" 12 | #include "daw_json_link.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::json { 23 | /// \brief Enable Boost PFR mappings for a type 24 | /// \tparam T 25 | template 26 | requires( std::is_aggregate_v and std::is_class_v and 27 | not std::is_empty_v ) // 28 | inline constexpr bool use_boost_pfr = false; 29 | 30 | namespace pfr_details { 31 | template 32 | DAW_CONSTEVAL auto get_member_name( ) { 33 | constexpr auto name = boost::pfr::get_name( ); 34 | static_assert( not name.empty( ) ); 35 | return json_name( 36 | name.data( ), std::make_index_sequence{ } ); 37 | } 38 | 39 | template 40 | struct make_data_contract; 41 | 42 | template 43 | struct make_data_contract> { 44 | private: 45 | using member_types = DAW_TYPEOF( 46 | boost::pfr::structure_to_tuple( std::declval( ) ) ); 47 | 48 | public: 49 | using type = 50 | json_member_list( ), 51 | std::tuple_element_t>...>; 52 | 53 | DAW_ATTRIB_INLINE static constexpr auto to_json_data( T const &value ) { 54 | return boost::pfr::structure_tie( value ); 55 | } 56 | }; 57 | } // namespace pfr_details 58 | 59 | template 60 | requires use_boost_pfr // 61 | struct json_data_contract 62 | : pfr_details::make_data_contract< 63 | T, std::make_index_sequence>> {}; 64 | } // namespace daw::json 65 | -------------------------------------------------------------------------------- /include/daw/json/daw_json_data_contract.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "impl/version.h" 12 | 13 | #include 14 | 15 | namespace daw::json { 16 | inline namespace DAW_JSON_VER { 17 | /// @brief This class is used as a way to indicate that a json_data_contract 18 | /// specialization has not been done for a user class. 19 | template 20 | struct missing_json_data_contract_for_or_unknown_type; 21 | 22 | template 23 | inline constexpr bool is_missing_data_contract_or_unknown_type_v = false; 24 | 25 | template<> 26 | inline constexpr bool is_missing_data_contract_or_unknown_type_v = 27 | true; 28 | 29 | template 30 | inline constexpr bool is_missing_data_contract_or_unknown_type_v< 31 | missing_json_data_contract_for_or_unknown_type> = true; 32 | 33 | /// @brief Mapping class for JSON data structures to C++. It must be 34 | /// specialized in order to parse to a user class 35 | /// @tparam T Class to map 36 | /// 37 | template 38 | struct json_data_contract { 39 | using type = missing_json_data_contract_for_or_unknown_type; 40 | }; 41 | 42 | /// @brief This trait gets us the mapping type from the contract. 43 | template 44 | using json_data_contract_trait_t = typename json_data_contract::type; 45 | } // namespace DAW_JSON_VER 46 | } // namespace daw::json 47 | -------------------------------------------------------------------------------- /include/daw/json/daw_json_iostream.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "impl/version.h" 12 | 13 | #include "daw_json_link.h" 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | namespace daw::json { 21 | inline namespace DAW_JSON_VER { 22 | namespace json_details { 23 | DAW_JSON_MAKE_REQ_TYPE_ALIAS_TRAIT( 24 | is_opted_into_json_iostreams_v, 25 | json_data_contract::opt_into_iostreams ); 26 | 27 | template 28 | inline constexpr bool is_container_opted_into_json_iostreams_v = false; 29 | 30 | template 31 | inline constexpr bool is_container_opted_into_json_iostreams_v< 32 | Container, std::void_t> = 33 | is_opted_into_json_iostreams_v; 34 | 35 | } // namespace json_details 36 | } // namespace DAW_JSON_VER 37 | } // namespace daw::json 38 | 39 | /// @brief An opt in ostream interface for types that have JSON mappings. 40 | template )> 42 | DAW_JSON_REQUIRES( daw::json::json_details::is_opted_into_json_iostreams_v ) 43 | std::ostream &operator<<( std::ostream &os, T const &value ) { 44 | return daw::json::to_json( value, os ); 45 | } 46 | 47 | /// @brief An opt in ostream interface for containers of types that have JSON 48 | /// mappings. 49 | template )> 52 | DAW_JSON_REQUIRES( 53 | daw::json::json_details::is_container_opted_into_json_iostreams_v ) 54 | std::ostream & 55 | operator<<( std::ostream &os, Container const &c ) { 56 | return daw::json::to_json_array( c, os ); 57 | } 58 | -------------------------------------------------------------------------------- /include/daw/json/daw_json_link.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, version 1.0. (see accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "impl/version.h" 12 | 13 | #include "daw_from_json.h" 14 | #include "daw_json_apply.h" 15 | #include "daw_json_exception.h" 16 | #include "daw_json_link_types.h" 17 | #include "daw_to_json.h" 18 | #include "impl/daw_json_alternate_mapping.h" 19 | #include "impl/daw_json_value.h" 20 | 21 | namespace daw::json::literals { 22 | /*** 23 | * Construct a json_value from a string literal 24 | * @return A json_value representing the json document 25 | */ 26 | constexpr daw::json::json_value operator""_dawjson( char const *ptr, 27 | std::size_t sz ) { 28 | return daw::json::json_value( ptr, sz ); 29 | } 30 | } // namespace daw::json::literals 31 | 32 | namespace daw::json { 33 | inline namespace DAW_JSON_VER { 34 | DAW_CONSTEVAL std::string_view json_link_version( ) { 35 | #define DAW_XSTR( x ) DAW_STR( x ) 36 | #define DAW_STR( x ) std::string_view( #x ) 37 | return DAW_XSTR( DAW_JSON_VER ); 38 | #undef DAW_STR 39 | #undef DAW_XSTR 40 | } 41 | } // namespace DAW_JSON_VER 42 | } // namespace daw::json 43 | -------------------------------------------------------------------------------- /include/daw/json/daw_json_serialize_options.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "impl/version.h" 12 | 13 | namespace daw::json { 14 | inline namespace DAW_JSON_VER { 15 | namespace options { 16 | /// 17 | /// @brief Serialization options used in to_json 18 | /// 19 | inline namespace serialize_options { 20 | /// 21 | /// @brief Choose whether to produce minified or pretty output 22 | /// 23 | /// default: Minified 24 | /// 25 | enum class SerializationFormat : unsigned { 26 | /* Output JSON without whitespace surrounding members and values */ 27 | Minified, 28 | /* Output JSON with indentation and newlines */ 29 | Pretty 30 | }; // 1bit 31 | 32 | /// 33 | ///@brief When Pretty mode is enabled, choose the indentation type. 34 | /// 35 | /// default: Space2 36 | /// 37 | enum class IndentationType : unsigned { 38 | /* Use tabs for indentation */ 39 | Tab, 40 | /* Use 1 space for indentation */ 41 | Space1, 42 | /* Use 2 spaces for indentation */ 43 | Space2, 44 | /* Use 3 spaces for indentation */ 45 | Space3, 46 | /* Use 4 spaces for indentation */ 47 | Space4, 48 | /* Use 5 spaces for indentation */ 49 | Space5, 50 | /* Use 8 spaces for indentation */ 51 | Space8, 52 | /* Use 10 spaces for indentation */ 53 | Space10, 54 | }; 55 | 56 | /// 57 | /// @brief Allow for restricting the output of strings to 7bits 58 | /// 59 | /// default: None 60 | /// 61 | enum class RestrictedStringOutput : unsigned { 62 | /* Do not impose any extra restrictions on string output during 63 | serialization */ 64 | None, 65 | /// When invalid UTF8 is encountered, throw an error 66 | ErrorInvalidUTF8, 67 | /* Restrict all string member values and all member names to 7bits. 68 | This will result in escaping all values >= 0x7F. This can affect 69 | round trips where the name contains high bits set*/ 70 | OnlyAllow7bitsStrings 71 | }; 72 | 73 | /// 74 | /// @brief When Pretty mode is enabled, set how newlines are expressed 75 | /// 76 | /// default: n 77 | /// 78 | enum class NewLineDelimiter : unsigned { n, rn }; 79 | 80 | /// @brief When outputting arrays, followitem with a comma 81 | /// 82 | /// default: No 83 | /// 84 | enum class OutputTrailingComma : unsigned { No, Yes }; 85 | } // namespace serialize_options 86 | } // namespace options 87 | } // namespace DAW_JSON_VER 88 | } // namespace daw::json 89 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_fp_fallback.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_assert.h" 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #if not defined( DAW_JSON_USE_STRTOD ) 24 | #include 25 | #endif 26 | 27 | namespace daw::json { 28 | inline namespace DAW_JSON_VER { 29 | namespace json_details { 30 | /// @brief When normal FP parsing cannot handle the value, this method 31 | /// used as a fallback for std floating point types. For others, it 32 | /// provides either a customization point or will call the overload found 33 | /// via ADL 34 | template 35 | DAW_ATTRIB_NOINLINE DAW_ATTRIB_NONNULL( ) [[nodiscard]] Real 36 | parse_with_strtod( char const *first, char const *last ) { 37 | static_assert( std::is_floating_point_v, 38 | "Unexpected type passed to parse_with_strtod" ); 39 | #if defined( DAW_JSON_USE_STRTOD ) 40 | (void)last; 41 | char **end = nullptr; 42 | if constexpr( std::is_same_v ) { 43 | return static_cast( std::strtof( first, end ) ); 44 | } else if( std::is_same_v ) { 45 | return static_cast( std::strtod( first, end ) ); 46 | } else { 47 | return static_cast( std::strtold( first, end ) ); 48 | } 49 | #else 50 | Real result; 51 | auto fc_res = std::from_chars( first, last, result ); 52 | if( fc_res.ec == std::errc::result_out_of_range ) { 53 | if( *first == '-' ) { 54 | return -std::numeric_limits::infinity( ); 55 | } 56 | return std::numeric_limits::infinity( ); 57 | } 58 | daw_json_ensure( fc_res.ec == std::errc( ), 59 | ErrorReason::InvalidNumber ); 60 | return result; 61 | #endif 62 | } 63 | } // namespace json_details 64 | } // namespace DAW_JSON_VER 65 | } // namespace daw::json -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_alternate_mapping.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_parse_common.h" 14 | #include "daw_json_traits.h" 15 | 16 | #include 17 | 18 | namespace daw::json { 19 | inline namespace DAW_JSON_VER { 20 | /// 21 | /// @brief Allows for alternate/multiple mappings of types that already have 22 | /// mappings. 23 | ///@tparam C Base type to map to 24 | ///@tparam Idx alternate mapping index 25 | /// 26 | template 27 | struct json_alt { 28 | using type = C; 29 | }; 30 | 31 | /// 32 | /// @brief Default constructor for json_alt is to construct the base type 33 | /// @tparam T base type to construct 34 | /// @tparam I index of alternate mapping 35 | /// 36 | template 37 | struct default_constructor> 38 | : default_constructor< 39 | json_details::json_result_t>> {}; 40 | 41 | } // namespace DAW_JSON_VER 42 | } // namespace daw::json 43 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_arrow_proxy.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | #pragma once 9 | 10 | #include "version.h" 11 | 12 | #include 13 | 14 | namespace daw::json { 15 | inline namespace DAW_JSON_VER { 16 | namespace json_details { 17 | template 18 | struct arrow_proxy { 19 | T value; 20 | 21 | [[nodiscard]] DAW_ATTRIB_RET_NONNULL constexpr T *operator->( ) && { 22 | return &value; 23 | } 24 | }; 25 | } // namespace json_details 26 | } // namespace DAW_JSON_VER 27 | } // namespace daw::json 28 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_exec_modes.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include 14 | 15 | namespace daw::json { 16 | inline namespace DAW_JSON_VER { 17 | struct constexpr_exec_tag { 18 | static constexpr std::string_view name = "constexpr"; 19 | static constexpr bool can_constexpr = true; 20 | }; 21 | struct runtime_exec_tag : constexpr_exec_tag { 22 | static constexpr std::string_view name = "runtime"; 23 | static constexpr bool can_constexpr = false; 24 | }; 25 | #if defined( DAW_ALLOW_SSE42 ) 26 | struct sse42_exec_tag : runtime_exec_tag { 27 | static constexpr std::string_view name = "sse4.2"; 28 | static constexpr bool can_constexpr = false; 29 | }; 30 | using simd_exec_tag = sse42_exec_tag; 31 | #else 32 | struct simd_exec_tag : runtime_exec_tag {}; 33 | #endif 34 | using default_exec_tag = constexpr_exec_tag; 35 | } // namespace DAW_JSON_VER 36 | } // namespace daw::json 37 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_find_result.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | namespace daw::json { 14 | inline namespace DAW_JSON_VER { 15 | namespace json_details { 16 | template 17 | struct find_result { 18 | ParseState location; 19 | bool known; 20 | }; 21 | } // namespace json_details 22 | } // namespace DAW_JSON_VER 23 | } // namespace daw::json 24 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_link_types_aggregate.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_enums.h" 14 | #include "daw_json_traits.h" 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | namespace daw::json { 24 | inline namespace DAW_JSON_VER { 25 | namespace json_details { 26 | DAW_JSON_MAKE_REQ_TRAIT( has_to_tuple_v, 27 | to_tuple( std::declval( ) ) ); 28 | 29 | template )> 30 | DAW_JSON_REQUIRES( has_to_tuple_v ) 31 | constexpr auto to_tuple_impl( T const &value ) { 32 | return to_tuple( value ); 33 | } 34 | 35 | DAW_JSON_MAKE_REQ_TRAIT( can_convert_to_tuple_v, 36 | to_tuple_impl( std::declval( ) ) ); 37 | 38 | template 39 | using tp_from_struct_binding_result_t = 40 | daw::remove_cvref_t( ) ) )>; 41 | } // namespace json_details 42 | } // namespace DAW_JSON_VER 43 | } // namespace daw::json 44 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_link_types_iso8601.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_parse_iso8601_utils.h" 14 | 15 | #include 16 | 17 | #include 18 | 19 | namespace daw::json { 20 | inline namespace DAW_JSON_VER { 21 | template 22 | struct construct_from_iso8601_timestamp; 23 | 24 | template 25 | struct construct_from_iso8601_timestamp< 26 | std::chrono::time_point> { 27 | using result_type = std::chrono::time_point; 28 | #if defined( DAW_JSON_HAS_STATIC_CALL_OP ) and DAW_HAS_CLANG_VER_GTE( 17, 0 ) 29 | #pragma clang diagnostic push 30 | #pragma clang diagnostic ignored "-Wc++23-extensions" 31 | #endif 32 | [[nodiscard]] DAW_ATTRIB_NONNULL( ) DAW_JSON_CPP23_STATIC_CALL_OP 33 | inline constexpr result_type 34 | operator( )( char const *ptr, 35 | std::size_t sz ) DAW_JSON_CPP23_STATIC_CALL_OP_CONST { 36 | return datetime::parse_iso8601_timestamp( 37 | daw::string_view( ptr, sz ) ); 38 | } 39 | #if defined( DAW_JSON_HAS_STATIC_CALL_OP ) and DAW_HAS_CLANG_VER_GTE( 17, 0 ) 40 | #pragma clang diagnostic pop 41 | #endif 42 | }; 43 | } // namespace DAW_JSON_VER 44 | } // namespace daw::json 45 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_parse_digit.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include 14 | 15 | namespace daw::json { 16 | inline namespace DAW_JSON_VER { 17 | namespace json_details { 18 | DAW_ATTRIB_FLATINLINE [[nodiscard]] static inline constexpr unsigned 19 | parse_digit( char c ) { 20 | return static_cast( 21 | static_cast( static_cast( c ) - 22 | static_cast( '0' ) ) ); 23 | } 24 | 25 | namespace parsed_constants { 26 | static inline constexpr unsigned decimal_char = parse_digit( '.' ); 27 | static inline constexpr unsigned e_char = parse_digit( 'e' ); 28 | static inline constexpr unsigned E_char = parse_digit( 'E' ); 29 | static inline constexpr unsigned plus_char = parse_digit( '+' ); 30 | static inline constexpr unsigned minus_char = parse_digit( '-' ); 31 | } // namespace parsed_constants 32 | } // namespace json_details 33 | } // namespace DAW_JSON_VER 34 | } // namespace daw::json 35 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_parse_string_need_slow.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include 14 | 15 | namespace daw::json { 16 | inline namespace DAW_JSON_VER { 17 | namespace json_details { 18 | /*** 19 | * When skip_string encounters escaping, it will note this as a non-zero 20 | * value 21 | */ 22 | template 23 | DAW_ATTRIB_INLINE constexpr bool 24 | needs_slow_path( ParseState const &parse_state ) { 25 | return static_cast( parse_state.counter ) >= 0; 26 | } 27 | } // namespace json_details 28 | } // namespace DAW_JSON_VER 29 | } // namespace daw::json 30 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_parse_value_fwd.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_parse_common.h" 14 | 15 | namespace daw::json { 16 | inline namespace DAW_JSON_VER { 17 | namespace json_details { 18 | template 20 | [[nodiscard]] DAW_ATTRIB_INLINE static constexpr json_result_t 21 | parse_value( ParseState &parse_state ); 22 | } // namespace json_details 23 | } // namespace DAW_JSON_VER 24 | } // namespace daw::json 25 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_serialize_options_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_option_bits.h" 14 | #include 15 | 16 | namespace daw::json { 17 | inline namespace DAW_JSON_VER { 18 | namespace json_details { 19 | template 20 | inline constexpr bool is_output_option_v = false; 21 | 22 | template<> 23 | inline constexpr bool is_output_option_v = 24 | true; 25 | 26 | template<> 27 | inline constexpr unsigned 28 | json_option_bits_width = 1; 29 | 30 | template<> 31 | inline constexpr auto 32 | default_json_option_value = 33 | options::SerializationFormat::Minified; 34 | 35 | template<> 36 | inline constexpr bool is_output_option_v = true; 37 | 38 | template<> 39 | inline constexpr unsigned 40 | json_option_bits_width = 3; 41 | 42 | template<> 43 | inline constexpr auto 44 | default_json_option_value = 45 | options::IndentationType::Space2; 46 | 47 | template<> 48 | inline constexpr bool 49 | is_output_option_v = true; 50 | 51 | template<> 52 | inline constexpr unsigned 53 | json_option_bits_width = 2; 54 | 55 | template<> 56 | inline constexpr auto 57 | default_json_option_value = 58 | options::RestrictedStringOutput::ErrorInvalidUTF8; 59 | 60 | template<> 61 | inline constexpr bool is_output_option_v = 62 | true; 63 | 64 | template<> 65 | inline constexpr unsigned 66 | json_option_bits_width = 1; 67 | 68 | template<> 69 | inline constexpr auto 70 | default_json_option_value = 71 | options::NewLineDelimiter::n; 72 | 73 | template<> 74 | inline constexpr bool is_output_option_v = 75 | true; 76 | 77 | template<> 78 | inline constexpr unsigned 79 | json_option_bits_width = 1; 80 | 81 | template<> 82 | inline constexpr auto 83 | default_json_option_value = 84 | options::OutputTrailingComma::No; 85 | } // namespace json_details 86 | } // namespace DAW_JSON_VER 87 | } // namespace daw::json 88 | -------------------------------------------------------------------------------- /include/daw/json/impl/daw_json_value_fwd.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "version.h" 12 | 13 | #include "daw_json_allocator_wrapper.h" 14 | #include "daw_json_option_bits.h" 15 | #include "daw_json_parse_options_impl.h" 16 | #include "daw_json_parse_policy.h" 17 | 18 | namespace daw::json { 19 | inline namespace DAW_JSON_VER { 20 | /// @brief A container for arbitrary JSON values 21 | /// @tparam ParseState see IteratorRange 22 | template 24 | struct basic_json_value; 25 | 26 | /// @brief An untyped JSON value 27 | using json_value = basic_json_value<>; 28 | 29 | template 30 | inline constexpr bool is_a_basic_json_value = false; 31 | 32 | template 33 | inline constexpr bool 34 | is_a_basic_json_value> = true; 35 | } // namespace DAW_JSON_VER 36 | } // namespace daw::json 37 | -------------------------------------------------------------------------------- /include/daw/json/impl/version.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include 14 | 15 | /// The version string used in namespace definitions. Must be a valid namespace 16 | /// name. 17 | #if not defined( DAW_JSON_VER_OVERRIDE ) 18 | // Should be updated when a potential ABI break is anticipated 19 | #if defined( DEBUG ) or not defined( NDEBUG ) 20 | #define DAW_JSON_VER v3_30_2d 21 | #else 22 | #define DAW_JSON_VER v3_30_2 23 | #endif 24 | #else 25 | #define DAW_JSON_VER DAW_JSON_VER_OVERRIDE 26 | #endif 27 | -------------------------------------------------------------------------------- /tests/cmake/git_properties.cmake: -------------------------------------------------------------------------------- 1 | if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../.git) 2 | find_package(Git REQUIRED) 3 | if(GIT_FOUND) 4 | execute_process( 5 | COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD 6 | WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" 7 | OUTPUT_VARIABLE "BUILD_VERSION" 8 | ERROR_QUIET 9 | OUTPUT_STRIP_TRAILING_WHITESPACE) 10 | message( STATUS "GIT TAG: ${BUILD_VERSION}" ) 11 | else(GIT_FOUND) 12 | set(BUILD_VERSION "unknown") 13 | message( WARNING "GIT TAG: Could not find git tag" ) 14 | endif(GIT_FOUND) 15 | endif( ) 16 | 17 | 18 | -------------------------------------------------------------------------------- /tests/extern/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Darrell Wright 2 | # 3 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | # file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | # 6 | # Official repository: https://github.com/beached/daw_json_link 7 | # 8 | 9 | # This prevents tests running on dependencies 10 | option( DAW_ENABLE_TESTING "Build unit tests" OFF ) 11 | if( DAW_ENABLE_TESTING ) 12 | set( DAW_ENABLE_TESTING OFF ) 13 | endif( ) 14 | 15 | #include( FetchContent ) 16 | #set( FETCHCONTENT_UPDATES_DISCONNECTED ON ) 17 | 18 | #FetchContent_Declare( 19 | # fmt 20 | # GIT_REPOSITORY https://github.com/fmtlib/fmt.git 21 | # GIT_TAG 10.0.0 22 | #) 23 | 24 | #FetchContent_MakeAvailable(fmt) 25 | #get_target_property(FMTLIB_IID fmt INTERFACE_INCLUDE_DIRECTORIES) 26 | #set_target_properties(fmt PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${FMTLIB_IID}") 27 | 28 | 29 | -------------------------------------------------------------------------------- /tests/include/apache_builds.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace apache_builds { 18 | struct jobs_t { 19 | std::string_view name; 20 | std::string_view url; 21 | std::string_view color; 22 | }; 23 | 24 | struct views_t { 25 | std::string_view name; 26 | std::string_view url; 27 | }; 28 | 29 | struct apache_builds { 30 | std::string_view mode; 31 | std::string_view nodeDescription; 32 | std::string_view nodeName; 33 | int64_t numExecutors; 34 | std::string_view description; 35 | std::vector jobs; 36 | views_t primaryView; 37 | bool quietingDown; 38 | int64_t slaveAgentPort; 39 | bool useCrumbs; 40 | bool useSecurity; 41 | std::vector views; 42 | }; 43 | } // namespace apache_builds 44 | -------------------------------------------------------------------------------- /tests/include/citm_test.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace daw::citm { 20 | struct events_value_t { 21 | std::int64_t id; 22 | std::optional logo; 23 | std::string_view name; 24 | std::vector subTopicIds; 25 | std::vector topicIds; 26 | }; // events_value_t 27 | 28 | struct prices_element_t { 29 | std::int64_t amount; 30 | std::int64_t audienceSubCategoryId; 31 | std::int64_t seatCategoryId; 32 | }; // prices_element_t 33 | 34 | struct areas_element_t { 35 | std::int64_t areaId; 36 | }; // areas_element_t 37 | 38 | struct seatCategories_element_t { 39 | std::vector areas; 40 | std::int64_t seatCategoryId; 41 | }; // seatCategories_element_t 42 | 43 | struct performances_element_t { 44 | std::int64_t eventId; 45 | std::int64_t id; 46 | std::optional logo; 47 | std::vector prices; 48 | std::vector seatCategories; 49 | std::int64_t start; 50 | std::string_view venueCode; 51 | }; // performances_element_t 52 | 53 | struct venueNames_t { 54 | std::string_view pleyel_pleyel; 55 | }; // venueNames_t 56 | 57 | struct citm_object_t { 58 | std::unordered_map areaNames; 59 | std::unordered_map audienceSubCategoryNames; 60 | std::unordered_map events; 61 | std::vector performances; 62 | std::unordered_map seatCategoryNames; 63 | std::unordered_map subTopicNames; 64 | std::unordered_map topicNames; 65 | std::unordered_map> 66 | topicSubTopics; 67 | std::optional venueNames; 68 | }; // citm_object_t 69 | 70 | citm_object_t parse_citm( std::string_view json_doc ); 71 | } // namespace daw::citm 72 | -------------------------------------------------------------------------------- /tests/include/citm_test_alloc.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "fixed_alloc.h" 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace daw::citm { 20 | template 21 | using Vector = std::vector>; 22 | template 23 | using Map = std::unordered_map, std::equal_to, 24 | fixed_allocator>>; 25 | 26 | struct events_value_t { 27 | std::int64_t id; 28 | std::optional logo; 29 | std::string_view name; 30 | Vector subTopicIds; 31 | Vector topicIds; 32 | }; // events_value_t 33 | 34 | struct prices_element_t { 35 | std::int64_t amount; 36 | std::int64_t audienceSubCategoryId; 37 | std::int64_t seatCategoryId; 38 | }; // prices_element_t 39 | 40 | struct areas_element_t { 41 | std::int64_t areaId; 42 | }; // areas_element_t 43 | 44 | struct seatCategories_element_t { 45 | Vector areas; 46 | std::int64_t seatCategoryId; 47 | }; // seatCategories_element_t 48 | 49 | struct performances_element_t { 50 | std::int64_t eventId; 51 | std::int64_t id; 52 | std::optional logo; 53 | Vector prices; 54 | Vector seatCategories; 55 | std::int64_t start; 56 | std::string_view venueCode; 57 | }; // performances_element_t 58 | 59 | struct venueNames_t { 60 | std::string_view pleyel_pleyel; 61 | }; // venueNames_t 62 | 63 | struct citm_object_t { 64 | Map areaNames; 65 | Map audienceSubCategoryNames; 66 | Map events; 67 | Vector performances; 68 | Map seatCategoryNames; 69 | Map subTopicNames; 70 | Map topicNames; 71 | Map> topicSubTopics; 72 | std::optional venueNames; 73 | }; // citm_object_t 74 | } // namespace daw::citm 75 | -------------------------------------------------------------------------------- /tests/include/defines.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "daw/json/daw_json_exception.h" 12 | #include "daw/json/daw_json_find_path.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | /*** 19 | * If requested to disable or MSVC, there is a bug in MSVC in C++20 mode 20 | */ 21 | #if defined( DAW_JSON_NO_CONST_EXPR ) or \ 22 | ( defined( _MSC_VER ) and not defined( __clang__ ) and \ 23 | defined( DAW_CXX_STANDARD ) and DAW_CXX_STANDARD == 20 ) 24 | #define DAW_CONSTEXPR 25 | #else 26 | #define DAW_CONSTEXPR constexpr 27 | #endif 28 | 29 | template 30 | DAW_ATTRIB_NOINLINE void daw_ensure_error( StringView &&msg ) { 31 | std::cerr << msg << std::endl << std::flush; 32 | std::terminate( ); 33 | } 34 | 35 | #define ensure( Bool ) \ 36 | if( DAW_UNLIKELY( not( Bool ) ) ) { \ 37 | daw_ensure_error( "Error in assertion: " #Bool ); \ 38 | } \ 39 | while( false ) 40 | 41 | #define test_assert( Bool, Msg ) \ 42 | if( DAW_UNLIKELY( not( Bool ) ) ) { \ 43 | DAW_UNLIKELY_BRANCH \ 44 | daw_ensure_error( Msg ); \ 45 | } \ 46 | while( false ) 47 | 48 | DAW_ATTRIB_NOINLINE inline void 49 | display_exception( daw::json::json_exception const &jex, 50 | char const *json_data ) { 51 | std::cerr << "Exception thrown by parser: " 52 | << to_formatted_string( jex, json_data ); 53 | if( jex.parse_location( ) ) { 54 | auto path_stack = find_json_path_stack_to( jex, json_data ); 55 | if( not path_stack.empty( ) ) { 56 | std::cerr << "The error is near line " 57 | << ( 1 + daw::json::find_line_number_of( jex.parse_location( ), 58 | json_data ) ) 59 | << " column " 60 | << ( 1 + daw::json::find_column_number_of( 61 | jex.parse_location( ), json_data ) ) 62 | << '\n'; 63 | std::cerr << "JSON Path to value close to error '" 64 | << to_json_path_string( path_stack ) << "'\n"; 65 | std::cerr << std::flush; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/include/geojson.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "defines.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::geojson { 23 | struct Property { 24 | std::string_view name; 25 | }; // Property 26 | 27 | struct Point { 28 | double x; 29 | double y; 30 | }; 31 | 32 | struct Polygon { 33 | std::string_view type; 34 | std::vector> coordinates; 35 | 36 | Polygon( std::string_view t, std::vector> &&coords ) 37 | : type( t ) 38 | , coordinates( std::move( coords ) ) {} 39 | }; // Polygon 40 | 41 | struct Feature { 42 | std::string_view type; 43 | Property properties; 44 | Polygon geometry; 45 | }; // Feature 46 | 47 | struct FeatureCollection { 48 | std::string_view type; 49 | std::vector features; 50 | }; // FeatureCollection 51 | } // namespace daw::geojson 52 | -------------------------------------------------------------------------------- /tests/include/island.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link/ 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | /* 16 | * { 17 | "obj/isBeach/archives/xgGroundCover_archiveRock0002_geo.obj": { 18 | "xgPrim13021986_xgGroundCover": [ 19 | -0.027957417791725492, 20 | -0.00010995744572427291, 21 | 0.008245142748421388, 22 | 0.0, 23 | 0.0004173780477003665, 24 | 0.02908927727233923, 25 | 0.0018031701253846634, 26 | ]*/ 27 | namespace daw::island { 28 | struct point_t { 29 | double x; 30 | double y; 31 | double z; 32 | double w; 33 | }; 34 | 35 | template 36 | struct IteratorPair { 37 | First first; 38 | Last last; 39 | }; 40 | 41 | struct obj_t { 42 | std::string_view name, point_t vertices[4]; 43 | 44 | template 45 | inline constexpr obj_t( std::string_view n, 46 | IteratorPairpoint_t points ) 47 | : name( n ) 48 | , vertices { 49 | { *( points.first++ ), *( points.first++ ), *( points.first++ ), 50 | *( points.first++ ) }, 51 | { *( points.first++ ), *( points.first++ ), *( points.first++ ), 52 | *( points.first++ ) }, 53 | { *( points.first++ ), *( points.first++ ), *( points.first++ ), 54 | *( points.first++ ) }, 55 | { 56 | *( points.first++ ), *( points.first++ ), *( points.first++ ), 57 | *( points.first++ ) 58 | } 59 | } 60 | } 61 | }; 62 | 63 | struct object_t { 64 | std::string object_name; 65 | std::vector objs; 66 | }; 67 | 68 | struct island_t { 69 | std::vector objects; 70 | }; 71 | } // namespace daw::island 72 | 73 | namespace daw::json { 74 | template<> 75 | daw::json::json_data_contract{ using type = }; 76 | } 77 | -------------------------------------------------------------------------------- /tests/include/tweet/tweet.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "twitter_user.h" 12 | 13 | #include 14 | #include 15 | 16 | namespace daw::twitter { 17 | struct tweet { 18 | std::uint32_t retweet_count; 19 | twitter_user user; 20 | std::string_view created_at; 21 | std::uint64_t in_reply_to_status_id; 22 | std::uint32_t favorite_count; 23 | std::uint64_t id; 24 | std::string_view text; 25 | }; 26 | } // namespace daw::twitter 27 | -------------------------------------------------------------------------------- /tests/include/tweet/twitter_timeline.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include "tweet.h" 12 | #include "twitter_user.h" 13 | 14 | #include "daw/json/daw_json_link.h" 15 | 16 | namespace daw::json { 17 | template<> 18 | struct json_data_contract { 19 | static constexpr char id[] = "id"; 20 | static constexpr char screen_name[] = "screen_name"; 21 | 22 | using type = 23 | json_member_list, 24 | json_number>; 25 | }; 26 | 27 | template<> 28 | struct json_data_contract { 29 | static constexpr char id[] = "id"; 30 | static constexpr char text[] = "text"; 31 | static constexpr char created_at[] = "created_at"; 32 | static constexpr char in_reply_to_status_id[] = "in_reply_to_status_id"; 33 | static constexpr char retweet_count[] = "retweet_count"; 34 | static constexpr char favorite_count[] = "favorite_count"; 35 | static constexpr char user[] = "user"; 36 | using type = 37 | json_member_list, 38 | json_class, 39 | json_string_raw, 40 | json_number_null, 41 | json_number_null, 42 | json_number, 43 | json_string_raw>; 44 | }; 45 | } // namespace daw::json 46 | -------------------------------------------------------------------------------- /tests/include/tweet/twitter_user.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | 14 | namespace daw::twitter { 15 | struct twitter_user { 16 | std::string_view screen_name; 17 | std::uint64_t id; 18 | }; 19 | } // namespace daw::twitter 20 | -------------------------------------------------------------------------------- /tests/src/canada_test_basic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // This test will test the parsing canada_test.json file 9 | // in the test_data folder. This is one of the many common JSON parsing 10 | // benchmark files and was included in the nativejson benchmark and this file 11 | // provides a cleaner way to profile without the benchmarking harness 12 | // 13 | 14 | #include "geojson_json.h" 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | 24 | int main( int argc, char **argv ) 25 | #if defined( DAW_USE_EXCEPTIONS ) 26 | try 27 | #endif 28 | { 29 | if( argc < 2 ) { 30 | puts( "Must supply a file name\n" ); 31 | exit( 1 ); 32 | } 33 | using namespace daw::json; 34 | auto json_data = std::string( daw::read_file( argv[1] ).value( ) ); 35 | #if defined( DAW_USE_EXCEPTIONS ) 36 | try { 37 | #endif 38 | for( int n = 0; n < 100; ++n ) { 39 | auto const canada_result = daw::json::from_json( 40 | json_data, "features[0].geometry" ); 41 | daw::do_not_optimize( canada_result ); 42 | 43 | auto new_json_result = std::string( ); 44 | new_json_result.resize( ( json_data.size( ) * 15U ) / 10U ); 45 | auto *last = daw::json::to_json( canada_result, new_json_result.data( ) ); 46 | (void)last; 47 | // new_json_result.resize( std::distance( new_json_result.data( ), last ) 48 | // ); 49 | daw::do_not_optimize( canada_result ); 50 | } 51 | #if defined( DAW_USE_EXCEPTIONS ) 52 | } catch( daw::json::json_exception const &jex ) { 53 | display_exception( jex, json_data.data( ) ); 54 | exit( 1 ); 55 | } 56 | #endif 57 | } 58 | #if defined( DAW_USE_EXCEPTIONS ) 59 | catch( std::exception const &ex ) { 60 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 61 | << '\n'; 62 | exit( 1 ); 63 | } catch( ... ) { 64 | std::cerr << "Unknown exception thrown during testing\n"; 65 | throw; 66 | } 67 | #endif -------------------------------------------------------------------------------- /tests/src/carray_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | struct Foo { 16 | int data[5]; 17 | }; 18 | 19 | struct FooConstructor { 20 | constexpr Foo operator( )( std::array const &ary ) const { 21 | return Foo{ { ary[0], ary[1], ary[2], ary[3], ary[4] } }; 22 | } 23 | }; 24 | 25 | namespace daw::json { 26 | template<> 27 | struct json_data_contract { 28 | using constructor_t = FooConstructor; 29 | static constexpr char const data[] = "data"; 30 | using type = json_member_list>>; 31 | 32 | static constexpr auto to_json_data( Foo const &f ) { 33 | return daw::forward_nonrvalue_as_tuple( 34 | std::array{ f.data[0], f.data[1], f.data[2], f.data[3], f.data[4] } ); 35 | } 36 | }; 37 | } // namespace daw::json 38 | 39 | template 40 | void display_all( Container const &values ) { 41 | for( auto const &v : values ) { 42 | std::cout << v << ' '; 43 | } 44 | std::cout << '\n'; 45 | } 46 | 47 | int main( ) { 48 | std::string_view json_doc = R"json( 49 | { 50 | "data": [1,2,3,4,5] 51 | } 52 | )json"; 53 | auto f = daw::json::from_json( json_doc ); 54 | display_all( f.data ); 55 | daw::json::to_json( f, std::cout ); 56 | } 57 | -------------------------------------------------------------------------------- /tests/src/citm_test_basic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // This test will test the parsing citm_catalog.json file 9 | // in the test_data folder. This is one of the many common JSON parsing 10 | // benchmark files and was included in the nativejson benchmark and this file 11 | // provides a cleaner way to profile without the benchmarking harness 12 | // 13 | 14 | #include "defines.h" 15 | 16 | #include "citm_test_json.h" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | int main( int argc, char **argv ) 28 | #if defined( DAW_USE_EXCEPTIONS ) 29 | try 30 | #endif 31 | { 32 | if( argc < 2 ) { 33 | std::cerr << "Must supply a filenames to open\n"; 34 | exit( 1 ); 35 | } 36 | using namespace daw::json; 37 | auto const json_data1 = daw::read_file( argv[1] ); 38 | if( not json_data1 ) { 39 | std::cerr << "Could find valid JSON in file '" << argv[1] << "'\n"; 40 | } 41 | 42 | auto citm_result = 43 | daw::json::from_json( *json_data1 ); 44 | daw::do_not_optimize( citm_result ); 45 | test_assert( not citm_result.areaNames.empty( ), "Expected values" ); 46 | test_assert( citm_result.areaNames.count( 205706005 ) == 1, 47 | "Expected value" ); 48 | test_assert( citm_result.areaNames[205706005] == "1er balcon jardin", 49 | "Incorrect value" ); 50 | } 51 | #if defined( DAW_USE_EXCEPTIONS ) 52 | catch( daw::json::json_exception const &jex ) { 53 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 54 | exit( 1 ); 55 | } catch( std::exception const &ex ) { 56 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 57 | << '\n'; 58 | exit( 1 ); 59 | } catch( ... ) { 60 | std::cerr << "Unknown exception thrown during testing\n"; 61 | throw; 62 | } 63 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_aliases1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | /*** 10 | * This example shows mapping types like json_string to user classes 11 | */ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | struct MyClass { 21 | std::string value; 22 | }; 23 | 24 | namespace daw::json { 25 | template<> 26 | struct json_data_contract { 27 | using type = json_type_alias; 28 | 29 | static inline auto to_json_data( MyClass const &v ) { 30 | return v.value; 31 | } 32 | }; 33 | } // namespace daw::json 34 | 35 | int main( int argc, char **argv ) 36 | #if defined( DAW_USE_EXCEPTIONS ) 37 | try 38 | #endif 39 | { 40 | if( argc <= 1 ) { 41 | puts( "Must supply path to cookbook_aliases1.json file\n" ); 42 | exit( EXIT_FAILURE ); 43 | } 44 | auto const data = daw::read_file( argv[1] ); 45 | assert( data and data->size( ) > 0 ); 46 | 47 | MyClass c0 = daw::json::from_json( *data ); 48 | std::string json0 = daw::json::to_json( c0 ); 49 | std::cout << json0 << '\n'; 50 | (void)c0; 51 | } 52 | #if defined( DAW_USE_EXCEPTIONS ) 53 | catch( daw::json::json_exception const &jex ) { 54 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 55 | exit( 1 ); 56 | } catch( std::exception const &ex ) { 57 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 58 | << '\n'; 59 | exit( 1 ); 60 | } catch( ... ) { 61 | std::cerr << "Unknown exception thrown during testing\n"; 62 | throw; 63 | } 64 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_array1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // See cookbook/array.md for the 1st example 9 | // 10 | 11 | #include "defines.h" 12 | 13 | #include "daw/json/daw_json_link.h" 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | int main( int argc, char **argv ) 25 | #if defined( DAW_USE_EXCEPTIONS ) 26 | try 27 | #endif 28 | { 29 | if( argc <= 1 ) { 30 | puts( "Must supply path to cookbook_array1.json file\n" ); 31 | exit( EXIT_FAILURE ); 32 | } 33 | auto const data = daw::read_file( argv[1] ).value( ); 34 | 35 | using namespace daw::json; 36 | 37 | std::vector const ve = from_json_array( data ); 38 | 39 | int count = 1; 40 | (void)count; 41 | for( auto val : ve ) { 42 | (void)val; 43 | test_assert( count++ == val, "Unexpected value" ); 44 | } 45 | } 46 | #if defined( DAW_USE_EXCEPTIONS ) 47 | catch( daw::json::json_exception const &jex ) { 48 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 49 | exit( 1 ); 50 | } catch( std::exception const &ex ) { 51 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 52 | << '\n'; 53 | exit( 1 ); 54 | } catch( ... ) { 55 | std::cerr << "Unknown exception thrown during testing\n"; 56 | throw; 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /tests/src/cookbook_class_alternate1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | struct Thing { 16 | int a; 17 | }; 18 | 19 | namespace daw::json { 20 | template<> 21 | struct json_data_contract { 22 | static constexpr char const a[] = "a"; 23 | using type = json_member_list>; 24 | 25 | static constexpr auto to_json_data( Thing const &thing ) { 26 | return std::forward_as_tuple( thing.a ); 27 | } 28 | }; 29 | 30 | template<> 31 | struct json_data_contract> { 32 | static constexpr char const a[] = "a"; 33 | using type = json_tuple_member_list; 34 | 35 | static constexpr auto to_json_data( Thing const &thing ) { 36 | return std::forward_as_tuple( thing.a ); 37 | } 38 | }; 39 | } // namespace daw::json 40 | 41 | int main( ) { 42 | using namespace daw::json; 43 | std::string_view const jsonA = R"({ "a": 42 } )"; 44 | std::string_view const jsonB = R"([ 42 ] )"; 45 | Thing a = from_json( jsonA ); 46 | Thing b = from_json>>( jsonB ); 47 | if( a.a != b.a ) { 48 | std::cerr << "Error parsing\n"; 49 | std::terminate( ); 50 | } 51 | auto const js = to_json>( b ); 52 | if( js != "[42]" ) { 53 | std::cerr << "Round-trip failure\n"; 54 | std::terminate( ); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/src/cookbook_class_from_array1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // See cookbook/class_from_array.md for the 1st example 9 | // 10 | 11 | #include "defines.h" 12 | 13 | #include 14 | 15 | #include "daw/json/daw_json_link.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace daw::cookbook_class_from_array1 { 25 | struct Point { 26 | double x; 27 | double y; 28 | }; 29 | 30 | bool operator!=( Point const &lhs, Point const &rhs ) { 31 | return lhs.x != rhs.x and lhs.y != rhs.y; 32 | } 33 | } // namespace daw::cookbook_class_from_array1 34 | 35 | namespace daw::json { 36 | template<> 37 | struct json_data_contract { 38 | using type = json_tuple_member_list; 39 | 40 | static inline auto 41 | to_json_data( daw::cookbook_class_from_array1::Point const &p ) { 42 | return std::forward_as_tuple( p.x, p.y ); 43 | } 44 | }; 45 | } // namespace daw::json 46 | 47 | int main( int argc, char **argv ) 48 | #if defined( DAW_USE_EXCEPTIONS ) 49 | try 50 | #endif 51 | { 52 | if( argc <= 1 ) { 53 | puts( "Must supply path to cookbook_class_from_array1.json file\n" ); 54 | exit( EXIT_FAILURE ); 55 | } 56 | auto data = daw::read_file( argv[1] ).value( ); 57 | puts( "Original\n" ); 58 | puts( data.data( ) ); 59 | 60 | auto const cls = daw::json::from_json( 61 | std::string_view( data.data( ), data.size( ) ) ); 62 | 63 | std::string const str = daw::json::to_json( cls ); 64 | 65 | puts( "Round trip\n" ); 66 | puts( str.c_str( ) ); 67 | auto const cls2 = 68 | daw::json::from_json( 69 | std::string_view( str.data( ), str.size( ) ) ); 70 | 71 | if( cls != cls2 ) { 72 | puts( "not exact same\n" ); 73 | } 74 | } 75 | #if defined( DAW_USE_EXCEPTIONS ) 76 | catch( daw::json::json_exception const &jex ) { 77 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 78 | exit( 1 ); 79 | } catch( std::exception const &ex ) { 80 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 81 | << '\n'; 82 | exit( 1 ); 83 | } catch( ... ) { 84 | std::cerr << "Unknown exception thrown during testing\n"; 85 | throw; 86 | } 87 | #endif 88 | -------------------------------------------------------------------------------- /tests/src/cookbook_class_from_array2_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // See cookbook/class_from_array.md for the 2nd example 9 | // 10 | 11 | #include "defines.h" 12 | 13 | #include 14 | 15 | #include "daw/json/daw_json_link.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | namespace daw::cookbook_class_from_array2 { 24 | struct Point { 25 | double x; 26 | double y; 27 | }; 28 | 29 | bool operator!=( Point const &lhs, Point const &rhs ) { 30 | return lhs.x != rhs.x and lhs.y != rhs.y; 31 | } 32 | } // namespace daw::cookbook_class_from_array2 33 | 34 | namespace daw::json { 35 | template<> 36 | struct json_data_contract { 37 | using type = 38 | json_tuple_member_list>>; 40 | 41 | static inline auto 42 | to_json_data( daw::cookbook_class_from_array2::Point const &p ) { 43 | return std::forward_as_tuple( p.x, p.y ); 44 | } 45 | }; 46 | } // namespace daw::json 47 | 48 | int main( int argc, char **argv ) 49 | #if defined( DAW_USE_EXCEPTIONS ) 50 | try 51 | #endif 52 | { 53 | if( argc <= 1 ) { 54 | puts( "Must supply path to cookbook_class_from_array2.json file\n" ); 55 | exit( EXIT_FAILURE ); 56 | } 57 | auto data = daw::read_file( argv[1] ).value( ); 58 | puts( "Original\n" ); 59 | puts( data.data( ) ); 60 | 61 | auto const cls = daw::json::from_json( 62 | std::string_view( data.data( ), data.size( ) ) ); 63 | 64 | std::string const str = daw::json::to_json( cls ); 65 | puts( "Round trip\n" ); 66 | puts( str.c_str( ) ); 67 | auto const cls2 = 68 | daw::json::from_json( 69 | std::string_view( str.data( ), str.size( ) ) ); 70 | 71 | if( cls != cls2 ) { 72 | puts( "not exact same\n" ); 73 | } 74 | } 75 | #if defined( DAW_USE_EXCEPTIONS ) 76 | catch( daw::json::json_exception const &jex ) { 77 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 78 | exit( 1 ); 79 | } catch( std::exception const &ex ) { 80 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 81 | << '\n'; 82 | exit( 1 ); 83 | } catch( ... ) { 84 | std::cerr << "Unknown exception thrown during testing\n"; 85 | throw; 86 | } 87 | #endif 88 | -------------------------------------------------------------------------------- /tests/src/cookbook_dates1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // See cookbook/dates.md for the 1st example 9 | // 10 | 11 | #include "defines.h" 12 | 13 | #include 14 | 15 | #include "daw/json/daw_json_link.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace daw::cookbook_dates1 { 25 | struct MyClass1 { 26 | std::string name; 27 | std::chrono::time_point 29 | timestamp; 30 | }; 31 | 32 | bool operator==( MyClass1 const &lhs, MyClass1 const &rhs ) { 33 | return std::tie( lhs.name, lhs.timestamp ) == 34 | std::tie( rhs.name, rhs.timestamp ); 35 | } 36 | } // namespace daw::cookbook_dates1 37 | 38 | namespace daw::json { 39 | template<> 40 | struct json_data_contract { 41 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 42 | using type = json_member_list, json_date<"timestamp">>; 43 | #else 44 | static constexpr char const name[] = "name"; 45 | static constexpr char const timestamp[] = "timestamp"; 46 | using type = json_member_list, json_date>; 47 | #endif 48 | static inline auto 49 | to_json_data( daw::cookbook_dates1::MyClass1 const &value ) { 50 | return std::forward_as_tuple( value.name, value.timestamp ); 51 | } 52 | }; 53 | } // namespace daw::json 54 | 55 | int main( int argc, char **argv ) 56 | #if defined( DAW_USE_EXCEPTIONS ) 57 | try 58 | #endif 59 | { 60 | if( argc <= 1 ) { 61 | puts( "Must supply path to cookbook_dates1.json file\n" ); 62 | exit( EXIT_FAILURE ); 63 | } 64 | auto data = daw::read_file( argv[1] ).value( ); 65 | 66 | auto const cls = daw::json::from_json( 67 | std::string_view( data.data( ), data.size( ) ) ); 68 | 69 | test_assert( cls.name == "Toronto", "Unexpected value" ); 70 | 71 | std::string const str = daw::json::to_json( cls ); 72 | puts( str.c_str( ) ); 73 | 74 | auto const cls2 = daw::json::from_json( str ); 75 | 76 | test_assert( cls == cls2, "Unexpected round trip error" ); 77 | } 78 | #if defined( DAW_USE_EXCEPTIONS ) 79 | catch( daw::json::json_exception const &jex ) { 80 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 81 | exit( 1 ); 82 | } catch( std::exception const &ex ) { 83 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 84 | << '\n'; 85 | exit( 1 ); 86 | } catch( ... ) { 87 | std::cerr << "Unknown exception thrown during testing\n"; 88 | throw; 89 | } 90 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_escaped_strings1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::cookbook_escaped_strings1 { 23 | struct WebData { 24 | std::vector uris; 25 | }; 26 | 27 | bool operator==( WebData const &lhs, WebData const &rhs ) { 28 | return lhs.uris == rhs.uris; 29 | } 30 | } // namespace daw::cookbook_escaped_strings1 31 | 32 | namespace daw::json { 33 | template<> 34 | struct json_data_contract { 35 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 36 | using type = json_member_list>; 37 | #else 38 | static constexpr char const uris[] = "uris"; 39 | using type = json_member_list>; 40 | #endif 41 | static inline auto 42 | to_json_data( daw::cookbook_escaped_strings1::WebData const &value ) { 43 | return std::forward_as_tuple( value.uris ); 44 | } 45 | }; 46 | } // namespace daw::json 47 | 48 | int main( int argc, char **argv ) 49 | #if defined( DAW_USE_EXCEPTIONS ) 50 | try 51 | #endif 52 | { 53 | if( argc <= 1 ) { 54 | puts( "Must supply path to cookbook_escaped_strings1.json file\n" ); 55 | exit( EXIT_FAILURE ); 56 | } 57 | auto const expected_wd = daw::cookbook_escaped_strings1::WebData{ 58 | { "example.com", "Bücher.ch", "happy.cn", "happy快乐.cn", "快乐.cn", 59 | "happy.中国", "快乐.中国", "www.ハンドボールサムズ.com", "🦄.com" } }; 60 | 61 | auto data = daw::read_file( argv[1] ).value( ); 62 | 63 | auto const cls = 64 | daw::json::from_json( 65 | std::string_view( data.data( ), data.size( ) ) ); 66 | 67 | test_assert( cls == expected_wd, "Unexpected value" ); 68 | auto const str = daw::json::to_json( cls ); 69 | puts( str.c_str( ) ); 70 | 71 | auto const cls2 = 72 | daw::json::from_json( 73 | std::string_view( str.data( ), str.size( ) ) ); 74 | 75 | test_assert( cls == cls2, "Unexpected round trip error" ); 76 | } 77 | #if defined( DAW_USE_EXCEPTIONS ) 78 | catch( daw::json::json_exception const &jex ) { 79 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 80 | exit( 1 ); 81 | } catch( std::exception const &ex ) { 82 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 83 | << '\n'; 84 | exit( 1 ); 85 | } catch( ... ) { 86 | std::cerr << "Unknown exception thrown during testing\n"; 87 | throw; 88 | } 89 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_inserting_extracting_raw_json1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | struct Foo { 17 | std::string bar; 18 | std::string raw_json; 19 | }; 20 | 21 | namespace daw::json { 22 | template<> 23 | struct json_data_contract { 24 | static constexpr char const bar[] = "bar"; 25 | static constexpr char const raw_json[] = "raw_json"; 26 | using type = json_member_list, 27 | json_raw>; 28 | 29 | static auto to_json_data( Foo const &v ) { 30 | return std::forward_as_tuple( v.bar, v.raw_json ); 31 | } 32 | }; 33 | } // namespace daw::json 34 | 35 | int main( int argc, char **argv ) 36 | #if defined( DAW_USE_EXCEPTIONS ) 37 | try 38 | #endif 39 | { 40 | if( argc <= 1 ) { 41 | puts( 42 | "Must supply path to cookbook_inserting_extracting_raw_json1.json " 43 | "file\n" ); 44 | exit( EXIT_FAILURE ); 45 | } 46 | auto data = daw::read_file( argv[1] ).value( ); 47 | 48 | puts( data.c_str( ) ); 49 | 50 | auto my_foo = daw::json::from_json( data ); 51 | my_foo.raw_json = R"({"a":55})"; 52 | 53 | auto data2 = daw::json::to_json( my_foo ); 54 | 55 | puts( data2.c_str( ) ); 56 | } 57 | #if defined( DAW_USE_EXCEPTIONS ) 58 | catch( daw::json::json_exception const &jex ) { 59 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 60 | exit( 1 ); 61 | } catch( std::exception const &ex ) { 62 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 63 | << '\n'; 64 | exit( 1 ); 65 | } catch( ... ) { 66 | std::cerr << "Unknown exception thrown during testing\n"; 67 | throw; 68 | } 69 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_kv1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::cookbook_kv1 { 23 | struct MyKeyValue1 { 24 | std::unordered_map kv; 25 | }; 26 | 27 | bool operator==( MyKeyValue1 const &lhs, MyKeyValue1 const &rhs ) { 28 | return lhs.kv == rhs.kv; 29 | } 30 | } // namespace daw::cookbook_kv1 31 | 32 | namespace daw::json { 33 | template<> 34 | struct json_data_contract { 35 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 36 | using type = json_member_list< 37 | json_key_value<"kv", std::unordered_map, int>>; 38 | #else 39 | static constexpr char const kv[] = "kv"; 40 | using type = json_member_list< 41 | json_key_value, int>>; 42 | #endif 43 | static inline auto 44 | to_json_data( daw::cookbook_kv1::MyKeyValue1 const &value ) { 45 | return std::forward_as_tuple( value.kv ); 46 | } 47 | }; 48 | } // namespace daw::json 49 | 50 | int main( int argc, char **argv ) 51 | #if defined( DAW_USE_EXCEPTIONS ) 52 | try 53 | #endif 54 | { 55 | if( argc <= 1 ) { 56 | puts( "Must supply path to cookbook_kv1.json file\n" ); 57 | exit( EXIT_FAILURE ); 58 | } 59 | auto data = daw::read_file( argv[1] ).value( ); 60 | 61 | auto kv = daw::json::from_json( 62 | std::string_view( data.data( ), data.size( ) ) ); 63 | 64 | test_assert( kv.kv.size( ) == 2, "Expected data to have 2 items" ); 65 | test_assert( kv.kv["key0"] == 353434, "Unexpected value" ); 66 | test_assert( kv.kv["key1"] == 314159, "Unexpected value" ); 67 | auto const str = daw::json::to_json( kv ); 68 | puts( str.c_str( ) ); 69 | auto const kv2 = daw::json::from_json( 70 | std::string_view( str.data( ), str.size( ) ) ); 71 | 72 | test_assert( kv == kv2, "Unexpected round trip error" ); 73 | } 74 | #if defined( DAW_USE_EXCEPTIONS ) 75 | catch( daw::json::json_exception const &jex ) { 76 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 77 | exit( 1 ); 78 | } catch( std::exception const &ex ) { 79 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 80 | << '\n'; 81 | exit( 1 ); 82 | } catch( ... ) { 83 | std::cerr << "Unknown exception thrown during testing\n"; 84 | throw; 85 | } 86 | #endif 87 | -------------------------------------------------------------------------------- /tests/src/cookbook_kv2_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::cookbook_kv2 { 23 | struct MyKeyValue2 { 24 | std::unordered_map kv; 25 | }; 26 | 27 | bool operator==( MyKeyValue2 const &lhs, MyKeyValue2 const &rhs ) { 28 | return lhs.kv == rhs.kv; 29 | } 30 | } // namespace daw::cookbook_kv2 31 | 32 | namespace daw::json { 33 | template<> 34 | struct json_data_contract { 35 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 36 | using type = json_member_list< 37 | json_key_value_array<"kv", std::unordered_map>>; 38 | #else 39 | static constexpr char const kv[] = "kv"; 40 | static constexpr char const value[] = "value"; 41 | using type = json_member_list< 42 | json_key_value_array>>; 43 | #endif 44 | static inline auto to_json_data( daw::cookbook_kv2::MyKeyValue2 const &v ) { 45 | return std::forward_as_tuple( v.kv ); 46 | } 47 | }; 48 | } // namespace daw::json 49 | 50 | int main( int argc, char **argv ) 51 | #if defined( DAW_USE_EXCEPTIONS ) 52 | try 53 | #endif 54 | { 55 | if( argc <= 1 ) { 56 | puts( "Must supply path to cookbook_kv2.json file\n" ); 57 | exit( EXIT_FAILURE ); 58 | } 59 | auto data = daw::read_file( argv[1] ).value( ); 60 | 61 | auto kv = daw::json::from_json( 62 | std::string_view( data.data( ), data.size( ) ) ); 63 | 64 | test_assert( kv.kv.size( ) == 2, "Expected data to have 2 items" ); 65 | test_assert( kv.kv[0] == "test_001", "Unexpected value" ); 66 | test_assert( kv.kv[1] == "test_002", "Unexpected value" ); 67 | auto const str = daw::json::to_json( kv ); 68 | puts( str.c_str( ) ); 69 | 70 | auto const kv2 = daw::json::from_json( 71 | std::string_view( str.data( ), str.size( ) ) ); 72 | test_assert( kv == kv2, "Unexpected round trip error" ); 73 | } 74 | #if defined( DAW_USE_EXCEPTIONS ) 75 | catch( daw::json::json_exception const &jex ) { 76 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 77 | exit( 1 ); 78 | } catch( std::exception const &ex ) { 79 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 80 | << '\n'; 81 | exit( 1 ); 82 | } catch( ... ) { 83 | std::cerr << "Unknown exception thrown during testing\n"; 84 | throw; 85 | } 86 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_kv3_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::cookbook_kv3 { 23 | struct MyKeyValue3 { 24 | std::multimap kv; 25 | 26 | friend bool operator==( MyKeyValue3 const &lhs, MyKeyValue3 const &rhs ) { 27 | return lhs.kv == rhs.kv; 28 | } 29 | }; 30 | } // namespace daw::cookbook_kv3 31 | 32 | namespace daw::json { 33 | template<> 34 | struct json_data_contract { 35 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 36 | using type = json_member_list, std::string>>; 38 | #else 39 | static constexpr char const kv[] = "kv"; 40 | using type = json_member_list< 41 | json_key_value, std::string>>; 42 | #endif 43 | static inline auto to_json_data( cookbook_kv3::MyKeyValue3 const &v ) { 44 | return std::forward_as_tuple( v.kv ); 45 | } 46 | }; 47 | } // namespace daw::json 48 | 49 | int main( int argc, char **argv ) 50 | #if defined( DAW_USE_EXCEPTIONS ) 51 | try 52 | #endif 53 | { 54 | if( argc <= 1 ) { 55 | puts( "Must supply path to cookbook_kv3.json file\n" ); 56 | exit( EXIT_FAILURE ); 57 | } 58 | auto data = daw::read_file( argv[1] ).value( ); 59 | 60 | auto kv = daw::json::from_json( 61 | std::string_view( data.data( ), data.size( ) ) ); 62 | 63 | test_assert( kv.kv.size( ) == 2, "Expected data to have 2 items" ); 64 | test_assert( kv.kv.begin( )->first == std::prev( kv.kv.end( ) )->first, 65 | "Unexpected value" ); 66 | test_assert( kv.kv.begin( )->second != std::prev( kv.kv.end( ) )->second, 67 | "Unexpected value" ); 68 | auto const str = daw::json::to_json( kv ); 69 | puts( str.c_str( ) ); 70 | 71 | auto const kv3 = daw::json::from_json( 72 | std::string_view( str.data( ), str.size( ) ) ); 73 | test_assert( kv == kv3, "Unexpected round trip error" ); 74 | } 75 | #if defined( DAW_USE_EXCEPTIONS ) 76 | catch( daw::json::json_exception const &jex ) { 77 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 78 | exit( 1 ); 79 | } catch( std::exception const &ex ) { 80 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 81 | << '\n'; 82 | exit( 1 ); 83 | } catch( ... ) { 84 | std::cerr << "Unknown exception thrown during testing\n"; 85 | throw; 86 | } 87 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_kv4_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | 13 | #include "daw/json/daw_json_link.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | int main( int argc, char **argv ) 24 | #if defined( DAW_USE_EXCEPTIONS ) 25 | try 26 | #endif 27 | { 28 | if( argc <= 1 ) { 29 | puts( "Must supply path to cookbook_kv4.json file\n" ); 30 | exit( EXIT_FAILURE ); 31 | } 32 | auto json_data = daw::read_file( argv[1] ).value( ); 33 | 34 | using namespace daw::json; 35 | auto val = json_value( json_data ); 36 | std::multimap kv = 37 | from_json, 38 | std::string>>( val ); 39 | test_assert( kv.size( ) == 2, "Expected data to have 2 items" ); 40 | test_assert( kv.begin( )->first == std::prev( kv.end( ) )->first, 41 | "Unexpected value" ); 42 | test_assert( kv.begin( )->second != std::prev( kv.end( ) )->second, 43 | "Unexpected value" ); 44 | } 45 | #if defined( DAW_USE_EXCEPTIONS ) 46 | catch( daw::json::json_exception const &jex ) { 47 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 48 | exit( 1 ); 49 | } catch( std::exception const &ex ) { 50 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 51 | << '\n'; 52 | exit( 1 ); 53 | } catch( ... ) { 54 | std::cerr << "Unknown exception thrown during testing\n"; 55 | throw; 56 | } 57 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_numbers3_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::json { 23 | template 25 | struct json_data_contract< 26 | boost::multiprecision::number> { 27 | 28 | using type = json_type_alias; 29 | 30 | static inline auto to_json_data( 31 | boost::multiprecision::number const 32 | &value ) { 33 | return value.str( ); 34 | } 35 | }; 36 | } // namespace daw::json 37 | 38 | int main( int argc, char **argv ) 39 | #if defined( DAW_USE_EXCEPTIONS ) 40 | try 41 | #endif 42 | { 43 | if( argc <= 1 ) { 44 | puts( "Must supply path to cookbook_numbers3.json file\n" ); 45 | exit( EXIT_FAILURE ); 46 | } 47 | auto data = daw::read_file( argv[1] ); 48 | assert( data and not data->empty( ) ); 49 | 50 | auto numbers = 51 | daw::json::from_json_array( 52 | *data ); 53 | 54 | assert( not numbers.empty( ) ); 55 | auto new_json_data = daw::json::to_json( numbers ); 56 | puts( new_json_data.c_str( ) ); 57 | 58 | auto const numbers2 = 59 | daw::json::from_json_array( 60 | new_json_data ); 61 | assert( not numbers2.empty( ) ); 62 | 63 | test_assert( numbers.front( ) == numbers2.front( ), 64 | "Unexpected round trip error" ); 65 | test_assert( numbers.size( ) == numbers2.size( ), 66 | "Unexpected round trip error" ); 67 | } 68 | #if defined( DAW_USE_EXCEPTIONS ) 69 | catch( daw::json::json_exception const &jex ) { 70 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 71 | exit( 1 ); 72 | } catch( std::exception const &ex ) { 73 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 74 | << '\n'; 75 | exit( 1 ); 76 | } catch( ... ) { 77 | std::cerr << "Unknown exception thrown during testing\n"; 78 | throw; 79 | } 80 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_output_flags1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | struct OutputFlags1 { 16 | std::string s; 17 | std::vector ary; 18 | }; 19 | 20 | namespace daw::json { 21 | template<> 22 | struct json_data_contract { 23 | static constexpr char const s[] = "s"; 24 | static constexpr char const ary[] = "ary"; 25 | using type = json_member_list, json_array>; 26 | 27 | static inline auto to_json_data( OutputFlags1 const &v ) { 28 | return std::forward_as_tuple( v.s, v.ary ); 29 | } 30 | }; 31 | } // namespace daw::json 32 | 33 | int main( ) { 34 | using namespace daw::json::options; 35 | auto const v = OutputFlags1{ "Hello", { 1, 2, 3 } }; 36 | daw::json::to_json( v, stdout ); 37 | puts( "\n" ); 38 | puts( daw::json::to_json( v, std::string{ }, 39 | output_flags ) 40 | .c_str( ) ); 41 | auto const v2 = OutputFlags1{ "😍😍 JSON is awesome", { 42 } }; 42 | auto v2_json = daw::json::to_json( 43 | v2, output_flags ); 45 | puts( v2_json.c_str( ) ); 46 | auto v2_2 = daw::json::from_json( v2_json ); 47 | puts( daw::json::to_json( v2_2 ).c_str( ) ); 48 | } 49 | -------------------------------------------------------------------------------- /tests/src/cookbook_parsing_individual_members1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | int main( int argc, char **argv ) 23 | #if defined( DAW_USE_EXCEPTIONS ) 24 | try 25 | #endif 26 | { 27 | if( argc <= 1 ) { 28 | puts( 29 | "Must supply path to cookbook_parsing_individual_members1.json file\n" ); 30 | exit( EXIT_FAILURE ); 31 | } 32 | auto const json_data = daw::read_file( argv[1] ).value( ); 33 | 34 | using namespace daw::json; 35 | auto const b_value = from_json( json_data, "member2.b" ); 36 | 37 | test_assert( b_value == "found me", "Unexpected value" ); 38 | 39 | std::optional opt_value = 40 | from_json>( json_data, "a.b" ); 41 | 42 | test_assert( not opt_value, "Did not expect a value" ); 43 | } 44 | #if defined( DAW_USE_EXCEPTIONS ) 45 | catch( daw::json::json_exception const &jex ) { 46 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 47 | exit( 1 ); 48 | } catch( std::exception const &ex ) { 49 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 50 | << '\n'; 51 | exit( 1 ); 52 | } catch( ... ) { 53 | std::cerr << "Unknown exception thrown during testing\n"; 54 | throw; 55 | } 56 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_parsing_individual_members2_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | int main( int argc, char **argv ) 23 | #if defined( DAW_USE_EXCEPTIONS ) 24 | try 25 | #endif 26 | { 27 | if( argc <= 1 ) { 28 | puts( 29 | "Must supply path to cookbook_parsing_individual_members2.json file\n" ); 30 | exit( EXIT_FAILURE ); 31 | } 32 | 33 | auto const file_data = daw::read_file( argv[1] ).value( ); 34 | auto const json_data = 35 | std::string_view( file_data.data( ), file_data.size( ) ); 36 | 37 | using namespace daw::json; 38 | std::vector value = 39 | from_json_array( json_data, "member1" ); 40 | 41 | test_assert( value.size( ) == 4, "Unexpected value" ); 42 | test_assert( value[1] == "is", "Unexpected value" ); 43 | } 44 | #if defined( DAW_USE_EXCEPTIONS ) 45 | catch( daw::json::json_exception const &jex ) { 46 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 47 | exit( 1 ); 48 | } catch( std::exception const &ex ) { 49 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 50 | << '\n'; 51 | exit( 1 ); 52 | } catch( ... ) { 53 | std::cerr << "Unknown exception thrown during testing\n"; 54 | throw; 55 | } 56 | #endif 57 | -------------------------------------------------------------------------------- /tests/src/cookbook_parsing_individual_members3_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | int main( int argc, char **argv ) 23 | #if defined( DAW_USE_EXCEPTIONS ) 24 | try 25 | #endif 26 | { 27 | if( argc <= 1 ) { 28 | puts( 29 | "Must supply path to cookbook_parsing_individual_members1.json file\n" ); 30 | exit( EXIT_FAILURE ); 31 | } 32 | 33 | auto const file_data = daw::read_file( argv[1] ).value( ); 34 | auto const json_data = 35 | std::string_view( file_data.data( ), file_data.size( ) ); 36 | 37 | using namespace daw::json; 38 | auto const value = from_json( json_data, "member1[1]" ); 39 | 40 | test_assert( value == "is", "Unexpected value" ); 41 | 42 | std::string_view opt_value = 43 | from_json>( 44 | json_data, "member1[1000]" ); 45 | 46 | test_assert( opt_value.empty( ), "Unexpected result" ); 47 | } 48 | #if defined( DAW_USE_EXCEPTIONS ) 49 | catch( daw::json::json_exception const &jex ) { 50 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 51 | exit( 1 ); 52 | } catch( std::exception const &ex ) { 53 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 54 | << '\n'; 55 | exit( 1 ); 56 | } catch( ... ) { 57 | std::cerr << "Unknown exception thrown during testing\n"; 58 | throw; 59 | } 60 | #endif -------------------------------------------------------------------------------- /tests/src/cookbook_unknown_types_and_raw_parsing1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | 13 | #include "daw/json/daw_json_link.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | int main( int argc, char **argv ) 22 | #if defined( DAW_USE_EXCEPTIONS ) 23 | try 24 | #endif 25 | { 26 | if( argc <= 1 ) { 27 | puts( 28 | "Must supply path to cookbook_unknown_types_and_delayed_parsing1.json " 29 | "file\n" ); 30 | exit( EXIT_FAILURE ); 31 | } 32 | auto data = daw::read_file( argv[1] ).value( ); 33 | 34 | auto val = daw::json::json_value( data ); 35 | for( auto member : val ) { 36 | if( member.name ) { 37 | std::cout << *member.name << " is a "; 38 | } else { 39 | std::cout << "Array element is a "; 40 | } 41 | std::cout << member.value.is_number( ); 42 | std::cout << to_string( member.value.type( ) ) << " with a value of " 43 | << member.value.get_string_view( ) << '\n'; 44 | } 45 | } 46 | #if defined( DAW_USE_EXCEPTIONS ) 47 | catch( daw::json::json_exception const &jex ) { 48 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 49 | exit( 1 ); 50 | } catch( std::exception const &ex ) { 51 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 52 | << '\n'; 53 | exit( 1 ); 54 | } catch( ... ) { 55 | std::cerr << "Unknown exception thrown during testing\n"; 56 | throw; 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /tests/src/cookbook_unknown_types_and_raw_parsing3_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | #include "defines.h" 9 | // See cookbook/unknown_types_and_delayed_parsing.md for the 4th example 10 | // This example shows how to use the json_raw type to store/retrieve raw 11 | // json as a string 12 | 13 | #include "daw/json/daw_json_link.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | struct Thing { 21 | std::string name; 22 | int type; 23 | std::string raw_json; 24 | }; 25 | 26 | namespace daw::json { 27 | template<> 28 | struct json_data_contract { 29 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 30 | using type = json_member_list, json_number<"type", int>, 31 | json_raw<"raw_json", std::string>>; 32 | #else 33 | static constexpr char const name[] = "name"; 34 | static constexpr char const type_member[] = "type"; 35 | static constexpr char const raw_json[] = "raw_json"; 36 | using type = 37 | json_member_list, json_number, 38 | json_raw>; 39 | #endif 40 | static auto to_json_data( Thing const &value ) { 41 | return std::forward_as_tuple( value.name, value.type, value.raw_json ); 42 | } 43 | }; 44 | } // namespace daw::json 45 | 46 | int main( ) 47 | #if defined( DAW_USE_EXCEPTIONS ) 48 | try 49 | #endif 50 | { 51 | std::string obj = R"({ "member": 12345, "member2": "hello" })"; 52 | auto my_thing = Thing{ "update", 1, obj }; 53 | auto json = daw::json::to_json( my_thing ); 54 | 55 | puts( json.c_str( ) ); 56 | auto const val2 = daw::json::from_json( json ); 57 | test_assert( my_thing.raw_json == val2.raw_json, 58 | "Unexpected round trip error" ); 59 | } 60 | #if defined( DAW_USE_EXCEPTIONS ) 61 | catch( daw::json::json_exception const &jex ) { 62 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 63 | exit( 1 ); 64 | } catch( std::exception const &ex ) { 65 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 66 | << '\n'; 67 | exit( 1 ); 68 | } catch( ... ) { 69 | std::cerr << "Unknown exception thrown during testing\n"; 70 | throw; 71 | } 72 | #endif -------------------------------------------------------------------------------- /tests/src/daw_json_fuzzing_gsoc.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "gsoc.h" 12 | 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | extern "C" int LLVMFuzzerTestOneInput( std::uint8_t const *data, 21 | std::size_t size ) { 22 | if( data == nullptr or size == 0 ) { 23 | return 0; 24 | } 25 | auto json_doc = 26 | std::string_view( reinterpret_cast( data ), size ); 27 | #if defined( DAW_USE_EXCEPTIONS ) 28 | try { 29 | #endif 30 | auto t = daw::json::from_json( 31 | json_doc, daw::json::options::parse_flags ); 32 | (void)t; 33 | #if defined( DAW_USE_EXCEPTIONS ) 34 | } catch( ... ) {} 35 | #endif 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /tests/src/daw_json_fuzzing_twitter.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "twitter_test_json.h" 12 | 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | extern "C" int LLVMFuzzerTestOneInput( std::uint8_t const *data, 21 | std::size_t size ) { 22 | if( data == nullptr or size == 0 ) { 23 | return 0; 24 | } 25 | auto json_doc = 26 | std::string_view( reinterpret_cast( data ), size ); 27 | #if defined( DAW_USE_EXCEPTIONS ) 28 | try { 29 | #endif 30 | auto t = daw::json::from_json( 31 | json_doc, daw::json::options::parse_flags ); 32 | (void)t; 33 | #if defined( DAW_USE_EXCEPTIONS ) 34 | } catch( ... ) {} 35 | #endif 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /tests/src/daw_json_fuzzing_twitter_replay.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "twitter_test_json.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | int main( int argc, char **argv ) { 22 | ensure( argc >= 2 ); 23 | auto json_doc = daw::filesystem::memory_mapped_file_t( argv[1] ); 24 | ensure( not json_doc.empty( ) ); 25 | #if defined( DAW_USE_EXCEPTIONS ) 26 | try { 27 | #endif 28 | auto t = daw::json::from_json( 29 | json_doc, daw::json::options::parse_flags ); 30 | (void)t; 31 | #if defined( DAW_USE_EXCEPTIONS ) 32 | } catch( ... ) {} 33 | #endif 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /tests/src/daw_json_iostream_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | struct NumberX { 21 | int x; 22 | }; 23 | 24 | namespace daw::json { 25 | template<> 26 | struct json_data_contract { 27 | // Having this type alias, any type on the RHS works, says that operator<<( 28 | // ostream &, NumberX const & ) is available. When used, it will output the 29 | // JSON serialization 30 | using opt_into_iostreams = void; 31 | 32 | static constexpr char const x[] = "x"; 33 | using type = json_member_list>; 34 | 35 | static constexpr auto to_json_data( NumberX const &v ) { 36 | return std::forward_as_tuple( v.x ); 37 | } 38 | }; 39 | } // namespace daw::json 40 | 41 | static_assert( 42 | daw::json::json_details::is_opted_into_json_iostreams_v ); 43 | static_assert( 44 | daw::json::json_details::is_container_opted_into_json_iostreams_v< 45 | std::vector> ); 46 | 47 | int main( ) { 48 | DAW_CONSTEXPR std::string_view const single_numberx = R"({"x":123})"; 49 | DAW_CONSTEXPR auto const nx = daw::json::from_json( single_numberx ); 50 | 51 | DAW_CONSTEXPR std::string_view const numberx_in_json_array = 52 | R"([{"x":1},{"x":2},{"x":3}])"; 53 | auto const vec_nx = 54 | daw::json::from_json_array( numberx_in_json_array ); 55 | auto const deq_nx = daw::json::from_json_array>( 56 | numberx_in_json_array ); 57 | auto const lst_nx = daw::json::from_json_array>( 58 | numberx_in_json_array ); 59 | 60 | std::cout << nx << '\n'; 61 | std::cout << vec_nx << '\n'; 62 | std::cout << deq_nx << '\n'; 63 | std::cout << lst_nx << '\n'; 64 | } 65 | -------------------------------------------------------------------------------- /tests/src/daw_murmur3_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/impl/daw_murmur3.h" 12 | 13 | #include 14 | 15 | static void test( daw::string_view key, std::uint32_t seed, 16 | std::uint32_t expected ) { 17 | daw::UInt32 answer = daw::murmur3_32( key, seed ); 18 | daw::expecting( answer == expected ); 19 | } 20 | 21 | int main( int, char ** ) { 22 | DAW_CONSTEXPR char const t0_a[] = { 0x01, 0x02, 0x03, 0x04, 0 }; 23 | DAW_CONSTEXPR daw::UInt32 t0_b = daw::to_uint32_buffer( t0_a ); 24 | daw::expecting( t0_b == 0x04030201 ); 25 | test( "", 0, 0 ); 26 | test( "", 1, 0x514E'28B7ULL ); 27 | test( "", 0xFFFF'FFFF, 28 | 0x81F1'6F39ULL ); // make sure your seed uses unsigned 32-bit math 29 | test( "\xFF\xFF\xFF\xFF", 0, 30 | 0x7629'3B50ULL ); // make sure 4-byte chunks use unsigned math 31 | test( "\x21\x43\x65\x87", 0, 0xF55B'516BULL ); 32 | test( "\x21\x43\x65\x87", 0x5082'EDEE, 0x2362'F9DE ); 33 | 34 | test( "aaaa", 0x9747b28c, 0x5A97808A ); // one full chunk 35 | test( "aaa", 0x9747b28c, 0x283E0130 ); // three characters 36 | test( "aa", 0x9747b28c, 0x5D211726 ); // two characters 37 | test( "a", 0x9747b28c, 0x7FA09EA6 ); // one character 38 | 39 | test( "abcd", 0x9747b28c, 0xF0478627 ); 40 | test( "abc", 0x9747b28c, 0xC84A62DD ); 41 | test( "ab", 0x9747b28c, 0x74875592 ); 42 | test( "a", 0x9747b28c, 0x7FA09EA6 ); 43 | 44 | test( "Hello, world!", 0x9747b28c, 0x24884CBA ); 45 | 46 | // Make sure you handle UTF-8 high characters. A bcrypt implementation messed 47 | // this up 48 | test( "ππππππππ", 0x9747b28c, 0xD58063C1 ); // U+03C0: Greek Small Letter Pi 49 | 50 | // String of 256 characters. 51 | // Make sure you don't store string lengths in a char, and overflow at 255 52 | // bytes (as OpenBSD's canonical BCrypt implementation did) 53 | std::string aas; 54 | aas.resize( 256, 'a' ); 55 | test( aas, 0x9747b28c, 0x37405BDC ); 56 | 57 | test( "abc", 0, 0xB3DD93FA ); 58 | test( "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 59 | 0xEE925B90 ); 60 | } 61 | -------------------------------------------------------------------------------- /tests/src/float_array_basic_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_iterator.h" 12 | 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #if not defined( DAW_NUM_RUNS ) 22 | #if not defined( DEBUG ) or defined( NDEBUG ) 23 | static inline constexpr std::size_t DAW_NUM_RUNS = 1000; 24 | #else 25 | static inline constexpr std::size_t DAW_NUM_RUNS = 3000; 26 | #endif 27 | #endif 28 | static_assert( DAW_NUM_RUNS > 0 ); 29 | 30 | template 31 | Float rand_float( ) { 32 | static DAW_CONSTEXPR Float fmin = 0; 33 | static DAW_CONSTEXPR Float fmax = 1; 34 | static auto e = std::default_random_engine( ); 35 | static auto dis = std::uniform_real_distribution( fmin, fmax ); 36 | return dis( e ); 37 | } 38 | 39 | template 40 | void test_func( ankerl::nanobench::Bench &b ) { 41 | using namespace daw::json; 42 | using iterator_t = daw::json::json_array_iterator; 43 | 44 | std::string json_data3 = [] { 45 | std::string result = "["; 46 | result.reserve( NUMVALUES * 23 + 8 ); 47 | daw::algorithm::do_n( NUMVALUES, [&result] { 48 | result += std::to_string( rand_float( ) ) + ','; 49 | } ); 50 | result.back( ) = ']'; 51 | return result; 52 | }( ); 53 | 54 | auto const json_sv = 55 | daw::string_view( json_data3.data( ), json_data3.size( ) ); 56 | auto data2 = std::unique_ptr( new float[NUMVALUES] ); 57 | b.batch( sizeof( float ) * NUMVALUES ); 58 | b.run( "float", [&]( ) noexcept { 59 | auto *ptr = std::copy( iterator_t( json_sv ), iterator_t( ), data2.get( ) ); 60 | ankerl::nanobench::doNotOptimizeAway( data2 ); 61 | ankerl::nanobench::doNotOptimizeAway( ptr ); 62 | } ); 63 | } 64 | 65 | int main( int argc, char ** ) 66 | #if defined( DAW_USE_EXCEPTIONS ) 67 | try 68 | #endif 69 | { 70 | 71 | auto b1 = ankerl::nanobench::Bench( ) 72 | .title( "nativejson parts" ) 73 | .unit( "byte" ) 74 | .warmup( 3000 ) 75 | .minEpochIterations( DAW_NUM_RUNS ); 76 | if( argc > 1 ) { 77 | test_func<1'000'000ULL>( b1 ); 78 | } else { 79 | test_func<1'000ULL>( b1 ); 80 | } 81 | } 82 | #if defined( DAW_USE_EXCEPTIONS ) 83 | catch( daw::json::json_exception const &jex ) { 84 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 85 | exit( 1 ); 86 | } catch( std::exception const &ex ) { 87 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 88 | << '\n'; 89 | exit( 1 ); 90 | } catch( ... ) { 91 | std::cerr << "Unknown exception thrown during testing\n"; 92 | throw; 93 | } 94 | #endif 95 | -------------------------------------------------------------------------------- /tests/src/int_ptr_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link/ 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | /*** 20 | * This example shows how one might encode a int * array type where another 21 | * member has the size. In the future there will be a mapping type for this to 22 | * make it explicit and declarative 23 | */ 24 | 25 | struct Foo { 26 | std::size_t n = 0; 27 | int *data = nullptr; 28 | }; 29 | 30 | template 31 | struct ArrayPointerConstructor { 32 | inline static thread_local std::size_t size = 33 | std::numeric_limits::max( ); 34 | 35 | struct SizeCtor { 36 | inline std::size_t operator( )( std::size_t v ) const noexcept { 37 | size = v; 38 | return v; 39 | } 40 | }; 41 | 42 | struct ArrayCtor { 43 | template 44 | T *operator( )( Iterator first, Iterator last ) const { 45 | auto result = std::unique_ptr( new T[size] ); 46 | T *ptr = result.get( ); 47 | while( first != last ) { 48 | *ptr = *first; 49 | ++ptr; 50 | ++first; 51 | } 52 | return result.release( ); 53 | } 54 | }; 55 | }; 56 | 57 | namespace daw::json { 58 | template<> 59 | struct json_data_contract { 60 | static constexpr char const member_n[] = "n"; 61 | static constexpr char const member_data[] = "data"; 62 | 63 | using type = json_member_list< 64 | json_number::SizeCtor>, 66 | json_array::ArrayCtor>>; 68 | 69 | static constexpr auto to_json_data( Foo const &f ) { 70 | return std::tuple{ f.n, daw::span( f.data, f.n ) }; 71 | } 72 | }; 73 | } // namespace daw::json 74 | 75 | int main( ) { 76 | std::string_view jdoc = R"({"n":5,"data":[1,2,3,4,5]})"; 77 | auto foo = daw::json::from_json( jdoc ); 78 | std::cout << foo.n << '\n'; 79 | 80 | auto jdoc2 = daw::json::to_json( foo ); 81 | std::cout << jdoc2 << '\n'; 82 | delete[] foo.data; 83 | } 84 | -------------------------------------------------------------------------------- /tests/src/issue_334_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | struct PersonObject { 16 | int id{ }; 17 | std::string name; 18 | int age{ }; 19 | std::unordered_map fields; 20 | }; 21 | 22 | namespace daw::json { 23 | template<> 24 | struct json_data_contract { 25 | static constexpr char const id[] = "id"; 26 | static constexpr char const name[] = "name"; 27 | static constexpr char const age[] = "age"; 28 | static constexpr char const fields[] = "fields"; 29 | using type = json_member_list< 30 | json_number, json_string, json_number_null, 31 | json_key_value, 32 | std::string>>; 33 | }; 34 | } // namespace daw::json 35 | 36 | int main( ) { 37 | std::string raw_json{ R"_( 38 | { 39 | "id": 10, 40 | "name": "Jason", 41 | "fields": {"city": "Chicago", "Title": "Manager"}, 42 | } 43 | )_" }; 44 | PersonObject person = daw::json::from_json( raw_json ); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /tests/src/issue_336_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link/ 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | struct PersonObject { 16 | int id{}; 17 | std::string name; 18 | std::unordered_map fields; 19 | }; 20 | 21 | namespace daw::json { 22 | template<> 23 | struct json_data_contract { 24 | static constexpr char const id[] = "id"; 25 | static constexpr char const name[] = "name"; 26 | static constexpr char const fields[] = "fields"; 27 | using type = json_member_list< 28 | json_number, 29 | json_string, 30 | json_key_value_null, std::string> 31 | >; 32 | }; 33 | } 34 | 35 | int main() { 36 | std::string raw_json{R"_( 37 | { 38 | "id": 10, 39 | "name": "Jason", 40 | "fields": {"city": "Chicago", "Title": "Manager"}, 41 | } 42 | )_"}; 43 | PersonObject person = daw::json::from_json(raw_json); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /tests/src/issue_361_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace daw::cookbook_kv1b { 23 | struct MyKeyValue1 { 24 | std::unordered_map kv; 25 | }; 26 | 27 | bool operator==( MyKeyValue1 const &lhs, MyKeyValue1 const &rhs ) { 28 | return lhs.kv == rhs.kv; 29 | } 30 | } // namespace daw::cookbook_kv1b 31 | 32 | namespace daw::json { 33 | template<> 34 | struct json_data_contract { 35 | static constexpr char const kv[] = "kv"; 36 | using type = json_member_list>>; /*, double, 38 | json_number_no_name>>;*/ 41 | 42 | static inline auto 43 | to_json_data( daw::cookbook_kv1b::MyKeyValue1 const &value ) { 44 | return std::forward_as_tuple( value.kv ); 45 | } 46 | }; 47 | } // namespace daw::json 48 | 49 | int main( int argc, char **argv ) 50 | #if defined( DAW_USE_EXCEPTIONS ) 51 | try 52 | #endif 53 | { 54 | if( argc <= 1 ) { 55 | puts( "Must supply path to cookbook_kv1b.json file\n" ); 56 | exit( EXIT_FAILURE ); 57 | } 58 | auto json_doc = daw::read_file( argv[1] ).value( ); 59 | 60 | auto kv = daw::json::from_json( json_doc ); 61 | 62 | test_assert( kv.kv.size( ) == 2, "Expected data to have 2 items" ); 63 | test_assert( kv.kv[123] == 1234, "Unexpected value" ); 64 | test_assert( kv.kv[545] == 314159, "Unexpected value" ); 65 | auto const str = daw::json::to_json( kv ); 66 | puts( str.c_str( ) ); 67 | auto const kv2 = daw::json::from_json( str ); 68 | 69 | test_assert( kv == kv2, "Unexpected round trip error" ); 70 | } 71 | #if defined( DAW_USE_EXCEPTIONS ) 72 | catch( daw::json::json_exception const &jex ) { 73 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 74 | exit( 1 ); 75 | } catch( std::exception const &ex ) { 76 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 77 | << '\n'; 78 | exit( 1 ); 79 | } catch( ... ) { 80 | std::cerr << "Unknown exception thrown during testing\n"; 81 | throw; 82 | } 83 | #endif 84 | -------------------------------------------------------------------------------- /tests/src/issue_389_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link/ 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | struct test_document { 17 | std::string_view in; 18 | }; 19 | 20 | namespace daw::json { 21 | template<> 22 | struct json_data_contract { 23 | static inline constexpr char const _in[] = "in"; 24 | using type = json_member_list>; 25 | 26 | static constexpr auto to_json_data( test_document const &doc ) { 27 | return std::forward_as_tuple( doc.in ); 28 | } 29 | }; 30 | } // namespace daw::json 31 | 32 | int main( ) { 33 | constexpr std::string_view const test_string = "\x0D\x0ATEST\xFETEST\x0D\x0A"; 34 | constexpr auto document = test_document{ test_string }; 35 | std::string buff; 36 | try { 37 | auto request_json_payload = daw::json::to_json( document, buff ); 38 | std::cerr << "Expected error\n"; 39 | return 1; 40 | } catch( daw::json::json_exception const & jex ) { 41 | if( jex.reason_type() != daw::json::ErrorReason::InvalidStringHighASCII ) { 42 | std::cerr << "Unexpected JSON error " << to_formatted_string( jex ) << '\n'; 43 | return 1; 44 | } 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /tests/src/issue_398_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | int main( ) { 12 | auto json = daw::json::json_value{ R"({"food":"lasagna"})" }; 13 | try { 14 | auto favourite_food = daw::json::as( json["garfield"] ); 15 | } catch( daw::json::json_exception const &je ) { 16 | if( je.reason_type( ) == daw::json::ErrorReason::UnexpectedNull ) { 17 | return 0; 18 | } 19 | throw; 20 | } 21 | return 1; 22 | } 23 | -------------------------------------------------------------------------------- /tests/src/issue_399_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include "defines.h" 12 | 13 | int main( ) { 14 | using namespace daw::json; 15 | auto const json = json_value{ R"({"example.com":"justanormalstring"})" }; 16 | auto const parsed_value = as( json["example\\.com"] ); 17 | ensure( parsed_value == "justanormalstring" ); 18 | } 19 | -------------------------------------------------------------------------------- /tests/src/issue_402_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include "defines.h" 12 | 13 | int main( ) { 14 | using namespace daw::json; 15 | try { 16 | auto value = json_value{ "name:, 4d2}" }; 17 | if( value ) {} 18 | if( value.is_null( ) ) {} 19 | } catch( json_exception const & ) { return 0; } 20 | return 1; 21 | } 22 | -------------------------------------------------------------------------------- /tests/src/issue_406_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | 13 | #include 14 | 15 | namespace daw::json { 16 | template<> 17 | struct json_data_contract { 18 | using type = json_member_list<>; 19 | 20 | static constexpr std::tuple<> to_json_data( std::monostate ) noexcept { 21 | return { }; 22 | } 23 | }; 24 | } // namespace daw::json 25 | 26 | struct SomeEmpty {}; 27 | 28 | int main( ) { 29 | using type = daw::json::json_variant_null_no_name< 30 | std::variant>; 31 | auto a = daw::json::from_json( R"("hello")" ); 32 | ensure( a.index( ) == 2 ); 33 | auto a_str = daw::json::to_json( a ); 34 | ensure( a_str == R"("hello")" ); 35 | using type2 = daw::json::json_variant_null_no_name< 36 | std::variant>; 37 | auto a2 = daw::json::from_json( R"("hello")" ); 38 | ensure( a2.index( ) == 2 ); 39 | auto a2_str = daw::json::to_json( a2 ); 40 | ensure( a2_str == R"("hello")" ); 41 | auto b2 = daw::json::from_json( "null" ); 42 | ensure( b2.index( ) == 0 ); 43 | auto b2_str = daw::json::to_json( b2 ); 44 | (void)b2_str; 45 | } 46 | -------------------------------------------------------------------------------- /tests/src/issue_413_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | struct Thing { 15 | std::uint8_t u; 16 | }; 17 | 18 | namespace daw::json { 19 | template<> 20 | struct json_data_contract { 21 | static constexpr char const u[] = "u"; 22 | using type = json_member_list>; 25 | }; 26 | } // namespace daw::json 27 | 28 | int main( ) { 29 | constexpr char const json_doc[] = R"({"u": 256})"; 30 | try { 31 | auto thing = daw::json::from_json( json_doc ); 32 | (void)thing; 33 | std::cout << "Expected and out of range error\n"; 34 | return EXIT_FAILURE; 35 | } catch( daw::json::json_exception const &e ) { 36 | if( e.reason_type() != daw::json::ErrorReason::NumberOutOfRange ) { 37 | std::cout << "Expected and out of range error\n"; 38 | return EXIT_FAILURE; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/src/issue_421_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | struct Variant { 20 | std::variant> value; 21 | }; 22 | 23 | struct VariantCtor { 24 | Variant operator( )( char const *ptr, std::size_t sz ) const { 25 | auto value = daw::json::json_value( std::string_view( ptr, sz ) ); 26 | return operator( )( value ); 27 | } 28 | 29 | Variant operator( )( daw::json::json_value value ) const { 30 | using namespace daw::json; 31 | switch( value.type( ) ) { 32 | case JsonBaseParseTypes::Number: 33 | return Variant{ from_json( value ) }; 34 | case JsonBaseParseTypes::Bool: 35 | return Variant{ from_json( value ) }; 36 | case JsonBaseParseTypes::Array: { 37 | auto res = std::vector( ); 38 | for( auto jp : value ) { 39 | res.push_back( operator( )( jp.value ) ); 40 | } 41 | return Variant{ std::move( res ) }; 42 | } 43 | default: 44 | std::abort( ); 45 | } 46 | } 47 | }; 48 | 49 | namespace daw::json { 50 | template<> 51 | struct json_data_contract { 52 | using type = json_type_alias>, VariantCtor>>; 54 | 55 | static auto to_json_data( const Variant &value ) { 56 | return value.value; 57 | } 58 | }; 59 | } // namespace daw::json 60 | 61 | int main( ) { 62 | { 63 | auto i = daw::json::from_json( "5" ); 64 | ensure( i.value.index( ) == 0 ); 65 | } 66 | { 67 | auto b0 = daw::json::from_json( "false" ); 68 | ensure( b0.value.index( ) == 1 ); 69 | } 70 | { 71 | auto b1 = daw::json::from_json( "true" ); 72 | ensure( b1.value.index( ) == 1 ); 73 | } 74 | { 75 | constexpr std::string_view json_doc = "[1, true, false, [1, false, []]]"; 76 | auto ary = daw::json::from_json( json_doc ); 77 | ensure( ary.value.index( ) == 2 ); 78 | } 79 | } -------------------------------------------------------------------------------- /tests/src/issue_462_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | struct request { 16 | std::unordered_map header; 17 | }; 18 | 19 | namespace daw::json { 20 | template<> 21 | struct json_data_contract { 22 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 23 | using type = json_member_list, std::string>>; 25 | #else 26 | static constexpr char const header[] = "header"; 27 | using type = json_member_list, std::string>>; 29 | #endif 30 | static inline auto to_json_data( request const &v ) { 31 | return std::forward_as_tuple( v.header ); 32 | } 33 | }; 34 | } // namespace daw::json 35 | 36 | int main( ) { 37 | constexpr std::string_view json_doc = 38 | R"json({ "header": { "field1": "a", "field2": "b" } })json"; 39 | 40 | auto req = daw::json::from_json( json_doc ); 41 | } 42 | -------------------------------------------------------------------------------- /tests/src/json_member_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | // 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | struct Foo { 18 | int a; 19 | std::string b; 20 | std::vector> c; 21 | }; 22 | 23 | namespace daw::json { 24 | template<> 25 | struct json_data_contract { 26 | static constexpr char const a[] = "a"; 27 | static constexpr char const b[] = "b"; 28 | static constexpr char const c[] = "c"; 29 | using type = json_member_list< 30 | json_link, json_link, 31 | json_link>>>; 32 | 33 | static constexpr auto to_json_data( Foo const &f ) { 34 | return std::forward_as_tuple( f.a, f.b, f.c ); 35 | } 36 | }; 37 | } // namespace daw::json 38 | 39 | int main( ) { 40 | using namespace std::literals; 41 | auto json_data = R"({ 42 | "a": 5, 43 | "b": "hello world", 44 | "c": [{"a": 1.1, "b": 2.2}, {"c": 3.3, "d": 4.4}]})"s; 45 | 46 | auto foo_val = daw::json::from_json( json_data ); 47 | daw_json_ensure( foo_val.a == 5, daw::json::ErrorReason::InvalidNumber ); 48 | auto json_data2 = daw::json::to_json( foo_val ); 49 | auto foo_val2 = daw::json::from_json( json_data2 ); 50 | daw_json_ensure( foo_val.a == foo_val2.a, 51 | daw::json::ErrorReason::InvalidNumber ); 52 | daw_json_ensure( foo_val.b == foo_val2.b, 53 | daw::json::ErrorReason::InvalidString ); 54 | daw_json_ensure( foo_val.c == foo_val2.c, daw::json::ErrorReason::Unknown ); 55 | } 56 | -------------------------------------------------------------------------------- /tests/src/kostya_bench2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | int main( int, char ** ) { 22 | std::string const json_text = daw::read_file( "/tmp/1.json" ).value( ); 23 | 24 | double x = 0; 25 | double y = 0; 26 | double z = 0; 27 | int len = 0; 28 | 29 | using namespace daw::json; 30 | auto rng = from_json>( 31 | json_text, "coordinates", 32 | options::parse_flags ); 33 | 34 | auto val = json_value_state( ); 35 | for( json_pair item : rng ) { 36 | val.reset( item.value ); 37 | x += from_json( 38 | val["x"], options::parse_flags ); 39 | y += from_json( 40 | val["y"], options::parse_flags ); 41 | z += from_json( 42 | val["z"], options::parse_flags ); 43 | ++len; 44 | } 45 | std::cout << x / len << '\n'; 46 | std::cout << y / len << '\n'; 47 | std::cout << z / len << '\n'; 48 | 49 | return EXIT_SUCCESS; 50 | } 51 | -------------------------------------------------------------------------------- /tests/src/kostya_bench3.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | struct coordinate_t { 23 | double x; 24 | double y; 25 | double z; 26 | 27 | constexpr bool operator!=( coordinate_t const &rhs ) const noexcept { 28 | return x != rhs.x and y != rhs.y and z != rhs.z; 29 | } 30 | 31 | friend std::ostream &operator<<( std::ostream &out, 32 | coordinate_t const &point ) { 33 | out << "coordinate_t {x: " << point.x << ", y: " << point.y 34 | << ", z: " << point.z << "}"; 35 | return out; 36 | } 37 | }; 38 | 39 | struct coordinates_t { 40 | std::vector coordinates; 41 | }; 42 | 43 | namespace daw::json { 44 | template<> 45 | struct json_data_contract { 46 | constexpr inline static char const x[] = "x"; 47 | constexpr inline static char const y[] = "y"; 48 | constexpr inline static char const z[] = "z"; 49 | using type = 50 | json_member_list, json_number, json_number>; 51 | }; 52 | } // namespace daw::json 53 | 54 | coordinate_t calc( std::string const &text ) { 55 | double x = 0.0; 56 | double y = 0.0; 57 | double z = 0.0; 58 | std::size_t len = 0; 59 | 60 | using namespace daw::json; 61 | using range_t = 62 | json_array_range; 63 | auto rng = range_t( text, "coordinates" ); 64 | auto f = rng.begin( ); 65 | auto l = rng.end( ); 66 | while( f != l ) { 67 | auto c = *f; 68 | ++len; 69 | x += c.x; 70 | y += c.y; 71 | z += c.z; 72 | ++f; 73 | } 74 | 75 | auto const dlen = static_cast( len ); 76 | return coordinate_t{ x / dlen, y / dlen, z / dlen }; 77 | } 78 | 79 | int main( ) { 80 | auto left = calc( R"({"coordinates":[{"x":1.1,"y":2.2,"z":3.3}]})" ); 81 | auto right = coordinate_t{ 1.1, 2.2, 3.3 }; 82 | if( left != right ) { 83 | std::cerr << left << " != " << right << '\n'; 84 | exit( EXIT_FAILURE ); 85 | } 86 | 87 | auto const text = daw::read_file( "/tmp/1.json" ).value( ); 88 | 89 | std::cout << calc( text ) << '\n'; 90 | } 91 | -------------------------------------------------------------------------------- /tests/src/kv_map_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_iterator.h" 12 | #include "daw/json/daw_json_link.h" 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | struct kv_t { 24 | std::unordered_map kv{ }; 25 | }; 26 | 27 | struct kv2_t { 28 | daw::bounded_hash_map kv{ }; 29 | }; 30 | 31 | namespace daw::json { 32 | template<> 33 | struct json_data_contract { 34 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 35 | using type = json_member_list< 36 | json_key_value<"kv", std::unordered_map, int>>; 37 | #else 38 | constexpr inline static char const kv[] = "kv"; 39 | using type = json_member_list< 40 | json_key_value, int>>; 41 | #endif 42 | }; 43 | 44 | template<> 45 | struct json_data_contract { 46 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 47 | using type = json_member_list, 49 | int, daw::string_view>>; 50 | #else 51 | constexpr inline static char const kv[] = "kv"; 52 | using type = json_member_list, 54 | int, daw::string_view>>; 55 | #endif 56 | }; 57 | } // namespace daw::json 58 | 59 | int main( int, char ** ) 60 | #if defined( DAW_USE_EXCEPTIONS ) 61 | try 62 | #endif 63 | { 64 | DAW_CONSTEXPR std::string_view const json_data3 = 65 | R"( {"kv": { 66 | "key0": 0, 67 | "key1": 1, 68 | "key2": 2 69 | }})"; 70 | kv_t kv_test = daw::json::from_json( json_data3 ); 71 | daw::do_not_optimize( kv_test ); 72 | 73 | DAW_CONSTEXPR 74 | kv2_t kv2_test = daw::json::from_json( json_data3 ); 75 | test_assert( kv2_test.kv.size( ) == 3, "Unexpected size" ); 76 | test_assert( kv2_test.kv["key0"] == 0, "Unexpected value" ); 77 | test_assert( kv2_test.kv["key1"] == 1, "Unexpected value" ); 78 | test_assert( kv2_test.kv["key2"] == 2, "Unexpected value" ); 79 | } 80 | #if defined( DAW_USE_EXCEPTIONS ) 81 | catch( daw::json::json_exception const &jex ) { 82 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 83 | exit( 1 ); 84 | } catch( std::exception const &ex ) { 85 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 86 | << '\n'; 87 | exit( 1 ); 88 | } catch( ... ) { 89 | std::cerr << "Unknown exception thrown during testing\n"; 90 | throw; 91 | } 92 | #endif 93 | -------------------------------------------------------------------------------- /tests/src/multi_tu_p0_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link.h 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | struct X { 36 | int y; 37 | }; 38 | 39 | namespace daw::json { 40 | template<> 41 | struct json_data_contract { 42 | static constexpr char const y[] = "y"; 43 | using type = json_member_list>; 44 | 45 | [[nodiscard]] static constexpr auto to_json_data( X const &x ) { 46 | return std::forward_as_tuple( x.y ); 47 | } 48 | }; 49 | } // namespace daw::json 50 | 51 | X parse_X( daw::string_view json ) { 52 | return daw::json::from_json( json ); 53 | } 54 | 55 | std::string from_X( X const &x ) { 56 | return daw::json::to_json( x ); 57 | } 58 | 59 | struct Z { 60 | int y; 61 | }; 62 | 63 | Z parse_Z( daw::string_view json ); 64 | std::string from_Z( Z const &x ); 65 | 66 | int main( ) { 67 | static constexpr std::string_view json_doc = R"json({ "y": 42 })json"; 68 | auto x = parse_X( json_doc ); 69 | auto const z = parse_Z( json_doc ); 70 | x.y += z.y; 71 | auto const xdoc = from_X( x ); 72 | auto const x2 = parse_X( xdoc ); 73 | ensure( x2.y == x.y ); 74 | } 75 | -------------------------------------------------------------------------------- /tests/src/multi_tu_p1_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link.h 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | struct Z { 33 | int y; 34 | }; 35 | 36 | namespace daw::json { 37 | template<> 38 | struct json_data_contract { 39 | static constexpr char const y[] = "y"; 40 | using type = json_member_list>; 41 | 42 | [[nodiscard]] static constexpr auto to_json_data( Z const &x ) { 43 | return std::forward_as_tuple( x.y ); 44 | } 45 | }; 46 | } // namespace daw::json 47 | 48 | Z parse_Z( daw::string_view json ) { 49 | return daw::json::from_json( json ); 50 | } 51 | 52 | std::string from_Z( Z const &x ) { 53 | return daw::json::to_json( x ); 54 | } 55 | -------------------------------------------------------------------------------- /tests/src/out_of_order_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | struct Foo { 18 | double a; 19 | double b; 20 | bool c; 21 | bool d; 22 | int e; 23 | int f; 24 | unsigned g; 25 | unsigned h; 26 | }; 27 | 28 | namespace daw::json { 29 | template<> 30 | struct json_data_contract { 31 | static constexpr char const a[] = "a"; 32 | static constexpr char const b[] = "b"; 33 | static constexpr char const c[] = "c"; 34 | static constexpr char const d[] = "d"; 35 | static constexpr char const e[] = "e"; 36 | static constexpr char const f[] = "f"; 37 | static constexpr char const g[] = "g"; 38 | static constexpr char const h[] = "h"; 39 | using type = json_member_list< 40 | json_number, 42 | json_number, 44 | json_bool, 46 | json_bool, 48 | json_number, 50 | json_number, 52 | json_number, 54 | json_number>; 56 | 57 | static constexpr auto to_json_data( Foo const &foo ) { 58 | return std::forward_as_tuple( foo.a, foo.b, foo.c, foo.d, foo.e, foo.f, 59 | foo.g, foo.h ); 60 | } 61 | }; 62 | } // namespace daw::json 63 | 64 | int main( ) { 65 | { 66 | std::string_view json_doc = 67 | R"json({"b":"1.23456","a":"6.54321","d":"false","c":"true","f":"-123","e":"-321","h":"123","g":"321"})json"; 68 | auto const f = daw::json::from_json( json_doc ); 69 | assert( f.a >= 6.54320 and f.a <= 6.54322 ); 70 | assert( f.b >= 1.23455 and f.b <= 1.23457 ); 71 | assert( f.c ); 72 | assert( not f.d ); 73 | assert( f.e == -321 ); 74 | assert( f.f == -123 ); 75 | assert( f.g == 321 ); 76 | assert( f.h == 123 ); 77 | std::cout << daw::json::to_json( f ) << '\n'; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/src/small_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "daw/json/daw_json_link.h" 12 | 13 | #include 14 | 15 | #include 16 | 17 | namespace daw { 18 | struct Data { 19 | int a; 20 | }; 21 | 22 | bool operator==( Data const &lhs, Data const &rhs ) { 23 | return lhs.a == rhs.a; 24 | } 25 | } // namespace daw 26 | 27 | namespace daw::json { 28 | template<> 29 | struct json_data_contract { 30 | #if defined( DAW_JSON_CNTTP_JSON_NAME ) 31 | using type = json_member_list>; 32 | #else 33 | static constexpr char const a[] = "a"; 34 | using type = json_member_list>; 35 | #endif 36 | static inline auto to_json_data( daw::Data const &value ) { 37 | return std::forward_as_tuple( value.a ); 38 | } 39 | }; // namespace daw::json 40 | } // namespace daw::json 41 | 42 | int main( int argc, char **argv ) 43 | #if defined( DAW_USE_EXCEPTIONS ) 44 | try 45 | #endif 46 | { 47 | if( argc <= 1 ) { 48 | puts( "Must supply path to small_test.json file\n" ); 49 | exit( EXIT_FAILURE ); 50 | } 51 | auto data = daw::read_file( argv[1] ).value( ); 52 | 53 | auto const cls = daw::json::from_json( 54 | std::string_view( data.data( ), data.size( ) ) ); 55 | 56 | test_assert( cls.a == 12345, "Unexpected value" ); 57 | } 58 | #if defined( DAW_USE_EXCEPTIONS ) 59 | catch( daw::json::json_exception const &jex ) { 60 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 61 | exit( 1 ); 62 | } catch( std::exception const &ex ) { 63 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 64 | << '\n'; 65 | exit( 1 ); 66 | } catch( ... ) { 67 | std::cerr << "Unknown exception thrown during testing\n"; 68 | throw; 69 | } 70 | #endif -------------------------------------------------------------------------------- /tests/src/test_details_parse_options.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | // Ensure that parse_options is working as expected 10 | // 11 | 12 | #include 13 | 14 | int main( ) { 15 | using namespace daw::json; 16 | { 17 | static constexpr auto precise_opt = 18 | parse_options( options::IEEE754Precise::yes ); 19 | static constexpr auto precise_val = 20 | json_details::get_bits_for( precise_opt ); 21 | static_assert( precise_val == options::IEEE754Precise::yes ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/src/test_details_parse_value_iso8601.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | #include "twitter_test_json.h" 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | int main( ) 22 | #if defined( DAW_USE_EXCEPTIONS ) 23 | try 24 | #endif 25 | { 26 | DAW_CONSTEXPR std::string_view sv = 27 | R"json(Sun Dec 4 12:51:18 +0000 2011")json"; 28 | // mo = 12, dy = 4, hr = 12, mn = 51, se=18, yr = 2011 29 | auto result = daw::twitter::TimestampConverter{ }( sv ); 30 | (void)result; 31 | } 32 | #if defined( DAW_USE_EXCEPTIONS ) 33 | catch( daw::json::json_exception const &jex ) { 34 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 35 | exit( 1 ); 36 | } catch( std::exception const &ex ) { 37 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 38 | << '\n'; 39 | exit( 1 ); 40 | } catch( ... ) { 41 | std::cerr << "Unknown exception thrown during testing\n"; 42 | throw; 43 | } 44 | #endif -------------------------------------------------------------------------------- /tests/src/test_json_array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | struct Array { 17 | std::vector v; 18 | }; 19 | 20 | namespace daw::json { 21 | template<> 22 | struct json_data_contract { 23 | static constexpr char const v[] = "v"; 24 | using type = json_member_list>>; 25 | 26 | static constexpr auto to_json_data( Array const &a ) { 27 | return std::forward_as_tuple( a.v ); 28 | } 29 | }; 30 | } // namespace daw::json 31 | 32 | struct NullableArray { 33 | std::optional> v; 34 | }; 35 | 36 | namespace daw::json { 37 | template<> 38 | struct json_data_contract { 39 | static constexpr char const v[] = "v"; 40 | using type = 41 | json_member_list>>>; 42 | 43 | static constexpr auto to_json_data( NullableArray const &a ) { 44 | return std::forward_as_tuple( a.v ); 45 | } 46 | }; 47 | } // namespace daw::json 48 | 49 | struct NullableArray2 { 50 | std::vector v; 51 | }; 52 | 53 | namespace daw::json { 54 | template<> 55 | struct json_data_contract { 56 | static constexpr char const v[] = "v"; 57 | using type = json_member_list>>; 58 | 59 | static constexpr auto to_json_data( NullableArray const &a ) { 60 | return std::forward_as_tuple( a.v ); 61 | } 62 | }; 63 | } // namespace daw::json 64 | 65 | int main( ) { 66 | { 67 | auto a0 = daw::json::from_json( R"json({"v":[1,2,3,4]})json" ); 68 | daw_ensure( a0.v == std::vector{ 1, 2, 3, 4 } ); 69 | } 70 | 71 | { 72 | auto na0 = 73 | daw::json::from_json( R"json({"v":[1,2,3,4]})json" ); 74 | daw_ensure( na0.v ); 75 | daw_ensure( na0.v == std::vector{ 1, 2, 3, 4 } ); 76 | } 77 | 78 | { 79 | auto na1 = daw::json::from_json( R"json({})json" ); 80 | daw_ensure( not na1.v ); 81 | } 82 | 83 | { 84 | auto na20 = 85 | daw::json::from_json( R"json({"v":[1,2,3,4]})json" ); 86 | daw_ensure( not na20.v.empty( ) ); 87 | daw_ensure( na20.v == std::vector{ 1, 2, 3, 4 } ); 88 | } 89 | 90 | { 91 | auto na21 = daw::json::from_json( R"json({})json" ); 92 | daw_ensure( na21.v.empty( ) ); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /tests/src/test_json_checked_number.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | struct Byte { 19 | std::byte x; 20 | }; 21 | 22 | namespace daw::json { 23 | template<> 24 | struct json_data_contract { 25 | static constexpr char const x[] = "x"; 26 | using type = json_member_list>; 27 | 28 | static constexpr auto to_json_data( Byte const &v ) { 29 | return std::forward_as_tuple( v.x ); 30 | } 31 | }; 32 | } // namespace daw::json 33 | 34 | struct NullableByte { 35 | std::optional x; 36 | }; 37 | 38 | namespace daw::json { 39 | template<> 40 | struct json_data_contract { 41 | static constexpr char const x[] = "x"; 42 | using type = 43 | json_member_list>>; 44 | 45 | static constexpr auto to_json_data( NullableByte const &v ) { 46 | return std::forward_as_tuple( v.x ); 47 | } 48 | }; 49 | } // namespace daw::json 50 | 51 | static_assert( daw::json::from_json( R"json({"x":42})json" ).x == 52 | std::byte{ 42 } ); 53 | static_assert( not daw::json::from_json( R"json({})json" ).x ); 54 | static_assert( daw::json::from_json( R"json({"x":42})json" ).x == 55 | std::byte{ 42 } ); 56 | 57 | int main( ) { 58 | #if defined( DAW_USE_EXCEPTIONS ) 59 | bool success = false; 60 | try { 61 | (void)daw::json::from_json( R"json({"x":1024})json" ); 62 | success = false; 63 | } catch( std::exception const & ) { success = true; } 64 | try { 65 | (void)daw::json::from_json( R"json({"x":1024})json" ); 66 | success = false; 67 | } catch( std::exception const & ) { success = true; } 68 | daw_ensure( success ); 69 | #endif 70 | } 71 | -------------------------------------------------------------------------------- /tests/src/test_json_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | struct X { 18 | int a; 19 | }; 20 | 21 | namespace daw::json { 22 | template<> 23 | struct json_data_contract { 24 | static constexpr char const a[] = "a"; 25 | using type = json_member_list>; 26 | 27 | static constexpr auto to_json_data( X const &x ) { 28 | return std::forward_as_tuple( x.a ); 29 | } 30 | }; 31 | } // namespace daw::json 32 | 33 | int main( ) { 34 | static constexpr daw::string_view json_doc = R"json( 35 | [ 36 | {"a":1}, 37 | {"a":2}, 38 | {"a":3} 39 | ] 40 | )json"; 41 | 42 | auto it = daw::json::json_array_iterator( json_doc ); 43 | int sum = 0; 44 | for( X x : it ) { 45 | sum += x.a; 46 | } 47 | daw_ensure( sum == 6 ); 48 | } 49 | -------------------------------------------------------------------------------- /tests/src/test_json_key_value_array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | struct KVArray { 17 | std::map kv; 18 | }; 19 | 20 | namespace daw::json { 21 | template<> 22 | struct json_data_contract { 23 | static constexpr char const kv[] = "kv"; 24 | using type = 25 | json_member_list>>; 26 | 27 | static inline auto to_json_data( KVArray const &v ) { 28 | return std::forward_as_tuple( v.kv ); 29 | } 30 | }; 31 | } // namespace daw::json 32 | 33 | struct NullableKVArray { 34 | std::optional> kv; 35 | }; 36 | 37 | namespace daw::json { 38 | template<> 39 | struct json_data_contract { 40 | static constexpr char const kv[] = "kv"; 41 | using type = json_member_list< 42 | json_key_value_array_null>>>; 43 | 44 | static inline auto to_json_data( NullableKVArray const &v ) { 45 | return std::forward_as_tuple( v.kv ); 46 | } 47 | }; 48 | } // namespace daw::json 49 | 50 | struct NullableKVArray2 { 51 | std::map kv; 52 | }; 53 | 54 | namespace daw::json { 55 | template<> 56 | struct json_data_contract { 57 | static constexpr char const kv[] = "kv"; 58 | using type = json_member_list< 59 | json_key_value_array_null>>; 60 | 61 | static inline auto to_json_data( NullableKVArray2 const &v ) { 62 | return std::forward_as_tuple( v.kv ); 63 | } 64 | }; 65 | } // namespace daw::json 66 | 67 | int main( ) { 68 | { 69 | auto kv0 = daw::json::from_json( 70 | R"json({"kv":[{"key":"meaning of life","value":42}]})json" ); 71 | daw_ensure( kv0.kv.find( "meaning of life" ) != kv0.kv.end( ) ); 72 | } 73 | { 74 | auto nkv0 = daw::json::from_json( 75 | R"json({"kv":[{"key":"meaning of life","value":42}]})json" ); 76 | daw_ensure( nkv0.kv ); 77 | daw_ensure( nkv0.kv->find( "meaning of life" ) != nkv0.kv->end( ) ); 78 | } 79 | { 80 | auto nkv0 = daw::json::from_json( R"json({})json" ); 81 | daw_ensure( not nkv0.kv ); 82 | } 83 | { 84 | auto nkv20 = daw::json::from_json( 85 | R"json({"kv":[{"key":"meaning of life","value":42}]})json" ); 86 | daw_ensure( not nkv20.kv.empty( ) ); 87 | daw_ensure( nkv20.kv.find( "meaning of life" ) != nkv20.kv.end( ) ); 88 | } 89 | { 90 | auto nkv20 = daw::json::from_json( R"json({})json" ); 91 | daw_ensure( nkv20.kv.empty( ) ); 92 | } 93 | } -------------------------------------------------------------------------------- /tests/src/test_json_raw.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | struct Raw { 14 | daw::json::json_value raw_json; 15 | }; 16 | 17 | namespace daw::json { 18 | template<> 19 | struct json_data_contract { 20 | static constexpr char const raw_json[] = "raw_json"; 21 | using type = json_member_list>; 22 | 23 | static auto to_json_data( Raw const &v ) { 24 | return std::forward_as_tuple( v.raw_json ); 25 | } 26 | }; 27 | } // namespace daw::json 28 | 29 | static_assert( daw::json::from_json( R"json({"raw_json":[1,2,3]})json" ) 30 | .raw_json.is_array( ) ); 31 | 32 | struct NullableRaw { 33 | std::optional raw_json; 34 | }; 35 | 36 | namespace daw::json { 37 | template<> 38 | struct json_data_contract { 39 | static constexpr char const raw_json[] = "raw_json"; 40 | using type = json_member_list>; 41 | 42 | static auto to_json_data( Raw const &v ) { 43 | return std::forward_as_tuple( v.raw_json ); 44 | } 45 | }; 46 | } // namespace daw::json 47 | 48 | constexpr auto nr0 = 49 | daw::json::from_json( R"json({"raw_json":[1,2,3]})json" ); 50 | static_assert( nr0.raw_json ); 51 | static_assert( nr0.raw_json->is_array( ) ); 52 | 53 | int main( ) { 54 | // 55 | } 56 | -------------------------------------------------------------------------------- /tests/src/test_json_sized_array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | struct Stuff { 17 | std::size_t size; 18 | std::unique_ptr values; 19 | }; 20 | 21 | template 22 | struct UniquePtrArrayCtor { 23 | constexpr std::unique_ptr operator( )( ) const { 24 | return { }; 25 | } 26 | 27 | template 28 | inline std::unique_ptr operator( )( Iterator first, Iterator last, 29 | std::size_t sz ) const { 30 | assert( sz <= 1024 ); // ensure size isn't too large 31 | auto result = std::unique_ptr( new T[static_cast( sz )] ); 32 | auto out_last = std::copy( first, last, result.get( ) ); 33 | (void)out_last; 34 | auto const elements_copied = out_last - result.get( ); 35 | (void)elements_copied; 36 | assert( elements_copied >= 0 ); 37 | assert( static_cast( elements_copied ) == sz ); 38 | 39 | return result; 40 | } 41 | }; 42 | 43 | namespace daw::json { 44 | template<> 45 | struct json_data_contract { 46 | static constexpr const char values[] = "values"; 47 | static constexpr const char size[] = "size"; 48 | using type = json_member_list< 49 | json_number, 50 | json_sized_array, 51 | std::unique_ptr, UniquePtrArrayCtor>>; 52 | 53 | static constexpr auto to_json_data( Stuff const &s ) { 54 | return std::forward_as_tuple( s.size, s.values ); 55 | } 56 | }; 57 | } // namespace daw::json 58 | 59 | int main( ) { 60 | static constexpr daw::string_view json_doc = R"json( 61 | { 62 | "size": 3, 63 | "values": [1,2,3] 64 | } 65 | )json"; 66 | 67 | auto my_stuff = daw::json::from_json( json_doc ); 68 | daw_ensure( my_stuff.size == 3 ); 69 | daw_ensure( my_stuff.values[0] == 1 ); 70 | daw_ensure( my_stuff.values[1] == 2 ); 71 | daw_ensure( my_stuff.values[2] == 3 ); 72 | } 73 | -------------------------------------------------------------------------------- /tests/src/test_json_tuple.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | struct Tuple { 17 | std::tuple a; 18 | 19 | constexpr bool operator==( Tuple const &rhs ) const { 20 | return a == rhs.a; 21 | } 22 | }; 23 | 24 | namespace daw::json { 25 | template<> 26 | struct json_data_contract { 27 | static constexpr char const a[] = "a"; 28 | using type = 29 | json_member_list>>; 30 | 31 | static constexpr auto to_json_data( Tuple const &t ) { 32 | return std::forward_as_tuple( t.a ); 33 | } 34 | }; 35 | } // namespace daw::json 36 | 37 | constexpr auto tp0 = 38 | daw::json::from_json( R"json({"a":[5,false,-42]})json" ); 39 | static_assert( std::get<0>( tp0.a ) == 5 ); 40 | static_assert( std::get<1>( tp0.a ) == false ); 41 | static_assert( std::get<2>( tp0.a ) == -42 ); 42 | // 43 | struct NullableTuple { 44 | std::optional> a; 45 | 46 | constexpr bool operator==( NullableTuple const &rhs ) const { 47 | return a == rhs.a; 48 | } 49 | }; 50 | 51 | namespace daw::json { 52 | template<> 53 | struct json_data_contract { 54 | static constexpr char const a[] = "a"; 55 | using type = json_member_list< 56 | json_tuple_null>>>; 57 | 58 | static constexpr auto to_json_data( NullableTuple const &t ) { 59 | return std::forward_as_tuple( t.a ); 60 | } 61 | }; 62 | } // namespace daw::json 63 | 64 | // optional's copy ctor is only constexpr in C++20 65 | #if defined( __cpp_lib_optional ) 66 | #if __cpp_lib_optional >= 202106L 67 | #define DAW_HAS_OPTIONAL_CX_COPY 68 | #endif 69 | #endif 70 | 71 | #if defined( DAW_HAS_OPTIONAL_CX_COPY ) 72 | constexpr auto tp1 = 73 | daw::json::from_json( R"json({"a":[5,false,-42]})json" ); 74 | static_assert( tp1.a ); 75 | static_assert( std::get<0>( *tp1.a ) == 5 ); 76 | static_assert( std::get<1>( *tp1.a ) == false ); 77 | static_assert( std::get<2>( *tp1.a ) == -42 ); 78 | static_assert( not daw::json::from_json( R"json({})json" ).a ); 79 | #endif 80 | 81 | int main( ) { 82 | #if not defined( DAW_HAS_OPTIONAL_CX_COPY ) 83 | auto tp = 84 | daw::json::from_json( R"json({"a":[5,false,-42]})json" ); 85 | daw_ensure( tp.a ); 86 | daw_ensure( std::get<0>( *tp.a ) == 5 ); 87 | daw_ensure( std::get<1>( *tp.a ) == false ); 88 | daw_ensure( std::get<2>( *tp.a ) == -42 ); 89 | daw_ensure( not daw::json::from_json( R"json({})json" ).a ); 90 | #endif 91 | } 92 | -------------------------------------------------------------------------------- /tests/src/test_stateful_json_value.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | struct coordinate_t { 21 | double x; 22 | double y; 23 | double z; 24 | 25 | DAW_CONSTEXPR bool operator!=( const coordinate_t &r ) const { 26 | return x != r.x and y != r.y and z != r.z; 27 | } 28 | }; 29 | 30 | coordinate_t calc( std::string_view text ) { 31 | using namespace daw::json; 32 | double x = 0.0; 33 | double y = 0.0; 34 | double z = 0.0; 35 | int len = 0; 36 | 37 | auto jv = basic_json_value( text ); 38 | auto state = basic_stateful_json_value( jv ); 39 | jv = state["coordinates"]; 40 | for( auto c : jv ) { 41 | state.reset( c.value ); 42 | ++len; 43 | static DAW_CONSTEXPR auto mem_x = json_member_name( "x" ); 44 | static DAW_CONSTEXPR auto mem_y = json_member_name( "y" ); 45 | static DAW_CONSTEXPR auto mem_z = json_member_name( "z" ); 46 | x += from_json( state[mem_x] ); 47 | y += from_json( state[mem_y] ); 48 | z += from_json( state[mem_z] ); 49 | } 50 | 51 | return coordinate_t{ x / len, y / len, z / len }; 52 | } 53 | 54 | int main( int argc, char **argv ) 55 | #if defined( DAW_USE_EXCEPTIONS ) 56 | try 57 | #endif 58 | { 59 | if( argc <= 1 ) { 60 | std::cout << "Must supply path to test_stateful_json_value.json file\n"; 61 | exit( EXIT_FAILURE ); 62 | } 63 | auto const json_data = daw::read_file( argv[1] ).value( ); 64 | auto coords = calc( json_data ); 65 | daw::do_not_optimize( coords ); 66 | std::cout << "x: " << coords.x << " y: " << coords.y << " z: " << coords.z 67 | << '\n'; 68 | } 69 | #if defined( DAW_USE_EXCEPTIONS ) 70 | catch( daw::json::json_exception const &jex ) { 71 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 72 | exit( 1 ); 73 | } catch( std::exception const &ex ) { 74 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 75 | << '\n'; 76 | exit( 1 ); 77 | } catch( ... ) { 78 | std::cerr << "Unknown exception thrown during testing\n"; 79 | throw; 80 | } 81 | #endif -------------------------------------------------------------------------------- /tests/src/twitter_output_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | #include "defines.h" 9 | 10 | #include "twitter_test_json.h" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #if not defined( DAW_NUM_RUNS ) 23 | #if not defined( DEBUG ) or defined( NDEBUG ) 24 | static inline constexpr std::size_t DAW_NUM_RUNS = 250; 25 | #else 26 | static inline constexpr std::size_t DAW_NUM_RUNS = 2; 27 | #endif 28 | #endif 29 | static_assert( DAW_NUM_RUNS > 0 ); 30 | 31 | using namespace daw::json::options; 32 | 33 | int main( int argc, char **argv ) 34 | #if defined( DAW_USE_EXCEPTIONS ) 35 | try 36 | #endif 37 | { 38 | using namespace daw::json; 39 | if( argc < 2 ) { 40 | std::cerr << "Must supply twitter.json path\n"; 41 | exit( 1 ); 42 | } 43 | 44 | std::string const json_data = [argv] { 45 | auto const mmf = daw::read_file( argv[1] ).value( ); 46 | test_assert( mmf.size( ) > 2, "Minimum json data size is 2 '{}'" ); 47 | return std::string( mmf.data( ), mmf.size( ) ); 48 | }( ); 49 | 50 | auto const sz = json_data.size( ); 51 | std::cout << "Processing: " << daw::utility::to_bytes_per_second( sz ) 52 | << '\n'; 53 | 54 | // ****************************** 55 | // Test serialization 56 | std::optional twitter_result = 57 | daw::json::from_json( json_data ); 58 | std::string str{ }; 59 | (void)daw::bench_n_test_mbs( 60 | "twitter bench(to_json_string)", sz, 61 | [&]( auto const &tr ) { 62 | str.clear( ); 63 | daw::json::to_json( *tr, str ); 64 | daw::do_not_optimize( str ); 65 | }, 66 | twitter_result ); 67 | test_assert( not str.empty( ), "Expected a string value" ); 68 | daw::do_not_optimize( str ); 69 | auto const twitter_result2 = 70 | daw::json::from_json( str ); 71 | daw::do_not_optimize( twitter_result2 ); 72 | } 73 | #if defined( DAW_USE_EXCEPTIONS ) 74 | catch( daw::json::json_exception const &jex ) { 75 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 76 | exit( 1 ); 77 | } catch( std::exception const &ex ) { 78 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 79 | << '\n'; 80 | exit( 1 ); 81 | } catch( ... ) { 82 | std::cerr << "Unknown exception thrown during testing\n"; 83 | throw; 84 | } 85 | #endif -------------------------------------------------------------------------------- /tests/src/twitter_test_basic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "twitter_test_json.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | int main( int argc, char **argv ) 21 | #if defined( DAW_USE_EXCEPTIONS ) 22 | try 23 | #endif 24 | { 25 | if( argc < 2 ) { 26 | std::cerr << "Must supply a file name\n"; 27 | exit( 1 ); 28 | } 29 | bool const do_asserts = [&] { 30 | if( argc > 2 ) { 31 | std::string_view arg2 = argv[2]; 32 | return arg2 != "noassert"; 33 | } 34 | return true; 35 | }( ); 36 | using namespace daw::json; 37 | std::string const json_data = [argv] { 38 | auto const mmf = daw::read_file( argv[1] ).value( ); 39 | return std::string( mmf.data( ), mmf.size( ) ); 40 | }( ); 41 | 42 | daw::twitter::twitter_object_t twitter_result; 43 | for( std::size_t n = 0; n < 1'000; ++n ) { 44 | daw::do_not_optimize( json_data ); 45 | twitter_result = 46 | daw::json::from_json( json_data ); 47 | daw::do_not_optimize( twitter_result ); 48 | } 49 | if( do_asserts ) { 50 | test_assert( not twitter_result.statuses.empty( ), "Expected values" ); 51 | test_assert( twitter_result.statuses.front( ).user.id == 1186275104, 52 | "Missing value" ); 53 | } 54 | } 55 | #if defined( DAW_USE_EXCEPTIONS ) 56 | catch( daw::json::json_exception const &jex ) { 57 | std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n'; 58 | exit( 1 ); 59 | } catch( std::exception const &ex ) { 60 | std::cerr << "Unknown exception thrown during testing: " << ex.what( ) 61 | << '\n'; 62 | exit( 1 ); 63 | } catch( ... ) { 64 | std::cerr << "Unknown exception thrown during testing\n"; 65 | throw; 66 | } 67 | #endif -------------------------------------------------------------------------------- /tests/src/twitter_test_basic2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include "defines.h" 10 | 11 | #include "twitter_test2_json.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | int main( int argc, char **argv ) { 21 | if( argc < 2 ) { 22 | std::cerr << "Must supply a file name\n"; 23 | exit( 1 ); 24 | } 25 | using namespace daw::json; 26 | auto const json_data1 = daw::read_file( argv[1] ).value( ); 27 | auto const json_sv1 = 28 | std::string_view( json_data1.data( ), json_data1.size( ) ); 29 | #if defined( DAW_USE_EXCEPTIONS ) 30 | try 31 | #endif 32 | { 33 | auto const twitter_result = 34 | daw::json::from_json( json_sv1 ); 35 | daw::do_not_optimize( twitter_result ); 36 | test_assert( not twitter_result.statuses.empty( ), "Expected values" ); 37 | test_assert( twitter_result.statuses.front( ).user.id == "1186275104", 38 | "Missing value" ); 39 | } 40 | #if defined( DAW_USE_EXCEPTIONS ) 41 | catch( daw::json::json_exception const &jex ) { 42 | std::cerr << "Exception thrown by parser: " 43 | << to_formatted_string( jex, json_data1.data( ) ) << '\n'; 44 | if( jex.parse_location( ) ) { 45 | std::cerr << "Error happened around " 46 | << ( jex.parse_location( ) - json_sv1.data( ) ) 47 | << " bytes into file\n"; 48 | } 49 | exit( 1 ); 50 | } 51 | #endif 52 | } 53 | -------------------------------------------------------------------------------- /tests/src/utf16_string_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Darrell Wright 2 | // 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 | // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | // 6 | // Official repository: https://github.com/beached/daw_json_link 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | /*** 16 | * This example shows how to parse a utf8 JSON string with the result being a 17 | * std::u16string. This applies equally to std::wstring on windows, I believe. 18 | */ 19 | 20 | struct ToConverter { 21 | std::u16string operator( )( ) const { 22 | return { }; 23 | } 24 | 25 | std::u16string operator( )( std::string_view sv ) const { 26 | char const *first = std::data( sv ); 27 | char const *last = daw::data_end( sv ); 28 | auto result = std::u16string( ); 29 | result.resize( sv.size( ) ); 30 | auto *olast = 31 | daw::utf8::unchecked::utf8to16( first, last, std::data( result ) ); 32 | result.resize( 33 | static_cast( std::distance( std::data( result ), olast ) ) ); 34 | return result; 35 | } 36 | }; 37 | 38 | struct FromConverter { 39 | std::string operator( )( std::u16string const &str ) const { 40 | char16_t const *first = std::data( str ); 41 | char16_t const *last = daw::data_end( str ); 42 | auto result = std::string( ); 43 | result.resize( str.size( ) * 3 ); 44 | auto *olast = 45 | daw::utf8::unchecked::utf16to8( first, last, std::data( result ) ); 46 | result.resize( 47 | static_cast( std::distance( std::data( result ), olast ) ) ); 48 | return result; 49 | } 50 | }; 51 | 52 | using json_u16string = 53 | daw::json::json_custom_no_name; 54 | 55 | int main( int, char ** ) { 56 | std::string in_str = R"json(["testing🎉", "🙈monkey"])json"; 57 | 58 | puts( "Before" ); 59 | puts( in_str.c_str( ) ); 60 | 61 | std::vector ary = 62 | daw::json::from_json_array( in_str ); 63 | 64 | assert( ary.size( ) == 2 ); 65 | 66 | auto out_str = daw::json::to_json_array( ary ); 67 | 68 | puts( "After" ); 69 | puts( out_str.c_str( ) ); 70 | } 71 | --------------------------------------------------------------------------------