Skip to content

Commit 1e4d45c

Browse files
committed
formatter specialisations for proxies
Signed-off-by: TymianekPL <[email protected]>
1 parent bc8a64d commit 1e4d45c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Test/Test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ int main()
88
object["test1"] = "Hello World";
99
object["test2"] = 123.0;
1010
std::println("{}", object);
11+
std::println("object[\"test1\"] = {}", object["test1"]);
1112
}

cppjson/include/cppjson/object.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ namespace cppjson
9292
}
9393
private:
9494
std::reference_wrapper<JsonObject> _object;
95+
96+
friend struct std::formatter<cppjson::Object::ObjectProxy>;
9597
};
9698

9799

@@ -106,6 +108,8 @@ namespace cppjson
106108
}
107109
private:
108110
std::reference_wrapper<const JsonObject> _object;
111+
112+
friend struct std::formatter<cppjson::Object::ConstObjectProxy>;
109113
};
110114

111115
ObjectProxy operator[](const std::string& key)
@@ -146,6 +150,29 @@ struct std::formatter<cppjson::JsonObject>
146150
}
147151
};
148152

153+
template <>
154+
struct std::formatter<cppjson::Object::ObjectProxy>
155+
{
156+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
157+
158+
auto format(const cppjson::Object::ObjectProxy& object, std::format_context& context) const
159+
{
160+
return std::format_to(context.out(), "{}", object._object.get());
161+
}
162+
};
163+
164+
165+
template <>
166+
struct std::formatter<cppjson::Object::ConstObjectProxy>
167+
{
168+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
169+
170+
auto format(const cppjson::Object::ConstObjectProxy& object, std::format_context& context) const
171+
{
172+
return std::format_to(context.out(), "{}", object._object.get());
173+
}
174+
};
175+
149176
template <>
150177
struct std::formatter<cppjson::Object>
151178
{

0 commit comments

Comments
 (0)