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" // for Stream
16 #include "lldb/Utility/StreamString.h"
17 #include "llvm/ADT/STLExtras.h" // for make_unique
21 #include <limits> // for numeric_limits
23 using namespace lldb_private;
25 //----------------------------------------------------------------------
26 // Functions that use a JSONParser to parse JSON into StructuredData
27 //----------------------------------------------------------------------
28 static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser);
29 static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser);
30 static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser);
32 StructuredData::ObjectSP
33 StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
34 StructuredData::ObjectSP return_sp;
35 if (!input_spec.Exists()) {
36 error.SetErrorStringWithFormatv("input file {0} does not exist.",
41 auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
42 if (!buffer_or_error) {
43 error.SetErrorStringWithFormatv("could not open input file: {0} - {1}.",
45 buffer_or_error.getError().message());
49 JSONParser json_parser(buffer_or_error.get()->getBuffer());
50 return_sp = ParseJSONValue(json_parser);
54 static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser) {
55 // The "JSONParser::Token::ObjectStart" token should have already been
56 // consumed by the time this function is called
57 auto dict_up = llvm::make_unique<StructuredData::Dictionary>();
62 JSONParser::Token token = json_parser.GetToken(value);
64 if (token == JSONParser::Token::String) {
66 token = json_parser.GetToken(value);
67 if (token == JSONParser::Token::Colon) {
68 StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser);
70 dict_up->AddItem(key, value_sp);
74 } else if (token == JSONParser::Token::ObjectEnd) {
75 return StructuredData::ObjectSP(dict_up.release());
76 } else if (token == JSONParser::Token::Comma) {
82 return StructuredData::ObjectSP();
85 static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser) {
86 // The "JSONParser::Token::ObjectStart" token should have already been
88 // by the time this function is called
89 auto array_up = llvm::make_unique<StructuredData::Array>();
94 StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser);
96 array_up->AddItem(value_sp);
100 JSONParser::Token token = json_parser.GetToken(value);
101 if (token == JSONParser::Token::Comma) {
103 } else if (token == JSONParser::Token::ArrayEnd) {
104 return StructuredData::ObjectSP(array_up.release());
109 return StructuredData::ObjectSP();
112 static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser) {
114 const JSONParser::Token token = json_parser.GetToken(value);
116 case JSONParser::Token::ObjectStart:
117 return ParseJSONObject(json_parser);
119 case JSONParser::Token::ArrayStart:
120 return ParseJSONArray(json_parser);
122 case JSONParser::Token::Integer: {
124 if (llvm::to_integer(value, uval, 0))
125 return std::make_shared<StructuredData::Integer>(uval);
128 case JSONParser::Token::Float: {
130 if (llvm::to_float(value, val))
131 return std::make_shared<StructuredData::Float>(val);
134 case JSONParser::Token::String:
135 return std::make_shared<StructuredData::String>(value);
137 case JSONParser::Token::True:
138 case JSONParser::Token::False:
139 return std::make_shared<StructuredData::Boolean>(token ==
140 JSONParser::Token::True);
142 case JSONParser::Token::Null:
143 return std::make_shared<StructuredData::Null>();
148 return StructuredData::ObjectSP();
151 StructuredData::ObjectSP StructuredData::ParseJSON(std::string json_text) {
152 JSONParser json_parser(json_text.c_str());
153 StructuredData::ObjectSP object_sp = ParseJSONValue(json_parser);
157 StructuredData::ObjectSP
158 StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
159 if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
160 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
161 std::string key = match.first.str();
162 ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
164 // Do we have additional words to descend? If not, return the
165 // value we're at right now.
166 if (match.second.empty()) {
169 return value->GetObjectForDotSeparatedPath(match.second);
175 if (this->GetType() == lldb::eStructuredDataTypeArray) {
176 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
177 if (match.second.size() == 0) {
178 return this->shared_from_this();
181 uint64_t val = strtoul(match.second.str().c_str(), NULL, 10);
183 return this->GetAsArray()->GetItemAtIndex(val);
188 return this->shared_from_this();
191 void StructuredData::Object::DumpToStdout(bool pretty_print) const {
193 Dump(stream, pretty_print);
194 llvm::outs() << stream.GetString();
197 void StructuredData::Array::Dump(Stream &s, bool pretty_print) const {
204 for (const auto &item_sp : m_items) {
215 item_sp->Dump(s, pretty_print);
225 void StructuredData::Integer::Dump(Stream &s, bool pretty_print) const {
226 s.Printf("%" PRIu64, m_value);
229 void StructuredData::Float::Dump(Stream &s, bool pretty_print) const {
230 s.Printf("%lg", m_value);
233 void StructuredData::Boolean::Dump(Stream &s, bool pretty_print) const {
235 s.PutCString("true");
237 s.PutCString("false");
240 void StructuredData::String::Dump(Stream &s, bool pretty_print) const {
242 const size_t strsize = m_value.size();
243 for (size_t i = 0; i < strsize; ++i) {
244 char ch = m_value[i];
245 if (ch == '"' || ch == '\\')
246 quoted.push_back('\\');
247 quoted.push_back(ch);
249 s.Printf("\"%s\"", quoted.c_str());
252 void StructuredData::Dictionary::Dump(Stream &s, bool pretty_print) const {
259 for (const auto &pair : m_dict) {
269 s << "\"" << pair.first.AsCString() << "\" : ";
270 pair.second->Dump(s, pretty_print);
280 void StructuredData::Null::Dump(Stream &s, bool pretty_print) const {
284 void StructuredData::Generic::Dump(Stream &s, bool pretty_print) const {
285 s << "0x" << m_object;