From a1db1e666459fde2aa2fd8e267545ca78e89faba Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 22 May 2020 13:57:23 +0200 Subject: [PATCH] AK: Make JsonValue and JsonObjectSerializer speak int/long/long long While width-oriented integer types are nicer from the programmer's perspective, we have to accept that C++ thinks in int/long/long long. --- AK/JsonObjectSerializer.h | 20 ++++++++++++++++---- AK/JsonValue.cpp | 28 ++++++++++++++++++++++++---- AK/JsonValue.h | 10 ++++++---- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/AK/JsonObjectSerializer.h b/AK/JsonObjectSerializer.h index 8de17cead6..9cffa30391 100644 --- a/AK/JsonObjectSerializer.h +++ b/AK/JsonObjectSerializer.h @@ -85,25 +85,37 @@ public: m_builder.append(value ? "true" : "false"); } - void add(const StringView& key, i32 value) + void add(const StringView& key, int value) { begin_item(key); m_builder.appendf("%d", value); } - void add(const StringView& key, u32 value) + void add(const StringView& key, unsigned value) { begin_item(key); m_builder.appendf("%u", value); } - void add(const StringView& key, i64 value) + void add(const StringView& key, long value) + { + begin_item(key); + m_builder.appendf("%ld", value); + } + + void add(const StringView& key, long unsigned value) + { + begin_item(key); + m_builder.appendf("%lu", value); + } + + void add(const StringView& key, long long value) { begin_item(key); m_builder.appendf("%lld", value); } - void add(const StringView& key, u64 value) + void add(const StringView& key, long long unsigned value) { begin_item(key); m_builder.appendf("%llu", value); diff --git a/AK/JsonValue.cpp b/AK/JsonValue.cpp index 7bf3ccb542..76e65f3f15 100644 --- a/AK/JsonValue.cpp +++ b/AK/JsonValue.cpp @@ -129,27 +129,47 @@ bool JsonValue::equals(const JsonValue& other) const return false; } -JsonValue::JsonValue(i32 value) +JsonValue::JsonValue(int value) : m_type(Type::Int32) { m_value.as_i32 = value; } -JsonValue::JsonValue(u32 value) +JsonValue::JsonValue(unsigned value) : m_type(Type::UnsignedInt32) { m_value.as_u32 = value; } -JsonValue::JsonValue(i64 value) +JsonValue::JsonValue(long value) + : m_type(sizeof(long) == 8 ? Type::Int64 : Type::Int32) +{ + if constexpr (sizeof(long) == 8) + m_value.as_i64 = value; + else + m_value.as_i32 = value; +} + +JsonValue::JsonValue(unsigned long value) + : m_type(sizeof(long) == 8 ? Type::UnsignedInt64 : Type::UnsignedInt32) +{ + if constexpr (sizeof(long) == 8) + m_value.as_u64 = value; + else + m_value.as_u32 = value; +} + +JsonValue::JsonValue(long long value) : m_type(Type::Int64) { + static_assert(sizeof(long long unsigned) == 8); m_value.as_i64 = value; } -JsonValue::JsonValue(u64 value) +JsonValue::JsonValue(long long unsigned value) : m_type(Type::UnsignedInt64) { + static_assert(sizeof(long long unsigned) == 8); m_value.as_u64 = value; } diff --git a/AK/JsonValue.h b/AK/JsonValue.h index ca596ce912..5ff6b4be99 100644 --- a/AK/JsonValue.h +++ b/AK/JsonValue.h @@ -63,10 +63,12 @@ public: JsonValue& operator=(const JsonValue&); JsonValue& operator=(JsonValue&&); - JsonValue(i32); - JsonValue(u32); - JsonValue(i64); - JsonValue(u64); + JsonValue(int); + JsonValue(unsigned); + JsonValue(long); + JsonValue(long unsigned); + JsonValue(long long); + JsonValue(long long unsigned); #if !defined(KERNEL) JsonValue(double);