我有一个scala case类:
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或创建新对象的解决方案
发布于 2015-10-15 04:52:48
完成了!看起来我们可以这样做:
import com.fasterxml.jackson.annotation.JsonProperty
case class Person(name: String, age: Int) {
@JsonProperty("isMillenial") def isMillenial: Boolean = age <= someConstantMillenialThreshold
}https://stackoverflow.com/questions/33135137
复制相似问题