mirror of
https://github.com/nlohmann/json.git
synced 2026-10-14 02:07:14 +00:00
deploy: 40a62947d3
This commit is contained in:
1 parent
79119509a9
commit
22fba27145
555 files changed
+17342
-1394
No files matched your search
@@ -36,13 +36,51 @@ provides `<format>`, controlled by the [`JSON_HAS_STD_FORMAT`](../macros/json_ha
|
||||
The example shows how to format JSON values with `std::format`.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/std_formatter.c++20.cpp"
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
json j = {{"one", 1}, {"two", 2}};
|
||||
|
||||
// compact formatting, like dump()
|
||||
std::cout << std::format("{}", j) << "\n\n";
|
||||
|
||||
// pretty-printed formatting, like dump(4)
|
||||
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;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/std_formatter.c++20.output"
|
||||
{"one":1,"two":2}
|
||||
|
||||
{
|
||||
"one": 1,
|
||||
"two": 2
|
||||
}
|
||||
|
||||
{
|
||||
"one": 1,
|
||||
"two": 2
|
||||
}
|
||||
|
||||
{
|
||||
...."one": 1,
|
||||
...."two": 2
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
Reference in new issue
Block a user