Integration with uPickle
Records can be converted to/from JSON by using uPickle.
Installation
You need an additional module to use records integrated with uPickle.
libraryDependencies ++= Seq(
"com.github.tarao" %% "record4s-upickle" % "0.13.0"
),
From / To JSON
Importing Record.readWriter
enables uPickle to decode/encode records in the ordinary way.
import com.github.tarao.record4s.%
import com.github.tarao.record4s.upickle.Record.readWriter
import upickle.default.{read, write}
type Person = % { val name: String; val age: Int }
val json = """{"name":"tarao","age":3}"""
// json: String = "{\"name\":\"tarao\",\"age\":3}"
// Read from JSON
val r = read[Person](json)
// r: % {
// val name: String
// val age: Int
// } = %(name = tarao, age = 3)
// Write it back
write(r)
// res0: String = "{\"name\":\"tarao\",\"age\":3}"