ItemStack to SQL

I’ve been searching around for awhile in attempt to turn an serialize an ItemStack container to an SQL database. All I’ve found is ways of serializing to a json, but I’m still really confused on how I would be able to do this in a database. Is there a way I could break the ItemStack down into properties store those in SQL? I’m just not sure of all the properties. Could someone point me in the right direction?

Thanks.

I’m not sure if there is a better way but what I’ve found that works is using configurate to convert Containers to strings and vice versa, and storing them in a database that way

	ConfigurationNode node = ConfigurateTranslator.instance().translateData(itemStack.toContainer());
	
	StringWriter stringWriter = new StringWriter();
	try {
	    HoconConfigurationLoader.builder().setSink(() -> new BufferedWriter(stringWriter)).build().save(node);
	} catch (IOException e) {
	    e.printStackTrace();
	}

	return stringWriter.toString();

Then back to ItemStack

	ConfigurationNode node = null;
	try {
		node = HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(item))).build().load();
	} catch (IOException e) {
		e.printStackTrace();
	}
	
        ConfigurateTranslator translator = ConfigurateTranslator.instance();
        DataManager manager = Main.getGame().getDataManager();

        DataView dataView = translator.translateFrom(node);

        Optional<ItemStack> deserializedOptional = manager.deserialize(ItemStack.class, dataView);

        if(deserializedOptional.isPresent()) {
            return deserializedOptional.get();
        }

Uh
return node.toString()

ItemStack deserialized = node.getValue(TypeToken.of(ItemStack.class))

Yes, there is a way. Implement your own DataTranslator, and write the translation code by hand. Alternatively, just translate it to a JSON/HOCON and save it as a string.

I think at the time I wrote this, node.getValue(TypeToken.of(ItemStack.class)) wasn’t working. I’ll have to revisit. Thanks.

Reasonably certain they fixed that little quirk,

Probably. this was back in API 3.x