繁体   English   中英

Scala Jackson序列化将计算字段添加到序列化案例类

[英]Scala Jackson Serialization add computed field to serialized case class

我有一个scala案例类:

case class Person(name: String, age: Int) {
    def isMillenial: Boolean = age <= someConstantMillenialThreshold
}

这将序列化为以下json(使用ScalaJsonFactory对象映射器进行序列化):

{
    name: "foo",
    age: 42
}

但是我希望将其序列化如下:

{
    name: "foo",
    age: 42,
    isMillenial: true
}

是否有任何一种简单的方法,最好使用某些Jackson注释或类似方法? 基本上寻找不涉及更改case class Person或创建新对象的解决方案

做完了! 看来我们可以做到这一点:

import com.fasterxml.jackson.annotation.JsonProperty
case class Person(name: String, age: Int) {
    @JsonProperty("isMillenial") def isMillenial: Boolean = age <= someConstantMillenialThreshold
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM