blob: e074316f79da6d2dac0235efa699bc09edf1f2f5 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous95c63072024-03-26 13:19:52 -07003#pragma once
4
5#include <nlohmann/json.hpp>
6
7#include <format>
8
9// Clang-tidy would rather these be static, but using static causes the template
10// specialization to not function. Ignore the warning.
11// NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
12
13template <>
14struct std::formatter<nlohmann::json::json_pointer>
15{
16 constexpr auto parse(std::format_parse_context& ctx)
17 {
18 return ctx.begin();
19 }
20 auto format(const nlohmann::json::json_pointer& ptr, auto& ctx) const
21 {
22 return std::format_to(ctx.out(), "{}", ptr.to_string());
23 }
24};
25
26template <>
27struct std::formatter<nlohmann::json>
28{
29 static constexpr auto parse(std::format_parse_context& ctx)
30 {
31 return ctx.begin();
32 }
33 auto format(const nlohmann::json& json, auto& ctx) const
34 {
35 return std::format_to(
36 ctx.out(), "{}",
37 json.dump(-1, ' ', false,
38 nlohmann::json::error_handler_t::replace));
39 }
40};
41// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)