diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-15 01:46:51 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-17 00:21:10 +0100 |
commit | 587f9af960daa9f003ec9e41751cdc4ce50b87dd (patch) | |
tree | 41bc5b51741dffcc23e3e4b63d4c17a37e2e03d7 /Userland/Libraries | |
parent | 304c03f457e294a3d756860325d93b809f50f7a6 (diff) | |
download | serenity-587f9af960daa9f003ec9e41751cdc4ce50b87dd.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/EventLoop.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/ProcessStatisticsReader.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCoredump/Reader.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibDebug/DebugSession.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibDesktop/Launcher.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/CommonLocationsProvider.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/GMLParser.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/JsonArrayModel.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/JSONObject.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibKeyboard/CharacterMapFile.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibSymbolication/Symbolication.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibTest/JavaScriptTestRunner.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOMTreeModel.h | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/StylePropertiesModel.h | 7 |
14 files changed, 23 insertions, 32 deletions
diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 1f99d51fef..83ce8851ce 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -144,7 +144,7 @@ private: auto request = m_socket->read(length); auto request_json = JsonValue::from_string(request); - if (!request_json.has_value() || !request_json.value().is_object()) { + if (request_json.is_error() || !request_json.value().is_object()) { dbgln("RPC client sent invalid request"); shutdown(); return; diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp index d17c88bbd6..9a9176d4a7 100644 --- a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp +++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp @@ -35,7 +35,7 @@ Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(RefPtr<Core::F auto file_contents = proc_all_file->read_all(); auto json = JsonValue::from_string(file_contents); - if (!json.has_value()) + if (json.is_error()) return {}; auto& json_obj = json.value().as_object(); diff --git a/Userland/Libraries/LibCoredump/Reader.cpp b/Userland/Libraries/LibCoredump/Reader.cpp index 661d92e05d..c5126e689a 100644 --- a/Userland/Libraries/LibCoredump/Reader.cpp +++ b/Userland/Libraries/LibCoredump/Reader.cpp @@ -149,7 +149,7 @@ const JsonObject Reader::process_info() const if (!process_info_notes_entry) return {}; auto process_info_json_value = JsonValue::from_string(process_info_notes_entry->json_data); - if (!process_info_json_value.has_value()) + if (process_info_json_value.is_error()) return {}; if (!process_info_json_value.value().is_object()) return {}; @@ -247,7 +247,7 @@ HashMap<String, String> Reader::metadata() const if (!metadata_notes_entry) return {}; auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data); - if (!metadata_json_value.has_value()) + if (metadata_json_value.is_error()) return {}; if (!metadata_json_value.value().is_object()) return {}; diff --git a/Userland/Libraries/LibDebug/DebugSession.cpp b/Userland/Libraries/LibDebug/DebugSession.cpp index ec76677a77..b91b0b801d 100644 --- a/Userland/Libraries/LibDebug/DebugSession.cpp +++ b/Userland/Libraries/LibDebug/DebugSession.cpp @@ -405,10 +405,9 @@ void DebugSession::update_loaded_libs() VERIFY(rc); 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(); - auto vm_entries = json.value().as_array(); + auto const& vm_entries = json.as_array(); Regex<PosixExtended> segment_name_re("(.+): "); auto get_path_to_object = [&segment_name_re](String const& vm_name) -> Optional<String> { diff --git a/Userland/Libraries/LibDesktop/Launcher.cpp b/Userland/Libraries/LibDesktop/Launcher.cpp index 89953ebe30..36a2af332c 100644 --- a/Userland/Libraries/LibDesktop/Launcher.cpp +++ b/Userland/Libraries/LibDesktop/Launcher.cpp @@ -17,9 +17,8 @@ namespace Desktop { auto Launcher::Details::from_details_str(const String& details_str) -> NonnullRefPtr<Details> { auto details = adopt_ref(*new Details); - auto json = JsonValue::from_string(details_str); - VERIFY(json.has_value()); - auto obj = json.value().as_object(); + auto json = JsonValue::from_string(details_str).release_value_but_fixme_should_propagate_errors(); + auto const& obj = json.as_object(); details->executable = obj.get("executable").to_string(); details->name = obj.get("name").to_string(); if (auto type_value = obj.get_ptr("type")) { diff --git a/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp b/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp index b23868c452..29d1f10b7f 100644 --- a/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp +++ b/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp @@ -45,7 +45,7 @@ void CommonLocationsProvider::load_from_json(const String& json_path) } auto json = JsonValue::from_string(file->read_all()); - if (!json.has_value()) { + if (json.is_error()) { dbgln("Common locations file {} is not a valid JSON file.", file->filename()); return; } @@ -55,7 +55,7 @@ void CommonLocationsProvider::load_from_json(const String& json_path) } s_common_locations.clear(); - auto contents = json.value().as_array(); + auto const& contents = json.value().as_array(); for (size_t i = 0; i < contents.size(); ++i) { auto entry_value = contents.at(i); if (!entry_value.is_object()) diff --git a/Userland/Libraries/LibGUI/GMLParser.cpp b/Userland/Libraries/LibGUI/GMLParser.cpp index 3a8baf25af..2ef1b995e7 100644 --- a/Userland/Libraries/LibGUI/GMLParser.cpp +++ b/Userland/Libraries/LibGUI/GMLParser.cpp @@ -94,7 +94,7 @@ static Optional<JsonValue> parse_core_object(Queue<GMLToken>& tokens) } else if (peek() == GMLToken::Type::JsonValue) { auto value_string = tokens.dequeue(); auto parsed_value = JsonValue::from_string(value_string.m_view); - if (!parsed_value.has_value()) { + if (parsed_value.is_error()) { dbgln("Expected property to be JSON value"); return {}; } diff --git a/Userland/Libraries/LibGUI/JsonArrayModel.cpp b/Userland/Libraries/LibGUI/JsonArrayModel.cpp index 409c8ed732..f5f609e50a 100644 --- a/Userland/Libraries/LibGUI/JsonArrayModel.cpp +++ b/Userland/Libraries/LibGUI/JsonArrayModel.cpp @@ -20,11 +20,10 @@ void JsonArrayModel::invalidate() return; } - auto json = JsonValue::from_string(file->read_all()); + auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors(); - VERIFY(json.has_value()); - VERIFY(json.value().is_array()); - m_array = json.value().as_array(); + VERIFY(json.is_array()); + m_array = json.as_array(); did_update(); } diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp index b7f4679b1d..ac3fc9cd3f 100644 --- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp @@ -352,7 +352,7 @@ JS_DEFINE_NATIVE_FUNCTION(JSONObject::parse) auto reviver = vm.argument(1); auto json = JsonValue::from_string(string); - if (!json.has_value()) + if (json.is_error()) return vm.throw_completion<SyntaxError>(global_object, ErrorType::JsonMalformed); Value unfiltered = parse_json_value(global_object, json.value()); if (reviver.is_function()) { diff --git a/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp b/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp index b36693c008..88f2df9b41 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp +++ b/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp @@ -31,7 +31,7 @@ Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& filena auto file_contents = file->read_all(); auto json_result = JsonValue::from_string(file_contents); - if (!json_result.has_value()) { + if (json_result.is_error()) { dbgln("Failed to load character map from file {}", path); return {}; } diff --git a/Userland/Libraries/LibSymbolication/Symbolication.cpp b/Userland/Libraries/LibSymbolication/Symbolication.cpp index 07cc2205ec..3484958218 100644 --- a/Userland/Libraries/LibSymbolication/Symbolication.cpp +++ b/Userland/Libraries/LibSymbolication/Symbolication.cpp @@ -158,7 +158,7 @@ Vector<Symbol> symbolicate_thread(pid_t pid, pid_t tid, IncludeSourcePosition in } auto json = JsonValue::from_string(file_or_error.value()->read_all()); - if (!json.has_value() || !json.value().is_array()) { + if (json.is_error() || !json.value().is_array()) { warnln("Invalid contents in {}", stack_path); return {}; } @@ -178,7 +178,7 @@ Vector<Symbol> symbolicate_thread(pid_t pid, pid_t tid, IncludeSourcePosition in } auto json = JsonValue::from_string(file_or_error.value()->read_all()); - if (!json.has_value() || !json.value().is_array()) { + if (json.is_error() || !json.value().is_array()) { warnln("Invalid contents in {}", vm_path); return {}; } diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunner.h b/Userland/Libraries/LibTest/JavaScriptTestRunner.h index 7993a97841..3641559c25 100644 --- a/Userland/Libraries/LibTest/JavaScriptTestRunner.h +++ b/Userland/Libraries/LibTest/JavaScriptTestRunner.h @@ -241,7 +241,7 @@ inline Optional<JsonValue> get_test_results(JS::Interpreter& interpreter) auto json_string = TRY_OR_DISCARD(JS::JSONObject::stringify_impl(interpreter.global_object(), results, JS::js_undefined(), JS::js_undefined())); auto json = JsonValue::from_string(json_string); - if (!json.has_value()) + if (json.is_error()) return {}; return json.value(); diff --git a/Userland/Libraries/LibWeb/DOMTreeModel.h b/Userland/Libraries/LibWeb/DOMTreeModel.h index edee42e1df..3fdeeb38c8 100644 --- a/Userland/Libraries/LibWeb/DOMTreeModel.h +++ b/Userland/Libraries/LibWeb/DOMTreeModel.h @@ -18,11 +18,8 @@ class DOMTreeModel final : public GUI::Model { public: static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view) { - auto json_or_error = JsonValue::from_string(dom_tree); - if (!json_or_error.has_value()) - VERIFY_NOT_REACHED(); - - return adopt_ref(*new DOMTreeModel(json_or_error.value().as_object(), tree_view)); + auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors(); + return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), tree_view)); } virtual ~DOMTreeModel() override; diff --git a/Userland/Libraries/LibWeb/StylePropertiesModel.h b/Userland/Libraries/LibWeb/StylePropertiesModel.h index 8eec03e2f8..974f403a75 100644 --- a/Userland/Libraries/LibWeb/StylePropertiesModel.h +++ b/Userland/Libraries/LibWeb/StylePropertiesModel.h @@ -23,11 +23,8 @@ public: static NonnullRefPtr<StylePropertiesModel> create(StringView properties) { - auto json_or_error = JsonValue::from_string(properties); - if (!json_or_error.has_value()) - VERIFY_NOT_REACHED(); - - return adopt_ref(*new StylePropertiesModel(json_or_error.value().as_object())); + auto json_or_error = JsonValue::from_string(properties).release_value_but_fixme_should_propagate_errors(); + return adopt_ref(*new StylePropertiesModel(json_or_error.as_object())); } virtual ~StylePropertiesModel() override; |