mirror of
https://github.com/nlohmann/json.git
synced 2026-03-10 03:01:24 +00:00
Merge branch 'develop' of https://github.com/nlohmann/json into feature/optional
Conflicts: test/src/unit-conversions.cpp
This commit is contained in:
@@ -135,7 +135,7 @@ TEST_CASE("lexer class")
|
||||
// store scan() result
|
||||
const auto res = scan_string(s.c_str());
|
||||
|
||||
CAPTURE(s);
|
||||
CAPTURE(s)
|
||||
|
||||
switch (c)
|
||||
{
|
||||
|
||||
@@ -509,7 +509,7 @@ TEST_CASE("parser class")
|
||||
CHECK(parser_helper("\"€\"").get<json::string_t>() == "€");
|
||||
CHECK(parser_helper("\"🎈\"").get<json::string_t>() == "🎈");
|
||||
|
||||
CHECK(parser_helper("\"\\ud80c\\udc60\"").get<json::string_t>() == u8"\U00013060");
|
||||
CHECK(parser_helper("\"\\ud80c\\udc60\"").get<json::string_t>() == "\xf0\x93\x81\xa0");
|
||||
CHECK(parser_helper("\"\\ud83c\\udf1e\"").get<json::string_t>() == "🌞");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1761,3 +1761,11 @@ TEST_CASE("std::optional")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef JSON_HAS_CPP_17
|
||||
#undef JSON_HAS_CPP_17
|
||||
#endif
|
||||
|
||||
#ifdef JSON_HAS_CPP_14
|
||||
#undef JSON_HAS_CPP_14
|
||||
#endif
|
||||
|
||||
@@ -1448,3 +1448,11 @@ TEST_CASE("items()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef JSON_HAS_CPP_17
|
||||
#undef JSON_HAS_CPP_17
|
||||
#endif
|
||||
|
||||
#ifdef JSON_HAS_CPP_14
|
||||
#undef JSON_HAS_CPP_14
|
||||
#endif
|
||||
|
||||
@@ -400,7 +400,7 @@ TEST_CASE("regression tests 1")
|
||||
|
||||
SECTION("issue #146 - character following a surrogate pair is skipped")
|
||||
{
|
||||
CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == u8"\U00013060abc");
|
||||
CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == "\xf0\x93\x81\xa0\x61\x62\x63");
|
||||
}
|
||||
|
||||
SECTION("issue #171 - Cannot index by key of type static constexpr const char*")
|
||||
|
||||
@@ -51,6 +51,10 @@ using nlohmann::json;
|
||||
#include <variant>
|
||||
#endif
|
||||
|
||||
#ifdef JSON_HAS_CPP_20
|
||||
#include <span>
|
||||
#endif
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// for #1021
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
@@ -484,4 +488,14 @@ TEST_CASE("regression tests 2")
|
||||
json j = json::parse(ss, nullptr, true, true);
|
||||
CHECK(j.dump() == "{}");
|
||||
}
|
||||
|
||||
#ifdef JSON_HAS_CPP_20
|
||||
SECTION("issue #2546 - parsing containers of std::byte")
|
||||
{
|
||||
const char DATA[] = R"("Hello, world!")";
|
||||
const auto s = std::as_bytes(std::span(DATA));
|
||||
json j = json::parse(s);
|
||||
CHECK(j.dump() == "\"Hello, world!\"");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -112,13 +112,13 @@ static void to_json(BasicJsonType& j, country c)
|
||||
switch (c)
|
||||
{
|
||||
case country::china:
|
||||
j = u8"中华人民共和国";
|
||||
j = "中华人民共和国";
|
||||
return;
|
||||
case country::france:
|
||||
j = "France";
|
||||
return;
|
||||
case country::russia:
|
||||
j = u8"Российская Федерация";
|
||||
j = "Российская Федерация";
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
@@ -201,9 +201,9 @@ static void from_json(const BasicJsonType& j, country& c)
|
||||
const auto str = j.template get<std::string>();
|
||||
static const std::map<std::string, country> m =
|
||||
{
|
||||
{u8"中华人民共和国", country::china},
|
||||
{"中华人民共和国", country::china},
|
||||
{"France", country::france},
|
||||
{u8"Российская Федерация", country::russia}
|
||||
{"Российская Федерация", country::russia}
|
||||
};
|
||||
|
||||
const auto it = m.find(str);
|
||||
@@ -248,7 +248,7 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
const udt::name n{"theo"};
|
||||
const udt::country c{udt::country::france};
|
||||
const udt::person sfinae_addict{a, n, c};
|
||||
const udt::person senior_programmer{{42}, {u8"王芳"}, udt::country::china};
|
||||
const udt::person senior_programmer{{42}, {"王芳"}, udt::country::china};
|
||||
const udt::address addr{"Paris"};
|
||||
const udt::contact cpp_programmer{sfinae_addict, addr};
|
||||
const udt::contact_book book{{"C++"}, {cpp_programmer, {senior_programmer, addr}}};
|
||||
@@ -265,14 +265,14 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
|
||||
CHECK(
|
||||
json(book) ==
|
||||
u8R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json);
|
||||
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json);
|
||||
|
||||
}
|
||||
|
||||
SECTION("conversion from json via free-functions")
|
||||
{
|
||||
const auto big_json =
|
||||
u8R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json;
|
||||
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json;
|
||||
SECTION("via explicit calls to get")
|
||||
{
|
||||
const auto parsed_book = big_json.get<udt::contact_book>();
|
||||
|
||||
@@ -106,7 +106,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2, age, name, met
|
||||
class person_with_private_alphabet
|
||||
{
|
||||
public:
|
||||
bool operator==(const person_with_private_alphabet& other)
|
||||
bool operator==(const person_with_private_alphabet& other) const
|
||||
{
|
||||
return a == other.a &&
|
||||
b == other.b &&
|
||||
@@ -169,7 +169,7 @@ class person_with_private_alphabet
|
||||
class person_with_public_alphabet
|
||||
{
|
||||
public:
|
||||
bool operator==(const person_with_public_alphabet& other)
|
||||
bool operator==(const person_with_public_alphabet& other) const
|
||||
{
|
||||
return a == other.a &&
|
||||
b == other.b &&
|
||||
|
||||
@@ -63,7 +63,7 @@ const char* end(const MyContainer& c)
|
||||
return c.data + strlen(c.data);
|
||||
}
|
||||
|
||||
TEST_CASE("Custom container")
|
||||
TEST_CASE("Custom container non-member begin/end")
|
||||
{
|
||||
|
||||
MyContainer data{"[1,2,3,4]"};
|
||||
@@ -75,6 +75,31 @@ TEST_CASE("Custom container")
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Custom container member begin/end")
|
||||
{
|
||||
struct MyContainer2
|
||||
{
|
||||
const char* data;
|
||||
|
||||
const char* begin() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
const char* end() const
|
||||
{
|
||||
return data + strlen(data);
|
||||
}
|
||||
};
|
||||
|
||||
MyContainer2 data{"[1,2,3,4]"};
|
||||
json as_json = json::parse(data);
|
||||
CHECK(as_json.at(0) == 1);
|
||||
CHECK(as_json.at(1) == 2);
|
||||
CHECK(as_json.at(2) == 3);
|
||||
CHECK(as_json.at(3) == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("Custom iterator")
|
||||
{
|
||||
const char* raw_data = "[1,2,3,4]";
|
||||
|
||||
Reference in New Issue
Block a user