Skip to content

Commit 3331528

Browse files
authored
Add the support to parsing a null value as std::string.
Fixes jbeder#590.
1 parent 4571e81 commit 3331528

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

include/yaml-cpp/node/impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ struct as_if<std::string, S> {
110110
const Node& node;
111111

112112
std::string operator()(const S& fallback) const {
113+
if (node.Type() == NodeType::Null)
114+
return "null";
113115
if (node.Type() != NodeType::Scalar)
114116
return fallback;
115117
return node.Scalar();
@@ -138,6 +140,8 @@ struct as_if<std::string, void> {
138140
const Node& node;
139141

140142
std::string operator()() const {
143+
if (node.Type() == NodeType::Null)
144+
return "null";
141145
if (node.Type() != NodeType::Scalar)
142146
throw TypedBadConversion<std::string>(node.Mark());
143147
return node.Scalar();

test/integration/load_node_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ TEST(NodeTest, IncorrectFlow) {
314314
TEST(NodeTest, LoadTildeAsNull) {
315315
Node node = Load("~");
316316
ASSERT_TRUE(node.IsNull());
317+
EXPECT_EQ(node.as<std::string>(), "null");
318+
EXPECT_EQ(node.as<std::string>("~"), "null");
317319
}
318320

319321
TEST(NodeTest, LoadNullWithStrTag) {

0 commit comments

Comments
 (0)