@@ -240,6 +240,50 @@ inline void from_json(const nlohmann::json& j, FooBar& fb)
240
240
j.at (" value" ).get_to (fb.foo .value );
241
241
}
242
242
243
+ // ///////////////////////////////////////////////////////////////////
244
+ // for #3171
245
+ // ///////////////////////////////////////////////////////////////////
246
+
247
+ struct for_3171_base // NOLINT(cppcoreguidelines-special-member-functions)
248
+ {
249
+ for_3171_base (const std::string& /* unused*/ = {}) {}
250
+ virtual ~for_3171_base () = default ;
251
+
252
+ virtual void _from_json (const json& j)
253
+ {
254
+ j.at (" str" ).get_to (str);
255
+ }
256
+
257
+ std::string str{};
258
+ };
259
+
260
+ struct for_3171_derived : public for_3171_base
261
+ {
262
+ for_3171_derived () = default;
263
+ explicit for_3171_derived (const std::string& /* unused*/ ) { }
264
+ };
265
+
266
+ inline void from_json (const json& j, for_3171_base& tb)
267
+ {
268
+ tb._from_json (j);
269
+ }
270
+
271
+ // ///////////////////////////////////////////////////////////////////
272
+ // for #3312
273
+ // ///////////////////////////////////////////////////////////////////
274
+
275
+ #ifdef JSON_HAS_CPP_20
276
+ struct for_3312
277
+ {
278
+ std::string name;
279
+ };
280
+
281
+ inline void from_json (const json& j, for_3312& obj)
282
+ {
283
+ j.at (" name" ).get_to (obj.name );
284
+ }
285
+ #endif
286
+
243
287
TEST_CASE (" regression tests 2" )
244
288
{
245
289
SECTION (" issue #1001 - Fix memory leak during parser callback" )
@@ -791,6 +835,31 @@ TEST_CASE("regression tests 2")
791
835
CHECK (jit->first == ojit->first );
792
836
CHECK (jit->second .get <std::string>() == ojit->second .get <std::string>());
793
837
}
838
+
839
+ SECTION (" issue #3171 - if class is_constructible from std::string wrong from_json overload is being selected, compilation failed" )
840
+ {
841
+ json j{{ " str" , " value" }};
842
+
843
+ // failed with: error: no match for ‘operator=’ (operand types are ‘for_3171_derived’ and ‘const nlohmann::basic_json<>::string_t’
844
+ // {aka ‘const std::__cxx11::basic_string<char>’})
845
+ // s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
846
+ auto td = j.get <for_3171_derived>();
847
+
848
+ CHECK (td.str == " value" );
849
+ }
850
+
851
+ #ifdef JSON_HAS_CPP_20
852
+ SECTION (" issue #3312 - Parse to custom class from unordered_json breaks on G++11.2.0 with C++20" )
853
+ {
854
+ // see test for #3171
855
+ ordered_json j = {{" name" , " class" }};
856
+ for_3312 obj{};
857
+
858
+ j.get_to (obj);
859
+
860
+ CHECK (obj.name == " class" );
861
+ }
862
+ #endif
794
863
}
795
864
796
865
DOCTEST_CLANG_SUPPRESS_WARNING_POP
0 commit comments