Skip to content

Commit 8d7e004

Browse files
authored
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>
1 parent ca49ab6 commit 8d7e004

21 files changed

Lines changed: 824 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# format_as(basic_json)
2+
3+
```cpp
4+
template <typename BasicJsonType>
5+
std::string format_as(const BasicJsonType& j);
6+
```
7+
8+
This function implements the [`format_as`](https://fmt.dev/latest/api/#formatting-user-defined-types)
9+
customization point used by the [{fmt}](https://github.com/fmtlib/fmt) library (fmtlib). It has no
10+
dependency on any `fmt` header and no effect at all unless a caller's translation unit also includes
11+
`fmt` and calls `fmt::format`/`fmt::print` on a JSON value.
12+
13+
## Template parameters
14+
15+
`BasicJsonType`
16+
: a specialization of [`basic_json`](index.md)
17+
18+
## Return value
19+
20+
string containing the serialization of the JSON value (same as [`dump()`](dump.md))
21+
22+
## Exception safety
23+
24+
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
25+
26+
## Exceptions
27+
28+
Throws [`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) if a string stored inside the JSON value
29+
is not UTF-8 encoded
30+
31+
## Complexity
32+
33+
Linear.
34+
35+
## Possible implementation
36+
37+
```cpp
38+
template <typename BasicJsonType>
39+
std::string format_as(const BasicJsonType& j)
40+
{
41+
return j.dump();
42+
}
43+
```
44+
45+
## Notes
46+
47+
!!! warning "Version-dependent effect on fmt"
48+
49+
`fmt` only picks up a `format_as` overload that returns a `std::string` in fmt **10.0.0 through
50+
11.0.2**. Starting with fmt **11.1.0**, `fmt` restricts automatic `format_as` pickup to overloads that
51+
return an arithmetic type, so this function has no effect there (it is simply unused, not a compile
52+
error).
53+
54+
If you use fmt \>= 11.1.0, or want the same pretty-print spec support that
55+
[`std::formatter<basic_json>`](std_formatter.md) has (`#!cpp "{:#}"`, a width to set the indent such
56+
as `#!cpp "{:2}"`/`#!cpp "{:#2}"`, and fill-and-align to pick the indent character such as
57+
`#!cpp "{:.>#}"`), define your own `fmt::formatter` specialization mirroring the same logic:
58+
59+
```cpp
60+
--8<-- "../../../tests/fmt_formatter/project/main.cpp:formatter_recipe"
61+
```
62+
63+
This recipe isn't shipped by the library itself, since doing so would make `fmt` a build dependency
64+
(see the FAQ entry on
65+
[using JSON values with `std::format` or `fmt`](../../home/faq.md#using-json-values-with-stdformat-or-fmt)
66+
for more background) — but it *is* compiled and exercised against a real, current `fmt` release as
67+
part of the library's own test suite (`tests/fmt_formatter`, via CMake `FetchContent`), so it's kept in
68+
sync with `std::formatter<basic_json>` and verified to actually work, not just illustrative.
69+
70+
## Examples
71+
72+
??? example
73+
74+
The following code shows how the library's `format_as()` function integrates with `fmt::format`,
75+
allowing argument-dependent lookup.
76+
77+
```cpp
78+
--8<-- "examples/format_as.cpp"
79+
```
80+
81+
Output:
82+
83+
```json
84+
--8<-- "examples/format_as.output"
85+
```
86+
87+
## See also
88+
89+
- [dump](dump.md)
90+
- [std::formatter<basic_json>](std_formatter.md) - the `std::format` (C++20) equivalent
91+
92+
## Version history
93+
94+
- Added in version 3.12.x.

docs/mkdocs/docs/api/basic_json/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,15 @@ Access to the JSON value
301301
- [**operator<<(std::ostream&)**](../operator_ltlt.md) - serialize to stream
302302
- [**operator>>(std::istream&)**](../operator_gtgt.md) - deserialize from stream
303303
- [**to_string**](to_string.md) - user-defined `to_string` function for JSON values
304+
- [**format_as**](format_as.md) - user-defined `format_as` function for JSON values (fmt support)
304305

305306
## Literals
306307

307308
- [**operator""_json**](../operator_literal_json.md) - user-defined string literal for JSON values
308309

309310
## Helper classes
310311

312+
- [**std::formatter&lt;basic_json&gt;**](std_formatter.md) - make JSON values formattable with `std::format`
311313
- [**std::hash&lt;basic_json&gt;**](std_hash.md) - return a hash value for a JSON object
312314
- [**std::swap&lt;basic_json&gt;**](std_swap.md) - exchanges the values of two JSON objects
313315

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# <small>std::</small>formatter<nlohmann::basic_json\>
2+
3+
```cpp
4+
namespace std {
5+
template <>
6+
struct formatter<nlohmann::basic_json, char>;
7+
}
8+
```
9+
10+
Specialization to make JSON values formattable with [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format)
11+
(and the other members of C++20's `<format>` header, such as `std::format_to`).
12+
13+
A subset of the [standard format spec grammar](https://en.cppreference.com/w/cpp/utility/format/spec) is
14+
supported, repurposed for JSON pretty-printing; any other spec component (sign, the `0` flag, precision,
15+
`L`, a dynamic width such as `#!cpp "{:{}}"`, or a trailing type character) throws
16+
[`std::format_error`](https://en.cppreference.com/w/cpp/utility/format/format_error):
17+
18+
- `#!cpp "{}"` serializes the value the same way as [`dump()`](dump.md) (compact, no whitespace).
19+
- `#!cpp "{:#}"` ("alternate form") serializes the value the same way as `#!cpp dump(4)` (pretty-printed
20+
with an indent of 4).
21+
- A width, with or without `#!cpp "#"` (e.g. `#!cpp "{:2}"` or `#!cpp "{:#2}"`), serializes the value the
22+
same way as `#!cpp dump(width)` — a width on its own implies pretty-printing, since an indent size has
23+
no meaning for compact output.
24+
- `fill-and-align` (e.g. `#!cpp "{:.>#}"` or `#!cpp "{:.>3}"`) picks a custom indent character, the same
25+
way as `#!cpp dump(indent, indent_char)`. The alignment direction itself (`#!cpp '<'`, `#!cpp '>'`,
26+
`#!cpp '^'`) has no separate meaning for JSON values — only the fill character before it is used, and
27+
any of the three directions is accepted.
28+
29+
This specialization is only available for `#!cpp char`-based JSON values and only if the standard library
30+
provides `<format>`, controlled by the [`JSON_HAS_STD_FORMAT`](../macros/json_has_std_format.md) macro.
31+
32+
## Examples
33+
34+
??? example
35+
36+
The example shows how to format JSON values with `std::format`.
37+
38+
```cpp
39+
--8<-- "examples/std_formatter.c++20.cpp"
40+
```
41+
42+
Output:
43+
44+
```json
45+
--8<-- "examples/std_formatter.c++20.output"
46+
```
47+
48+
## See also
49+
50+
- [dump](dump.md) - serialization
51+
- [operator<<(std::ostream&)](../operator_ltlt.md) - serialize to stream
52+
- [format_as](format_as.md) - customization point used by `fmt::format` (fmtlib)
53+
54+
## Version history
55+
56+
- Added in version 3.12.x.

docs/mkdocs/docs/api/macros/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ header. See also the [macro overview page](../../features/macros.md).
1919
- [**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
2020
- [**JSON_HAS_FILESYSTEM**<br>**JSON_HAS_EXPERIMENTAL_FILESYSTEM**](json_has_filesystem.md) - control `std::filesystem` support
2121
- [**JSON_HAS_RANGES**](json_has_ranges.md) - control `std::ranges` support
22+
- [**JSON_HAS_STD_FORMAT**](json_has_std_format.md) - control `std::format`/`std::formatter` support
2223
- [**JSON_HAS_THREE_WAY_COMPARISON**](json_has_three_way_comparison.md) - control 3-way comparison support
2324
- [**JSON_NO_IO**](json_no_io.md) - switch off functions relying on certain C++ I/O headers
2425
- [**JSON_SKIP_UNSUPPORTED_COMPILER_CHECK**](json_skip_unsupported_compiler_check.md) - do not warn about unsupported compilers
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# JSON_HAS_STD_FORMAT
2+
3+
```cpp
4+
#define JSON_HAS_STD_FORMAT /* value */
5+
```
6+
7+
This macro indicates whether the standard library has support for `std::format`/`std::formatter` (that
8+
is, the `<format>` header). Possible values are `1` when supported or `0` when unsupported.
9+
10+
## Default definition
11+
12+
The default value is detected based on the preprocessor macros `#!cpp JSON_HAS_CPP_20` and
13+
`#!cpp __cpp_lib_format`.
14+
15+
When the macro is not defined, the library will define it to its default value.
16+
17+
## Notes
18+
19+
!!! info "Enabled functionality"
20+
21+
When this macro evaluates to `1`, the library provides a
22+
[`std::formatter<basic_json>`](../basic_json/std_formatter.md) specialization so JSON values can be
23+
used directly with `std::format`.
24+
25+
## Examples
26+
27+
??? example
28+
29+
The code below forces the library to disable support for `std::format`, even if the standard library
30+
would otherwise support it:
31+
32+
```cpp
33+
#define JSON_HAS_STD_FORMAT 0
34+
#include <nlohmann/json.hpp>
35+
36+
...
37+
```
38+
39+
## Version history
40+
41+
- Added in version 3.12.x.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// create a JSON value
9+
json j = {{"one", 1}, {"two", 2}};
10+
11+
// format_as() is found via argument-dependent lookup, the same way
12+
// fmt::format/fmt::print would find it
13+
auto j_str = format_as(j);
14+
15+
std::cout << j_str << std::endl;
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"one":1,"two":2}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <format>
2+
#include <iostream>
3+
#include <nlohmann/json.hpp>
4+
5+
using json = nlohmann::json;
6+
7+
int main()
8+
{
9+
json j = {{"one", 1}, {"two", 2}};
10+
11+
// compact formatting, like dump()
12+
std::cout << std::format("{}", j) << "\n\n";
13+
14+
// pretty-printed formatting, like dump(4)
15+
std::cout << std::format("{:#}", j) << "\n\n";
16+
17+
// a width sets the indent, like dump(2)
18+
std::cout << std::format("{:2}", j) << "\n\n";
19+
20+
// fill-and-align sets the indent character, like dump(4, '.')
21+
std::cout << std::format("{:.>#}", j) << std::endl;
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{"one":1,"two":2}
2+
3+
{
4+
"one": 1,
5+
"two": 2
6+
}
7+
8+
{
9+
"one": 1,
10+
"two": 2
11+
}
12+
13+
{
14+
...."one": 1,
15+
...."two": 2
16+
}

docs/mkdocs/docs/home/faq.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,31 @@ The library uses `std::numeric_limits<number_float_t>::digits10` (15 for IEEE `d
171171
172172
See [this section](../features/types/number_handling.md#number-serialization) on the library's number handling for more information.
173173
174+
### Using JSON values with `std::format` or `fmt`
175+
176+
!!! question
177+
178+
- Can I use `std::format("{}", j)` on a JSON value?
179+
- Can I use `fmt::format("{}", j)` or `fmt::print("{}", j)` (the [{fmt}](https://github.com/fmtlib/fmt) library) on a JSON value?
180+
181+
`std::format` works out of the box since version 3.12.x, as long as the standard library provides
182+
`<format>` (see [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)); see
183+
[`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) for details, including the `#!cpp "{:#}"`
184+
pretty-print spec, indent widths (`#!cpp "{:2}"`), and custom indent characters (`#!cpp "{:.>#}"`).
185+
186+
For `fmt`, the library ships [`format_as`](../api/basic_json/format_as.md), a small customization point
187+
`fmt` looks for via argument-dependent lookup. It only has an effect on fmt 10.0.0 through 11.0.2 — from
188+
fmt 11.1.0 onwards, `fmt` no longer picks up a `format_as` overload that returns a `std::string`. On such
189+
versions (or any version, if you also want the same `#!cpp "{:#}"`/width/fill-and-align spec support that
190+
`std::formatter<basic_json>` has), define your own `fmt::formatter` specialization; see
191+
[`format_as`](../api/basic_json/format_as.md) for a recipe that mirrors it.
192+
193+
If you get ambiguous-overload errors when passing a JSON value to `fmt::format`/`fmt::print` without any
194+
`fmt::formatter<json>` specialization in scope, that's `fmt` picking up `basic_json`'s implicit
195+
`operator ValueType()` conversion operator (see [#964](https://github.com/nlohmann/json/issues/964) and
196+
[#958](https://github.com/nlohmann/json/issues/958)); disabling it via
197+
[`JSON_USE_IMPLICIT_CONVERSIONS 0`](../api/macros/json_use_implicit_conversions.md) avoids the ambiguity.
198+
174199
## Compilation issues
175200
176201
### Android SDK

0 commit comments

Comments
 (0)