使用Gson反序列化JavaFX颜色
我正在尝试用Gson序列化一个使用Gson反序列化JavaFX颜色,java,javafx,gson,Java,Javafx,Gson,我正在尝试用Gson序列化一个Color,并反序列化它。问题是,反序列化类型是com.google.gson.internal.LinkedTreeMap,而不是Color 下面是我创建Gson对象的地方: Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Color.class, new ColorGsonConverter()).create(); 这是我的转换器类: public class Col
Color
,并反序列化它。问题是,反序列化类型是com.google.gson.internal.LinkedTreeMap
,而不是Color
下面是我创建Gson对象的地方:
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Color.class, new ColorGsonConverter()).create();
这是我的转换器类:
public class ColorGsonConverter implements JsonSerializer<Color>, JsonDeserializer<Color> {
@Override
public Color deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
Log.debug("d"); // 'd' is not printed while deserializing
JsonObject colorSave = arg0.getAsJsonObject();
Color color = new Color(colorSave.get("red").getAsDouble(), colorSave.get("green").getAsDouble(), colorSave.get("blue").getAsDouble(), colorSave.get("opacity").getAsDouble());
return color;
}
@Override
public JsonElement serialize(Color arg0, Type arg1, JsonSerializationContext arg2) {
Log.debug("s"); // 's' is printed when serializing
JsonObject colorSave = new JsonObject();
colorSave.addProperty("red", arg0.getRed());
colorSave.addProperty("green", arg0.getGreen());
colorSave.addProperty("blue", arg0.getBlue());
colorSave.addProperty("opacity", arg0.getOpacity());
return arg2.serialize(colorSave);
}
}
String json=gson.toJson(新颜色(1,1,0,0),Color.class);Color c=gson.fromJson(json,Color.class)代码>正在为我工作。你能展示一个复制错误的小程序吗?我一直在做一些挖掘工作,这似乎是因为我有一个MapI正在将我的Color
s存储在一个bean中,而不是Map
它现在可以工作了。
"appearance.derbydisplay.stoplightbordercolor": {
"red": 1.0,
"green": 1.0,
"blue": 0.0,
"opacity": 1.0
}