JSON & Serialization

41.1 circe (Typelevel)

//> using dep io.circe::circe-generic:0.14.10
//> using dep io.circe::circe-parser:0.14.10
import io.circe.*, io.circe.generic.auto.*, io.circe.parser.*, io.circe.syntax.*

case class User(name: String, age: Int, tags: List[String])

val json: String = User("Ann", 30, List("admin")).asJson.noSpaces
val decoded: Either[Error, User] = decode[User]("""{"name":"Bob","age":25,"tags":[]}""")

// Semi-automatic (preferred) — Scala 3 derives syntax
case class Item(id: Int, title: String) derives Codec.AsObject

// Cursor navigation
parse("""{"a":{"b":[1,2]}}""").flatMap(_.hcursor.downField("a").downField("b").as[List[Int]])

41.2 upickle (Li Haoyi)

import upickle.default.*
case class Point(x: Int, y: Int) derives ReadWriter
write(Point(1, 2))                    // {"x":1,"y":2}
read[Point]("""{"x":3,"y":4}""")
ujson.read("""{"a":1}""")("a").num

41.3 Others

fastest, compile-time codecs ZIO ecosystem, fast avro4s, vulcan

pureconfig, ciris, zio-config typed configuration (HOCON/env)