1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:37:45 +00:00

Everywhere: Make JSON serialization fallible

This allows us to eliminate a major source of infallible allocation in
the Kernel, as well as lay down the groundwork for OOM fallibility in
userland.
This commit is contained in:
Idan Horowitz 2022-02-24 20:08:48 +02:00 committed by Andreas Kling
parent 6682afb5d4
commit feb00b7105
18 changed files with 837 additions and 592 deletions

View file

@ -111,22 +111,22 @@ ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject
void Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const
{
json.add("width", m_size.width());
json.add("height", m_size.height());
MUST(json.add("width", m_size.width()));
MUST(json.add("height", m_size.height()));
{
auto json_layers = json.add_array("layers");
auto json_layers = MUST(json.add_array("layers"));
for (const auto& layer : m_layers) {
Gfx::BMPWriter bmp_dumber;
auto json_layer = json_layers.add_object();
json_layer.add("width", layer.size().width());
json_layer.add("height", layer.size().height());
json_layer.add("name", layer.name());
json_layer.add("locationx", layer.location().x());
json_layer.add("locationy", layer.location().y());
json_layer.add("opacity_percent", layer.opacity_percent());
json_layer.add("visible", layer.is_visible());
json_layer.add("selected", layer.is_selected());
json_layer.add("bitmap", encode_base64(bmp_dumber.dump(layer.bitmap())));
auto json_layer = MUST(json_layers.add_object());
MUST(json_layer.add("width", layer.size().width()));
MUST(json_layer.add("height", layer.size().height()));
MUST(json_layer.add("name", layer.name()));
MUST(json_layer.add("locationx", layer.location().x()));
MUST(json_layer.add("locationy", layer.location().y()));
MUST(json_layer.add("opacity_percent", layer.opacity_percent()));
MUST(json_layer.add("visible", layer.is_visible()));
MUST(json_layer.add("selected", layer.is_selected()));
MUST(json_layer.add("bitmap", encode_base64(bmp_dumber.dump(layer.bitmap()))));
}
}
}
@ -134,9 +134,9 @@ void Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const
ErrorOr<void> Image::write_to_file(const String& file_path) const
{
StringBuilder builder;
JsonObjectSerializer json(builder);
auto json = MUST(JsonObjectSerializer<>::try_create(builder));
serialize_as_json(json);
json.finish();
MUST(json.finish());
auto file = TRY(Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)));
if (!file->write(builder.string_view()))