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

@ -138,16 +138,16 @@ Optional<JsonArray const&> JsonObject::get_array(StringView key) const
Optional<double> JsonObject::get_double_with_precision_loss(StringView key) const
{
auto maybe_value = get(key);
if (maybe_value.has_value() && maybe_value->is_number())
return maybe_value->to_number<double>();
if (maybe_value.has_value())
return maybe_value->get_double_with_precision_loss();
return {};
}
Optional<float> JsonObject::get_float_with_precision_loss(StringView key) const
{
auto maybe_value = get(key);
if (maybe_value.has_value() && maybe_value->is_number())
return maybe_value->to_number<float>();
if (maybe_value.has_value())
return maybe_value->get_float_with_precision_loss();
return {};
}
#endif

View file

@ -77,15 +77,31 @@ bool JsonValue::equals(JsonValue const& other) const
if (is_string() && other.is_string() && as_string() == other.as_string())
return true;
#if !defined(KERNEL)
if (is_number() && other.is_number() && to_number<double>() == other.to_number<double>()) {
return true;
if (is_number() && other.is_number()) {
auto normalize = [](Variant<u64, i64, double> representation, bool& is_negative) {
return representation.visit(
[&](u64& value) -> Variant<u64, double> {
is_negative = false;
return value;
},
[&](i64& value) -> Variant<u64, double> {
is_negative = value < 0;
return static_cast<u64>(abs(value));
},
[&](double& value) -> Variant<u64, double> {
is_negative = value < 0;
value = abs(value);
if (static_cast<double>(static_cast<u64>(value)) == value)
return static_cast<u64>(value);
return value;
});
};
bool is_this_negative;
auto normalized_this = normalize(as_number(), is_this_negative);
bool is_that_negative;
auto normalized_that = normalize(other.as_number(), is_that_negative);
return is_this_negative == is_that_negative && normalized_this == normalized_that;
}
#else
if (is_number() && other.is_number() && to_number<i64>() == other.to_number<i64>()) {
return true;
}
#endif
if (is_array() && other.is_array() && as_array().size() == other.as_array().size()) {
bool result = true;

View file

@ -92,29 +92,29 @@ public:
return serialized<StringBuilder>();
}
int to_int(int default_value = 0) const { return to_i32(default_value); }
i32 to_i32(i32 default_value = 0) const { return to_number<i32>(default_value); }
i64 to_i64(i64 default_value = 0) const { return to_number<i64>(default_value); }
Optional<int> get_int() const { return get_integer<int>(); }
Optional<i32> get_i32() const { return get_integer<i32>(); }
Optional<i64> get_i64() const { return get_integer<i64>(); }
unsigned to_uint(unsigned default_value = 0) const { return to_u32(default_value); }
u32 to_u32(u32 default_value = 0) const { return to_number<u32>(default_value); }
u64 to_u64(u64 default_value = 0) const { return to_number<u64>(default_value); }
float to_float(float default_value = 0) const { return to_number<float>(default_value); }
double to_double(double default_value = 0) const { return to_number<double>(default_value); }
Optional<unsigned> get_uint() const { return get_integer<unsigned>(); }
Optional<u32> get_u32() const { return get_integer<u32>(); }
Optional<u64> get_u64() const { return get_integer<u64>(); }
Optional<float> get_float_with_precision_loss() const { return get_number_with_precision_loss<float>(); }
Optional<double> get_double_with_precision_loss() const { return get_number_with_precision_loss<double>(); }
FlatPtr to_addr(FlatPtr default_value = 0) const
Optional<FlatPtr> get_addr() const
{
#ifdef __LP64__
return to_u64(default_value);
return get_u64();
#else
return to_u32(default_value);
return get_u32();
#endif
}
bool to_bool(bool default_value = false) const
Optional<bool> get_bool() const
{
if (!is_bool())
return default_value;
return {};
return as_bool();
}
@ -199,19 +199,22 @@ public:
}
template<typename T>
T to_number(T default_value = 0) const
Optional<T> get_number_with_precision_loss() const
{
if (type() == Type::Double)
return (T)m_value.as_double;
if (type() == Type::Int32)
return (T)m_value.as_i32;
if (type() == Type::UnsignedInt32)
return (T)m_value.as_u32;
if (type() == Type::Int64)
return (T)m_value.as_i64;
if (type() == Type::UnsignedInt64)
return (T)m_value.as_u64;
return default_value;
switch (m_type) {
case Type::Double:
return static_cast<T>(m_value.as_double);
case Type::Int32:
return static_cast<T>(m_value.as_i32);
case Type::UnsignedInt32:
return static_cast<T>(m_value.as_u32);
case Type::Int64:
return static_cast<T>(m_value.as_i64);
case Type::UnsignedInt64:
return static_cast<T>(m_value.as_u64);
default:
return {};
}
}
template<Integral T>
@ -250,6 +253,14 @@ public:
}
}
template<Integral T>
Optional<T> get_integer() const
{
if (!is_integer<T>())
return {};
return as_integer<T>();
}
bool equals(JsonValue const& other) const;
private: