De-/Serialization of Either through Jackson

2019-06-02 01:11发布

问题:

I try to implement Either in my project either by using the implementation from Arrow (https://arrow-kt.io/docs/datatypes/either/) or by using my own. The problem I'm currently facing is de-/serialization of this class through Jackson. I tried adding annotations to my own the (not working) code is the following:

@JsonTypeInfo(use= JsonTypeInfo.Id.CLASS, include= JsonTypeInfo.As.PROPERTY, property="class")
@CordaSerializable
sealed class Either<out L, out R> {

    data class Left<out L>(val a: L) : Either<L, Nothing>()
    data class Right<out R>(val b: R) : Either<Nothing, R>()

    val isLeft: Boolean get() = this is Left<L>
    val isRight: Boolean get() = this is Right<R>

    @JsonValue
    fun <C> fold(ifLeft: (L) -> C, ifRight: (R) -> C): C {
        when (this) {
            is Left -> return ifLeft(a)
            is Right -> return ifRight(b)
        }
    }
}

Otherwise I would also be willing to use a mixin with the Arrow lib, but I think I will run into similiar issues.

Thanks in advance for any help!