I have an Item interface that is used to create and store game items. These items are stored in the player’s inventory in the form of a matrix (IItem[][] inventory). When trying to implement the read and write functions (translating an object into a string, and converting it), I got an error. If I create an inventory as an array of these objects, then the JSON is confused in the data types (the error code is below), I tried to create the same matrix, only from 1 specific object, without using the interface, everything works, but this is not what I need . I can conclude that the problem is in the annotations.
Error code:
Exception in thread “main” com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id ‘IItem;’ as a subtype of items.IItem[]
: known type ids = [Diamond, Grass, Sword] (for POJO property ‘inventory’)
`import items.Grass;
import items.IItem;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Player player = new Player(new IItem[][]{{new Grass(5, 2)}},12,20);
player.print();
System.out.println();
String data = player.write();
System.out.println(data);
System.out.println();
Player player2 = Player.read(data);
player2.print();
}
}`
I can’t post all the code because the forum constantly complains about the formatting of the code
I tried to make annotations, but they only help in the case of 1 object, and with
a regular matrix of objects (IItem[] inventory).