2 回答

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
您正在嘗試存儲(chǔ)Circle
類,它是一個(gè) JavaFX 類,它實(shí)際上不是一個(gè)數(shù)據(jù)類(它是一個(gè) UI 元素),具有許多屬性(如半徑、厚度、顏色、填充、邊框等)。因此,它以各種方式與 JavaFX 系統(tǒng)捆綁在一起,并且不會(huì)很好地存儲(chǔ)。
相反,只需將您想要的信息存儲(chǔ)在您自己的一個(gè)簡(jiǎn)單類中,Circle
當(dāng)您讀回它時(shí),它具有您再次創(chuàng)建對(duì)象所需的信息。

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
通常在課堂Jackson上效果最好。POJO當(dāng)您想要序列化業(yè)務(wù)對(duì)象時(shí),可能會(huì)發(fā)生許多意外錯(cuò)誤??赡茏詈玫慕鉀Q方案是創(chuàng)建代表和狀態(tài)的新模型類。和之類的東西。這兩個(gè)類應(yīng)該遵循規(guī)則:, , , 等。當(dāng)你需要保存狀態(tài)時(shí),你可以將你的業(yè)務(wù)模型轉(zhuǎn)換為狀態(tài)模型并序列化狀態(tài)模型。當(dāng)您需要反序列化時(shí),您需要將其反序列化為狀態(tài)模型,然后再將其轉(zhuǎn)換為業(yè)務(wù)模型。為了PlayerSnowballPlayerStateSnowballStatePOJOgetterssettersno-arg constructorJSONJSONJavaFX如果需要,您需要實(shí)現(xiàn)自定義序列化器和反序列化器的類。他們也不是正規(guī)POJO班級(jí),需要特殊對(duì)待。
讓我們實(shí)現(xiàn)兩個(gè)序列化器和一個(gè)反序列化器:
class CircleJsonSerializer extends JsonSerializer<Circle> {
@Override
public void serialize(Circle value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("radius", value.getRadius());
gen.writeNumberField("centerX", value.getCenterX());
gen.writeNumberField("centerY", value.getCenterY());
gen.writeEndObject();
}
}
class CircleJsonDeserializer extends JsonDeserializer<Circle> {
@Override
public Circle deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
TreeNode node = p.readValueAsTree();
NumericNode radius = (NumericNode) node.get("radius");
NumericNode centerX = (NumericNode) node.get("centerX");
NumericNode centerY = (NumericNode) node.get("centerY");
return new Circle(centerX.doubleValue(), centerY.doubleValue(), radius.doubleValue());
}
}
class ColorJsonDeserializer extends JsonDeserializer<Color> {
@Override
public Color deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
TreeNode node = p.readValueAsTree();
NumericNode red = (NumericNode) node.get("red");
NumericNode green = (NumericNode) node.get("green");
NumericNode blue = (NumericNode) node.get("blue");
NumericNode opacity = (NumericNode) node.get("opacity");
return Color.color(red.doubleValue(), green.doubleValue(), blue.doubleValue(), opacity.doubleValue());
}
}
您可以按如下方式使用它們:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.NumericNode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
Player player = new Player("N1", Color.BLUE);
SimpleModule javafxModule = new SimpleModule();
javafxModule.addSerializer(Circle.class, new CircleJsonSerializer());
javafxModule.addDeserializer(Circle.class, new CircleJsonDeserializer());
javafxModule.addDeserializer(Color.class, new ColorJsonDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(javafxModule);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String json = mapper.writeValueAsString(player);
System.out.println(json);
System.out.println(mapper.readValue(json, Player.class));
}
}
上面的代碼打?。?/p>
{
"circle" : {
"radius" : 1.0,
"centerX" : 0.0,
"centerY" : 0.0
},
"color" : {
"red" : 0.0,
"green" : 0.0,
"blue" : 1.0,
"opacity" : 1.0,
"opaque" : true,
"hue" : 240.0,
"saturation" : 1.0,
"brightness" : 1.0
},
"lives" : 3,
"snowballs" : [ {
"state" : "CREATED",
"direction" : 0.0,
"circle" : null
}, {
"state" : "CREATED",
"direction" : 0.0,
"circle" : null
}, {
"state" : "CREATED",
"direction" : 0.0,
"circle" : null
} ]
}
//ToString
Player{circle=Circle[centerX=0.0, centerY=0.0, radius=1.0, fill=0x000000ff], name='null', color=0x0000ffff, points=0, lives=3, snowballs=[Snowball{player=null, state=CREATED, direction=0.0, circle=null}, Snowball{player=null, state=CREATED, direction=0.0, circle=null}, Snowball{player=null, state=CREATED, direction=0.0, circle=null}], oldCircle=null, stepSize=10}
如您所見(jiàn),我們可以序列化和反序列化Player類,但它需要做很多額外的工作。同樣對(duì)于每個(gè)getter執(zhí)行業(yè)務(wù)邏輯的方法,我都忽略了它們,如下所示:
@JsonIgnore
public int getHit() {
removeLife();
return getLives();
}
另一個(gè)提示:getHint方法有副作用。它消除了生命——不管它意味著什么。這通常是一種不好的做法,但這個(gè)問(wèn)題與命名無(wú)關(guān)。
添加回答
舉報(bào)