fix issue590: add the support to null type for as<std::string>

This commit is contained in:
dota17 2020-07-15 09:22:08 +08:00
parent c82d3129dd
commit 269227f6bd
2 changed files with 6 additions and 0 deletions

View File

@ -110,6 +110,8 @@ struct as_if<std::string, S> {
const Node& node;
std::string operator()(const S& fallback) const {
if (node.Type() == NodeType::Null)
return "null";
if (node.Type() != NodeType::Scalar)
return fallback;
return node.Scalar();
@ -138,6 +140,8 @@ struct as_if<std::string, void> {
const Node& node;
std::string operator()() const {
if (node.Type() == NodeType::Null)
return "null";
if (node.Type() != NodeType::Scalar)
throw TypedBadConversion<std::string>(node.Mark());
return node.Scalar();

View File

@ -314,6 +314,8 @@ TEST(NodeTest, IncorrectFlow) {
TEST(NodeTest, LoadTildeAsNull) {
Node node = Load("~");
ASSERT_TRUE(node.IsNull());
EXPECT_EQ(node.as<std::string>(), "null");
EXPECT_EQ(node.as<std::string>("~"), "null");
}
TEST(NodeTest, LoadNullWithStrTag) {