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

Incorrect deserialization with the new okio stream methods

Open aftabahmadTW opened this issue 3 years ago • 1 comments

Describe the bug While trying out the new okio streams with kotlinx-serialization I noticed an issue with the result being different. I've attached a small project showing the serialization with okio stream methods and another with decodeFromString.

The encodeToBufferedSink works properly and we can see the correct value in the file persisted. The problem is with decoding a unicode emoji for example this one 🌴. Using the method Json.decodeFromBufferedSource it doesn't deserialize to the correct unicode value but if we read the file manually and then use decodeFromString it gives the correct value.

I assumed that since both methods encodeToBufferedSink and decodeFromBufferedSource use UTF-8 encoding the correct value would be deserialized. I'm wondering if I'm missing some encoding setting with using the okio streams or if it's something else.

To Reproduce I've attached a small working project showing the difference between the two serialization methods. MyApplication.zip

Screenshot Screenshot 2022-09-06 at 4 11 41 pm

Expected behavior I thought the two methods decodeFromBufferedSource and decodeFromString would deserialise the same content regardless of the implementation. The call to decodeFromString we read from the file using okio FileSystem fileSystem.read(path) { readUtf8() }.

Environment

  • Kotlin version: 1.7.10
  • Library version: 1.4.0
  • Kotlin platforms: Android
  • Gradle version: 7.3.3
  • IDE version IntelliJ IDEA 2022.2.1

aftabahmadTW avatar Sep 06 '22 15:09 aftabahmadTW

Thanks for the reproducer! We'll look into that

sandwwraith avatar Sep 06 '22 16:09 sandwwraith