[Support] json::Value construction from std::vector<T> and std::map<string,T>.

Summary: Previously this required a conversion to json::Array/json::Object first.

Reviewers: ioeric

Subscribers: kristina, llvm-commits

Differential Revision: https://reviews.llvm.org/D53385

llvm-svn: 344732
This commit is contained in:
Sam McCall 2018-10-18 08:47:24 +00:00
parent 6a208e8c5f
commit 5ee0188f2b
2 changed files with 6 additions and 0 deletions

View File

@ -294,9 +294,13 @@ public:
Value(json::Array &&Elements) : Type(T_Array) {
create<json::Array>(std::move(Elements));
}
template <typename Elt>
Value(const std::vector<Elt> &C) : Value(json::Array(C)) {}
Value(json::Object &&Properties) : Type(T_Object) {
create<json::Object>(std::move(Properties));
}
template <typename Elt>
Value(const std::map<std::string, Elt> &C) : Value(json::Object(C)) {}
// Strings: types with value semantics. Must be valid UTF-8.
Value(std::string V) : Type(T_String) {
if (LLVM_UNLIKELY(!isUTF8(V))) {

View File

@ -47,6 +47,8 @@ TEST(JSONTest, Constructors) {
s(Object{{"A", Object{{"B", Object{{"X", "Y"}}}}}}));
EXPECT_EQ("null", s(llvm::Optional<double>()));
EXPECT_EQ("2.5", s(llvm::Optional<double>(2.5)));
EXPECT_EQ("[[2.5,null]]", s(std::vector<std::vector<llvm::Optional<double>>>{
{2.5, llvm::None}}));
}
TEST(JSONTest, StringOwnership) {