kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Properties.encodeToStringMap when a map passed to it return all keys and values with index

Open mobinyardim opened this issue 1 year ago • 1 comments

I ran the code below and couldn't figure out why the output is behaving this way. The Code:

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.properties.Properties
import kotlinx.serialization.properties.encodeToStringMap


@OptIn(ExperimentalSerializationApi::class)
fun main(): Unit {
    val map = Properties.encodeToStringMap(mapOf("key" to "value"))
    println(map)
}

Output: {0=key, 1=value} My expected output: {key=value}

I encountered this behavior when I have a Box class with a field named 'content' of generic type. When I specify the type of 'content' as Map and attempt to encode the Box class instance to a map, it behaves as I described above. I believe this behavior is inappropriate.

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.properties.Properties
import kotlinx.serialization.properties.encodeToStringMap

@Serializable
class Box<out T>(
    val content: T
)

@OptIn(ExperimentalSerializationApi::class)
fun main(): Unit {
    val boxOfMap = Box(mapOf("key" to "value"))
    println(Properties.encodeToStringMap(boxOfMap))
}

output: {content.0=key, content.1=value}

what I expect: {content.key=value}

mobinyardim avatar Apr 18 '24 12:04 mobinyardim

Ran into the same problem. Did you come up with a good workaround?

Sage-Kreiter avatar Oct 10 '24 18:10 Sage-Kreiter