mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 20:17:44 +00:00
AK: Make JSON parser return ErrorOr<JsonValue> (instead of Optional)
Also add slightly richer parse errors now that we can include a string literal with returned errors. This will allow us to use TRY() when working with JSON data.
This commit is contained in:
parent
304c03f457
commit
587f9af960
54 changed files with 172 additions and 228 deletions
|
@ -100,10 +100,9 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
Vector<JsonValue> sorted_regions = json.value().as_array().values();
|
||||
Vector<JsonValue> sorted_regions = json.as_array().values();
|
||||
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
||||
return a.as_object().get("ip_address").to_string() < b.as_object().get("ip_address").to_string();
|
||||
});
|
||||
|
|
|
@ -47,9 +47,8 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json_result = JsonValue::from_string(file_contents);
|
||||
VERIFY(json_result.has_value());
|
||||
auto json = json_result.value().as_array();
|
||||
auto json_result = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
auto const& json = json_result.as_array();
|
||||
json.for_each([](auto& value) {
|
||||
auto& fs_object = value.as_object();
|
||||
auto fs = fs_object.get("class_name").to_string();
|
||||
|
|
|
@ -100,16 +100,16 @@ int main(int argc, char** argv)
|
|||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
if (!json.has_value()) {
|
||||
if (json.is_error()) {
|
||||
warnln("Couldn't parse {} as JSON", path);
|
||||
return 1;
|
||||
}
|
||||
if (!json->is_array()) {
|
||||
if (!json.value().is_array()) {
|
||||
warnln("{} does not contain an array of quotes", path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const auto quotes = parse_all(json->as_array());
|
||||
const auto quotes = parse_all(json.value().as_array());
|
||||
if (quotes.is_empty()) {
|
||||
warnln("{} does not contain any valid quotes", path);
|
||||
return 1;
|
||||
|
|
|
@ -69,7 +69,7 @@ int main(int argc, char** argv)
|
|||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
|
||||
if (!json.has_value()) {
|
||||
if (json.is_error()) {
|
||||
if (path) {
|
||||
warnln("Failed to parse '{}' as JSON", path);
|
||||
} else {
|
||||
|
|
|
@ -45,9 +45,8 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
json.value().as_array().for_each([](auto& value) {
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
json.as_array().for_each([](auto& value) {
|
||||
auto& if_object = value.as_object();
|
||||
|
||||
auto name = if_object.get("name").to_string();
|
||||
|
|
|
@ -53,7 +53,7 @@ int main(int argc, char** argv)
|
|||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
if (!json.has_value()) {
|
||||
if (json.is_error()) {
|
||||
warnln("Couldn't parse {} as JSON", path);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -918,7 +918,7 @@ static JS::ThrowCompletionOr<JS::Value> load_json_impl(JS::VM& vm, JS::GlobalObj
|
|||
return vm.throw_completion<JS::Error>(global_object, String::formatted("Failed to open '{}': {}", filename, file->error_string()));
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
if (!json.has_value())
|
||||
if (json.is_error())
|
||||
return vm.throw_completion<JS::SyntaxError>(global_object, JS::ErrorType::JsonMalformed);
|
||||
return JS::JSONObject::parse_json_value(global_object, json.value());
|
||||
}
|
||||
|
|
|
@ -39,9 +39,8 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
|
|||
|
||||
outln(" CPU0");
|
||||
auto file_contents = proc_interrupts->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
json.value().as_array().for_each([](auto& value) {
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
json.as_array().for_each([](auto& value) {
|
||||
auto& handler = value.as_object();
|
||||
auto purpose = handler.get("purpose").to_string();
|
||||
auto interrupt = handler.get("interrupt_line").to_string();
|
||||
|
|
|
@ -70,15 +70,10 @@ static Vector<OpenFile> get_open_files_by_pid(pid_t pid)
|
|||
}
|
||||
auto data = file.value()->read_all();
|
||||
|
||||
JsonParser parser(data);
|
||||
auto result = parser.parse();
|
||||
|
||||
if (!result.has_value()) {
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
auto json = JsonValue::from_string(data).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
Vector<OpenFile> files;
|
||||
result.value().as_array().for_each([pid, &files](const JsonValue& object) {
|
||||
json.as_array().for_each([pid, &files](const JsonValue& object) {
|
||||
OpenFile open_file;
|
||||
open_file.pid = pid;
|
||||
open_file.fd = object.as_object().get("fd").to_int();
|
||||
|
|
|
@ -66,9 +66,8 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = proc_pci->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
json.value().as_array().for_each([db, format](auto& value) {
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
json.as_array().for_each([db, format](auto& value) {
|
||||
auto& dev = value.as_object();
|
||||
auto domain = dev.get("domain").to_u32();
|
||||
auto bus = dev.get("bus").to_u32();
|
||||
|
|
|
@ -59,10 +59,9 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto contents = proc_usb_device->read_all();
|
||||
auto json = JsonValue::from_string(contents);
|
||||
VERIFY(json.has_value());
|
||||
auto json = JsonValue::from_string(contents).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
json.value().as_array().for_each([usb_db](auto& value) {
|
||||
json.as_array().for_each([usb_db](auto& value) {
|
||||
auto& device_descriptor = value.as_object();
|
||||
|
||||
auto device_address = device_descriptor.get("device_address").to_u32();
|
||||
|
|
|
@ -124,10 +124,9 @@ static bool print_mounts()
|
|||
}
|
||||
|
||||
auto content = df->read_all();
|
||||
auto json = JsonValue::from_string(content);
|
||||
VERIFY(json.has_value());
|
||||
auto json = JsonValue::from_string(content).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
json.value().as_array().for_each([](auto& value) {
|
||||
json.as_array().for_each([](auto& value) {
|
||||
auto& fs_object = value.as_object();
|
||||
auto class_name = fs_object.get("class_name").to_string();
|
||||
auto mount_point = fs_object.get("mount_point").to_string();
|
||||
|
|
|
@ -156,10 +156,9 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
Vector<JsonValue> sorted_regions = json.value().as_array().values();
|
||||
Vector<JsonValue> sorted_regions = json.as_array().values();
|
||||
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
||||
return a.as_object().get("local_port").to_u32() < b.as_object().get("local_port").to_u32();
|
||||
});
|
||||
|
@ -210,10 +209,9 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
Vector<JsonValue> sorted_regions = json.value().as_array().values();
|
||||
Vector<JsonValue> sorted_regions = json.as_array().values();
|
||||
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
||||
return a.as_object().get("local_port").to_u32() < b.as_object().get("local_port").to_u32();
|
||||
});
|
||||
|
|
|
@ -56,10 +56,9 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
VERIFY(json.has_value());
|
||||
auto json = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
||||
|
||||
Vector<JsonValue> sorted_regions = json.value().as_array().values();
|
||||
Vector<JsonValue> sorted_regions = json.as_array().values();
|
||||
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
||||
return a.as_object().get("address").to_addr() < b.as_object().get("address").to_addr();
|
||||
});
|
||||
|
|
|
@ -63,7 +63,7 @@ int main(int argc, char** argv)
|
|||
JsonObject json;
|
||||
|
||||
if (!file_contents.is_empty()) {
|
||||
if (!previous_json.has_value() || !previous_json.value().is_object()) {
|
||||
if (previous_json.is_error() || !previous_json.value().is_object()) {
|
||||
dbgln("Error: Could not parse JSON");
|
||||
} else {
|
||||
json = previous_json.value().as_object();
|
||||
|
|
|
@ -50,7 +50,7 @@ int main()
|
|||
}
|
||||
auto& file = *file_or_error.value();
|
||||
auto json = JsonValue::from_string(file.read_all());
|
||||
if (!json.has_value() || !json.value().is_object()) {
|
||||
if (json.is_error() || !json.value().is_object()) {
|
||||
warnln("Error: Could not parse /var/run/utmp");
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue