https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Comic Playz

03/29/2022, 2:00 PM
That is where the serializer is actually being used
🧵 3
nvm got it
Copy code
object ItemEnchantSerializer : KSerializer<ItemEnchant> {
    override val descriptor: SerialDescriptor = ItemEnchant.serializer().descriptor

    override fun serialize(encoder: Encoder, value: ItemEnchant) {
        println("serialize $value");
        val enchantment = Enchantment.getByKey(NamespacedKey.minecraft(value.id.toString().lowercase())) ?: return
        val itemEnchant = ItemEnchant(enchantment, value.level)
        encoder.encodeSerializableValue(ItemEnchant.serializer(), itemEnchant)
    }

    override fun deserialize(decoder: Decoder): ItemEnchant {
        println(decoder.decodeString())
        return decoder.decodeSerializableValue(ItemEnchantSerializer)
    }
}
Copy code
@Serializable
@SerialName("ItemEnchant")
data class ItemEnchant(@Contextual val id: Enchantment, val level: Int)
Copy code
@Serializable
data class LemonItemData(val id: String,
                         val name: String,
                         @EncodeDefault val lore: List<String> = listOf(),
                         @Serializable(with = MaterialSerializer::class) val material: Material,
                         @EncodeDefault @Serializable(with = ItemEnchantSerializer::class) val enchants: ArrayList<ItemEnchant> = arrayListOf(),
                         @EncodeDefault val unbreakable: Boolean = false
                    )
t

Tim Oltjenbruns

03/29/2022, 2:03 PM
Kotlin Bukkit dev here, feel free to reach out over DM. Won’t always be fast but I can eventually respond.
c

Comic Playz

03/29/2022, 2:04 PM
Alright
t

Tim Oltjenbruns

03/29/2022, 2:04 PM
Still having the issue?
c

Comic Playz

03/29/2022, 2:04 PM
Yeah
I'm sure there is an easy fix for this, but I just started w/ Kotlin
t

Tim Oltjenbruns

03/29/2022, 2:05 PM
You’re using an ArrayList, maybe the serializer you are using expect an Array
c

Comic Playz

03/29/2022, 2:06 PM
I'll send the error in DMs
r

Richard Gomez

03/29/2022, 2:08 PM
It always saddens me when people solve things in private. 😞 https://xkcd.com/979/
t

Tim Oltjenbruns

03/29/2022, 2:18 PM
I’ll make sure the solve makes it’s way here 🙂
🙇 1
s

S.

03/29/2022, 2:23 PM
this picture is missing the "fixed :>" message the thread creator posted like half a year later
👍 1
t

Tim Oltjenbruns

03/29/2022, 2:28 PM
The issue is the
println(decoder.decodeString())
in deserialize.
It’s decoding a string before the array or object structures are started
I don’t think
@Serializable(with = ItemEnchantSerializer::class)
is necessary either
Copy code
@Serializable
@SerialName("ItemEnchant")
data class ItemEnchant(@Contextual @Serializable(with = ItemEnchantSerializer::class) id: Enchantment, val level: Int)
It probably needs to move to here
c

Comic Playz

03/29/2022, 3:11 PM
Fixed it!
One quick thing
When decoding the json, the & gets turned into an ?
Maybe it's the println encoding
Once it goes trough the decodeFromString it turns into ?
nvm got it
4 Views