使用Jackson Databind序列化对象时出现Java InvalidDefinitionException

时间:2019-05-25 13:11:13

标签: java javafx jackson

我正在尝试使用Jackson的ObjectMapper将以下Player对象编写为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);
}

public void moveUp() {
    saveOld();
    circle.setTranslateY(circle.getTranslateY() - stepSize);
}

public void moveDown() {
    saveOld();
    circle.setTranslateY(circle.getTranslateY() + stepSize);
}

public void undo() {
    circle.setTranslateX(oldCircle.getTranslateX());
    circle.setTranslateY(oldCircle.getTranslateY());
}

private void saveOld() {
    oldCircle.setTranslateX(circle.getTranslateX());
    oldCircle.setTranslateY(circle.getTranslateY());
}

public Snowball shootSnowball(Snowball ball, double mouseX, double mouseY) {

    double polarDirection = Math.atan2(mouseY - circle.getTranslateY(), mouseX - circle.getTranslateX() + 50);
    ball.setState(SnowballState.ALIVE);
    ball.setDirection(polarDirection);
    ball.getCircle().setTranslateX(circle.getTranslateX() + 50);
    ball.getCircle().setTranslateY(circle.getTranslateY());
    return ball;
}

我正在使用以下命令来执行此操作:

 String json = null;
        try {
            json = objectMapper.writeValueAsString(instanceOfPlayerClass);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

不幸的是,我收到以下相关错误消息:

  

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:类型com.sun.javafx.scene.NodeEventDispatcher的无效类型定义:无法为[简单类型,类com.sun.javafx.scene.NodeEventDispatcher]构造BeanSerializer:(java。 lang.reflect.InaccessibleObjectException)无法公开最终的com.sun.javafx.event.BasicEventDispatcher com.sun.javafx.event.BasicEventDispatcher.getPreviousDispatcher()可访问:模块javafx.base不会“导出com.sun.javafx.event “到com.fasterxml.jackson.databind模块(通过参考链:models.communication.Websockets.ConnectionSubmitModel [“ player”]-> models.Game.Player [“ circle”]-> javafx.scene.shape.Circle [“ parent“]-> javafx.scene.layout.GridPane [” parent“]-> javafx.scene.layout.AnchorPane [” eventDispatcher“])

就像错误一样,它与JavaFx没有导出特定的依赖关系有关,但是由于我不受JavaFx的控制,因此我不确定如何解决此问题。

2 个答案:

答案 0 :(得分:1)

您正在尝试存储Circle类,它是一个JavaFX类,实际上不是一个数据类(它是一个UI元素),它具有许多属性(例如半径,厚度,颜色,填充) ,边框等)。因此,它以各种方式与JavaFX系统绑定,并且不会很好地存储。

相反,只需将所需的信息存储在您自己的简单类中,该类具有在您读回时再次创建Circle对象所需的信息。

答案 1 :(得分:1)

通常JacksonPOJO类中效果最好。当您要序列化业务对象时,可能会发生许多意外错误。最好的解决方案可能是创建代表PlayerSnowball状态的新模型类。类似于PlayerStateSnowballState。这两个类应遵循POJO规则:getterssettersno-arg constructor等。当您需要将状态保存到JSON时,可以将业务模型状态模型并序列化状态模型。当您需要反序列化JSON时,您需要将其反序列化为状态模型,然后将其转换为业务模型。对于JavaFX类,如果需要,您需要实现自定义序列化器和反序列化器。他们也不是常规的POJO班,需要特殊对待。

让我们实现两个串行器和一个反序列器:

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));
    }
}

上面的代码显示:

{
  "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}

如您所见,我们可以对Player类进行序列化和反序列化,但是它需要做很多额外的工作。同样,对于执行业务逻辑的每个getter方法,我也将其忽略,如下所示:

@JsonIgnore
public int getHit() {
    removeLife();
    return getLives();
}

另一个提示:getHint方法有副作用。它消除生命-意味着什么。通常这是一种不好的做法,但这个问题与命名无关。