2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
#pragma once
|
|
|
|
|
2023-11-14 06:10:20 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
# error "JsonValue does not propagate allocation failures, so it is not safe to use in the kernel."
|
|
|
|
#endif
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/Forward.h>
|
2019-07-08 14:03:23 +03:00
|
|
|
#include <AK/Optional.h>
|
2019-08-07 22:28:07 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
class JsonValue {
|
|
|
|
public:
|
|
|
|
enum class Type {
|
|
|
|
Null,
|
2019-10-29 18:36:50 +03:00
|
|
|
Int32,
|
|
|
|
UnsignedInt32,
|
|
|
|
Int64,
|
|
|
|
UnsignedInt64,
|
2019-06-17 20:47:35 +03:00
|
|
|
Double,
|
|
|
|
Bool,
|
|
|
|
String,
|
|
|
|
Array,
|
|
|
|
Object,
|
|
|
|
};
|
|
|
|
|
2021-11-15 03:46:51 +03:00
|
|
|
static ErrorOr<JsonValue> from_string(StringView);
|
2019-06-24 12:25:10 +03:00
|
|
|
|
2022-11-08 00:30:55 +03:00
|
|
|
JsonValue() = default;
|
2019-06-17 20:47:35 +03:00
|
|
|
~JsonValue() { clear(); }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonValue(JsonValue const&);
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue(JsonValue&&);
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonValue& operator=(JsonValue const&);
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue& operator=(JsonValue&&);
|
|
|
|
|
2020-05-22 14:57:23 +03:00
|
|
|
JsonValue(int);
|
|
|
|
JsonValue(unsigned);
|
|
|
|
JsonValue(long);
|
|
|
|
JsonValue(long unsigned);
|
|
|
|
JsonValue(long long);
|
|
|
|
JsonValue(long long unsigned);
|
2019-10-29 18:36:50 +03:00
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue(double);
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonValue(char const*);
|
2023-12-16 17:19:34 +03:00
|
|
|
JsonValue(ByteString const&);
|
2022-01-27 15:01:10 +03:00
|
|
|
JsonValue(StringView);
|
2022-12-08 19:03:39 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires(SameAs<RemoveCVReference<T>, bool>)
|
|
|
|
JsonValue(T value)
|
|
|
|
: m_type(Type::Bool)
|
|
|
|
, m_value { .as_bool = value }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonValue(JsonArray const&);
|
|
|
|
JsonValue(JsonObject const&);
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2019-08-04 12:46:31 +03:00
|
|
|
JsonValue(JsonArray&&);
|
|
|
|
JsonValue(JsonObject&&);
|
|
|
|
|
|
|
|
// FIXME: Implement these
|
|
|
|
JsonValue& operator=(JsonArray&&) = delete;
|
|
|
|
JsonValue& operator=(JsonObject&&) = delete;
|
|
|
|
|
2019-08-07 22:28:07 +03:00
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType serialized() const;
|
|
|
|
template<typename Builder>
|
|
|
|
void serialize(Builder&) const;
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString as_string_or(ByteString const& alternative) const
|
2019-08-07 23:03:25 +03:00
|
|
|
{
|
|
|
|
if (is_string())
|
|
|
|
return as_string();
|
|
|
|
return alternative;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString to_byte_string() const
|
2019-06-24 15:25:45 +03:00
|
|
|
{
|
|
|
|
if (is_string())
|
|
|
|
return as_string();
|
2019-08-07 22:28:07 +03:00
|
|
|
return serialized<StringBuilder>();
|
2019-06-24 15:25:45 +03:00
|
|
|
}
|
|
|
|
|
2023-11-14 06:10:20 +03:00
|
|
|
int to_int(int default_value = 0) const { return to_i32(default_value); }
|
2019-10-29 18:36:50 +03:00
|
|
|
i32 to_i32(i32 default_value = 0) const { return to_number<i32>(default_value); }
|
2021-03-17 20:19:30 +03:00
|
|
|
i64 to_i64(i64 default_value = 0) const { return to_number<i64>(default_value); }
|
2019-10-29 18:36:50 +03:00
|
|
|
|
|
|
|
unsigned to_uint(unsigned default_value = 0) const { return to_u32(default_value); }
|
|
|
|
u32 to_u32(u32 default_value = 0) const { return to_number<u32>(default_value); }
|
2021-03-17 20:19:30 +03:00
|
|
|
u64 to_u64(u64 default_value = 0) const { return to_number<u64>(default_value); }
|
2023-11-14 06:10:20 +03:00
|
|
|
float to_float(float default_value = 0) const { return to_number<float>(default_value); }
|
2022-03-06 15:18:26 +03:00
|
|
|
double to_double(double default_value = 0) const { return to_number<double>(default_value); }
|
2022-02-27 03:31:36 +03:00
|
|
|
|
2021-07-21 20:57:05 +03:00
|
|
|
FlatPtr to_addr(FlatPtr default_value = 0) const
|
|
|
|
{
|
|
|
|
#ifdef __LP64__
|
|
|
|
return to_u64(default_value);
|
|
|
|
#else
|
|
|
|
return to_u32(default_value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:50 +03:00
|
|
|
bool to_bool(bool default_value = false) const
|
2019-07-18 09:18:39 +03:00
|
|
|
{
|
2019-10-29 18:36:50 +03:00
|
|
|
if (!is_bool())
|
2019-07-18 09:18:39 +03:00
|
|
|
return default_value;
|
2019-10-29 18:36:50 +03:00
|
|
|
return as_bool();
|
2019-07-18 09:18:39 +03:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:17:26 +03:00
|
|
|
i32 as_i32() const
|
2019-07-18 09:18:39 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_i32());
|
2019-10-29 18:36:50 +03:00
|
|
|
return m_value.as_i32;
|
2019-07-18 09:18:39 +03:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:17:26 +03:00
|
|
|
u32 as_u32() const
|
2019-07-08 15:06:03 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_u32());
|
2019-10-29 18:36:50 +03:00
|
|
|
return m_value.as_u32;
|
2019-07-08 15:06:03 +03:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:17:26 +03:00
|
|
|
i64 as_i64() const
|
2019-06-29 13:04:36 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_i64());
|
2019-10-29 18:36:50 +03:00
|
|
|
return m_value.as_i64;
|
2019-06-29 13:04:36 +03:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:17:26 +03:00
|
|
|
u64 as_u64() const
|
2019-06-29 13:04:36 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_u64());
|
2019-10-29 18:36:50 +03:00
|
|
|
return m_value.as_u64;
|
2019-06-29 13:04:36 +03:00
|
|
|
}
|
|
|
|
|
2021-12-15 16:47:26 +03:00
|
|
|
bool as_bool() const
|
2019-06-29 13:04:36 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_bool());
|
2019-06-29 13:04:36 +03:00
|
|
|
return m_value.as_bool;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString as_string() const
|
2019-06-18 09:55:58 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_string());
|
2019-06-24 13:03:31 +03:00
|
|
|
return *m_value.as_string;
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:17:13 +03:00
|
|
|
JsonObject& as_object()
|
|
|
|
{
|
|
|
|
VERIFY(is_object());
|
|
|
|
return *m_value.as_object;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonObject const& as_object() const
|
2019-06-24 13:03:31 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_object());
|
2019-06-24 13:03:31 +03:00
|
|
|
return *m_value.as_object;
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:17:13 +03:00
|
|
|
JsonArray& as_array()
|
|
|
|
{
|
|
|
|
VERIFY(is_array());
|
|
|
|
return *m_value.as_array;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
JsonArray const& as_array() const
|
2019-06-24 13:03:31 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_array());
|
2019-06-24 13:03:31 +03:00
|
|
|
return *m_value.as_array;
|
2019-06-18 09:55:58 +03:00
|
|
|
}
|
|
|
|
|
2019-07-18 09:18:39 +03:00
|
|
|
double as_double() const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_double());
|
2019-07-18 09:18:39 +03:00
|
|
|
return m_value.as_double;
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:28:07 +03:00
|
|
|
Type type() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
2019-06-19 14:08:07 +03:00
|
|
|
|
|
|
|
bool is_null() const { return m_type == Type::Null; }
|
2019-06-29 13:04:36 +03:00
|
|
|
bool is_bool() const { return m_type == Type::Bool; }
|
2019-06-19 14:08:07 +03:00
|
|
|
bool is_string() const { return m_type == Type::String; }
|
2019-10-29 18:36:50 +03:00
|
|
|
bool is_i32() const { return m_type == Type::Int32; }
|
|
|
|
bool is_u32() const { return m_type == Type::UnsignedInt32; }
|
|
|
|
bool is_i64() const { return m_type == Type::Int64; }
|
|
|
|
bool is_u64() const { return m_type == Type::UnsignedInt64; }
|
2023-11-14 06:10:20 +03:00
|
|
|
bool is_double() const { return m_type == Type::Double; }
|
|
|
|
bool is_array() const { return m_type == Type::Array; }
|
2019-06-19 14:08:07 +03:00
|
|
|
bool is_object() const { return m_type == Type::Object; }
|
2019-06-29 10:04:45 +03:00
|
|
|
bool is_number() const
|
|
|
|
{
|
2019-10-29 18:36:50 +03:00
|
|
|
switch (m_type) {
|
|
|
|
case Type::Int32:
|
|
|
|
case Type::UnsignedInt32:
|
|
|
|
case Type::Int64:
|
|
|
|
case Type::UnsignedInt64:
|
|
|
|
case Type::Double:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-29 10:04:45 +03:00
|
|
|
}
|
2019-06-19 14:08:07 +03:00
|
|
|
|
2019-10-29 18:36:50 +03:00
|
|
|
template<typename T>
|
|
|
|
T to_number(T default_value = 0) const
|
2019-06-19 14:08:07 +03:00
|
|
|
{
|
2019-10-29 18:36:50 +03:00
|
|
|
if (is_double())
|
|
|
|
return (T)as_double();
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return (T)as_i32();
|
|
|
|
if (type() == Type::UnsignedInt32)
|
|
|
|
return (T)as_u32();
|
|
|
|
if (type() == Type::Int64)
|
|
|
|
return (T)as_i64();
|
|
|
|
if (type() == Type::UnsignedInt64)
|
|
|
|
return (T)as_u64();
|
|
|
|
return default_value;
|
2019-06-19 14:08:07 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 19:07:14 +03:00
|
|
|
template<Integral T>
|
|
|
|
bool is_integer() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Int32:
|
|
|
|
return is_within_range<T>(m_value.as_i32);
|
|
|
|
case Type::UnsignedInt32:
|
|
|
|
return is_within_range<T>(m_value.as_u32);
|
|
|
|
case Type::Int64:
|
|
|
|
return is_within_range<T>(m_value.as_i64);
|
|
|
|
case Type::UnsignedInt64:
|
|
|
|
return is_within_range<T>(m_value.as_u64);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral T>
|
|
|
|
T as_integer() const
|
|
|
|
{
|
|
|
|
VERIFY(is_integer<T>());
|
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Int32:
|
|
|
|
return static_cast<T>(m_value.as_i32);
|
|
|
|
case Type::UnsignedInt32:
|
|
|
|
return static_cast<T>(m_value.as_u32);
|
|
|
|
case Type::Int64:
|
|
|
|
return static_cast<T>(m_value.as_i64);
|
|
|
|
case Type::UnsignedInt64:
|
|
|
|
return static_cast<T>(m_value.as_u64);
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool equals(JsonValue const& other) const;
|
2020-03-31 21:44:34 +03:00
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
private:
|
|
|
|
void clear();
|
2022-04-01 20:58:27 +03:00
|
|
|
void copy_from(JsonValue const&);
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2020-06-11 07:40:27 +03:00
|
|
|
Type m_type { Type::Null };
|
2019-06-17 20:47:35 +03:00
|
|
|
|
|
|
|
union {
|
|
|
|
StringImpl* as_string { nullptr };
|
|
|
|
JsonArray* as_array;
|
|
|
|
JsonObject* as_object;
|
|
|
|
double as_double;
|
2019-10-29 18:36:50 +03:00
|
|
|
i32 as_i32;
|
|
|
|
u32 as_u32;
|
|
|
|
i64 as_i64;
|
|
|
|
u64 as_u64;
|
2019-06-17 20:47:35 +03:00
|
|
|
bool as_bool;
|
|
|
|
} m_value;
|
|
|
|
};
|
2019-06-17 22:34:12 +03:00
|
|
|
|
2020-10-13 19:34:27 +03:00
|
|
|
template<>
|
|
|
|
struct Formatter<JsonValue> : Formatter<StringView> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, JsonValue const& value)
|
2020-10-13 19:34:27 +03:00
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
return Formatter<StringView>::format(builder, value.to_byte_string());
|
2020-10-13 19:34:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-17 22:34:12 +03:00
|
|
|
using AK::JsonValue;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|