1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:57:35 +00:00

LibGUI: Make it easy to construct a GVariant from a JsonValue.

This commit is contained in:
Andreas Kling 2019-06-29 12:06:26 +02:00
parent 2735ff4687
commit 57a589a6e7
2 changed files with 43 additions and 1 deletions

View file

@ -1,3 +1,4 @@
#include <AK/JsonValue.h>
#include <LibGUI/GVariant.h> #include <LibGUI/GVariant.h>
const char* to_string(GVariant::Type type) const char* to_string(GVariant::Type type)
@ -77,6 +78,42 @@ GVariant::GVariant(const String& value)
AK::ref_if_not_null(m_value.as_string); AK::ref_if_not_null(m_value.as_string);
} }
GVariant::GVariant(const JsonValue& value)
{
if (value.is_null()) {
m_value.as_string = nullptr;
return;
}
if (value.is_int()) {
m_type = Type::Int;
m_value.as_int = value.as_int();
return;
}
if (value.is_uint()) {
ASSERT(value.as_uint() < INT32_MAX);
m_type = Type::Int;
m_value.as_int = value.as_uint();
return;
}
if (value.is_string()) {
m_type = Type::String;
m_value.as_string = value.as_string().impl();
m_value.as_string->ref();
return;
}
if (value.is_bool()) {
m_type = Type::Bool;
m_value.as_bool = value.as_bool();
return;
}
ASSERT_NOT_REACHED();
}
GVariant::GVariant(const GraphicsBitmap& value) GVariant::GVariant(const GraphicsBitmap& value)
: m_type(Type::Bitmap) : m_type(Type::Bitmap)
{ {
@ -209,7 +246,7 @@ bool GVariant::operator==(const GVariant& other) const
case Type::Rect: case Type::Rect:
return as_rect() == other.as_rect(); return as_rect() == other.as_rect();
case Type::Invalid: case Type::Invalid:
break; return true;
} }
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }

View file

@ -4,6 +4,10 @@
#include <LibGUI/GIcon.h> #include <LibGUI/GIcon.h>
#include <SharedGraphics/GraphicsBitmap.h> #include <SharedGraphics/GraphicsBitmap.h>
namespace AK {
class JsonValue;
}
class GVariant { class GVariant {
public: public:
GVariant(); GVariant();
@ -17,6 +21,7 @@ public:
GVariant(const Point&); GVariant(const Point&);
GVariant(const Size&); GVariant(const Size&);
GVariant(const Rect&); GVariant(const Rect&);
GVariant(const AK::JsonValue&);
GVariant(Color); GVariant(Color);
GVariant(const GVariant&); GVariant(const GVariant&);