From 726b57f98a72c0694b9d8f0ed8af97fb95340247 Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Mon, 29 Apr 2024 18:03:22 +0000 Subject: [PATCH] extend docs --- docs/decoding.md | 17 +++++++++++++++++ docs/encoding.md | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/docs/decoding.md b/docs/decoding.md index 5a72f12c..87155664 100644 --- a/docs/decoding.md +++ b/docs/decoding.md @@ -85,6 +85,23 @@ object Fruit { Almost all of the standard library data types are supported as fields on the case class, and it is easy to add support if one is missing. +### Sealed families and enums for Scala 3 +Sealed families where all members are only objects, or a Scala 3 enum with all cases parameterless are interpreted as enumerations and will encode 1:1 with their value-names. +```scala +enum Foo derives JsonDecoder: + case Bar + case Baz + case Qux +``` +or +```scala +sealed trait Foo derives JsonDecoder +object Foo: + case object Bar extends Foo + case object Baz extends Foo + case object Qux extends Foo +``` + ## Manual instances Sometimes it is easier to reuse an existing `JsonDecoder` rather than generate a new one. This can be accomplished using convenience methods on the `JsonDecoder` typeclass to *derive* new decoders diff --git a/docs/encoding.md b/docs/encoding.md index b43123d8..6c9f92d4 100644 --- a/docs/encoding.md +++ b/docs/encoding.md @@ -55,6 +55,23 @@ apple.toJson Almost all of the standard library data types are supported as fields on the case class, and it is easy to add support if one is missing. +### Sealed families and enums for Scala 3 +Sealed families where all members are only objects, or a Scala 3 enum with all cases parameterless are interpreted as enumerations and will encode 1:1 with their value-names. +```scala +enum Foo derives JsonEncoder: + case Bar + case Baz + case Qux +``` +or +```scala +sealed trait Foo derives JsonEncoder +object Foo: + case object Bar extends Foo + case object Baz extends Foo + case object Qux extends Foo +``` + ## Manual instances Sometimes it is easier to reuse an existing `JsonEncoder` rather than generate a new one. This can be accomplished using convenience methods on the `JsonEncoder` typeclass to *derive* new decoders: