1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 14:27:34 +00:00

AK+WebContent: Remove JsonValue::as_{i,u}{32,64}()

This commit is contained in:
Dan Klishch 2023-11-13 22:44:41 -05:00 committed by Andrew Kaster
parent 0388c828be
commit 5230d2af91
4 changed files with 16 additions and 37 deletions

View file

@ -162,16 +162,16 @@ inline void JsonValue::serialize(Builder& builder) const
break; break;
#endif #endif
case Type::Int32: case Type::Int32:
builder.appendff("{}", as_i32()); builder.appendff("{}", m_value.as_i32);
break; break;
case Type::Int64: case Type::Int64:
builder.appendff("{}", as_i64()); builder.appendff("{}", m_value.as_i64);
break; break;
case Type::UnsignedInt32: case Type::UnsignedInt32:
builder.appendff("{}", as_u32()); builder.appendff("{}", m_value.as_u32);
break; break;
case Type::UnsignedInt64: case Type::UnsignedInt64:
builder.appendff("{}", as_u64()); builder.appendff("{}", m_value.as_u64);
break; break;
case Type::Null: case Type::Null:
builder.append("null"sv); builder.append("null"sv);

View file

@ -118,30 +118,6 @@ public:
return as_bool(); return as_bool();
} }
i32 as_i32() const
{
VERIFY(is_i32());
return m_value.as_i32;
}
u32 as_u32() const
{
VERIFY(is_u32());
return m_value.as_u32;
}
i64 as_i64() const
{
VERIFY(is_i64());
return m_value.as_i64;
}
u64 as_u64() const
{
VERIFY(is_u64());
return m_value.as_u64;
}
bool as_bool() const bool as_bool() const
{ {
VERIFY(is_bool()); VERIFY(is_bool());
@ -217,15 +193,15 @@ public:
T to_number(T default_value = 0) const T to_number(T default_value = 0) const
{ {
if (is_double()) if (is_double())
return (T)as_double(); return (T)m_value.as_double;
if (type() == Type::Int32) if (type() == Type::Int32)
return (T)as_i32(); return (T)m_value.as_i32;
if (type() == Type::UnsignedInt32) if (type() == Type::UnsignedInt32)
return (T)as_u32(); return (T)m_value.as_u32;
if (type() == Type::Int64) if (type() == Type::Int64)
return (T)as_i64(); return (T)m_value.as_i64;
if (type() == Type::UnsignedInt64) if (type() == Type::UnsignedInt64)
return (T)as_u64(); return (T)m_value.as_u64;
return default_value; return default_value;
} }

View file

@ -99,8 +99,11 @@ TEST_CASE(json_64_bit_value)
{ {
auto big_value = 0x12345678aabbccddull; auto big_value = 0x12345678aabbccddull;
JsonValue big_json_value(big_value); JsonValue big_json_value(big_value);
EXPECT(big_json_value.is_integer<u64>());
EXPECT_EQ(big_json_value.as_integer<u64>(), big_value);
JsonValue big_json_value_copy = big_json_value; JsonValue big_json_value_copy = big_json_value;
EXPECT_EQ(big_json_value.as_u64(), big_json_value_copy.as_u64()); EXPECT(big_json_value.equals(big_json_value_copy));
} }
TEST_CASE(json_duplicate_keys) TEST_CASE(json_duplicate_keys)
@ -118,7 +121,7 @@ TEST_CASE(json_u64_roundtrip)
auto json = JsonValue(big_value).serialized<StringBuilder>(); auto json = JsonValue(big_value).serialized<StringBuilder>();
auto value = JsonValue::from_string(json); auto value = JsonValue::from_string(json);
EXPECT_EQ_FORCE(value.is_error(), false); EXPECT_EQ_FORCE(value.is_error(), false);
EXPECT_EQ(value.value().as_u64(), big_value); EXPECT_EQ(value.value().as_integer<u64>(), big_value);
} }
TEST_CASE(json_parse_empty_string) TEST_CASE(json_parse_empty_string)

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 Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Boolean", key));
return property->as_bool(); return property->as_bool();
} else if constexpr (IsSame<PropertyType, u32>) { } else if constexpr (IsSame<PropertyType, u32>) {
if (!property->is_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 Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Number", key));
return property->as_u32(); return property->to_u32();
} else if constexpr (IsSame<PropertyType, JsonArray const*>) { } else if constexpr (IsSame<PropertyType, JsonArray const*>) {
if (!property->is_array()) if (!property->is_array())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not an Array", key)); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not an Array", key));