1 //===-- SBStructuredData.cpp ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBStructuredData.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBStringList.h"
14 #include "lldb/Core/StructuredDataImpl.h"
15 #include "lldb/Target/StructuredDataPlugin.h"
16 #include "lldb/Utility/Event.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StructuredData.h"
22 using namespace lldb_private;
25 #pragma mark SBStructuredData
27 SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
28 LLDB_INSTRUMENT_VA(this);
31 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
32 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
33 LLDB_INSTRUMENT_VA(this, rhs);
36 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
37 : m_impl_up(new StructuredDataImpl(event_sp)) {
38 LLDB_INSTRUMENT_VA(this, event_sp);
41 SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
42 : m_impl_up(new StructuredDataImpl(impl)) {
43 LLDB_INSTRUMENT_VA(this, impl);
46 SBStructuredData::~SBStructuredData() = default;
48 SBStructuredData &SBStructuredData::
49 operator=(const lldb::SBStructuredData &rhs) {
50 LLDB_INSTRUMENT_VA(this, rhs);
52 *m_impl_up = *rhs.m_impl_up;
56 lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
57 LLDB_INSTRUMENT_VA(this, stream);
60 std::string json_str(stream.GetData());
62 StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
63 m_impl_up->SetObjectSP(json_obj);
65 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
66 error.SetErrorString("Invalid Syntax");
70 lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
71 LLDB_INSTRUMENT_VA(this, json);
74 return SetFromJSON(s);
77 bool SBStructuredData::IsValid() const {
78 LLDB_INSTRUMENT_VA(this);
79 return this->operator bool();
82 SBStructuredData::operator bool() const {
83 LLDB_INSTRUMENT_VA(this);
85 return m_impl_up->IsValid();
88 void SBStructuredData::Clear() {
89 LLDB_INSTRUMENT_VA(this);
94 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
95 LLDB_INSTRUMENT_VA(this, stream);
98 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
102 lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
103 LLDB_INSTRUMENT_VA(this, stream);
105 Status error = m_impl_up->GetDescription(stream.ref());
107 sb_error.SetError(error);
111 StructuredDataType SBStructuredData::GetType() const {
112 LLDB_INSTRUMENT_VA(this);
114 return m_impl_up->GetType();
117 size_t SBStructuredData::GetSize() const {
118 LLDB_INSTRUMENT_VA(this);
120 return m_impl_up->GetSize();
123 bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
124 LLDB_INSTRUMENT_VA(this, keys);
126 if (GetType() != eStructuredDataTypeDictionary)
129 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
133 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
134 // We claimed we were a dictionary, so this can't be null.
136 // The return kind of GetKeys is an Array:
137 StructuredData::ObjectSP array_sp = dict->GetKeys();
138 StructuredData::Array *key_arr = array_sp->GetAsArray();
141 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
142 llvm::StringRef key = object->GetStringValue("");
143 keys.AppendString(key.str().c_str());
149 lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
150 LLDB_INSTRUMENT_VA(this, key);
152 SBStructuredData result;
153 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
157 lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
158 LLDB_INSTRUMENT_VA(this, idx);
160 SBStructuredData result;
161 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
165 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
166 LLDB_INSTRUMENT_VA(this, fail_value);
168 return m_impl_up->GetIntegerValue(fail_value);
171 double SBStructuredData::GetFloatValue(double fail_value) const {
172 LLDB_INSTRUMENT_VA(this, fail_value);
174 return m_impl_up->GetFloatValue(fail_value);
177 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
178 LLDB_INSTRUMENT_VA(this, fail_value);
180 return m_impl_up->GetBooleanValue(fail_value);
183 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
184 LLDB_INSTRUMENT_VA(this, dst, dst_len);
186 return m_impl_up->GetStringValue(dst, dst_len);