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
+16
View File
@@ -0,0 +1,16 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON value
json j = {{"one", 1}, {"two", 2}};
// format_as() is found via argument-dependent lookup, the same way
// fmt::format/fmt::print would find it
auto j_str = format_as(j);
std::cout << j_str << std::endl;
}
@@ -0,0 +1 @@
{"one":1,"two":2}
@@ -0,0 +1,22 @@
#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;
}
@@ -0,0 +1,16 @@
{"one":1,"two":2}
{
"one": 1,
"two": 2
}
{
"one": 1,
"two": 2
}
{
...."one": 1,
...."two": 2
}