more tests in unit-udt
This commit is contained in:
parent
f2c71fafda
commit
f1482d1f01
1 changed files with 11 additions and 3 deletions
|
@ -394,15 +394,22 @@ namespace nlohmann
|
||||||
template <>
|
template <>
|
||||||
struct adl_serializer<std::vector<float>>
|
struct adl_serializer<std::vector<float>>
|
||||||
{
|
{
|
||||||
static void to_json(json& j, std::vector<float> const&)
|
using type = std::vector<float>;
|
||||||
|
static void to_json(json& j, type const&)
|
||||||
{
|
{
|
||||||
j = "hijacked!";
|
j = "hijacked!";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void from_json(json const&, std::vector<float>& opt)
|
static void from_json(json const&, type& opt)
|
||||||
{
|
{
|
||||||
opt = {42.0, 42.0, 42.0};
|
opt = {42.0, 42.0, 42.0};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// preferred version
|
||||||
|
static type from_json(json const&)
|
||||||
|
{
|
||||||
|
return {4.0, 5.0, 6.0};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,7 +418,8 @@ TEST_CASE("even supported types can be specialized", "[udt]")
|
||||||
json j = std::vector<float> {1.0, 2.0, 3.0};
|
json j = std::vector<float> {1.0, 2.0, 3.0};
|
||||||
CHECK(j.dump() == R"("hijacked!")");
|
CHECK(j.dump() == R"("hijacked!")");
|
||||||
auto f = j.get<std::vector<float>>();
|
auto f = j.get<std::vector<float>>();
|
||||||
CHECK((f == std::vector<float>{42.0, 42.0, 42.0}));
|
// the single argument from_json method is preferred
|
||||||
|
CHECK((f == std::vector<float>{4.0, 5.0, 6.0}));
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace nlohmann
|
namespace nlohmann
|
||||||
|
|
Loading…
Reference in a new issue