Hi! Did anyone faced this exception before? ```Unk...
# serialization
a
Hi! Did anyone faced this exception before?
Copy code
UnknownFieldException: Unknown field for index -2
I'm getting this when I try to use Mapper.unmap(serializer, map) call. I made a minimal reproduction code snipet.
Reproduction:
Copy code
import kotlinx.serialization.Mapper
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertTrue

class TestUnknownFieldException {
    @Test
    fun `Unknown field repro`() {
        val input = mapOf(
            "key" to "randomKey",
            "value" to 12.345
        )

        val output = Mapper.unmap(KeyValueData.serializer(), input)

        assertTrue { output.key == "randomKey" && output.value == 12.345 }
    }

    @Serializable
    data class KeyValueData(
        val key: String,
        val value: Double
    )
}
JVM test succeeds, but if I test it on iOS target it fails
r
Mapper isn’t supported on Native, unfortunately.
a
Ohh, my bad... Than I have to search for something else... Thanks
r
What are you trying to do? The reason for the incompatibility is that
TaggedDecoder
which
Mapper
calls into internally returns
READ_ALL
from
decodeElementIndex()
, and that constant isn’t supported on native. But you could manually handle item indexes yourself and get something similar working on native
a
I'm trying to simplify data access to firebase realtime database using Mapper (or CustomMapper in this case). I already extended TaggedDecoder to get things working. Figured out the internals of the generated serializers and added the indexing logic. But I will have to write a bunch of tests to make sure everything is working as expected. Currently I have a sort of working version for the decoder part, but I have to make the same workarounds for the Encoding part, and then cover everything with tests.