[Lldb-commits] [lldb] [lldb/API] Fix SBStructuredData JSON Array parsing (PR #101929)

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 5 00:40:30 PDT 2024


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

This patch loosen the parsing requirement to allow parsing not only JSON dictionaries but also JSON arrays.

>From 54081f56181d954365f9ab762ca6715e28119ad4 Mon Sep 17 00:00:00 2001
From: Med Ismail Bennani <ismail at bennani.ma>
Date: Mon, 5 Aug 2024 00:37:51 -0700
Subject: [PATCH] [lldb/API] Fix SBStructuredData JSON Array parsing

This patch loosen the parsing requirement to allow parsing not only
JSON dictionaries but also JSON arrays.

Signed-off-by: Med Ismail Bennani <ismail at bennani.ma>
---
 lldb/source/API/SBStructuredData.cpp           |  8 ++++++--
 .../sbstructureddata/TestStructuredDataAPI.py  | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/lldb/source/API/SBStructuredData.cpp b/lldb/source/API/SBStructuredData.cpp
index b18fc5655fc81..32fa5d7bb30f9 100644
--- a/lldb/source/API/SBStructuredData.cpp
+++ b/lldb/source/API/SBStructuredData.cpp
@@ -86,7 +86,11 @@ 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 structured_data_record_type[] = {
+      eStructuredDataTypeArray, eStructuredDataTypeDictionary};
+
+  if (!json_obj ||
+      !llvm::is_contained(structured_data_record_type, json_obj->GetType()))
     error.SetErrorString("Invalid Syntax");
   return error;
 }
@@ -106,7 +110,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..c0ce482860c85 100644
--- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -110,6 +110,24 @@ class MyRandomClass:
         self.assertTrue(my_random_class)
         self.assertEqual(my_random_class.payload, MyRandomClass.payload)
 
+        example_arr = [1, 2.3, "4", {"5": False}]
+        arr_str = json.dumps(example_arr)
+        s.Clear()
+        s.Print(arr_str)
+        example = lldb.SBStructuredData()
+
+        # 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")



More information about the lldb-commits mailing list