Kotlinx.serialization: `serializer().list` does not compile

Created on 5 May 2018  路  3Comments  路  Source: Kotlin/kotlinx.serialization

Since 0.5.0, the list property of serializers returned from a @Serializable does not compile.

package reproducer

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JSON
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

@Serializable
data class Descriptor(
        var name: String = "",
        var description: String = ""
)


class DescriptorTests {
    companion object {
        const val singleInstanceString = "{\"name\":\"Test Descriptor\",\"description\":\"Just a test\"}"
    }

    @Test
    fun `serialize single instance`() {

        val instance = Descriptor(
                "Test Descriptor",
                "Just a test"
        )

        val serialized = JSON.stringify(Descriptor.serializer(), instance)

        // passes
        assertEquals(singleInstanceString, serialized)
    }

    @Test
    fun `deserialize single instance`() {
        val deserialized = JSON.parse(Descriptor.serializer(),
                singleInstanceString) as Descriptor

        // all pass
        assertEquals("Test Descriptor", deserialized.name)
        assertEquals("Just a test", deserialized.description)
    }


    @Test
    fun `serialize list`() {

        val instance = Descriptor(
                "Test Descriptor",
                "Just a test"
        )
        val instanceList = listOf(instance)

        // does not compile (Unresolved reference: list)
        val serialized = JSON.stringify(Descriptor.serializer().list,
                instance)

        assertEquals(singleInstanceString, serialized.first())
    }

    @Test
    fun `deserialize list`() {
        // does not compile (Unresolved reference: list)
        val projectList: Iterable<Descriptor> = JSON.parse(Descriptor.serializer().list,
                "[$singleInstanceString]")
        val deserialized = projectList.first()
        assertEquals("Test Descriptor", deserialized.name)
        assertEquals("Just a test", deserialized.description)
    }

}

Most helpful comment

Sorry for bumping this absurdly old issue, but I just came across this issue when updating from the prerelease version of kotlinx-serialization to the release version, and I wanted to leave a note for anyone else who might be chasing the same problem. Because of the refactoring that took place, when using version 1.0.0 or greater, the example of list serialization from the comment above would instead be modified like so:

- import kotlinx.serialization.list
+ import kotlinx.serialization.builtins.ListSerializer

...

- val serialized = JSON.stringify(ListSerializer(Descriptor.serializer()),
+ val serialized = JSON.stringify(Descriptor.serializer().list,
                instance)

Hopefully that saves someone having the same problem a little bit of time 馃槉

All 3 comments

In the serialize list test you use instance instead of instanceList.

~kt
val serialized = JSON.stringify(Descriptor.serializer().list,
instance)
~

And you need to import the list extension property:

~kt
import kotlinx.serialization.list
~

@hastebrot thank you.

I did get the tests to pass after importing kotlinx.serialization.list and a couple other corrections.

IDEA wasn't helping with any of this (a) without the serialization plugin being installed or (b) with any older version of the plugin.

Without the correct plugin, IDEA can't resolve Descriptor.serializer(), so it does not suggest that import. Then when you do add the import, it marks the import as unused and removes it if you ever run "optimize imports".

Thanks for the help

Sorry for bumping this absurdly old issue, but I just came across this issue when updating from the prerelease version of kotlinx-serialization to the release version, and I wanted to leave a note for anyone else who might be chasing the same problem. Because of the refactoring that took place, when using version 1.0.0 or greater, the example of list serialization from the comment above would instead be modified like so:

- import kotlinx.serialization.list
+ import kotlinx.serialization.builtins.ListSerializer

...

- val serialized = JSON.stringify(ListSerializer(Descriptor.serializer()),
+ val serialized = JSON.stringify(Descriptor.serializer().list,
                instance)

Hopefully that saves someone having the same problem a little bit of time 馃槉

Was this page helpful?
0 / 5 - 0 ratings