1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-08-03 10:17:36 +00:00

AK+Everywhere: Remove JsonValue APIs with implicit default values

This commit is contained in:
Dan Klishch 2024-01-12 20:52:38 -05:00 committed by Andrew Kaster
parent c49819cced
commit b5f1a48a7c
16 changed files with 132 additions and 106 deletions

View file

@ -47,10 +47,11 @@ void UsersMapWidget::get_users()
auto json_users = result.release_value().as_array();
for (size_t i = 0; i < json_users.size(); i++) {
auto const& json_user = json_users.at(i).as_object();
auto const& coordinates = json_user.get_array("coordinates"sv).release_value();
User user {
MUST(String::from_byte_string(json_user.get_byte_string("nick"sv).release_value())),
{ json_user.get_array("coordinates"sv).release_value().at(0).to_double(),
json_user.get_array("coordinates"sv).release_value().at(1).to_double() },
{ coordinates[0].get_double_with_precision_loss().value(),
coordinates[1].get_double_with_precision_loss().value() },
json_user.has_bool("contributor"sv),
};
m_users.value().append(user);

View file

@ -1469,7 +1469,7 @@ ImageEditor& MainWidget::create_new_editor(NonnullRefPtr<Image> image)
else
return;
image_editor.add_guide(PixelPaint::Guide::construct(orientation, offset_value->to_number<float>()));
image_editor.add_guide(PixelPaint::Guide::construct(orientation, offset_value->get_float_with_precision_loss().value_or(0)));
});
}

View file

