12 #include "llvm/Support/MemoryBuffer.h"
26 llvm::Expected<json::Value> value = json::parse(json_text);
28 llvm::consumeError(value.takeError());
38 auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.
GetPath());
39 if (!buffer_or_error) {
40 error.SetErrorStringWithFormatv(
"could not open input file: {0} - {1}.",
42 buffer_or_error.getError().message());
45 llvm::Expected<json::Value> value =
46 json::parse(buffer_or_error.get()->getBuffer().str());
54 if (json::Object *O = value.getAsObject())
57 if (json::Array *A = value.getAsArray())
60 if (
auto s = value.getAsString())
61 return std::make_shared<StructuredData::String>(*s);
63 if (
auto b = value.getAsBoolean())
64 return std::make_shared<StructuredData::Boolean>(*b);
66 if (
auto i = value.getAsInteger())
67 return std::make_shared<StructuredData::Integer>(*i);
69 if (
auto d = value.getAsNumber())
70 return std::make_shared<StructuredData::Float>(*d);
76 auto dict_up = std::make_unique<StructuredData::Dictionary>();
77 for (
auto &KV : *
object) {
78 StringRef key = KV.first;
79 json::Value value = KV.second;
81 dict_up->AddItem(key, value_sp);
83 return std::move(dict_up);
87 auto array_up = std::make_unique<StructuredData::Array>();
88 for (json::Value &value : *array) {
90 array_up->AddItem(value_sp);
92 return std::move(array_up);
98 std::pair<llvm::StringRef, llvm::StringRef> match = path.split(
'.');
100 ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
104 if (match.second.empty()) {
107 return value->GetObjectForDotSeparatedPath(match.second);
114 std::pair<llvm::StringRef, llvm::StringRef> match = path.split(
'[');
115 if (match.second.empty()) {
116 return this->shared_from_this();
119 uint64_t val = strtoul(match.second.str().c_str(),
nullptr, 10);
121 return this->GetAsArray()->GetItemAtIndex(val);
126 return this->shared_from_this();
130 json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
136 for (
const auto &item_sp : m_items) {
137 item_sp->Serialize(s);
143 s.value(
static_cast<int64_t
>(m_value));
160 for (
const auto &pair : m_dict) {
161 s.attributeBegin(pair.first.GetStringRef());
162 pair.second->Serialize(s);
173 s.value(llvm::formatv(
"{0:X}", m_object));