mirror of
https://github.com/nlohmann/json.git
synced 2026-07-03 01:04:18 +00:00
✨ add std::format and fmt support
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -153,6 +153,14 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef JSON_HAS_STD_FORMAT
|
||||
#if defined(JSON_HAS_CPP_20) && defined(__cpp_lib_format)
|
||||
#define JSON_HAS_STD_FORMAT 1
|
||||
#else
|
||||
#define JSON_HAS_STD_FORMAT 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef JSON_HAS_STATIC_RTTI
|
||||
#if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
|
||||
#define JSON_HAS_STATIC_RTTI 1
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#undef JSON_HAS_THREE_WAY_COMPARISON
|
||||
#undef JSON_HAS_RANGES
|
||||
#undef JSON_HAS_STD_FORMAT
|
||||
#undef JSON_HAS_STATIC_RTTI
|
||||
#undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#endif
|
||||
|
||||
@@ -79,6 +79,10 @@
|
||||
#include <string_view>
|
||||
#endif
|
||||
|
||||
#if JSON_HAS_STD_FORMAT
|
||||
#include <format> // format_parse_context, format_context, formatter, format_error
|
||||
#endif
|
||||
|
||||
/*!
|
||||
@brief namespace for Niels Lohmann
|
||||
@see https://github.com/nlohmann
|
||||
@@ -5260,6 +5264,14 @@ std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
|
||||
return j.dump();
|
||||
}
|
||||
|
||||
/// @brief user-defined format_as function for JSON values (fmt <= 11.0.x support)
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/format_as/
|
||||
NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
||||
std::string format_as(const NLOHMANN_BASIC_JSON_TPL& j)
|
||||
{
|
||||
return j.dump();
|
||||
}
|
||||
|
||||
inline namespace literals
|
||||
{
|
||||
inline namespace json_literals
|
||||
@@ -5363,6 +5375,40 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC
|
||||
|
||||
#endif
|
||||
|
||||
#if JSON_HAS_STD_FORMAT
|
||||
|
||||
/// @brief std::formatter specialization for JSON values
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/std_formatter/
|
||||
NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
||||
struct formatter<nlohmann::NLOHMANN_BASIC_JSON_TPL, char> // NOLINT(cert-dcl58-cpp)
|
||||
{
|
||||
bool pretty = false;
|
||||
|
||||
constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator
|
||||
{
|
||||
auto it = ctx.begin();
|
||||
if (it != ctx.end() && *it == '#')
|
||||
{
|
||||
pretty = true;
|
||||
++it;
|
||||
}
|
||||
if (it != ctx.end() && *it != '}')
|
||||
{
|
||||
JSON_THROW(format_error("invalid format args for nlohmann::json"));
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename FormatContext>
|
||||
auto format(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j, FormatContext& ctx) const -> decltype(ctx.out())
|
||||
{
|
||||
const auto dumped = pretty ? j.dump(4) : j.dump();
|
||||
return std::copy(dumped.begin(), dumped.end(), ctx.out());
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace std
|
||||
|
||||
#if JSON_USE_GLOBAL_UDLS
|
||||
|
||||
Reference in New Issue
Block a user