1 //===---------------------StructuredData.cpp ---------------------*- C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/StructuredData.h"
11 #include "lldb/Utility/DataBuffer.h"
12 #include "lldb/Utility/FileSpec.h"
13 #include "lldb/Utility/JSON.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/Utility/Stream.h"
16 #include "lldb/Utility/StreamString.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/MemoryBuffer.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // Functions that use a JSONParser to parse JSON into StructuredData
28 //----------------------------------------------------------------------
29 static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser);
30 static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser);
31 static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser);
33 StructuredData::ObjectSP
34 StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
35 StructuredData::ObjectSP return_sp;
37 auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
38 if (!buffer_or_error) {
39 error.SetErrorStringWithFormatv("could not open input file: {0} - {1}.",
41 buffer_or_error.getError().message());
45 JSONParser json_parser(buffer_or_error.get()->getBuffer());
46 return_sp = ParseJSONValue(json_parser);
50 static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser) {
51 // The "JSONParser::Token::ObjectStart" token should have already been
52 // consumed by the time this function is called
53 auto dict_up = llvm::make_unique<StructuredData::Dictionary>();
58 JSONParser::Token token = json_parser.GetToken(value);
60 if (token == JSONParser::Token::String) {
62 token = json_parser.GetToken(value);
63 if (token == JSONParser::Token::Colon) {
64 StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser);
66 dict_up->AddItem(key, value_sp);
70 } else if (token == JSONParser::Token::ObjectEnd) {
71 return StructuredData::ObjectSP(dict_up.release());
72 } else if (token == JSONParser::Token::Comma) {
78 return StructuredData::ObjectSP();
81 static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser) {
82 // The "JSONParser::Token::ObjectStart" token should have already been
83 // consumed by the time this function is called
84 auto array_up = llvm::make_unique<StructuredData::Array>();
89 StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser);
91 array_up->AddItem(value_sp);
95 JSONParser::Token token = json_parser.GetToken(value);
96 if (token == JSONParser::Token::Comma) {
98 } else if (token == JSONParser::Token::ArrayEnd) {
99 return StructuredData::ObjectSP(array_up.release());
104 return StructuredData::ObjectSP();
107 static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser) {
109 const JSONParser::Token token = json_parser.GetToken(value);
111 case JSONParser::Token::ObjectStart:
112 return ParseJSONObject(json_parser);
114 case JSONParser::Token::ArrayStart:
115 return ParseJSONArray(json_parser);
117 case JSONParser::Token::Integer: {
119 if (llvm::to_integer(value, uval, 0))
120 return std::make_shared<StructuredData::Integer>(uval);
123 case JSONParser::Token::Float: {
125 if (llvm::to_float(value, val))
126 return std::make_shared<StructuredData::Float>(val);
129 case JSONParser::Token::String:
130 return std::make_shared<StructuredData::String>(value);
132 case JSONParser::Token::True:
133 case JSONParser::Token::False:
134 return std::make_shared<StructuredData::Boolean>(token ==
135 JSONParser::Token::True);
137 case JSONParser::Token::Null:
138 return std::make_shared<StructuredData::Null>();
143 return StructuredData::ObjectSP();
146 StructuredData::ObjectSP StructuredData::ParseJSON(std::string json_text) {
147 JSONParser json_parser(json_text);
148 StructuredData::ObjectSP object_sp = ParseJSONValue(json_parser);
152 StructuredData::ObjectSP
153 StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
154 if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
155 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
156 std::string key = match.first.str();
157 ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
159 // Do we have additional words to descend? If not, return the value
160 // we're at right now.
161 if (match.second.empty()) {
164 return value->GetObjectForDotSeparatedPath(match.second);
170 if (this->GetType() == lldb::eStructuredDataTypeArray) {
171 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
172 if (match.second.empty()) {
173 return this->shared_from_this();
176 uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
178 return this->GetAsArray()->GetItemAtIndex(val);
183 return this->shared_from_this();
186 void StructuredData::Object::DumpToStdout(bool pretty_print) const {
188 Dump(stream, pretty_print);
189 llvm::outs() << stream.GetString();
192 void StructuredData::Array::Dump(Stream &s, bool pretty_print) const {
199 for (const auto &item_sp : m_items) {
210 item_sp->Dump(s, pretty_print);
220 void StructuredData::Integer::Dump(Stream &s, bool pretty_print) const {
221 s.Printf("%" PRIu64, m_value);
224 void StructuredData::Float::Dump(Stream &s, bool pretty_print) const {
225 s.Printf("%lg", m_value);
228 void StructuredData::Boolean::Dump(Stream &s, bool pretty_print) const {
230 s.PutCString("true");
232 s.PutCString("false");
235 void StructuredData::String::Dump(Stream &s, bool pretty_print) const {
237 const size_t strsize = m_value.size();
238 for (size_t i = 0; i < strsize; ++i) {
239 char ch = m_value[i];
240 if (ch == '"' || ch == '\\')
241 quoted.push_back('\\');
242 quoted.push_back(ch);
244 s.Printf("\"%s\"", quoted.c_str());
247 void StructuredData::Dictionary::Dump(Stream &s, bool pretty_print) const {
254 for (const auto &pair : m_dict) {
264 s << "\"" << pair.first.AsCString() << "\" : ";
265 pair.second->Dump(s, pretty_print);
275 void StructuredData::Null::Dump(Stream &s, bool pretty_print) const {
279 void StructuredData::Generic::Dump(Stream &s, bool pretty_print) const {
280 s << "0x" << m_object;