首页 > 解决方案 > 通过 Jackson 对 Either 进行反序列化

问题描述

我尝试通过使用 Arrow ( https://arrow-kt.io/docs/datatypes/either/ ) 中的实现或使用我自己的实现Either在我的项目中实现。我目前面临的问题是通过杰克逊对此类进行反序列化。我尝试将注释添加到我自己的(不工作的)代码如下:

@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 lib 中使用 mixin,但我想我会遇到类似的问题。

提前感谢您的帮助!

标签: kotlinjackson

解决方案


推荐阅读