Skip to content

Instantly share code, notes, and snippets.

@bmaggi
Created October 22, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmaggi/1504df3b4a913f4658622adea9c3ea0d to your computer and use it in GitHub Desktop.
Save bmaggi/1504df3b4a913f4658622adea9c3ea0d to your computer and use it in GitHub Desktop.
Simple serialization/deserialization with renaming field in Scala
import org.json4s.FieldSerializer._
import org.json4s.jackson.Serialization
import org.json4s.{DefaultFormats, FieldSerializer}
case class Data(id: Int)
implicit val format = new DefaultFormats {} + FieldSerializer[Data](
renameTo("id", "@id"),
renameFrom("@id","id")
)
val id = 1
val ser = Serialization.write[Data](Data(id))
val dSer = Serialization.read[Data](ser)
assert(dSer.id == id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment