support construction from other basic_json types
Before this patch, `basic_json` types with different template arguments were treated as `CompatibleArrayType`. Which sometimes leads to recursive calls and stack overflows. This patch adds a constructor and a `get` overload to deal with different `basic_json` types.
This commit is contained in:
parent
c22f2d41f3
commit
8711ec6034
5 changed files with 281 additions and 8 deletions
|
@ -316,8 +316,8 @@ TEST_CASE("object inspection")
|
|||
SECTION("round trips")
|
||||
{
|
||||
for (const auto& s :
|
||||
{"3.141592653589793", "1000000000000000010E5"
|
||||
})
|
||||
{"3.141592653589793", "1000000000000000010E5"
|
||||
})
|
||||
{
|
||||
json j1 = json::parse(s);
|
||||
std::string s1 = j1.dump();
|
||||
|
|
|
@ -693,6 +693,83 @@ TEST_CASE("custom serializer that does adl by default", "[udt]")
|
|||
CHECK(me == cj.get<udt::person>());
|
||||
}
|
||||
|
||||
TEST_CASE("different basic_json types conversions")
|
||||
{
|
||||
using json = nlohmann::json;
|
||||
|
||||
SECTION("null")
|
||||
{
|
||||
json j;
|
||||
custom_json cj = j;
|
||||
CHECK(cj == nullptr);
|
||||
}
|
||||
|
||||
SECTION("boolean")
|
||||
{
|
||||
json j = true;
|
||||
custom_json cj = j;
|
||||
CHECK(cj == true);
|
||||
}
|
||||
|
||||
SECTION("discarded")
|
||||
{
|
||||
json j(json::value_t::discarded);
|
||||
custom_json cj;
|
||||
CHECK_NOTHROW(cj = j);
|
||||
CHECK(cj.type() == custom_json::value_t::discarded);
|
||||
}
|
||||
|
||||
SECTION("array")
|
||||
{
|
||||
json j = {1, 2, 3};
|
||||
custom_json cj = j;
|
||||
CHECK((cj == std::vector<int> {1, 2, 3}));
|
||||
}
|
||||
|
||||
SECTION("integer")
|
||||
{
|
||||
json j = 42;
|
||||
custom_json cj = j;
|
||||
CHECK(cj == 42);
|
||||
}
|
||||
|
||||
SECTION("float")
|
||||
{
|
||||
json j = 42.0;
|
||||
custom_json cj = j;
|
||||
CHECK(cj == 42.0);
|
||||
}
|
||||
|
||||
SECTION("unsigned")
|
||||
{
|
||||
json j = 42u;
|
||||
custom_json cj = j;
|
||||
CHECK(cj == 42u);
|
||||
}
|
||||
|
||||
SECTION("string")
|
||||
{
|
||||
json j = "forty-two";
|
||||
custom_json cj = j;
|
||||
CHECK(cj == "forty-two");
|
||||
}
|
||||
|
||||
SECTION("object")
|
||||
{
|
||||
json j = {{"forty", "two"}};
|
||||
custom_json cj = j;
|
||||
auto m = j.get<std::map<std::string, std::string>>();
|
||||
CHECK(cj == m);
|
||||
}
|
||||
|
||||
SECTION("get<custom_json>")
|
||||
{
|
||||
json j = 42;
|
||||
custom_json cj = j.get<custom_json>();
|
||||
CHECK(cj == 42);
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct incomplete;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue