Store itemstack and recover it

I'm trying to store an ItemStack to a JSON file. I would like to know if it's possible to convert the string itemstack to an itemstack. So what I do in my code is that I store the string itemstack in my json file and try to recover it.

ItemStack pane = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.LIGHT_BLUE.getData());
String itemstack = pane.toString();

1 answer

  • answered 2020-01-18 13:57 albogdano

    You can use the Jackson library to serialize and deserialize objects. Example:

    // serialize pane and save object to file in the JSON format
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.writeValue(new File("target/pane.json"), pane);
    
    // load object from file and deserialize it to ItemStack
    ItemStack pane2 = objectMapper.readValue(new URL("file:src/test/resources/pane.json"), ItemStack.class);