2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/Forward.h>
|
2019-07-08 14:03:23 +03:00
|
|
|
#include <AK/IPv4Address.h>
|
|
|
|
#include <AK/Optional.h>
|
2019-10-29 18:36:50 +03:00
|
|
|
#include <AK/String.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,
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-06-17 20:47:35 +03:00
|
|
|
Double,
|
2019-06-29 10:04:45 +03:00
|
|
|
#endif
|
2019-06-17 20:47:35 +03:00
|
|
|
Bool,
|
|
|
|
String,
|
|
|
|
Array,
|
|
|
|
Object,
|
|
|
|
};
|
|
|
|
|
2020-06-11 07:40:27 +03:00
|
|
|
static Optional<JsonValue> from_string(const StringView&);
|
2019-06-24 12:25:10 +03:00
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
explicit JsonValue(Type = Type::Null);
|
|
|
|
~JsonValue() { clear(); }
|
|
|
|
|
|
|
|
JsonValue(const JsonValue&);
|
|
|
|
JsonValue(JsonValue&&);
|
|
|
|
|
|
|
|
JsonValue& operator=(const JsonValue&);
|
|
|
|
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
|
|
|
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue(double);
|
2019-06-29 10:04:45 +03:00
|
|
|
#endif
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue(bool);
|
2019-06-18 10:11:31 +03:00
|
|
|
JsonValue(const char*);
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue(const String&);
|
2019-07-08 14:03:23 +03:00
|
|
|
JsonValue(const IPv4Address&);
|
2019-06-17 20:47:35 +03:00
|
|
|
JsonValue(const JsonArray&);
|
|
|
|
JsonValue(const JsonObject&);
|
|
|
|
|
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
|
|
|
|
2019-08-07 23:03:25 +03:00
|
|
|
String as_string_or(const String& alternative)
|
|
|
|
{
|
|
|
|
if (is_string())
|
|
|
|
return as_string();
|
|
|
|
return alternative;
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:28:07 +03:00
|
|
|
String to_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
|
|
|
}
|
|
|
|
|
2019-07-08 14:03:23 +03:00
|
|
|
Optional<IPv4Address> to_ipv4_address() const
|
|
|
|
{
|
|
|
|
if (!is_string())
|
|
|
|
return {};
|
|
|
|
return IPv4Address::from_string(as_string());
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:50 +03:00
|
|
|
int to_int(int default_value = 0) const { return to_i32(default_value); }
|
|
|
|
i32 to_i32(i32 default_value = 0) const { return to_number<i32>(default_value); }
|
|
|
|
|
|
|
|
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); }
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int as_bool() const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_bool());
|
2019-06-29 13:04:36 +03:00
|
|
|
return m_value.as_bool;
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:55:58 +03:00
|
|
|
String as_string() const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_string());
|
2019-06-24 13:03:31 +03:00
|
|
|
return *m_value.as_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonObject& as_object() const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_object());
|
2019-06-24 13:03:31 +03:00
|
|
|
return *m_value.as_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonArray& as_array() const
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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; }
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-08-07 22:28:07 +03:00
|
|
|
bool is_double() const
|
|
|
|
{
|
|
|
|
return m_type == Type::Double;
|
|
|
|
}
|
2019-06-29 10:04:45 +03:00
|
|
|
#endif
|
2019-08-07 22:28:07 +03:00
|
|
|
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:
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-10-29 18:36:50 +03:00
|
|
|
case Type::Double:
|
2019-06-29 10:04:45 +03:00
|
|
|
#endif
|
2019-10-29 18:36:50 +03:00
|
|
|
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
|
|
|
{
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-10-29 18:36:50 +03:00
|
|
|
if (is_double())
|
|
|
|
return (T)as_double();
|
2019-06-29 10:04:45 +03:00
|
|
|
#endif
|
2019-10-29 18:36:50 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-31 21:44:34 +03:00
|
|
|
bool equals(const JsonValue& other) const;
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
private:
|
|
|
|
void clear();
|
|
|
|
void copy_from(const JsonValue&);
|
|
|
|
|
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;
|
2020-05-16 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-06-17 20:47:35 +03:00
|
|
|
double as_double;
|
2019-06-29 10:04:45 +03:00
|
|
|
#endif
|
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> {
|
2020-12-30 14:14:15 +03:00
|
|
|
void format(FormatBuilder& builder, const JsonValue& value)
|
2020-10-13 19:34:27 +03:00
|
|
|
{
|
2020-12-30 14:14:15 +03:00
|
|
|
Formatter<StringView>::format(builder, value.to_string());
|
2020-10-13 19:34:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonValue;
|