russhwolf / multiplatform-settings

A Kotlin Multiplatform library for saving simple key-value data
Apache License 2.0
1.6k stars 67 forks source link

Add encodeValue and decodeValue using serializer<T>() #172

Closed findusl closed 10 months ago

findusl commented 10 months ago

kotlinx.serialization provides a utility function to determine the serializer of a type. It makes using the encodeValue and decodeValue APIs even easier.

I will add more and cleaner tests (not just foo2), if you are interested in this change. I just thought I check before putting in the work. You might have some reason to avoid this function.

russhwolf commented 10 months ago

Thanks! I wasn't aware that the auto-detecting serializer machinery worked on non-JVM targets (hadn't really looked closely at that in a couple years), so that's the only reason this isn't already in there.

In addition to tests, I'd also like to add KSerializer-free versions of the rest of the APIs. I can help out there if you're uncertain about anything.

(Once that's done, it's past time to split SettingsSerialization.kt into multiple files, but I'll handle that later in a follow-up)

findusl commented 10 months ago

According to this page, it should be part of common. There is some special version for the JVM though, maybe that confused you: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/serializer.html

I added all the functions and duplicated all the basic tests. I also added test cases for the Exception, when a class is not serializable.

And the file is definitely too big now. But I'll leave that up to you.

russhwolf commented 10 months ago

Thanks for this! I've rebased and merged it into a new branch which will make up the 1.2.0 release.