https://github.com/medismailben updated 
https://github.com/llvm/llvm-project/pull/101929

>From 18fcfda3baadceca0ec666bd86948baf991f8800 Mon Sep 17 00:00:00 2001
From: Med Ismail Bennani <ism...@bennani.ma>
Date: Wed, 7 Aug 2024 00:17:36 -0700
Subject: [PATCH] [lldb/API] Fix SBStructuredData support any JSON type

This patch loosen the parsing requirement to allow parsing not only
JSON dictionaries but also valid JSON type (integer, float, string,
bool, array, null).

Signed-off-by: Med Ismail Bennani <ism...@bennani.ma>
---
 lldb/source/API/SBStructuredData.cpp          |  9 +++--
 .../sbstructureddata/TestStructuredDataAPI.py | 36 +++++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/lldb/source/API/SBStructuredData.cpp 
b/lldb/source/API/SBStructuredData.cpp
index b18fc5655fc81..6361aa6babca7 100644
--- a/lldb/source/API/SBStructuredData.cpp
+++ b/lldb/source/API/SBStructuredData.cpp
@@ -86,7 +86,12 @@ lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream 
&stream) {
       StructuredData::ParseJSON(stream.GetData());
   m_impl_up->SetObjectSP(json_obj);
 
-  if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
+  static constexpr StructuredDataType unsupported_type[] = {
+      eStructuredDataTypeInvalid,
+      eStructuredDataTypeGeneric,
+  };
+
+  if (!json_obj || llvm::is_contained(unsupported_type, json_obj->GetType()))
     error.SetErrorString("Invalid Syntax");
   return error;
 }
@@ -106,7 +111,7 @@ bool SBStructuredData::IsValid() const {
 SBStructuredData::operator bool() const {
   LLDB_INSTRUMENT_VA(this);
 
-  return m_impl_up->IsValid();
+  return m_impl_up && m_impl_up->IsValid();
 }
 
 void SBStructuredData::Clear() {
diff --git a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py 
b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
index b3db3bc61e4dc..4493961d612a0 100644
--- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -110,6 +110,42 @@ class MyRandomClass:
         self.assertTrue(my_random_class)
         self.assertEqual(my_random_class.payload, MyRandomClass.payload)
 
+        example = lldb.SBStructuredData()
+        self.assertSuccess(example.SetFromJSON("1"))
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeInteger)
+        self.assertEqual(example.GetIntegerValue(), 1)
+
+        self.assertSuccess(example.SetFromJSON("4.19"))
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeFloat)
+        self.assertEqual(example.GetFloatValue(), 4.19)
+
+        self.assertSuccess(example.SetFromJSON('"Bonjour, 123!"'))
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeString)
+        self.assertEqual(example.GetStringValue(42), "Bonjour, 123!")
+
+        self.assertSuccess(example.SetFromJSON("true"))
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeBoolean)
+        self.assertTrue(example.GetBooleanValue())
+
+        self.assertSuccess(example.SetFromJSON("null"))
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeNull)
+
+        example_arr = [1, 2.3, "4", {"5": False}]
+        arr_str = json.dumps(example_arr)
+        s.Clear()
+        s.Print(arr_str)
+
+        # Check SetFromJSON API for dictionaries, integers, floating point
+        # values, strings and arrays
+        error = example.SetFromJSON(s)
+        if not error.Success():
+            self.fail("FAILED:   " + error.GetCString())
+
+        s.Clear()
+        self.assertSuccess(example.GetAsJSON(s))
+        sb_data = json.loads(s.GetData())
+        self.assertEqual(sb_data, example_arr)
+
     def invalid_struct_test(self, example):
         invalid_struct = lldb.SBStructuredData()
         invalid_struct = example.GetValueForKey("invalid_key")

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to