Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 548 Bytes

2-json-codec.md

File metadata and controls

20 lines (16 loc) · 548 Bytes
optionId scastieLink codeTitle description
json-codec
Encode and decode custom data types to JSON
The pluggable derivation system gives custom types new capabilities.
case class Pet(
  name: String,
  kind: String
) derives Codec // enable coding Pet to and from text

val coco = Pet(name = "Coco", kind = "Cat")

val message = writeJson(coco)
//  ^^^^^^^ contains the text: {"name":"Coco","kind":"Cat"}

readJson[Pet](message) // convert message back to a Pet!