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

Sam McCall via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 17 13:33:50 PDT 2018


sammccall created this revision.
sammccall added a reviewer: ioeric.
Herald added subscribers: llvm-commits, kristina.

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


Repository:
  rL LLVM

https://reviews.llvm.org/D53385

Files:
  include/llvm/Support/JSON.h
  unittests/Support/JSONTest.cpp


Index: unittests/Support/JSONTest.cpp
===================================================================
--- unittests/Support/JSONTest.cpp
+++ unittests/Support/JSONTest.cpp
@@ -47,6 +47,8 @@
             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) {
Index: include/llvm/Support/JSON.h
===================================================================
--- include/llvm/Support/JSON.h
+++ include/llvm/Support/JSON.h
@@ -294,9 +294,13 @@
   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))) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53385.170034.patch
Type: text/x-patch
Size: 1279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181017/052c3186/attachment.bin>


More information about the llvm-commits mailing list