Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.41k stars 620 forks source link

Properties serializer does not handle map null value correctly. #1623

Open devsvc opened 3 years ago

devsvc commented 3 years ago

Describe the bug Properties serializer does not handle map null value correctly. Properties.encodeToMap serialized the key without value. So the map it encoded could not be decoded.

To Reproduce

class TestSer {

    @Serializable
    data class Data(
        val m: Map<String, String?> = mapOf()
    )

    @Test
    fun test() {
        val data = Data(mapOf(
            "a" to "1",
            "b" to null
        ))

        val map = Properties.encodeToMap(data)
        println(map)
        val data1: Data = Properties.decodeFromMap(map)
        println(data1)
    }

}

{m.0=a, m.1=1, m.2=b}

Expected behavior map should be {m.0=a, m.1=1, m.2=b, m.3=null}

if it produce {m.a=1, m.b=null} will be much better

Environment

sandwwraith commented 3 years ago

It can't return map with nulls because encodeToMap has return type Map<String, Any>. I believe this was done according to Java's Properties behavior

sandwwraith commented 3 years ago

But it should be indeed fixed: dangling key shouldn't be present in map

mkotsbak commented 2 years ago

My feature request https://github.com/Kotlin/kotlinx.serialization/issues/1823 would probably solve this case.

Sage-Kreiter commented 1 year ago

Any update on this?

sandwwraith commented 1 year ago

Not as far as I know