arrow icon indicating copy to clipboard operation
arrow copied to clipboard

["Request"] Kotlin serialization support?

Open dotCipher opened this issue 4 years ago • 5 comments

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.

dotCipher avatar Dec 04 '19 18:12 dotCipher

I'd cross-post this to https://github.com/47deg/helios too!

pakoito avatar Dec 04 '19 21:12 pakoito

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.

dotCipher avatar Dec 04 '19 22:12 dotCipher

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

1Jajen1 avatar Dec 04 '19 22:12 1Jajen1

^^^^

That's my concern, and why we encourage the typeclass approach.

pakoito avatar Dec 04 '19 22:12 pakoito

+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

dotCipher avatar Dec 05 '19 14:12 dotCipher

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.

andre-krueger avatar Sep 29 '22 21:09 andre-krueger

@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.

nomisRev avatar Sep 30 '22 08:09 nomisRev

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())
  }
}

freynder avatar Mar 19 '23 10:03 freynder