Java InvalidDefinitionException when serializing object with jackson databind

Kevin Tinnemans :

I'm trying to write the following Player object as String using ObjectMapper from Jackson.

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

I'm using the following command to do this:

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

unfortunately I'm getting the following relevant error message:

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"])

Like the error says it has something to do with JavaFx not exporting a certain dependency, but since I'm not in control of JavaFx I'm not really sure about how to fix this.

john16384 :

You're trying to store the Circle class, which is a JavaFX class, that is not really a data class (it is a UI element), with many properties (like radius, thickness, color, fills, borders etc.). As such it is tied in with the JavaFX system in various ways and will not store well.

Instead, just store the information you want in a simple class of your own, which has the information you need to create the Circle object again when you read it back.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=68702&siteId=1