@@ -68,12 +68,14 @@ namespace cppjson
68
68
explicit ObjectProxy (JsonObject& object) : _object(std::ref(object)) {}
69
69
70
70
template <typename T>
71
+ requires (!std::same_as<std::remove_cvref_t <T>, JsonObject>)
71
72
explicit (false ) operator T&()
72
73
{
73
74
return this ->_object .get ().As <T>();
74
75
}
75
76
76
77
template <typename T>
78
+ requires (!std::same_as<std::remove_cvref_t <T>, JsonObject>)
77
79
explicit (false ) operator const T&() const
78
80
{
79
81
return this ->_object .get ().As <T>();
@@ -90,6 +92,8 @@ namespace cppjson
90
92
{
91
93
return static_cast <std::string&>(*this ) = std::string{ str };
92
94
}
95
+
96
+ ObjectProxy operator [](const std::string& key);
93
97
private:
94
98
std::reference_wrapper<JsonObject> _object;
95
99
@@ -106,6 +110,8 @@ namespace cppjson
106
110
{
107
111
return this ->_object .get ().As <T>();
108
112
}
113
+
114
+ ConstObjectProxy operator [](const std::string& key) const ;
109
115
private:
110
116
std::reference_wrapper<const JsonObject> _object;
111
117
@@ -127,10 +133,12 @@ namespace cppjson
127
133
private:
128
134
std::unordered_map<std::string, JsonObject> _nodes{};
129
135
136
+ friend struct std ::formatter<cppjson::JsonObject>;
130
137
friend struct std ::formatter<cppjson::Object>;
131
138
};
132
139
} // namespace cppjson
133
140
141
+
134
142
template <>
135
143
struct std ::formatter<cppjson::JsonObject>
136
144
{
@@ -144,54 +152,73 @@ struct std::formatter<cppjson::JsonObject>
144
152
case cppjson::JsonType::Bool: return std::format_to (context.out (), " {}" , object.DangerousAs <bool >());
145
153
case cppjson::JsonType::Number: return std::format_to (context.out (), " {}" , object.DangerousAs <double >());
146
154
case cppjson::JsonType::String: return std::format_to (context.out (), " \" {}\" " , object.DangerousAs <std::string>());
155
+ case cppjson::JsonType::Object:
156
+ {
157
+ const auto & node = object.DangerousAs <cppjson::Object>();
158
+
159
+ std::string built = " { " ;
160
+ for (const auto & [key, value] : node._nodes )
161
+ built += std::format (" \" {}\" : {}, " , key, value);
162
+
163
+ if (!node._nodes .empty ()) // remove trailing commas
164
+ {
165
+ built.pop_back ();
166
+ built.pop_back ();
167
+ built += " }" ;
168
+ }
169
+ else built += " }" ;
170
+
171
+ return std::format_to (context.out (), " {}" , built);
172
+ }
147
173
}
148
174
149
175
throw std::logic_error (" Unknown type" );
150
176
}
151
177
};
152
178
153
179
template <>
154
- struct std ::formatter<cppjson::Object::ObjectProxy >
180
+ struct std ::formatter<cppjson::Object>
155
181
{
156
182
constexpr auto parse (std::format_parse_context& context) { return context.begin (); }
157
183
158
- auto format (const cppjson::Object::ObjectProxy & object, std::format_context& context) const
184
+ auto format (const cppjson::Object& object, std::format_context& context) const
159
185
{
160
- return std::format_to (context.out (), " {}" , object._object .get ());
186
+ std::string built = " { " ;
187
+ for (const auto & [key, value] : object._nodes )
188
+ built += std::format (" \" {}\" : {}, " , key, value);
189
+
190
+ if (!object._nodes .empty ()) // remove trailing commas
191
+ {
192
+ built.pop_back ();
193
+ built.pop_back ();
194
+ built += " }" ;
195
+ }
196
+ else built += " }" ;
197
+
198
+ return std::format_to (context.out (), " {}" , built);
161
199
}
162
200
};
163
201
164
202
165
203
template <>
166
- struct std ::formatter<cppjson::Object::ConstObjectProxy >
204
+ struct std ::formatter<cppjson::Object::ObjectProxy >
167
205
{
168
206
constexpr auto parse (std::format_parse_context& context) { return context.begin (); }
169
207
170
- auto format (const cppjson::Object::ConstObjectProxy & object, std::format_context& context) const
208
+ auto format (const cppjson::Object::ObjectProxy & object, std::format_context& context) const
171
209
{
172
210
return std::format_to (context.out (), " {}" , object._object .get ());
173
211
}
174
212
};
175
213
214
+
176
215
template <>
177
- struct std ::formatter<cppjson::Object>
216
+ struct std ::formatter<cppjson::Object::ConstObjectProxy >
178
217
{
179
218
constexpr auto parse (std::format_parse_context& context) { return context.begin (); }
180
219
181
- auto format (const cppjson::Object& object, std::format_context& context) const
220
+ auto format (const cppjson::Object::ConstObjectProxy & object, std::format_context& context) const
182
221
{
183
- std::string built = " { " ;
184
- for (const auto & [key, value] : object._nodes )
185
- built += std::format (" \" {}\" : {}, " , key, value);
186
-
187
- if (!object._nodes .empty ()) // remove trailing commas
188
- {
189
- built.pop_back ();
190
- built.pop_back ();
191
- built += " }" ;
192
- }
193
- else built += " }" ;
194
-
195
- return std::format_to (context.out (), " {}" , built);
222
+ return std::format_to (context.out (), " {}" , object._object .get ());
196
223
}
197
224
};
0 commit comments