From 269227f6bd7d4cf40bb2308d14e7f8a74001b449 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 15 Jul 2020 09:22:08 +0800 Subject: [PATCH] fix issue590: add the support to null type for as --- include/yaml-cpp/node/impl.h | 4 ++++ test/integration/load_node_test.cpp | 2 ++ 2 files changed, 6 insertions(+) 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) {