diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 06a4884..7065404 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -110,6 +110,8 @@ struct as_if { 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 { const Node& node; std::string operator()() const { + if (node.Type() == NodeType::Null) + return "null"; if (node.Type() != NodeType::Scalar) throw TypedBadConversion(node.Mark()); return node.Scalar(); diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp index 8f3bece..d0bbd4b 100644 --- a/test/integration/load_node_test.cpp +++ b/test/integration/load_node_test.cpp @@ -314,6 +314,8 @@ TEST(NodeTest, IncorrectFlow) { TEST(NodeTest, LoadTildeAsNull) { Node node = Load("~"); ASSERT_TRUE(node.IsNull()); + EXPECT_EQ(node.as(), "null"); + EXPECT_EQ(node.as("~"), "null"); } TEST(NodeTest, LoadNullWithStrTag) {