@ -149,7 +149,7 @@ ErrorOr<Gfx::IntSize> Presentation::parse_presentation_size(JsonObject const& me
return Error::from_string_view("Width or aspect in incorrect format"sv);
// We intentionally discard floating-point data here. If you need more resolution, just use a larger width.
auto const width = maybe_width->to_number<int>();
auto const width = maybe_width->get_number_with_precision_loss<int>().value();
auto const aspect_parts = maybe_aspect->split_view(':');
if (aspect_parts.size() != 2)
return Error::from_string_view("Aspect specification must have the exact format `width:height`"sv);

View file

@ -8,6 +8,7 @@
#include "Presentation.h"
#include <AK/JsonObject.h>
#include <AK/URL.h>
#include <LibGUI/PropertyDeserializer.h>
#include <LibGfx/Font/FontStyleMapping.h>
#include <LibGfx/Rect.h>
@ -42,16 +43,8 @@ ErrorOr<NonnullRefPtr<SlideObject>> SlideObject::parse_slide_object(JsonObject c
void SlideObject::set_property(StringView name, JsonValue value)
{
if (name == "rect"sv) {
if (value.is_array() && value.as_array().size() == 4) {
Gfx::IntRect rect;
rect.set_x(value.as_array()[0].to_i32());
rect.set_y(value.as_array()[1].to_i32());
rect.set_width(value.as_array()[2].to_i32());
rect.set_height(value.as_array()[3].to_i32());
m_rect = rect;
}
}
if (name == "rect"sv)
m_rect = GUI::PropertyDeserializer<Gfx::IntRect> {}(value).release_value_but_fixme_should_propagate_errors();
m_properties.set(name, move(value));
}
@ -74,7 +67,7 @@ void Text::set_property(StringView name, JsonValue value)
} else if (name == "font-weight"sv) {
m_font_weight = Gfx::name_to_weight(value.as_string());
} else if (name == "font-size"sv) {
m_font_size_in_pt = value.to_float();
m_font_size_in_pt = value.get_float_with_precision_loss().value();
} else if (name == "text-alignment"sv) {
m_text_align = value.as_string();
}

View file

@ -479,7 +479,7 @@ ErrorOr<NonnullOwnPtr<Profile>> Profile::load_from_perfcore_file(StringView path
auto const& stack_array = stack.value();
for (ssize_t i = stack_array.values().size() - 1; i >= 0; --i) {
auto const& frame = stack_array.at(i);
auto ptr = frame.to_number<u64>();
auto ptr = frame.as_integer<u64>();
u32 offset = 0;
DeprecatedFlyString object_name;
ByteString symbol;

View file

@ -66,16 +66,10 @@ ErrorOr<Gfx::IntRect> PropertyDeserializer<Gfx::IntRect>::operator()(JsonValue c
} else {
auto const& array = value.as_array();
auto get_i32 = [](JsonValue const& value) -> Optional<int> {
if (value.is_integer<i32>())
return value.to_i32();
return {};
};
x = get_i32(array[0]);
y = get_i32(array[1]);
width = get_i32(array[2]);
height = get_i32(array[3]);
x = array[0].get_i32();
y = array[1].get_i32();
width = array[2].get_i32();
height = array[3].get_i32();
}
if (!x.has_value())
@ -103,16 +97,16 @@ ErrorOr<Gfx::IntSize> PropertyDeserializer<Gfx::IntSize>::operator()(JsonValue c
auto const& array = value.as_array();
auto const& width = array[0];
if (!width.is_integer<i32>())
auto const& width = array[0].get_i32();
if (!width.has_value())
return Error::from_string_literal("Width must be an integer");
auto const& height = array[1];
if (!height.is_integer<i32>())
auto const& height = array[1].get_i32();
if (!height.has_value())
return Error::from_string_literal("Height must be an integer");
Gfx::IntSize size;
size.set_width(width.to_i32());
size.set_height(height.to_i32());
size.set_width(width.value());
size.set_height(height.value());
return size;
}
@ -129,10 +123,10 @@ ErrorOr<GUI::Margins> PropertyDeserializer<GUI::Margins>::operator()(JsonValue c
int m[4];
for (size_t i = 0; i < size; ++i) {
auto const& margin = array[i];
if (!margin.is_integer<i32>())
auto const& margin = array[i].get_i32();
if (!margin.has_value())
return Error::from_string_literal("Margin value should be an integer");
m[i] = margin.to_i32();
m[i] = margin.value();
}
if (size == 1)

View file

@ -23,7 +23,7 @@ struct PropertyDeserializer<T> {
{
if (!value.is_integer<T>())
return Error::from_string_literal("Value is either not an integer or out of range for requested type");
return value.to_number<T>();
return value.as_integer<T>();
}
};

View file

@ -434,8 +434,8 @@ Value JSONObject::parse_json_value(VM& vm, JsonValue const& value)
return Value(parse_json_array(vm, value.as_array()));
if (value.is_null())
return js_null();
if (value.is_number())
return Value(value.to_double(0));
if (auto double_value = value.get_double_with_precision_loss(); double_value.has_value())
return Value(double_value.value());
if (value.is_string())
return PrimitiveString::create(vm, value.as_string());
if (value.is_bool())

View file

@ -175,7 +175,7 @@ Vector<Symbol> symbolicate_thread(pid_t pid, pid_t tid, IncludeSourcePosition in
stack.ensure_capacity(json.value().as_array().size());
for (auto& value : json.value().as_array().values()) {
stack.append(value.to_addr());
stack.append(value.get_addr().value());
}
}

View file

@ -51,13 +51,24 @@ ErrorOr<TimeoutsConfiguration, Error> json_deserialize_as_a_timeouts_configurati
auto script_duration = value.as_object().get("script"sv);
// 2. If script duration is a number and less than 0 or greater than maximum safe integer, or it is not null, return error with error code invalid argument.
if (script_duration.has_value() && script_duration->is_number() && (script_duration->to_i64() < 0 || script_duration->to_i64() > max_safe_integer))
return Error::from_code(ErrorCode::InvalidArgument, "Invalid script duration");
if (script_duration.has_value() && !script_duration->is_number() && !script_duration->is_null())
return Error::from_code(ErrorCode::InvalidArgument, "Invalid script duration");
Optional<u64> script_timeout;
if (script_duration.has_value()) {
bool is_valid;
if (auto duration = script_duration->get_double_with_precision_loss(); duration.has_value()) {
is_valid = *duration >= 0 && *duration <= max_safe_integer;
// FIXME: script_timeout should be double.
script_timeout = static_cast<u64>(*duration);
} else if (script_duration->is_null()) {
is_valid = true;
} else {
is_valid = false;
}
if (!is_valid)
return Error::from_code(ErrorCode::InvalidArgument, "Invalid script duration");
}
// 3. Set timeoutss script timeout to script duration.
timeouts.script_timeout = (!script_duration.has_value() || script_duration->is_null()) ? Optional<u64> {} : script_duration->to_u64();
timeouts.script_timeout = script_timeout;
}
// 4. If value has a property with the key "pageLoad":

View file

@ -247,9 +247,9 @@ static ErrorOr<PropertyType, Web::WebDriver::Error> get_property(JsonValue const
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Boolean", key));
return property->as_bool();
} else if constexpr (IsSame<PropertyType, u32>) {
if (!property->is_integer<u32>())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Number", key));
return property->to_u32();
if (auto maybe_u32 = property->get_u32(); maybe_u32.has_value())
return *maybe_u32;
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Number", key));
} else if constexpr (IsSame<PropertyType, JsonArray const*>) {
if (!property->is_array())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not an Array", key));
@ -617,20 +617,18 @@ Messages::WebDriverClient::SetWindowRectResponse WebDriverConnection::set_window
auto const& properties = payload.as_object();
auto resolve_property = [](auto name, auto const& property, auto min, auto max) -> ErrorOr<Optional<i32>, Web::WebDriver::Error> {
auto resolve_property = [](auto name, auto const& property, i32 min, i32 max) -> ErrorOr<Optional<i32>, Web::WebDriver::Error> {
if (property.is_null())
return Optional<i32> {};
if (!property.is_number())
auto value = property.template get_integer<i32>();
if (!value.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Number", name));
if (*value < min)
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' value {} exceeds the minimum allowed value {}", name, *value, min));
if (*value > max)
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' value {} exceeds the maximum allowed value {}", name, *value, max));
auto number = property.template to_number<i64>();
if (number < min)
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' value {} exceeds the minimum allowed value {}", name, number, min));
if (number > max)
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' value {} exceeds the maximum allowed value {}", name, number, max));
return static_cast<i32>(number);
return value;
};
// 1. Let width be the result of getting a property named width from the parameters argument, else let it be null.