summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-11-07 16:30:55 -0500
committerTim Flynn <trflynn89@pm.me>2022-11-08 19:58:34 -0500
commit3994a7971888fe636c59fc49e2b9b6a12b66c581 (patch)
tree95fd576dc473ab9d96f8c613d767e2f7e17064a3 /Userland
parent08750f69e4c720e85d73af22f2f938f091fe818e (diff)
downloadserenity-3994a7971888fe636c59fc49e2b9b6a12b66c581.zip
AK+LibIPC: Add a convenience encoder/decoder for JsonValue
This requires that JsonValue is implicitly default-constructible.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibIPC/Decoder.cpp9
-rw-r--r--Userland/Libraries/LibIPC/Decoder.h1
-rw-r--r--Userland/Libraries/LibIPC/Encoder.cpp8
-rw-r--r--Userland/Libraries/LibIPC/Encoder.h1
4 files changed, 19 insertions, 0 deletions
diff --git a/Userland/Libraries/LibIPC/Decoder.cpp b/Userland/Libraries/LibIPC/Decoder.cpp
index 731a5fb669..8e2f25b4e6 100644
--- a/Userland/Libraries/LibIPC/Decoder.cpp
+++ b/Userland/Libraries/LibIPC/Decoder.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/JsonValue.h>
#include <AK/MemoryStream.h>
#include <AK/URL.h>
#include <LibCore/AnonymousBuffer.h>
@@ -128,6 +129,14 @@ ErrorOr<void> Decoder::decode(ByteBuffer& value)
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)
{
String string;
diff --git a/Userland/Libraries/LibIPC/Decoder.h b/Userland/Libraries/LibIPC/Decoder.h
index b938b21063..06115bbbef 100644
--- a/Userland/Libraries/LibIPC/Decoder.h
+++ b/Userland/Libraries/LibIPC/Decoder.h
@@ -49,6 +49,7 @@ public:
ErrorOr<void> decode(double&);
ErrorOr<void> decode(String&);
ErrorOr<void> decode(ByteBuffer&);
+ ErrorOr<void> decode(JsonValue&);
ErrorOr<void> decode(URL&);
ErrorOr<void> decode(Dictionary&);
ErrorOr<void> decode(File&);
diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp
index 64e67b3c2b..132cc8adaa 100644
--- a/Userland/Libraries/LibIPC/Encoder.cpp
+++ b/Userland/Libraries/LibIPC/Encoder.cpp
@@ -7,6 +7,8 @@
#include <AK/BitCast.h>
#include <AK/ByteBuffer.h>
+#include <AK/JsonObject.h>
+#include <AK/JsonValue.h>
#include <AK/String.h>
#include <AK/URL.h>
#include <LibCore/AnonymousBuffer.h>
@@ -159,6 +161,12 @@ Encoder& Encoder::operator<<(ByteBuffer const& value)
return *this;
}
+Encoder& Encoder::operator<<(JsonValue const& value)
+{
+ *this << value.serialized<StringBuilder>();
+ return *this;
+}
+
Encoder& Encoder::operator<<(URL const& value)
{
return *this << value.to_string();
diff --git a/Userland/Libraries/LibIPC/Encoder.h b/Userland/Libraries/LibIPC/Encoder.h
index c5bbc72bea..5da7b9ac7c 100644
--- a/Userland/Libraries/LibIPC/Encoder.h
+++ b/Userland/Libraries/LibIPC/Encoder.h
@@ -45,6 +45,7 @@ public:
Encoder& operator<<(StringView);
Encoder& operator<<(String const&);
Encoder& operator<<(ByteBuffer const&);
+ Encoder& operator<<(JsonValue const&);
Encoder& operator<<(URL const&);
Encoder& operator<<(Dictionary const&);
Encoder& operator<<(File const&);