Unverified Commit 33315286 authored by Chen's avatar Chen Committed by GitHub
Browse files

Add the support to parsing a null value as `std::string`.

Fixes #590.
parent 4571e817
...@@ -110,6 +110,8 @@ struct as_if<std::string, S> { ...@@ -110,6 +110,8 @@ struct as_if<std::string, S> {
const Node& node; const Node& node;
std::string operator()(const S& fallback) const { std::string operator()(const S& fallback) const {
if (node.Type() == NodeType::Null)
return "null";
if (node.Type() != NodeType::Scalar) if (node.Type() != NodeType::Scalar)
return fallback; return fallback;
return node.Scalar(); return node.Scalar();
...@@ -138,6 +140,8 @@ struct as_if<std::string, void> { ...@@ -138,6 +140,8 @@ struct as_if<std::string, void> {
const Node& node; const Node& node;
std::string operator()() const { std::string operator()() const {
if (node.Type() == NodeType::Null)
return "null";
if (node.Type() != NodeType::Scalar) if (node.Type() != NodeType::Scalar)
throw TypedBadConversion<std::string>(node.Mark()); throw TypedBadConversion<std::string>(node.Mark());
return node.Scalar(); return node.Scalar();
......
...@@ -314,6 +314,8 @@ TEST(NodeTest, IncorrectFlow) { ...@@ -314,6 +314,8 @@ TEST(NodeTest, IncorrectFlow) {
TEST(NodeTest, LoadTildeAsNull) { TEST(NodeTest, LoadTildeAsNull) {
Node node = Load("~"); Node node = Load("~");
ASSERT_TRUE(node.IsNull()); ASSERT_TRUE(node.IsNull());
EXPECT_EQ(node.as<std::string>(), "null");
EXPECT_EQ(node.as<std::string>("~"), "null");
} }
TEST(NodeTest, LoadNullWithStrTag) { TEST(NodeTest, LoadNullWithStrTag) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment