通过Jackson对Either进行反序列化

时间:2018-11-02 13:21:52

标签: kotlin jackson

我尝试通过使用Arrow(https://arrow-kt.io/docs/datatypes/either/)中的实现或使用自己的实现在项目中实现Either。我当前面临的问题是通过Jackson对该课程进行反序列化。我尝试将注释添加到我自己的(不起作用的)代码中,如下所示:

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

否则,我也很愿意在Arrow Arrow库中使用mixin,但我想我会遇到类似的问题。

在此先感谢您的帮助!

0 个答案:

没有答案