使用 jackson 数据绑定(bind)序列化对象时出现 Java InvalidDefinitionException

标签 java javafx jackson

我正在尝试使用 Jackson 的 ObjectMapper 将以下 Player 对象编写为字符串。

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: Invalid type definition for type com.sun.javafx.scene.NodeEventDispatcher: Failed to construct BeanSerializer for [simple type, class com.sun.javafx.scene.NodeEventDispatcher]: (java.lang.reflect.InaccessibleObjectException) Unable to make public final com.sun.javafx.event.BasicEventDispatcher com.sun.javafx.event.BasicEventDispatcher.getPreviousDispatcher() accessible: module javafx.base does not "exports com.sun.javafx.event" to module com.fasterxml.jackson.databind (through reference chain: 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,所以我不确定如何解决此问题。

最佳答案

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

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

关于使用 jackson 数据绑定(bind)序列化对象时出现 Java InvalidDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305131/

相关文章:

java - 如何表示 "unset"Java 成员,以便可以使用 Jackson 将它们序列化为未设置的 JSON

java - 在不使用 @JsonTypeInfo 的情况下使用 Jackson 反序列化为多态集合

java - 仅使用合理的键在 JTable 单元格中开始编辑

java - 如何使用依赖对象中字段的生成值?

java - 场景生成器 javafx 找不到我的 Controller 类?

java - 使用JavaFX 8观看YouTube直播

java - 将毫秒时间戳反序列化为 java.time.Instant

java - 映射过程数据在hadoop中可能为空

java - 无法抓取标题

JavaFX:舞台的 minHeight 考虑标题栏的高度