1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:07: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:
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

@ -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())

View file

@ -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 {};
}

View file

@ -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();
}