Skip to content

Commit 5855237

Browse files
authored
[lldb/API] Fix SBStructuredData support any JSON type (#101929)
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 <[email protected]>
1 parent 04e7eaf commit 5855237

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lldb/source/API/SBStructuredData.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
8686
StructuredData::ParseJSON(stream.GetData());
8787
m_impl_up->SetObjectSP(json_obj);
8888

89-
if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
89+
static constexpr StructuredDataType unsupported_type[] = {
90+
eStructuredDataTypeInvalid,
91+
eStructuredDataTypeGeneric,
92+
};
93+
94+
if (!json_obj || llvm::is_contained(unsupported_type, json_obj->GetType()))
9095
error.SetErrorString("Invalid Syntax");
9196
return error;
9297
}

lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,37 @@ class MyRandomClass:
110110
self.assertTrue(my_random_class)
111111
self.assertEqual(my_random_class.payload, MyRandomClass.payload)
112112

113+
example = lldb.SBStructuredData()
114+
self.assertSuccess(example.SetFromJSON("1"))
115+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeInteger)
116+
self.assertEqual(example.GetIntegerValue(), 1)
117+
118+
self.assertSuccess(example.SetFromJSON("4.19"))
119+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeFloat)
120+
self.assertEqual(example.GetFloatValue(), 4.19)
121+
122+
self.assertSuccess(example.SetFromJSON('"Bonjour, 123!"'))
123+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeString)
124+
self.assertEqual(example.GetStringValue(42), "Bonjour, 123!")
125+
126+
self.assertSuccess(example.SetFromJSON("true"))
127+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeBoolean)
128+
self.assertTrue(example.GetBooleanValue())
129+
130+
self.assertSuccess(example.SetFromJSON("null"))
131+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeNull)
132+
133+
example_arr = [1, 2.3, "4", {"5": False}]
134+
arr_str = json.dumps(example_arr)
135+
s.Clear()
136+
s.Print(arr_str)
137+
self.assertSuccess(example.SetFromJSON(s))
138+
139+
s.Clear()
140+
self.assertSuccess(example.GetAsJSON(s))
141+
sb_data = json.loads(s.GetData())
142+
self.assertEqual(sb_data, example_arr)
143+
113144
def invalid_struct_test(self, example):
114145
invalid_struct = lldb.SBStructuredData()
115146
invalid_struct = example.GetValueForKey("invalid_key")

0 commit comments

Comments
 (0)