2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2022-02-24 21:08:48 +03:00
|
|
|
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-08-27 14:12:44 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
#include <AK/Error.h>
|
2019-08-27 14:12:44 +03:00
|
|
|
#include <AK/JsonArraySerializer.h>
|
2024-06-18 01:12:53 +03:00
|
|
|
#include <AK/JsonValue.h>
|
2022-02-24 21:08:48 +03:00
|
|
|
#include <AK/Try.h>
|
2021-06-30 12:31:12 +03:00
|
|
|
|
2019-08-27 14:12:44 +03:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
class JsonObjectSerializer {
|
|
|
|
public:
|
2022-02-24 21:08:48 +03:00
|
|
|
static ErrorOr<JsonObjectSerializer> try_create(Builder& builder)
|
|
|
|
{
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(builder.try_append('{'));
|
|
|
|
else
|
|
|
|
TRY(builder.append('{'));
|
|
|
|
return JsonObjectSerializer { builder };
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObjectSerializer(JsonObjectSerializer&& other)
|
|
|
|
: m_builder(other.m_builder)
|
|
|
|
, m_empty(other.m_empty)
|
|
|
|
, m_finished(exchange(other.m_finished, true))
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonObjectSerializer(JsonObjectSerializer const&) = delete;
|
2019-08-27 14:12:44 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> add(StringView key, JsonValue const& value)
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
2019-08-27 14:12:44 +03:00
|
|
|
value.serialize(m_builder);
|
2022-02-24 21:08:48 +03:00
|
|
|
return {};
|
2019-08-27 14:12:44 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, StringView value)
|
2019-09-04 15:39:08 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>) {
|
|
|
|
TRY(m_builder.try_append('"'));
|
|
|
|
TRY(m_builder.try_append_escaped_for_json(value));
|
|
|
|
TRY(m_builder.try_append('"'));
|
|
|
|
} else {
|
|
|
|
TRY(m_builder.append('"'));
|
|
|
|
TRY(m_builder.append_escaped_for_json(value));
|
|
|
|
TRY(m_builder.append('"'));
|
|
|
|
}
|
|
|
|
return {};
|
2019-09-04 15:39:08 +03:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ErrorOr<void> add(StringView key, ByteString const& value)
|
2019-09-04 15:39:08 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>) {
|
|
|
|
TRY(m_builder.try_append('"'));
|
|
|
|
TRY(m_builder.try_append_escaped_for_json(value));
|
|
|
|
TRY(m_builder.try_append('"'));
|
|
|
|
} else {
|
|
|
|
TRY(m_builder.append('"'));
|
|
|
|
TRY(m_builder.append_escaped_for_json(value));
|
|
|
|
TRY(m_builder.append('"'));
|
|
|
|
}
|
|
|
|
return {};
|
2019-09-04 15:39:08 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> add(StringView key, char const* value)
|
2019-09-04 15:39:08 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>) {
|
|
|
|
TRY(m_builder.try_append('"'));
|
2023-01-07 23:57:33 +03:00
|
|
|
TRY(m_builder.try_append_escaped_for_json({ value, __builtin_strlen(value) }));
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(m_builder.try_append('"'));
|
|
|
|
} else {
|
|
|
|
TRY(m_builder.append('"'));
|
2023-01-07 23:57:33 +03:00
|
|
|
TRY(m_builder.append_escaped_for_json({ value, __builtin_strlen(value) }));
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(m_builder.append('"'));
|
|
|
|
}
|
|
|
|
return {};
|
2019-09-04 15:39:08 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, bool value)
|
2020-02-08 04:48:27 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(m_builder.try_append(value ? "true"sv : "false"sv));
|
2022-02-24 21:08:48 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(m_builder.append(value ? "true"sv : "false"sv));
|
2022-02-24 21:08:48 +03:00
|
|
|
return {};
|
2020-02-08 04:48:27 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, int value)
|
2020-02-01 12:55:16 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-02-01 12:55:16 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, unsigned value)
|
2020-02-01 12:55:16 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-02-01 12:55:16 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, long value)
|
2020-05-22 14:57:23 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-05-22 14:57:23 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, long unsigned value)
|
2020-05-22 14:57:23 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-05-22 14:57:23 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, long long value)
|
2020-02-01 12:55:16 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-02-01 12:55:16 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, long long unsigned value)
|
2020-02-01 12:55:16 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-02-01 12:55:16 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 15:18:26 +03:00
|
|
|
ErrorOr<void> add(StringView key, float value)
|
|
|
|
{
|
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> add(StringView key, double value)
|
2020-03-31 14:30:09 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_appendff("{}", value));
|
|
|
|
else
|
|
|
|
TRY(m_builder.appendff("{}", value));
|
|
|
|
return {};
|
2020-03-31 14:30:09 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<JsonArraySerializer<Builder>> add_array(StringView key)
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
return JsonArraySerializer<Builder>::try_create(m_builder);
|
2019-08-27 14:12:44 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<JsonObjectSerializer<Builder>> add_object(StringView key)
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item(key));
|
|
|
|
return JsonObjectSerializer::try_create(m_builder);
|
2019-08-27 14:12:44 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> finish()
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_finished);
|
2019-08-27 14:12:44 +03:00
|
|
|
m_finished = true;
|
2022-02-24 21:08:48 +03:00
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_append('}'));
|
|
|
|
else
|
|
|
|
TRY(m_builder.append('}'));
|
|
|
|
return {};
|
2019-08-27 14:12:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-02-24 21:08:48 +03:00
|
|
|
explicit JsonObjectSerializer(Builder& builder)
|
|
|
|
: m_builder(builder)
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> begin_item(StringView key)
|
|
|
|
{
|
|
|
|
VERIFY(!m_finished);
|
|
|
|
if (!m_empty) {
|
|
|
|
if constexpr (IsLegacyBuilder<Builder>)
|
|
|
|
TRY(m_builder.try_append(','));
|
|
|
|
else
|
|
|
|
TRY(m_builder.append(','));
|
|
|
|
}
|
2019-08-27 14:12:44 +03:00
|
|
|
m_empty = false;
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
if constexpr (IsLegacyBuilder<Builder>) {
|
|
|
|
TRY(m_builder.try_append('"'));
|
|
|
|
TRY(m_builder.try_append_escaped_for_json(key));
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(m_builder.try_append("\":"sv));
|
2022-02-24 21:08:48 +03:00
|
|
|
} else {
|
|
|
|
TRY(m_builder.append('"'));
|
|
|
|
TRY(m_builder.append_escaped_for_json(key));
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(m_builder.append("\":"sv));
|
2022-02-24 21:08:48 +03:00
|
|
|
}
|
|
|
|
return {};
|
2019-08-27 14:12:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Builder& m_builder;
|
|
|
|
bool m_empty { true };
|
|
|
|
bool m_finished { false };
|
|
|
|
};
|
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
// Template magic to allow for JsonObjectSerializer<>::try_create(...) - Blame CxByte
|
|
|
|
template<>
|
|
|
|
struct JsonObjectSerializer<void> {
|
|
|
|
template<typename Builder>
|
|
|
|
static ErrorOr<JsonObjectSerializer<Builder>> try_create(Builder& builder)
|
|
|
|
{
|
|
|
|
return JsonObjectSerializer<Builder>::try_create(builder);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-27 14:12:44 +03:00
|
|
|
template<typename Builder>
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<JsonObjectSerializer<Builder>> JsonArraySerializer<Builder>::add_object()
|
2019-08-27 14:12:44 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(begin_item());
|
|
|
|
return JsonObjectSerializer<Builder>::try_create(m_builder);
|
2019-08-27 14:12:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-08-27 14:12:44 +03:00
|
|
|
using AK::JsonObjectSerializer;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|