How can I rename a field during serialization with Json4s?

时光毁灭记忆、已成空白 提交于 2019-12-05 05:19:12
kenrose

The code you have is returning the correct JSON with id as a field. Here is a slightly fuller example to evaluate in the console:

import org.json4s._
import org.json4s.FieldSerializer._
import org.json4s.jackson.Serialization.write

case class Person(serial: Int, firstName: String)
val rename = FieldSerializer[Person](renameTo("serial", "id"))
implicit val format: Formats = DefaultFormats + rename
write(Person(1, "Guest")) // actually returns {"id":1,"firstName":"Guest"}

Your code snippet has wrongly named implicit. It should be:

implicit val formats: Formats = DefaultFormats + rename
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!