mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 16:07:46 +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
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 {};
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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")) {
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue