mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:17:35 +00:00
AK+LibIPC: Add a convenience encoder/decoder for JsonValue
This requires that JsonValue is implicitly default-constructible.
This commit is contained in:
parent
08750f69e4
commit
3994a79718
5 changed files with 21 additions and 1 deletions
|
@ -35,7 +35,8 @@ public:
|
||||||
|
|
||||||
static ErrorOr<JsonValue> from_string(StringView);
|
static ErrorOr<JsonValue> from_string(StringView);
|
||||||
|
|
||||||
explicit JsonValue(Type = Type::Null);
|
JsonValue() = default;
|
||||||
|
explicit JsonValue(Type);
|
||||||
~JsonValue() { clear(); }
|
~JsonValue() { clear(); }
|
||||||
|
|
||||||
JsonValue(JsonValue const&);
|
JsonValue(JsonValue const&);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
#include <LibCore/AnonymousBuffer.h>
|
#include <LibCore/AnonymousBuffer.h>
|
||||||
|
@ -128,6 +129,14 @@ ErrorOr<void> Decoder::decode(ByteBuffer& value)
|
||||||
return m_stream.try_handle_any_error();
|
return m_stream.try_handle_any_error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> Decoder::decode(JsonValue& value)
|
||||||
|
{
|
||||||
|
String string;
|
||||||
|
TRY(decode(string));
|
||||||
|
value = TRY(JsonValue::from_string(string));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<void> Decoder::decode(URL& value)
|
ErrorOr<void> Decoder::decode(URL& value)
|
||||||
{
|
{
|
||||||
String string;
|
String string;
|
||||||
|
|
|
@ -49,6 +49,7 @@ public:
|
||||||
ErrorOr<void> decode(double&);
|
ErrorOr<void> decode(double&);
|
||||||
ErrorOr<void> decode(String&);
|
ErrorOr<void> decode(String&);
|
||||||
ErrorOr<void> decode(ByteBuffer&);
|
ErrorOr<void> decode(ByteBuffer&);
|
||||||
|
ErrorOr<void> decode(JsonValue&);
|
||||||
ErrorOr<void> decode(URL&);
|
ErrorOr<void> decode(URL&);
|
||||||
ErrorOr<void> decode(Dictionary&);
|
ErrorOr<void> decode(Dictionary&);
|
||||||
ErrorOr<void> decode(File&);
|
ErrorOr<void> decode(File&);
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
#include <AK/BitCast.h>
|
#include <AK/BitCast.h>
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
#include <LibCore/AnonymousBuffer.h>
|
#include <LibCore/AnonymousBuffer.h>
|
||||||
|
@ -159,6 +161,12 @@ Encoder& Encoder::operator<<(ByteBuffer const& value)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Encoder& Encoder::operator<<(JsonValue const& value)
|
||||||
|
{
|
||||||
|
*this << value.serialized<StringBuilder>();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Encoder& Encoder::operator<<(URL const& value)
|
Encoder& Encoder::operator<<(URL const& value)
|
||||||
{
|
{
|
||||||
return *this << value.to_string();
|
return *this << value.to_string();
|
||||||
|
|
|
@ -45,6 +45,7 @@ public:
|
||||||
Encoder& operator<<(StringView);
|
Encoder& operator<<(StringView);
|
||||||
Encoder& operator<<(String const&);
|
Encoder& operator<<(String const&);
|
||||||
Encoder& operator<<(ByteBuffer const&);
|
Encoder& operator<<(ByteBuffer const&);
|
||||||
|
Encoder& operator<<(JsonValue const&);
|
||||||
Encoder& operator<<(URL const&);
|
Encoder& operator<<(URL const&);
|
||||||
Encoder& operator<<(Dictionary const&);
|
Encoder& operator<<(Dictionary const&);
|
||||||
Encoder& operator<<(File const&);
|
Encoder& operator<<(File const&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue