arrow
arrow copied to clipboard
["Request"] Kotlin serialization support?
What version are you currently using?
0.10.3
What would you like to see?
Since Arrow is a kotlin specific library, it would be nice to have native kotlin serialization support in a separate module or otherwise.
This is semi-related to #1192, but with targeting kotlinx.serialization as the serialization implementation.
I'd cross-post this to https://github.com/47deg/helios too!
Hmm I think my ask is slightly different from what helios is accomplishing. If I am understanding it correctly, Helios is a JSON parser using Arrow right?
However I would be asking to essentially have certain Arrow specific types be @Serializable
via kotlinx.serialization
. For example, having a serializer for Option<T>
.
I feel like that's more in the realm of Arrow proper, not necessarily Helios, but I could be wrong based on my glance of the docs.
Can this be added without adding kotlinx.serialization as a dep to arrow-core? Because that'd be quite annoying, especially when looking at even more serialization libraries
^^^^
That's my concern, and why we encourage the typeclass approach.
+1 and totally agree on not adding deps to serialization libs, that was never my intent or ask.
It can if we were to treat the arrow-core
datatypes as an 'external library' and just write serializers for them in a new module / repo / whatever using the external serializers pattern kotlinx.serialization references
I could get it to work for at least Option
(this code needs to be in a module without kapt, for reference, see this: https://github.com/Kotlin/kotlinx.serialization/issues/1562)
@Serializable
data class Data(
@Contextual
val someData: Option<String> = None,
)
Without providing None
it doesn't work, though.
@dotCipher @andre-krueger we're absolutely open to such contributions! This is currently not high on our own priority list however.
If someone prefers to build it outside of this repo, we'd also be happy to promote community libraries on the website and reference from the Arrow Core documentation. We're actually trying to improve the website to promote all Arrow related libraries out there, since we don't want to continue pushing new things in this repo. Like SuspendApp.
I could get it to work for at least
Option
(this code needs to be in a module without kapt, for reference, see this: Kotlin/kotlinx.serialization#1562)@Serializable data class Data( @Contextual val someData: Option<String> = None, )
Without providing
None
it doesn't work, though.
Currently testing with this, may be helpful as a start:
typealias OptionAsNullable<T> = @Serializable(ArrowOptionAsNullableSerializer::class) Option<T>
class ArrowOptionAsNullableSerializer<T>(private val dataSerializer: KSerializer<T?>) :
KSerializer<Option<T>> {
override val descriptor: SerialDescriptor = dataSerializer.descriptor
override fun deserialize(decoder: Decoder): Option<T> =
dataSerializer.deserialize(decoder).toOption()
override fun serialize(encoder: Encoder, value: Option<T>) {
dataSerializer.serialize(encoder, value.getOrNull())
}
}