Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
[Support] json::Value construction from std::vector<T> and std::map<s…
Browse files Browse the repository at this point in the history
…tring,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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@344732 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
sam-mccall committed Oct 18, 2018
1 parent fc6feb9 commit 6f53b9f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/llvm/Support/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,13 @@ class Value {
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))) {
Expand Down
2 changes: 2 additions & 0 deletions unittests/Support/JSONTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6f53b9f

Please sign in to comment.