yaml-cpp/Add-the-support-to-parsing-a-null-value.patch
cherry530 87ade4134b Add the support to parsing a null value.
Signed-off-by: cherry530 <707078654@qq.com>
2024-06-14 14:53:37 +08:00

39 lines
1.3 KiB
Diff

diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h
index 7a3deac..58ae529 100644
--- a/include/yaml-cpp/node/impl.h
+++ b/include/yaml-cpp/node/impl.h
@@ -112,8 +112,10 @@ template <typename S>
struct as_if<std::string, S> {
explicit as_if(const Node& node_) : node(node_) {}
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();
@@ -142,6 +144,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();
diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp
index 4f4f28e..1fb8415 100644
--- a/test/integration/load_node_test.cpp
+++ b/test/integration/load_node_test.cpp
@@ -249,6 +249,8 @@ TEST(NodeTest, IncompleteJson) {
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, LoadTagWithParenthesis) {