This commit is contained in:
nlohmann
2026-10-08 14:12:31 +00:00
parent 207a4df1e4
commit 56189bffb2
17 changed files with 41 additions and 68 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ By default, implicit conversions are enabled.
`JSON_USE_IMPLICIT_CONVERSIONS` is defined to `0`:
```cpp
using wjson = nlohmann::basic_json<std::map, std::vector, std::wstring>;
using wjson = nlohmann::json::with_string_t<std::wstring>;
void load(const nlohmann::json& j);
File diff suppressed because one or more lines are too long
@@ -55,7 +55,7 @@ Example: conversion between `basic_json` specializations
A `basic_json` specialization with a different string type is also no longer converted implicitly when `JSON_USE_IMPLICIT_CONVERSIONS` is defined to `0`:
```
using wjson = nlohmann::basic_json<std::map, std::vector, std::wstring>;
using wjson = nlohmann::json::with_string_t<std::wstring>;
void load(const nlohmann::json& j);
+1 -2
View File
@@ -1,11 +1,10 @@
#include <iostream>
#include <map>
#include <nlohmann/json.hpp>
#include "custom_array_type.hpp"
using custom_json = nlohmann::basic_json<std::map, custom_array_type>;
using custom_json = nlohmann::json::with_array_t<custom_array_type>;
int main()
{
+1 -7
View File
@@ -1,16 +1,10 @@
#include <cstdint>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
#include "custom_binary_type.hpp"
using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, bool,
std::int64_t, std::uint64_t, double, std::allocator,
nlohmann::adl_serializer, custom_binary_type>;
using custom_json = nlohmann::json::with_binary_t<custom_binary_type>;
int main()
{
+1 -2
View File
@@ -1,12 +1,11 @@
#include <iostream>
#include <type_traits>
#include <vector>
#include <nlohmann/json.hpp>
#include "custom_object_type.hpp"
using custom_json = nlohmann::basic_json<custom_object_type, std::vector>;
using custom_json = nlohmann::json::with_object_t<custom_object_type>;
int main()
{
+1 -3
View File
@@ -1,12 +1,10 @@
#include <iostream>
#include <map>
#include <vector>
#include <nlohmann/json.hpp>
#include "custom_string_type.hpp"
using custom_json = nlohmann::basic_json<std::map, std::vector, custom_string_type>;
using custom_json = nlohmann::json::with_string_t<custom_string_type>;
int main()
{
+2 -3
View File
@@ -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<std::map, std::vector, std::string, bool,
std::int64_t, std::uint64_t, long double>;
```cpp hl_lines="1"
using json_ld = nlohmann::json::with_float_t<long double>;
```
Note values should then be parsed with `json_ld::parse` rather than `json::parse` as the latter would parse
File diff suppressed because one or more lines are too long
+1 -2
View File
@@ -288,8 +288,7 @@ Example
A `basic_json` type that uses `long double` as floating-point type.
```
using json_ld = nlohmann::basic_json<std::map, std::vector, std::string, bool,
std::int64_t, std::uint64_t, long double>;
using json_ld = nlohmann::json::with_float_t<long double>;
```
Note values should then be parsed with `json_ld::parse` rather than `json::parse` as the latter would parse floating-point values to `double` before then converting them to `long double`.
+6 -2
View File
@@ -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<long double>` 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<unordered_map_object>;
using unordered_json = nlohmann::json::with_object_t<unordered_map_object>;
```
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<flat_hash_object>;
using flat_hash_json = nlohmann::json::with_object_t<flat_hash_object>;
```
`absl::node_hash_map` keeps references to the mapped values valid across insertions; `absl::flat_hash_map` does not,
File diff suppressed because one or more lines are too long
+8 -16
View File
@@ -2,6 +2,8 @@
Class [`basic_json`](https://json.nlohmann.me/api/basic_json/index.md) is configurable through eleven template parameters. The library never formally states what a type passed for one of these parameters has to provide -- the requirements are implied by the way the library uses the resulting [`object_t`](https://json.nlohmann.me/api/basic_json/object_t/index.md), [`array_t`](https://json.nlohmann.me/api/basic_json/array_t/index.md), [`string_t`](https://json.nlohmann.me/api/basic_json/string_t/index.md), etc. This page collects these requirements so they do not have to be discovered by trial and error. Each section lists the concrete types 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`](https://json.nlohmann.me/api/basic_json/with_t/index.md); for instance, `nlohmann::json::with_float_t<long double>` is `json` with `long double` as [`number_float_t`](https://json.nlohmann.me/api/basic_json/number_float_t/index.md).
## How to read this page
Requirements are split into two groups:
@@ -94,7 +96,7 @@ struct unordered_map_object
using base_t::base_t;
};
using unordered_json = nlohmann::basic_json<unordered_map_object>;
using unordered_json = nlohmann::json::with_object_t<unordered_map_object>;
```
Whether `std::unordered_map` can be instantiated at all depends on the standard library: `object_t` is formed while `basic_json` is still incomplete (see the warning above), and libstdc++ 9 needs the size of the mapped type to instantiate the hash map's node type, so the adapter does not compile there. Newer libstdc++ versions, and the hash maps listed below, do not have that problem.
@@ -119,7 +121,7 @@ struct flat_hash_object
using base_t::base_t;
};
using flat_hash_json = nlohmann::basic_json<flat_hash_object>;
using flat_hash_json = nlohmann::json::with_object_t<flat_hash_object>;
```
`absl::node_hash_map` keeps references to the mapped values valid across insertions; `absl::flat_hash_map` does not, which makes it behave like [`ordered_json`](https://json.nlohmann.me/api/ordered_json/index.md) with respect to [iterator invalidation](https://json.nlohmann.me/api/basic_json/#iterator-invalidation). Both expose a `capacity()` member function, so [`JSON_DIAGNOSTICS`](https://json.nlohmann.me/api/macros/json_diagnostics/index.md) treats them conservatively and keeps the parent pointers correct either way.
@@ -292,13 +294,12 @@ Example: use the custom `ObjectType`
```
#include <iostream>
#include <type_traits>
#include <vector>
#include <nlohmann/json.hpp>
#include "custom_object_type.hpp"
using custom_json = nlohmann::basic_json<custom_object_type, std::vector>;
using custom_json = nlohmann::json::with_object_t<custom_object_type>;
int main()
{
@@ -553,13 +554,12 @@ Example: use the custom `ArrayType`
```
#include <iostream>
#include <map>
#include <nlohmann/json.hpp>
#include "custom_array_type.hpp"
using custom_json = nlohmann::basic_json<std::map, custom_array_type>;
using custom_json = nlohmann::json::with_array_t<custom_array_type>;
int main()
{
@@ -828,14 +828,12 @@ Example: use the custom `StringType`
```
#include <iostream>
#include <map>
#include <vector>
#include <nlohmann/json.hpp>
#include "custom_string_type.hpp"
using custom_json = nlohmann::basic_json<std::map, std::vector, custom_string_type>;
using custom_json = nlohmann::json::with_string_t<custom_string_type>;
int main()
{
@@ -1128,19 +1126,13 @@ class custom_binary_type
Example: use the custom `BinaryType`
```
#include <cstdint>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
#include "custom_binary_type.hpp"
using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, bool,
std::int64_t, std::uint64_t, double, std::allocator,
nlohmann::adl_serializer, custom_binary_type>;
using custom_json = nlohmann::json::with_binary_t<custom_binary_type>;
int main()
{
+1 -1
View File
@@ -116,7 +116,7 @@ function to use instead.
=== "Deprecated"
```cpp
using my_json = nlohmann::basic_json<std::map, std::vector, my_string_type>;
using my_json = nlohmann::json::with_string_t<my_string_type>;
nlohmann::json_pointer<my_json> ptr("/foo/bar/1");
```
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -67,7 +67,7 @@ Define [`JSON_DELETE_DEPRECATED_FUNCTIONS`](https://json.nlohmann.me/api/macros/
- Passing a `basic_json` specialization as template parameter `RefStringType` to [`json_pointer`](https://json.nlohmann.me/api/json_pointer/index.md) is deprecated since 3.11.0. The string type can now be directly provided. This also applies to passing such a JSON pointer to [`at`](https://json.nlohmann.me/api/basic_json/at/index.md), [`contains`](https://json.nlohmann.me/api/basic_json/contains/index.md), [`operator[]`](https://json.nlohmann.me/api/basic_json/operator%5B%5D/index.md), and [`value`](https://json.nlohmann.me/api/basic_json/value/index.md).
```
using my_json = nlohmann::basic_json<std::map, std::vector, my_string_type>;
using my_json = nlohmann::json::with_string_t<my_string_type>;
nlohmann::json_pointer<my_json> ptr("/foo/bar/1");
```
File diff suppressed because one or more lines are too long