mirror of
https://github.com/nlohmann/json.git
synced 2026-07-03 01:04:18 +00:00
@@ -49,21 +49,24 @@ std::string format_as(const BasicJsonType& j)
|
||||
`fmt` only picks up a `format_as` overload that returns a `std::string` in fmt **10.0.0 through
|
||||
11.0.2**. Starting with fmt **11.1.0**, `fmt` restricts automatic `format_as` pickup to overloads that
|
||||
return an arithmetic type, so this function has no effect there (it is simply unused, not a compile
|
||||
error). If you use fmt \>= 11.1.0 (or want the same `#!cpp "{:#}"` pretty-print support that
|
||||
[`std::formatter<basic_json>`](std_formatter.md) has), define your own `fmt::formatter` specialization,
|
||||
for example:
|
||||
error).
|
||||
|
||||
If you use fmt \>= 11.1.0, or want the same pretty-print spec support that
|
||||
[`std::formatter<basic_json>`](std_formatter.md) has (`#!cpp "{:#}"`, a width to set the indent such
|
||||
as `#!cpp "{:2}"`/`#!cpp "{:#2}"`, and fill-and-align to pick the indent character such as
|
||||
`#!cpp "{:.>#}"`), define your own `fmt::formatter` specialization mirroring the same logic:
|
||||
|
||||
```cpp
|
||||
template <>
|
||||
struct fmt::formatter<nlohmann::json> : fmt::formatter<std::string>
|
||||
{
|
||||
auto format(const nlohmann::json& j, format_context& ctx) const
|
||||
{
|
||||
return fmt::formatter<std::string>::format(j.dump(), ctx);
|
||||
}
|
||||
};
|
||||
--8<-- "../../../tests/fmt_formatter/project/main.cpp:formatter_recipe"
|
||||
```
|
||||
|
||||
This recipe isn't shipped by the library itself, since doing so would make `fmt` a build dependency
|
||||
(see the FAQ entry on
|
||||
[using JSON values with `std::format` or `fmt`](../../home/faq.md#using-json-values-with-stdformat-or-fmt)
|
||||
for more background) — but it *is* compiled and exercised against a real, current `fmt` release as
|
||||
part of the library's own test suite (`tests/fmt_formatter`, via CMake `FetchContent`), so it's kept in
|
||||
sync with `std::formatter<basic_json>` and verified to actually work, not just illustrative.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
|
||||
@@ -10,11 +10,21 @@ namespace std {
|
||||
Specialization to make JSON values formattable with [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format)
|
||||
(and the other members of C++20's `<format>` header, such as `std::format_to`).
|
||||
|
||||
Only an empty format spec (`#!cpp "{}"`) or the single flag `#!cpp "{:#}"` are accepted; any other spec
|
||||
throws [`std::format_error`](https://en.cppreference.com/w/cpp/utility/format/format_error).
|
||||
A subset of the [standard format spec grammar](https://en.cppreference.com/w/cpp/utility/format/spec) is
|
||||
supported, repurposed for JSON pretty-printing; any other spec component (sign, the `0` flag, precision,
|
||||
`L`, a dynamic width such as `#!cpp "{:{}}"`, or a trailing type character) throws
|
||||
[`std::format_error`](https://en.cppreference.com/w/cpp/utility/format/format_error):
|
||||
|
||||
- `#!cpp "{}"` serializes the value the same way as [`dump()`](dump.md) (compact, no whitespace).
|
||||
- `#!cpp "{:#}"` serializes the value the same way as `#!cpp dump(4)` (pretty-printed with an indent of 4).
|
||||
- `#!cpp "{:#}"` ("alternate form") serializes the value the same way as `#!cpp dump(4)` (pretty-printed
|
||||
with an indent of 4).
|
||||
- A width, with or without `#!cpp "#"` (e.g. `#!cpp "{:2}"` or `#!cpp "{:#2}"`), serializes the value the
|
||||
same way as `#!cpp dump(width)` — a width on its own implies pretty-printing, since an indent size has
|
||||
no meaning for compact output.
|
||||
- `fill-and-align` (e.g. `#!cpp "{:.>#}"` or `#!cpp "{:.>3}"`) picks a custom indent character, the same
|
||||
way as `#!cpp dump(indent, indent_char)`. The alignment direction itself (`#!cpp '<'`, `#!cpp '>'`,
|
||||
`#!cpp '^'`) has no separate meaning for JSON values — only the fill character before it is used, and
|
||||
any of the three directions is accepted.
|
||||
|
||||
This specialization is only available for `#!cpp char`-based JSON values and only if the standard library
|
||||
provides `<format>`, controlled by the [`JSON_HAS_STD_FORMAT`](../macros/json_has_std_format.md) macro.
|
||||
|
||||
@@ -12,5 +12,11 @@ int main()
|
||||
std::cout << std::format("{}", j) << "\n\n";
|
||||
|
||||
// pretty-printed formatting, like dump(4)
|
||||
std::cout << std::format("{:#}", j) << std::endl;
|
||||
std::cout << std::format("{:#}", j) << "\n\n";
|
||||
|
||||
// a width sets the indent, like dump(2)
|
||||
std::cout << std::format("{:2}", j) << "\n\n";
|
||||
|
||||
// fill-and-align sets the indent character, like dump(4, '.')
|
||||
std::cout << std::format("{:.>#}", j) << std::endl;
|
||||
}
|
||||
|
||||
@@ -4,3 +4,13 @@
|
||||
"one": 1,
|
||||
"two": 2
|
||||
}
|
||||
|
||||
{
|
||||
"one": 1,
|
||||
"two": 2
|
||||
}
|
||||
|
||||
{
|
||||
...."one": 1,
|
||||
...."two": 2
|
||||
}
|
||||
|
||||
@@ -181,13 +181,14 @@ See [this section](../features/types/number_handling.md#number-serialization) on
|
||||
`std::format` works out of the box since version 3.12.x, as long as the standard library provides
|
||||
`<format>` (see [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)); see
|
||||
[`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) for details, including the `#!cpp "{:#}"`
|
||||
pretty-print spec.
|
||||
pretty-print spec, indent widths (`#!cpp "{:2}"`), and custom indent characters (`#!cpp "{:.>#}"`).
|
||||
|
||||
For `fmt`, the library ships [`format_as`](../api/basic_json/format_as.md), a small customization point
|
||||
`fmt` looks for via argument-dependent lookup. It only has an effect on fmt 10.0.0 through 11.0.2 — from
|
||||
fmt 11.1.0 onwards, `fmt` no longer picks up a `format_as` overload that returns a `std::string`. On such
|
||||
versions (or any version, if you also want `#!cpp "{:#}"` pretty-print support), define your own
|
||||
`fmt::formatter` specialization; see [`format_as`](../api/basic_json/format_as.md) for a two-line recipe.
|
||||
versions (or any version, if you also want the same `#!cpp "{:#}"`/width/fill-and-align spec support that
|
||||
`std::formatter<basic_json>` has), define your own `fmt::formatter` specialization; see
|
||||
[`format_as`](../api/basic_json/format_as.md) for a recipe that mirrors it.
|
||||
|
||||
If you get ambiguous-overload errors when passing a JSON value to `fmt::format`/`fmt::print` without any
|
||||
`fmt::formatter<json>` specialization in scope, that's `fmt` picking up `basic_json`'s implicit
|
||||
|
||||
Reference in New Issue
Block a user