visit
Assume your application has a nested class named Person which has a member named address of class Address.
case class Address(doorNo: Int, street: String, city: String, zipCode: String)
case class Person(name: String, age: Int, address: Address)
import io.circe._, io.circe.generic.semiauto._, io.circe.syntax._
case class Address(doorNo: Int, street: String, city: String, zipCode: String)
object Address {
implicit val codec = deriveCodec[Address]
}
case class Person(name: String, age: Int, address: Address)
object Person {
implicit val codec = deriveCodec[Person]
def encode(person: Person): String = {
person.asJson.toString
}
def decode(jsonString: String): Person = {
parser.decode[Person](jsonString).toOption.get
}
}
That’s it. You can now use the Person.encode
and Person.decode
methods.
import io.circe._, io.circe.generic.semiauto._, io.circe.syntax._
val johnDoe = Person("John Doe", 24, Address(5, "Times Square Street", "New York", "12345"))
println(Person.encode(johnDoe))
val encodedText = """
{
"name" : "Fin",
"age" : 21,
"address" : {
"doorNo" : 7,
"street" : "Staten Island",
"city" : "New York",
"zipCode" : "23456"
}
}
"""
val decodedJohnDoe = Person.decode(encodedText)
println(decodedJohnDoe)
First published