diff --git a/docs/mkdocs/docs/api/macros/json_use_implicit_conversions.md b/docs/mkdocs/docs/api/macros/json_use_implicit_conversions.md index 675a8acee..2e7781378 100644 --- a/docs/mkdocs/docs/api/macros/json_use_implicit_conversions.md +++ b/docs/mkdocs/docs/api/macros/json_use_implicit_conversions.md @@ -67,7 +67,7 @@ By default, implicit conversions are enabled. `JSON_USE_IMPLICIT_CONVERSIONS` is defined to `0`: ```cpp - using wjson = nlohmann::basic_json; + using wjson = nlohmann::json::with_string_t; void load(const nlohmann::json& j); diff --git a/docs/mkdocs/docs/examples/custom_array_type.cpp b/docs/mkdocs/docs/examples/custom_array_type.cpp index 63651cadf..9765c51e8 100644 --- a/docs/mkdocs/docs/examples/custom_array_type.cpp +++ b/docs/mkdocs/docs/examples/custom_array_type.cpp @@ -1,11 +1,10 @@ #include -#include #include #include "custom_array_type.hpp" -using custom_json = nlohmann::basic_json; +using custom_json = nlohmann::json::with_array_t; int main() { diff --git a/docs/mkdocs/docs/examples/custom_binary_type.cpp b/docs/mkdocs/docs/examples/custom_binary_type.cpp index cea34ea38..0a835d475 100644 --- a/docs/mkdocs/docs/examples/custom_binary_type.cpp +++ b/docs/mkdocs/docs/examples/custom_binary_type.cpp @@ -1,16 +1,10 @@ -#include #include -#include -#include -#include #include #include "custom_binary_type.hpp" -using custom_json = nlohmann::basic_json; +using custom_json = nlohmann::json::with_binary_t; int main() { diff --git a/docs/mkdocs/docs/examples/custom_object_type.cpp b/docs/mkdocs/docs/examples/custom_object_type.cpp index d4f99ccf0..9d4b43daf 100644 --- a/docs/mkdocs/docs/examples/custom_object_type.cpp +++ b/docs/mkdocs/docs/examples/custom_object_type.cpp @@ -1,12 +1,11 @@ #include #include -#include #include #include "custom_object_type.hpp" -using custom_json = nlohmann::basic_json; +using custom_json = nlohmann::json::with_object_t; int main() { diff --git a/docs/mkdocs/docs/examples/custom_string_type.cpp b/docs/mkdocs/docs/examples/custom_string_type.cpp index 63b798fc6..2e71c9378 100644 --- a/docs/mkdocs/docs/examples/custom_string_type.cpp +++ b/docs/mkdocs/docs/examples/custom_string_type.cpp @@ -1,12 +1,10 @@ #include -#include -#include #include #include "custom_string_type.hpp" -using custom_json = nlohmann::basic_json; +using custom_json = nlohmann::json::with_string_t; int main() { diff --git a/docs/mkdocs/docs/features/types/number_handling.md b/docs/mkdocs/docs/features/types/number_handling.md index bae315160..13b6dfd43 100644 --- a/docs/mkdocs/docs/features/types/number_handling.md +++ b/docs/mkdocs/docs/features/types/number_handling.md @@ -351,9 +351,8 @@ The number types can be changed with template parameters. A `basic_json` type that uses `#!c long double` as floating-point type. - ```cpp hl_lines="2" - using json_ld = nlohmann::basic_json; + ```cpp hl_lines="1" + using json_ld = nlohmann::json::with_float_t; ``` Note values should then be parsed with `json_ld::parse` rather than `json::parse` as the latter would parse diff --git a/docs/mkdocs/docs/features/types/template_parameters.md b/docs/mkdocs/docs/features/types/template_parameters.md index a83b7a56a..f20102878 100644 --- a/docs/mkdocs/docs/features/types/template_parameters.md +++ b/docs/mkdocs/docs/features/types/template_parameters.md @@ -8,6 +8,10 @@ these requirements so they do not have to be discovered by trial and error. Each that are known to work for that parameter and the ones that do not, checked against Boost 1.83, Abseil 20250127.0, Folly, EASTL 3.21, `ankerl::unordered_dense`, `phmap`, `gtl`, `robin_hood`, `tsl::ordered_map`, and Qt 6. +To change a single template parameter and keep the others, use the member alias templates +[`with_*_t`](../../api/basic_json/with_t.md); for instance, `nlohmann::json::with_float_t` is `json` with +`#!cpp long double` as [`number_float_t`](../../api/basic_json/number_float_t.md). + ## How to read this page Requirements are split into two groups: @@ -143,7 +147,7 @@ struct unordered_map_object using base_t::base_t; }; -using unordered_json = nlohmann::basic_json; +using unordered_json = nlohmann::json::with_object_t; ``` Whether `#!cpp std::unordered_map` can be instantiated at all depends on the standard library: `object_t` is formed @@ -176,7 +180,7 @@ struct flat_hash_object using base_t::base_t; }; -using flat_hash_json = nlohmann::basic_json; +using flat_hash_json = nlohmann::json::with_object_t; ``` `absl::node_hash_map` keeps references to the mapped values valid across insertions; `absl::flat_hash_map` does not, diff --git a/docs/mkdocs/docs/integration/migration_guide.md b/docs/mkdocs/docs/integration/migration_guide.md index 17b2eb360..bfbf850e0 100644 --- a/docs/mkdocs/docs/integration/migration_guide.md +++ b/docs/mkdocs/docs/integration/migration_guide.md @@ -116,7 +116,7 @@ function to use instead. === "Deprecated" ```cpp - using my_json = nlohmann::basic_json; + using my_json = nlohmann::json::with_string_t; nlohmann::json_pointer ptr("/foo/bar/1"); ``` diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 07eb21c4e..4062870d5 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -370,14 +370,7 @@ TEST_CASE("copy of a deeply nested value survives a failing allocation (#5640)") #if !(defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0) SECTION("std::map-backed object_t") { - using bad_alloc_json = nlohmann::basic_json; + using bad_alloc_json = nlohmann::json::with_allocator_t; check_deep_copy_survives_failing_allocation(false); check_deep_copy_survives_failing_allocation(true); @@ -385,14 +378,7 @@ TEST_CASE("copy of a deeply nested value survives a failing allocation (#5640)") SECTION("ordered_map-backed object_t") { - using bad_alloc_ordered_json = nlohmann::basic_json; + using bad_alloc_ordered_json = nlohmann::ordered_json::with_allocator_t; check_deep_copy_survives_failing_allocation(false); check_deep_copy_survives_failing_allocation(true); @@ -450,14 +436,7 @@ struct scratch_counting_allocator : std::allocator TEST_CASE("deep copy uses the provided allocator") { - using counting_json = nlohmann::basic_json; + using counting_json = nlohmann::json::with_allocator_t; // deeper than the 128 levels the copy constructor descends into, so the // innermost objects are copied by the iterative deep copy @@ -516,14 +495,7 @@ TEST_CASE("converting a deeply nested value from another specialization fails cl // the allocator in noexcept constructors, so a failing construction crashes // the program there instead of throwing std::bad_alloc. Nothing to check. #if !(defined(_MSC_VER) && _MSC_VER < 1910 && defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0) - using countdown_json = nlohmann::basic_json; + using countdown_json = nlohmann::json::with_allocator_t; // deeper than the 128 levels the converting constructor descends into, so // that failures land on both sides of the bound - or, built with @@ -631,14 +603,7 @@ TEST_CASE("destructor performs no allocation, only deallocation") // Since that stack could itself throw bad_alloc from inside the // noexcept destructor (#5135), destroy() no longer allocates anything: // it only ever frees what is already there. - using counting_json = nlohmann::basic_json; + using counting_json = nlohmann::json::with_allocator_t; SECTION("array") { @@ -683,14 +648,7 @@ TEST_CASE("destructor performs no allocation, only deallocation") TEST_CASE("a failed allocation leaves the value unchanged") { // create JSON type using the throwing allocator - using my_json = nlohmann::basic_json; + using my_json = nlohmann::json::with_allocator_t; // Each of these creates a string, array, object, or binary value. The // value must be created before the type is changed: otherwise, a failed diff --git a/tests/src/unit-binary_formats.cpp b/tests/src/unit-binary_formats.cpp index 3e5edf765..03dbc94da 100644 --- a/tests/src/unit-binary_formats.cpp +++ b/tests/src/unit-binary_formats.cpp @@ -237,7 +237,7 @@ namespace // the binary formats as function pointers for "Binary formats with narrow number types"; // named functions rather than lambdas, because clang 3.5 cannot convert a lambda // to a function pointer in the braced initializer of the format table -using narrow_json = nlohmann::basic_json; +using narrow_json = nlohmann::json::with_integers_t::with_float_t; using bytes = std::vector; bytes encode_cbor(const json& j) diff --git a/tests/src/unit-comparison.cpp b/tests/src/unit-comparison.cpp index b9257cb17..1e409bfb0 100644 --- a/tests/src/unit-comparison.cpp +++ b/tests/src/unit-comparison.cpp @@ -824,7 +824,7 @@ struct unordered_object_t : std::map, Allocator> return !(lhs == rhs); } }; -using unordered_json = nlohmann::basic_json; +using unordered_json = nlohmann::json::with_object_t; // the entries "0" to "9", enumerated in ascending or in descending order unordered_json make_unordered_object(const bool descending) @@ -875,7 +875,7 @@ struct key_case_less template using key_case_map = std::map; -using key_case_json = nlohmann::basic_json; +using key_case_json = nlohmann::json::with_object_t; // the innermost value of a chain of single-element arrays template @@ -905,7 +905,7 @@ struct case_insensitive_less template using case_insensitive_map = std::map; -using ci_json = nlohmann::basic_json; +using ci_json = nlohmann::json::with_object_t; } // namespace TEST_CASE("equality of objects whose entries have no fixed order") diff --git a/tests/src/unit-custom-array-type.cpp b/tests/src/unit-custom-array-type.cpp index 7a374b612..6f015d234 100644 --- a/tests/src/unit-custom-array-type.cpp +++ b/tests/src/unit-custom-array-type.cpp @@ -22,7 +22,7 @@ namespace // std::deque has no capacity() member function, which the library only needs // to detect a reallocation for JSON_DIAGNOSTICS -using deque_json = nlohmann::basic_json; +using deque_json = nlohmann::json::with_array_t; // a std::vector whose at() is hidden: the library performs its own bounds // check and must not fall back to the container's checked accessor @@ -39,7 +39,7 @@ class vector_without_at : public std::vector void at() = delete; }; -using no_at_json = nlohmann::basic_json; +using no_at_json = nlohmann::json::with_array_t; } // namespace diff --git a/tests/src/unit-custom-object-type.cpp b/tests/src/unit-custom-object-type.cpp index 0eaaf2d3c..fdd063292 100644 --- a/tests/src/unit-custom-object-type.cpp +++ b/tests/src/unit-custom-object-type.cpp @@ -179,7 +179,7 @@ class no_key_compare_map } }; -using no_key_compare_json = nlohmann::basic_json; +using no_key_compare_json = nlohmann::json::with_object_t; // An ObjectType whose erase(iterator) returns void rather than the following // iterator, as for instance Abseil's hash maps do @@ -196,7 +196,7 @@ struct void_erase_map : std::map } }; -using void_erase_json = nlohmann::basic_json; +using void_erase_json = nlohmann::json::with_object_t; // wraps an iterator, but only offers the LegacyForwardIterator operations, // like the iterators of std::unordered_map and other hash maps @@ -388,7 +388,7 @@ class forward_only_map } }; -using forward_only_json = nlohmann::basic_json; +using forward_only_json = nlohmann::json::with_object_t; } // namespace diff --git a/tests/src/unit-locale-cpp.cpp b/tests/src/unit-locale-cpp.cpp index 626296825..4865e5687 100644 --- a/tests/src/unit-locale-cpp.cpp +++ b/tests/src/unit-locale-cpp.cpp @@ -172,7 +172,7 @@ TEST_CASE("locale-dependent test (LC_NUMERIC=de_DE)") // a floating-point type that is not a float or a double is written // with snprintf, whose locale-specific decimal point and thousands // separator are undone afterwards - using long_double_json = nlohmann::basic_json; + using long_double_json = nlohmann::json::with_float_t; CHECK(long_double_json(12345.5L).dump() == "12345.5"); CHECK(long_double_json(1.0L).dump() == "1.0"); CHECK(long_double_json(-0.25L).dump() == "-0.25"); @@ -272,7 +272,7 @@ TEST_CASE("locale changes between lexer construction and number conversion (#519 } text += "]"; - using long_double_json = nlohmann::basic_json; + using long_double_json = nlohmann::json::with_float_t; // reference values, parsed without a locale switch REQUIRE(std::setlocale(LC_NUMERIC, "C") != nullptr); @@ -432,7 +432,7 @@ TEST_CASE("locale changes during a single dump() (#5709 item 3)") // long double on 64-bit Arm, where it is IEEE-754 double) takes the // locale-independent to_chars() path instead, and this test is a no-op // there. - using long_double_json = nlohmann::basic_json; + using long_double_json = nlohmann::json::with_float_t; using ld_limits = std::numeric_limits; const bool is_ieee_single_or_double = (ld_limits::is_iec559 && ld_limits::digits == 24 && ld_limits::max_exponent == 128) || diff --git a/tests/src/unit-msgpack.cpp b/tests/src/unit-msgpack.cpp index b705f94b7..d29769738 100644 --- a/tests/src/unit-msgpack.cpp +++ b/tests/src/unit-msgpack.cpp @@ -2432,8 +2432,8 @@ TEST_CASE("MessagePack numbers use the active union member (see #5644)") // used to read the union member that was not the active one, writing // wrong bytes for some values; std::int64_t/std::uint64_t (the default // types, where both members have the same width) were not affected - using int32_json = nlohmann::basic_json; - using int16_json = nlohmann::basic_json; + using int32_json = nlohmann::json::with_integers_t; + using int16_json = nlohmann::json::with_integers_t; SECTION("number_integer_t = std::int32_t") { diff --git a/tests/src/unit-regression1.cpp b/tests/src/unit-regression1.cpp index 8f0d4c747..084eb79fd 100644 --- a/tests/src/unit-regression1.cpp +++ b/tests/src/unit-regression1.cpp @@ -39,7 +39,7 @@ using nlohmann::json; template using my_workaround_fifo_map = nlohmann::fifo_map, A>; -using my_json = nlohmann::basic_json; +using my_json = nlohmann::json::with_object_t; ///////////////////////////////////////////////////////////////////// // for #977 @@ -86,8 +86,7 @@ struct foo_serializer < T, typename std::enable_if < !std::is_same::valu }; } // namespace ns -using foo_json = nlohmann::basic_json>; +using foo_json = nlohmann::json::with_json_serializer_t; ///////////////////////////////////////////////////////////////////// // for #805 @@ -254,7 +253,7 @@ TEST_CASE("regression tests 1") { // create JSON class with nonstandard integer number type using custom_json = - nlohmann::basic_json; + nlohmann::json::with_integers_t::with_float_t; custom_json j; j["int_1"] = 1; CHECK(j["int_1"] == 1); @@ -470,18 +469,17 @@ TEST_CASE("regression tests 1") // create JSON class with nonstandard float number type // float - nlohmann::basic_json const j_float = + nlohmann::json::with_integers_t::with_float_t const j_float = 1.23e25f; CHECK(j_float.get() == 1.23e25f); // double - nlohmann::basic_json const j_double = + nlohmann::json const j_double = 1.23e35; CHECK(j_double.get() == 1.23e35); // long double - nlohmann::basic_json - const j_long_double = 1.23e45L; + nlohmann::json::with_float_t const j_long_double = 1.23e45L; CHECK(j_long_double.get() == 1.23e45L); } diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index d084e6432..b532e75ae 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -64,18 +64,7 @@ using ordered_json = nlohmann::ordered_json; ///////////////////////////////////////////////////////////////////// // for #4804 ///////////////////////////////////////////////////////////////////// - using json_4804 = nlohmann::basic_json, // BinaryType - void // CustomBaseClass - >; + using json_4804 = nlohmann::json::with_binary_t>; #endif #ifdef JSON_HAS_CPP_20 @@ -107,7 +96,7 @@ DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") // for #1021 ///////////////////////////////////////////////////////////////////// -using float_json = nlohmann::basic_json; +using float_json = nlohmann::json::with_float_t; #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) namespace @@ -155,10 +144,8 @@ struct failing_allocator : std::allocator }; }; -using failing_json = nlohmann::basic_json; -using failing_ordered_json = nlohmann::basic_json; +using failing_json = nlohmann::json::with_allocator_t; +using failing_ordered_json = nlohmann::ordered_json::with_allocator_t; // builds `depth` levels of nesting around a scalar, iteratively (never // recursing: each wrap only moves the previous, already-built value, which diff --git a/tests/src/unit-regression3.cpp b/tests/src/unit-regression3.cpp index 69a95fb9b..f8d9740fa 100644 --- a/tests/src/unit-regression3.cpp +++ b/tests/src/unit-regression3.cpp @@ -62,18 +62,7 @@ using ordered_json = nlohmann::ordered_json; ///////////////////////////////////////////////////////////////////// // for #4804 ///////////////////////////////////////////////////////////////////// - using json_4804 = nlohmann::basic_json, // BinaryType - void // CustomBaseClass - >; + using json_4804 = nlohmann::json::with_binary_t>; #endif #ifdef JSON_HAS_CPP_20 @@ -930,7 +919,7 @@ TEST_CASE("regression test #5476 - array type without reserve()") { // the capacity reserved for definite-length arrays must not require the // array type to have a reserve() member function - using deque_json = nlohmann::basic_json; + using deque_json = nlohmann::json::with_array_t; SECTION("std::deque") { diff --git a/tests/src/unit-serialization.cpp b/tests/src/unit-serialization.cpp index 1aa2111e7..c56c4f5c1 100644 --- a/tests/src/unit-serialization.cpp +++ b/tests/src/unit-serialization.cpp @@ -367,8 +367,7 @@ TEST_CASE("dump for basic_json with long double number_float_t") // serializer::dump_float(x, std::false_type). That branch must use the // "%.*Lg" format specifier; using "%.*g" with a long double argument is // undefined behavior and corrupts the output. - using long_double_json = nlohmann::basic_json; + using long_double_json = nlohmann::json::with_float_t; SECTION("round-trip dump/parse") {