Skip to content

Commit

Permalink
refactor: automatic to_json and from_json function by macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Jul 26, 2023
1 parent ebaca03 commit 60c1bcb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 139 deletions.
23 changes: 2 additions & 21 deletions WeaselIPC/ContextUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,8 @@ namespace weasel {
void from_json(const json& j, std::wstring& w) {
w = string_to_wstring(j.get<std::string>(), CP_UTF8);
}
void to_json(json& j, const TextAttributeType& tat) {
switch (tat) {
case TextAttributeType::NONE:
j = "none";
break;
case TextAttributeType::HIGHLIGHTED:
j = "highlighted";
break;
case TextAttributeType::LAST_TYPE:
j = "last_type";
break;
}
}
void from_json(const json& j, TextAttributeType& tat) {
if (j == "none")
tat = TextAttributeType::NONE;
else if (j == "highlighted")
tat = TextAttributeType::HIGHLIGHTED;
else if(j == "last_type")
tat = TextAttributeType::LAST_TYPE;
}

DEFINE_ENUM_JSON_SERIALIZATION(TextAttributeType)
// auto serialize and deserialize TextRange
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TextRange, start, end, cursor)
// auto serialize and deserialize TextAttribute
Expand Down
97 changes: 5 additions & 92 deletions WeaselIPC/Styler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,11 @@ using namespace weasel;

namespace weasel {

void to_json(json& j, const UIStyle::AntiAliasMode& aam) {
switch(aam) {
case UIStyle::DEFAULT:
j = "default";
break;
case UIStyle::CLEARTYPE:
j = "cleartype";
break;
case UIStyle::GRAYSCALE:
j = "grayscale";
break;
case UIStyle::ALIASED:
j = "aliased";
break;
case UIStyle::FORCE_DWORD:
j = "force_dword";
break;
}
}
void from_json(const json& j, UIStyle::AntiAliasMode& aam) {
if(j == "default") aam = UIStyle::DEFAULT;
else if(j == "cleartype") aam = UIStyle::CLEARTYPE;
else if(j == "grayscale") aam = UIStyle::GRAYSCALE;
else if(j == "aliased") aam = UIStyle::ALIASED;
else if(j == "force_dword") aam = UIStyle::FORCE_DWORD;
}
void to_json(json& j, const UIStyle::LayoutAlignType& lat) {
switch(lat){
case UIStyle::ALIGN_TOP:
j = "top";
break;
case UIStyle::ALIGN_CENTER:
j = "center";
break;
case UIStyle::ALIGN_BOTTOM:
j = "bottom";
break;
}
}
void from_json(const json& j, UIStyle::LayoutAlignType& lat) {
if(j == "top") lat = UIStyle::ALIGN_TOP;
else if(j == "center") lat = UIStyle::ALIGN_CENTER;
else if(j == "bottom") lat = UIStyle::ALIGN_BOTTOM;
}
void to_json(json& j, const UIStyle::LayoutType& lt) {
switch(lt){
case UIStyle::LAYOUT_VERTICAL:
j = "vertical";
break;
case UIStyle::LAYOUT_HORIZONTAL:
j = "horizontal";
break;
case UIStyle::LAYOUT_VERTICAL_TEXT:
j = "vertical_text";
break;
case UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN:
j = "horizontal+fullscreen";
break;
case UIStyle::LAYOUT_VERTICAL_FULLSCREEN:
j = "vertical+fullscreen";
break;
case UIStyle::LAYOUT_TYPE_LAST:
j = "last_type";
break;
}
}
void from_json(const json& j, UIStyle::LayoutType& lt) {
if(j == "vertical") lt = UIStyle::LAYOUT_VERTICAL;
else if(j == "horizontal") lt = UIStyle::LAYOUT_HORIZONTAL;
else if(j == "vertical_text") lt = UIStyle::LAYOUT_VERTICAL_TEXT;
else if(j == "horizontal+fullscreen") lt = UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN;
else if(j == "vertical+fullscreen") lt = UIStyle::LAYOUT_VERTICAL_FULLSCREEN;
else if(j == "last_type") lt = UIStyle::LAYOUT_TYPE_LAST;
}
void to_json(json& j, const UIStyle::PreeditType& pt) {
switch(pt){
case UIStyle::COMPOSITION:
j = "composition";
break;
case UIStyle::PREVIEW:
j = "preview";
break;
case UIStyle::PREVIEW_ALL:
j = "preview_all";
break;
}
}
void from_json(const json& j, UIStyle::PreeditType& pt) {
if(j == "composition") pt = UIStyle::COMPOSITION;
else if(j == "preview") pt = UIStyle::PREVIEW;
else if(j == "preview_all") pt = UIStyle::PREVIEW_ALL;
}
DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::AntiAliasMode)
DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::PreeditType)
DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::LayoutType)
DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::LayoutAlignType)

// auto serialize and deserialize ColorScheme
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ColorScheme, \
text_color, candidate_text_color, candidate_back_color,\
Expand Down
53 changes: 27 additions & 26 deletions include/WeaselCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ using json = nlohmann::json;
namespace weasel
{

#define DEFINE_ENUM_JSON_SERIALIZATION(EnumType) \
void to_json(json& j, const EnumType& value) \
{ \
j = static_cast<int>(value); \
} \
void from_json(const json& j, EnumType& value) \
{ \
value = static_cast<EnumType>(j.get<int>()); \
}
// macro for easily define extern prototype
#define EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TypeName) \
extern void to_json(json& j, const TypeName& var);\
extern void from_json(const json& j, TypeName& var);

enum TextAttributeType
{
NONE = 0,
Expand Down Expand Up @@ -498,31 +512,18 @@ namespace weasel
};

// UIStyle vs json
extern void to_json(json& j, const UIStyle::AntiAliasMode& aam);
extern void from_json(const json& j, UIStyle::AntiAliasMode& aam);
extern void to_json(json& j, const UIStyle::LayoutAlignType& lat);
extern void from_json(const json& j, UIStyle::LayoutAlignType& lat);
extern void to_json(json& j, const UIStyle::LayoutType& lt);
extern void from_json(const json& j, UIStyle::LayoutType& lt);
extern void to_json(json& j, const UIStyle::PreeditType& pt);
extern void from_json(const json& j, UIStyle::PreeditType& pt);
extern void to_json(json& j, const ColorScheme& color);
extern void from_json(const json& j, ColorScheme& color);
extern void to_json(json& j, const UIStyle& style);
extern void from_json(const json& j, UIStyle& style);
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::AntiAliasMode)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::LayoutAlignType)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::LayoutType)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::PreeditType)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(ColorScheme)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle)
// Context vs json
extern void to_json(json& j, const std::wstring& w);
extern void from_json(const json& j, std::wstring& w);
extern void to_json(json& j, const TextAttributeType& tat);
extern void from_json(const json& j, TextAttributeType& tat);
extern void to_json(json& j, const TextAttribute& ta);
extern void from_json(const json& j, TextAttribute& ta);
extern void to_json(json& j, const TextRange& tr);
extern void from_json(const json& j, TextRange& tr);
extern void to_json(json& j, const Text& t);
extern void from_json(const json& j, Text& t);
extern void to_json(json& j, const CandidateInfo& ci);
extern void from_json(const json& j, CandidateInfo& ci);
extern void to_json(json& j, const Context& ctx);
extern void from_json(const json& j, Context& ctx);
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(std::wstring)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TextAttributeType)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TextAttribute)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TextRange)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(Text)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(CandidateInfo)
EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(Context)
}

0 comments on commit 60c1bcb

Please sign in to comment.