第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 jackson 數(shù)據(jù)綁定序列化對(duì)象時(shí)出現(xiàn) Java InvalidDefinition

使用 jackson 數(shù)據(jù)綁定序列化對(duì)象時(shí)出現(xiàn) Java InvalidDefinition

慕田峪7331174 2022-12-15 11:22:23
我正在嘗試使用 Jackson 的 ObjectMapper 將以下 Player 對(duì)象編寫(xiě)為 String。package models.Game;import models.Game.Enums.SnowballState;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import java.util.ArrayList;import java.util.List;public class Player {    private Circle circle;    private String name;    private Color color;    private int points = 0;public int getLives() {    return lives;}private int lives = 3;private List<Snowball> snowballs;private Circle oldCircle;private int stepSize = 10;public Player(String name, Color color) {    this.name = name;    circle = new Circle();    oldCircle = new Circle();    this.color = color;    snowballs = new ArrayList<>();    snowballs.add(new Snowball(this));    snowballs.add(new Snowball(this));    snowballs.add(new Snowball(this));}public Player() {}private void removeLife() {    this.lives--;}public int getHit() {    removeLife();    return getLives();}public int shotSuccess() {    points+= 50;    return points;}public int getSnowballAmount() {    int balls = 0;    for (Snowball ball : snowballs) {        if (ball.getState() == SnowballState.CREATED) {            balls++;        }    }    return balls;}public List<Snowball> getSnowballs() {    return snowballs;}public Snowball getNextSnowball() {    for (Snowball ball : snowballs) {        if (ball.getState() == SnowballState.CREATED) {            return ball;        }    }    return null;}public void createSnowball() {    if (getSnowballAmount() < 3) {        snowballs.add(new Snowball(this));    }}public Color getColor() {    return this.color;}public Circle getCircle() {    return this.circle;}public void moveLeft() {    saveOld();    circle.setTranslateX(circle.getTranslateX() - stepSize);}public void moveRight() {    saveOld();    circle.setTranslateX(circle.getTranslateX() + stepSize);}
查看完整描述

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ì)象所需的信息。


查看完整回答
反對(duì) 回復(fù) 2022-12-15
?
繁星淼淼

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)。


查看完整回答
反對(duì) 回復(fù) 2022-12-15
  • 2 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)