diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 292c5d3..d0eb450 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -18,6 +18,10 @@ #include #include +#if __cplusplus >= 201703L +#include +#endif + #include "yaml-cpp/binary.h" #include "yaml-cpp/node/impl.h" #include "yaml-cpp/node/iterator.h" @@ -89,6 +93,20 @@ struct convert { static Node encode(const char* rhs) { return Node(rhs); } }; +#if __cplusplus >= 201703L +template <> +struct convert { + static Node encode(std::string_view rhs) { return Node(std::string(rhs)); } + + static bool decode(const Node& node, std::string_view& rhs) { + if (!node.IsScalar()) + return false; + rhs = node.Scalar(); + return true; + } +}; +#endif + template <> struct convert<_Null> { static Node encode(const _Null& /* rhs */) { return Node(); } diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index ff3d799..5f41ef2 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -356,6 +356,15 @@ TEST(NodeTest, ConstInteratorOnSequence) { EXPECT_EQ(3, count); } +#if __cplusplus >= 201703L +TEST(NodeTest, StdStringViewAsKey) { + Node node; + std::string_view key = "username"; + node[key] = "monkey"; + EXPECT_EQ("monkey", node[key].as()); +} +#endif + TEST(NodeTest, SimpleSubkeys) { Node node; node["device"]["udid"] = "12345";