mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
JsonValue: Add to_uint(), to_int() and as_double().
The to_foo() functions are for converting when you might not be sure of the underlying value type. The as_foo() family assumes that you know exactly what the underlying value type is.
This commit is contained in:
parent
b12d1fbb81
commit
f60d7e5d1f
1 changed files with 34 additions and 0 deletions
|
@ -67,6 +67,32 @@ public:
|
||||||
return IPv4Address::from_string(as_string());
|
return IPv4Address::from_string(as_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int to_int(int default_value = 0) const
|
||||||
|
{
|
||||||
|
if (!is_number())
|
||||||
|
return default_value;
|
||||||
|
#ifndef KERNEL
|
||||||
|
if (is_double())
|
||||||
|
return (int)as_double();
|
||||||
|
#endif
|
||||||
|
if (is_uint())
|
||||||
|
return (int)as_uint();
|
||||||
|
return as_int();
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned to_uint(unsigned default_value = 0) const
|
||||||
|
{
|
||||||
|
if (!is_number())
|
||||||
|
return default_value;
|
||||||
|
#ifndef KERNEL
|
||||||
|
if (is_double())
|
||||||
|
return (unsigned)as_double();
|
||||||
|
#endif
|
||||||
|
if (is_int())
|
||||||
|
return (unsigned)as_int();
|
||||||
|
return as_uint();
|
||||||
|
}
|
||||||
|
|
||||||
bool to_bool(bool default_value = false) const
|
bool to_bool(bool default_value = false) const
|
||||||
{
|
{
|
||||||
if (!is_bool())
|
if (!is_bool())
|
||||||
|
@ -110,6 +136,14 @@ public:
|
||||||
return *m_value.as_array;
|
return *m_value.as_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef KERNEL
|
||||||
|
double as_double() const
|
||||||
|
{
|
||||||
|
ASSERT(is_double());
|
||||||
|
return m_value.as_double;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Type type() const { return m_type; }
|
Type type() const { return m_type; }
|
||||||
|
|
||||||
bool is_null() const { return m_type == Type::Null; }
|
bool is_null() const { return m_type == Type::Null; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue