Skip to content

Instantly share code, notes, and snippets.

@n4to4
Created September 20, 2016 21:50
Show Gist options
  • Save n4to4/0197295661b1d8bf3c6ae1d9316fdb17 to your computer and use it in GitHub Desktop.
Save n4to4/0197295661b1d8bf3c6ae1d9316fdb17 to your computer and use it in GitHub Desktop.
circe example
object CirceMain extends App {
import io.circe.Encoder
import io.circe.syntax._
case class Person(firstName: String, lastName: String, age: Int)
val person = Person("Joe", "Black", 42)
{
implicit val personEnc: Encoder[Person] = Encoder.forProduct3(
"firstName", "lastName", "age") {
src => (src.firstName, src.lastName, src.age)
}
println(person.asJson)
}
{
import io.circe.generic.semiauto._
implicit val personEnc = deriveEncoder[Person]
println(person.asJson)
}
{
import io.circe.generic.auto._
println(person.asJson)
}
import io.circe.Decoder
import io.circe.jawn._
val jsonStr = """{"firstName":"Joe","lastName":"Black","age":42,"departments":["dev","hr","qa"]}"""
{
implicit val personDecoder: Decoder[Person] = Decoder.forProduct3(
"firstName", "lastName", "age")(Person.apply)
val person = decode[Person](jsonStr)
println(person)
}
{
implicit val personDecoder = for {
firstName <- Decoder.instance(_.get[String]("firstName"))
lastName <- Decoder.instance(_.get[String]("lastName"))
age <- Decoder.instance(_.get[Int]("age"))
} yield Person(firstName, lastName, age)
val person = decode[Person](jsonStr)
println(person)
}
{
import cats.syntax.cartesian._
val firstName = Decoder.instance(_.get[String]("firstName"))
val lastName = Decoder.instance(_.get[String]("lastName"))
val age = Decoder.instance(_.get[Int]("age"))
implicit val personDecoder = (
firstName |@| lastName |@| age
).map(Person.apply)
val person = decode[Person](jsonStr)
println(person)
}
{
import io.circe.generic.semiauto._
implicit val personDec = deriveDecoder[Person]
val person = decode[Person](jsonStr)
println(person)
}
{
import io.circe.generic.auto._
val person = decode[Person](jsonStr)
println(person)
}
{
import io.circe.Json
val result = parse(jsonStr)
println(result)
val json = result.getOrElse(Json.Null)
val cursor = json.hcursor
assert { cursor.downField("age").focus == Some(42.asJson) }
assert { cursor.downField("age").set(43.asJson).focus == Some(43.asJson) }
assert { cursor.downField("departments").downArray.focus == Some("dev".asJson) }
assert { cursor.downField("departments").downArray.last.focus == Some("qa".asJson) }
assert { cursor.downField("departments").downArray.
right.withFocus(_.withString(_.toUpperCase.asJson)).focus == Some("HR".asJson) }
}
{
import io.circe.Json
import io.circe.optics.JsonPath._
import io.circe.Printer
val jsonStr = """{"firstName":"Joe","address":{"street":"Market st.","city":"Sydney"}}"""
val result = parse(jsonStr)
val json = result.getOrElse(Json.Null)
println(json)
val city0 = root.address.city.string
println(city0.getOption(json))
val updatedJson = city0.set("Newcastle")(json)
val updatedStr = updatedJson.pretty(Printer.spaces2)
println(updatedStr)
}
}
@zzzz465
Copy link

zzzz465 commented Aug 12, 2022

my head exploded just by seeing this
🤯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment