Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: prioritize scala 3 #1201

Open
wants to merge 2 commits into
base: series/2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
distribution: temurin
java-version: 17
check-latest: true
- name: Setup Sbt
uses: sbt/setup-sbt@v1
- name: Check artifacts build process
run: sbt +publishLocal
- name: Check website build process
Expand All @@ -46,6 +48,8 @@ jobs:
distribution: temurin
java-version: 17
check-latest: true
- name: Setup Sbt
uses: sbt/setup-sbt@v1
- name: Setup NodeJs
uses: actions/setup-node@v3
with:
Expand Down
96 changes: 66 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The goal of this project is to create the best all-round JSON library for Scala:
In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-json" % "0.6.2"
libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3"
```

## Example
Expand All @@ -50,15 +50,21 @@ into a Scala `case class`
case class Banana(curvature: Double)
```

To do this, we create an *instance* of the `JsonDecoder` typeclass for `Banana` using the `zio-json` code generator. It is best practice to put it on the companion of `Banana`, like so
To do this, we derive an *instance* of the `JsonDecoder` typeclass for `Banana`.

```scala
object Banana {
implicit val decoder: JsonDecoder[Banana] = DeriveJsonDecoder.gen[Banana]
}
case class Banana(curvature: Double) derives JsonDecoder
```

_Note: If you’re using Scala 3 and your case class is defining default parameters, `-Yretain-trees` needs to be added to `scalacOptions`._
> [!NOTE]
>
> In scala 2, we need to use the `zio-json` code generator. It is best practice to put it on the companion of `Banana`, like so
>
> ```scala
> object Banana {
> implicit val decoder: JsonDecoder[Banana] = DeriveJsonDecoder.gen[Banana]
> }
> ```

Now we can parse JSON into our object

Expand All @@ -67,13 +73,10 @@ scala> """{"curvature":0.5}""".fromJson[Banana]
val res: Either[String, Banana] = Right(Banana(0.5))
```

Likewise, to produce JSON from our data we define a `JsonEncoder`
Likewise, to produce JSON from our data we derive a `JsonEncoder`

```scala
object Banana {
...
implicit val encoder: JsonEncoder[Banana] = DeriveJsonEncoder.gen[Banana]
}
case class Banana(curvature: Double) derives JsonEncoder

scala> Banana(0.5).toJson
val res: String = {"curvature":0.5}
Expand All @@ -85,6 +88,16 @@ val res: String =
}
```

> [!NOTE]
>
> In scala 2:
> ```scala
> object Banana {
> ...
> implicit val encoder: JsonEncoder[Banana] = DeriveJsonEncoder.gen[Banana]
> }
> ```

And bad JSON will produce an error in `jq` syntax with an additional piece of contextual information (in parentheses)

```
Expand All @@ -95,20 +108,49 @@ val res: Either[String, Banana] = Left(.curvature(expected a number, got w))
Say we extend our data model to include more data types

```scala
sealed trait Fruit
case class Banana(curvature: Double) extends Fruit
case class Apple (poison: Boolean) extends Fruit
enum Fruit:
case Banana(curvature: Double) extends Fruit
case Apple(poison: Boolean) extends Fruit
```

we can generate the encoder and decoder for the entire `sealed` family
we can generate the encoder and decoder for the entire `sealed` family using `JsonCodec`

```scala
object Fruit {
implicit val decoder: JsonDecoder[Fruit] = DeriveJsonDecoder.gen[Fruit]
implicit val encoder: JsonEncoder[Fruit] = DeriveJsonEncoder.gen[Fruit]
}
enum Fruit derives JsonCodec:
case Banana(curvature: Double)
case Apple(poison: Boolean)
```

> [!NOTE]
>
> In scala 2:
>
> ```scala mdoc:compile-only
> import zio.json._
>
> sealed trait Fruit extends Product with Serializable
> case class Banana(curvature: Double) extends Fruit
> case class Apple(poison: Boolean) extends Fruit
>
> object Fruit {
> implicit val decoder: JsonDecoder[Fruit] =
> DeriveJsonDecoder.gen[Fruit]
>
> implicit val encoder: JsonEncoder[Fruit] =
> DeriveJsonEncoder.gen[Fruit]
> }
>
> val json1 = """{ "Banana":{ "curvature":0.5 }}"""
> val json2 = """{ "Apple": { "poison": false }}"""
> val malformedJson = """{ "Banana":{ "curvature": true }}"""
>
> json1.fromJson[Fruit]
> json2.fromJson[Fruit]
> malformedJson.fromJson[Fruit]
>
> List(Apple(false), Banana(0.4)).toJsonPretty
> ```

allowing us to load the fruit based on a single field type tag in the JSON

```
Expand All @@ -122,19 +164,13 @@ val res: Either[String, Fruit] = Right(Apple(false))
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.

```scala
import zio.json._
import zio.json.*

sealed trait Fruit extends Product with Serializable
case class Banana(curvature: Double) extends Fruit
case class Apple(poison: Boolean) extends Fruit
enum Fruit extends Product, Serializable derives JsonCodec:
case Banana(curvature: Double) extends Fruit
case Apple(poison: Boolean) extends Fruit

