1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:57:35 +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:
Andreas Kling 2021-11-15 01:46:51 +01:00
parent 304c03f457
commit 587f9af960
54 changed files with 172 additions and 228 deletions

View file

@ -67,9 +67,8 @@ int main(int argc, char** argv)
if (!proc_keymap->open(Core::OpenMode::ReadOnly))
VERIFY_NOT_REACHED();
auto json = JsonValue::from_string(proc_keymap->read_all());
VERIFY(json.has_value());
JsonObject keymap_object = json.value().as_object();
auto json = JsonValue::from_string(proc_keymap->read_all()).release_value_but_fixme_should_propagate_errors();
auto const& keymap_object = json.as_object();
VERIFY(keymap_object.has("keymap"));
String current_keymap = keymap_object.get("keymap").to_string();
dbgln("KeyboardSettings thinks the current keymap is: {}", current_keymap);

View file

@ -26,7 +26,7 @@ ErrorOr<void> ProjectLoader::try_load_from_fd_and_close(int fd, StringView path)
auto contents = file->read_all();
auto json_or_error = JsonValue::from_string(contents);
if (!json_or_error.has_value()) {
if (json_or_error.is_error()) {
m_is_raw_image = true;
auto mapped_file = TRY(MappedFile::map_from_fd_and_close(fd, path));

View file

@ -84,10 +84,9 @@ static void fill_mounts(Vector<MountInfo>& output)
}
auto content = file->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([&output](auto& value) {
json.as_array().for_each([&output](auto& value) {
auto& filesystem_object = value.as_object();
MountInfo mount_info;
mount_info.mount_point = filesystem_object.get("mount_point").to_string();

View file

@ -209,7 +209,7 @@ Result<NonnullRefPtrVector<Sheet>, String> ImportDialog::make_and_run_for(String
auto import_worksheet = [&]() -> Result<NonnullRefPtrVector<Sheet>, String> {
auto json_value_option = JsonParser(file.read_all()).parse();
if (!json_value_option.has_value()) {
if (json_value_option.is_error()) {
StringBuilder sb;
sb.append("Failed to parse ");
sb.append(file.filename());

View file

@ -667,7 +667,7 @@ JsonObject Sheet::gather_documentation() const
JsonParser parser(doc.to_string_without_side_effects());
auto doc_object = parser.parse();
if (doc_object.has_value())
if (!doc_object.is_error())
object.set(it.key.to_display_string(), doc_object.value());
else
dbgln("Sheet::gather_documentation(): Failed to parse the documentation for '{}'!", it.key.to_display_string());

View file

@ -84,9 +84,8 @@ void MemoryStatsWidget::refresh()
VERIFY_NOT_REACHED();
auto file_contents = proc_memstat->read_all();
auto json_result = JsonValue::from_string(file_contents);
VERIFY(json_result.has_value());
auto json = json_result.value().as_object();
auto json_result = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
auto const& json = json_result.as_object();
[[maybe_unused]] u32 kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
u32 kmalloc_allocated = json.get("kmalloc_allocated").to_u32();