Is there a way to provide some "context" to a dese...
# serialization
m
Is there a way to provide some "context" to a deserializer? I'd like to deserialize relative file paths as
File
p
try a custom serializer: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#custom-serializers you need to instantiate
File
in
deserialize
method
m
Well sure I'll have to use that, there is no default serializer for
File
. But how can I pass the context into that custom serializer
p
oh, sorry, I didn't understand your original question then!
m
no prob
Actually I think I have it
I'll just make the serializer a class, which takes the absolute path as an argument and pass that to the method (using KToml's deserialization from file)
Oh I couldn't because I need a serializer for the type that holds it
p
yeah, and kotlinx.serialization instantiates the serializer class, right?
m
it either automatically takes one or you pass one
but
I have this config class, where the
texture
field is the one I'm trying to solve
image.png
Yeah, that wouldn't work
p
can you make an extra step of serialization? so in the first step you'd deserialize relative paths, and then you'd make the paths absolute using the extra piece of info which is probably some directory path
maybe it's acceptable to make this
name: Component
field contain such relative path "for a minute", before returning a fully deserialized object
m
well I can always just process the paths and map that into a different object or so, but that's what I'm trying to avoid
p
so you'd like to access the default value of some field from the serializer's class?
oh, sorry - you have the absolute path somewhere else
m
yeah
I'm basically processing files in a folder structure
checking if
@Contextual
could do what I'm trying to make
seems good so far
p
please share a snippet once you have it working
👍 1
m
Seems like I'm rather having a problem with KToml now 😄
Time to test it with the same input but in JSON
Yeah, it worked with JSON 😐
Well anyways, first you have the class itself, in my case
ItemConfig
Then the serializer, takes the current location and uses that (serialization is unimplemented as I won't use it but it wouldn't be hard to write
And then comes the deserialization itself, you just add it to the
serializersModule
, as described here
👍 1
cc @akuleshov7
l
Could you show us the stacktrace? Where is the exception thrown?
m
It cannot find a serializer for
File
, sending the stacktrace asap
Copy code
kotlinx.serialization.SerializationException: Serializer for class 'File' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

	at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:90) ~[?:?]
	at kotlinx.serialization.ContextualSerializer.serializer(ContextualSerializer.kt:51) ~[?:?]
	at kotlinx.serialization.ContextualSerializer.deserialize(ContextualSerializer.kt:67) ~[?:?]
	at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:257) ~[?:?]
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16) ~[?:?]
	at com.akuleshov7.ktoml.decoders.TomlAbstractDecoder.decodeSerializableValue(TomlAbstractDecoder.kt:96) ~[?:?]
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43) ~[?:?]
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70) ~[?:?]
	at ResourceCraft.jar/dev.matytyma.resourcecraft.config.ItemConfig$$serializer.deserialize(ItemConfig.kt:11) ~[ResourceCraft.jar:?]
	at ResourceCraft.jar/dev.matytyma.resourcecraft.config.ItemConfig$$serializer.deserialize(ItemConfig.kt:11) ~[ResourceCraft.jar:?]
	at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:257) ~[?:?]
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16) ~[?:?]
	at com.akuleshov7.ktoml.decoders.TomlAbstractDecoder.decodeSerializableValue(TomlAbstractDecoder.kt:96) ~[?:?]
	at com.akuleshov7.ktoml.decoders.TomlMainDecoder$Companion.decode(TomlMainDecoder.kt:293) ~[?:?]
	at com.akuleshov7.ktoml.Toml.decodeFromString(Toml.kt:93) ~[?:?]
	at com.akuleshov7.ktoml.source.TomlSourceReader.decodeFromSource(TomlSourceReader.kt:43) ~[?:?]
	at com.akuleshov7.ktoml.file.TomlFileReader.decodeFromFile(TomlFileReader.kt:41) ~[?:?]
	
=== Unrelated part of stacktrace ===
	
	at ResourceCraft.jar/dev.matytyma.resourcecraft.ResourceCraft.onEnable(ResourceCraft.kt:45) ~[ResourceCraft.jar:?]
	at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:288) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
	at io.papermc.paper.plugin.manager.PaperPluginInstanceManager.enablePlugin(PaperPluginInstanceManager.java:202) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.enablePlugin(PaperPluginManagerImpl.java:109) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:520) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
	at org.bukkit.craftbukkit.CraftServer.enablePlugin(CraftServer.java:654) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:603) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:330) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1230) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:333) ~[purpur-1.21.1.jar:1.21.1-2321-2eaea6a]
	at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]
I guess I'll have to use tomlkt for the project instead...
l
That's OK. I'm the maintainer of tomlkt. :)
😁 2