Add std::format and fmt support (#5224)

*  add std::format and fmt support

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* ♻️ reorganize PR

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 💚 fix build

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 💚 fix build

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 💚 fix build

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-07-02 15:59:36 +02:00
committed by GitHub
parent ca49ab6123
commit 8d7e0046f4
21 changed files with 824 additions and 0 deletions
+1
View File
@@ -19,6 +19,7 @@ header. See also the [macro overview page](../../features/macros.md).
- [**JSON_HAS_CPP_11**<br>**JSON_HAS_CPP_14**<br>**JSON_HAS_CPP_17**<br>**JSON_HAS_CPP_20**](json_has_cpp_11.md) - set supported C++ standard
- [**JSON_HAS_FILESYSTEM**<br>**JSON_HAS_EXPERIMENTAL_FILESYSTEM**](json_has_filesystem.md) - control `std::filesystem` support
- [**JSON_HAS_RANGES**](json_has_ranges.md) - control `std::ranges` support
- [**JSON_HAS_STD_FORMAT**](json_has_std_format.md) - control `std::format`/`std::formatter` support
- [**JSON_HAS_THREE_WAY_COMPARISON**](json_has_three_way_comparison.md) - control 3-way comparison support
- [**JSON_NO_IO**](json_no_io.md) - switch off functions relying on certain C++ I/O headers
- [**JSON_SKIP_UNSUPPORTED_COMPILER_CHECK**](json_skip_unsupported_compiler_check.md) - do not warn about unsupported compilers
@@ -0,0 +1,41 @@
# JSON_HAS_STD_FORMAT
```cpp
#define JSON_HAS_STD_FORMAT /* value */
```
This macro indicates whether the standard library has support for `std::format`/`std::formatter` (that
is, the `<format>` header). Possible values are `1` when supported or `0` when unsupported.
## Default definition
The default value is detected based on the preprocessor macros `#!cpp JSON_HAS_CPP_20` and
`#!cpp __cpp_lib_format`.
When the macro is not defined, the library will define it to its default value.
## Notes
!!! info "Enabled functionality"
When this macro evaluates to `1`, the library provides a
[`std::formatter<basic_json>`](../basic_json/std_formatter.md) specialization so JSON values can be
used directly with `std::format`.
## Examples
??? example
The code below forces the library to disable support for `std::format`, even if the standard library
would otherwise support it:
```cpp
#define JSON_HAS_STD_FORMAT 0
#include <nlohmann/json.hpp>
...
```
## Version history
- Added in version 3.12.x.