How can I rename a field during serialization with Json4s? How can I rename a field during serialization with Json4s? json json

How can I rename a field during serialization with Json4s?


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.writecase class Person(serial: Int, firstName: String)val rename = FieldSerializer[Person](renameTo("serial", "id"))implicit val format: Formats = DefaultFormats + renamewrite(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