|
| 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. |
0 commit comments