object Fruit {
implicit val decoder: JsonDecoder[Fruit] =
DeriveJsonDecoder.gen[Fruit]

implicit val encoder: JsonEncoder[Fruit] =
DeriveJsonEncoder.gen[Fruit]
}
export Fruit.*

val json1 = """{ "Banana":{ "curvature":0.5 }}"""
val json2 = """{ "Apple": { "poison": false }}"""
Expand Down
96 changes: 66 additions & 30 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ into a Scala `case class`
case class Banana(curvature: Double)
```

To do this, we create an *instance* of the `JsonDecoder` typeclass for `Banana` using the `zio-json` code generator. It is best practice to put it on the companion of `Banana`, like so
To do this, we derive an *instance* of the `JsonDecoder` typeclass for `Banana`.

```scala
object Banana {
implicit val decoder: JsonDecoder[Banana] = DeriveJsonDecoder.gen[Banana]
}
case class Banana(curvature: Double) derives JsonDecoder
```

_Note: If you’re using Scala 3 and your case class is defining default parameters, `-Yretain-trees` needs to be added to `scalacOptions`._
> [!NOTE]
>
> In scala 2, we need to use the `zio-json` code generator. It is best practice to put it on the companion of `Banana`, like so
>
> ```scala
> object Banana {
> implicit val decoder: JsonDecoder[Banana] = DeriveJsonDecoder.gen[Banana]
> }
> ```

Now we can parse JSON into our object

Expand All @@ -67,13 +73,10 @@ scala> """{"curvature":0.5}""".fromJson[Banana]
val res: Either[String, Banana] = Right(Banana(0.5))
```

Likewise, to produce JSON from our data we define a `JsonEncoder`
Likewise, to produce JSON from our data we derive a `JsonEncoder`

```scala
object Banana {
...
implicit val encoder: JsonEncoder[Banana] = DeriveJsonEncoder.gen[Banana]
}
case class Banana(curvature: Double) derives JsonEncoder

scala> Banana(0.5).toJson
val res: String = {"curvature":0.5}
Expand All @@ -85,6 +88,16 @@ val res: String =
}
```

> [!NOTE]
>
> In scala 2:
> ```scala
> object Banana {
> ...
> implicit val encoder: JsonEncoder[Banana] = DeriveJsonEncoder.gen[Banana]
> }
> ```

And bad JSON will produce an error in `jq` syntax with an additional piece of contextual information (in parentheses)

```
Expand All @@ -95,20 +108,49 @@ val res: Either[String, Banana] = Left(.curvature(expected a number, got w))
Say we extend our data model to include more data types

```scala
sealed trait Fruit
case class Banana(curvature: Double) extends Fruit
case class Apple (poison: Boolean) extends Fruit
enum Fruit:
case Banana(curvature: Double) extends Fruit
case Apple(poison: Boolean) extends Fruit
```

we can generate the encoder and decoder for the entire `sealed` family
we can generate the encoder and decoder for the entire `sealed` family using `JsonCodec`

```scala
object Fruit {
implicit val decoder: JsonDecoder[Fruit] = DeriveJsonDecoder.gen[Fruit]
implicit val encoder: JsonEncoder[Fruit] = DeriveJsonEncoder.gen[Fruit]
}
enum Fruit derives JsonCodec:
case Banana(curvature: Double)
case Apple(poison: Boolean)
```

> [!NOTE]
>
> In scala 2:
>
> ```scala mdoc:compile-only
> import zio.json._
>
> sealed trait Fruit extends Product with Serializable
> case class Banana(curvature: Double) extends Fruit
> case class Apple(poison: Boolean) extends Fruit
>
> object Fruit {
> implicit val decoder: JsonDecoder[Fruit] =
> DeriveJsonDecoder.gen[Fruit]
>
> implicit val encoder: JsonEncoder[Fruit] =
> DeriveJsonEncoder.gen[Fruit]
> }
>
> val json1 = """{ "Banana":{ "curvature":0.5 }}"""
> val json2 = """{ "Apple": { "poison": false }}"""
> val malformedJson = """{ "Banana":{ "curvature": true }}"""
>
> json1.fromJson[Fruit]
> json2.fromJson[Fruit]
> malformedJson.fromJson[Fruit]
>
> List(Apple(false), Banana(0.4)).toJsonPretty
> ```

allowing us to load the fruit based on a single field type tag in the JSON

```
Expand All @@ -121,20 +163,14 @@ val res: Either[String, Fruit] = Right(Apple(false))

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.

```scala mdoc:compile-only
import zio.json._

sealed trait Fruit extends Product with Serializable
case class Banana(curvature: Double) extends Fruit
case class Apple(poison: Boolean) extends Fruit
```scala
import zio.json.*

object Fruit {
implicit val decoder: JsonDecoder[Fruit] =
DeriveJsonDecoder.gen[Fruit]
enum Fruit extends Product, Serializable derives JsonCodec:
case Banana(curvature: Double) extends Fruit
case Apple(poison: Boolean) extends Fruit

implicit val encoder: JsonEncoder[Fruit] =
DeriveJsonEncoder.gen[Fruit]
}
export Fruit.*

val json1 = """{ "Banana":{ "curvature":0.5 }}"""
val json2 = """{ "Apple": { "poison": false }}"""
Expand Down
Loading