mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:57:45 +00:00
AK: Make it easy to convert between JsonValue and IPv4Address.
They still use string storage, but this change makes it nice and easy to work with IPv4 addresses in JSON data.
This commit is contained in:
parent
d8f1a7e046
commit
7bb1e465c6
2 changed files with 15 additions and 0 deletions
|
@ -110,6 +110,11 @@ JsonValue::JsonValue(const String& value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue::JsonValue(const IPv4Address& value)
|
||||||
|
: JsonValue(value.to_string())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
JsonValue::JsonValue(const JsonObject& value)
|
JsonValue::JsonValue(const JsonObject& value)
|
||||||
: m_type(Type::Object)
|
: m_type(Type::Object)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
|
#include <AK/IPv4Address.h>
|
||||||
|
#include <AK/Optional.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -44,6 +46,7 @@ public:
|
||||||
JsonValue(bool);
|
JsonValue(bool);
|
||||||
JsonValue(const char*);
|
JsonValue(const char*);
|
||||||
JsonValue(const String&);
|
JsonValue(const String&);
|
||||||
|
JsonValue(const IPv4Address&);
|
||||||
JsonValue(const JsonArray&);
|
JsonValue(const JsonArray&);
|
||||||
JsonValue(const JsonObject&);
|
JsonValue(const JsonObject&);
|
||||||
|
|
||||||
|
@ -57,6 +60,13 @@ public:
|
||||||
return default_value;
|
return default_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<IPv4Address> to_ipv4_address() const
|
||||||
|
{
|
||||||
|
if (!is_string())
|
||||||
|
return {};
|
||||||
|
return IPv4Address::from_string(as_string());
|
||||||
|
}
|
||||||
|
|
||||||
int as_int() const
|
int as_int() const
|
||||||
{
|
{
|
||||||
ASSERT(is_int());
|
ASSERT(is_int());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue