Skip to content

docs: add tabs for scala 3 #1201

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

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
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
109 changes: 107 additions & 2 deletions docs/index.md → docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: "Getting Started with ZIO Json"
sidebar_label: "Getting Started"
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

[ZIO Json](https://github.com/zio/zio-json) is a fast and secure JSON library with tight ZIO integration.

@PROJECT_BADGES@
Expand Down Expand Up @@ -34,10 +37,23 @@ Let's try a simple example of encoding and decoding JSON using ZIO JSON.

All the following code snippets assume that the following imports have been declared

<Tabs groupId="language">
<TabItem value="scala 2" label="Scala 2">

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

</TabItem>
<TabItem value="scala 3" label="Scala 3" default>

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

</TabItem>
</Tabs>

Say we want to be able to read some JSON like

```json
Expand All @@ -50,6 +66,9 @@ into a Scala `case class`
case class Banana(curvature: Double)
```

<Tabs groupId="language">
<TabItem value="scala 2" label="Scala 2">

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

```scala
Expand All @@ -58,7 +77,19 @@ object Banana {
}
```

_Note: If you’re using Scala 3 and your case class is defining default parameters, `-Yretain-trees` needs to be added to `scalacOptions`._
</TabItem>
<TabItem value="scala 3" label="Scala 3" default>

To do this, we derive an *instance* of the `JsonDecoder` typeclass for `Banana`.

```scala
case class Banana(curvature: Double) derives JsonDecoder
```

Note: If your case class is defining default parameters, -Yretain-trees needs to be added to scalacOptions.

</TabItem>
</Tabs>

Now we can parse JSON into our object

Expand All @@ -67,14 +98,29 @@ 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`

<Tabs groupId="language">
<TabItem value="scala 2" label="Scala 2">

```scala
object Banana {
...
implicit val encoder: JsonEncoder[Banana] = DeriveJsonEncoder.gen[Banana]
}
```

</TabItem>
<TabItem value="scala 3" label="Scala 3" default>

```scala
case class Banana(curvature: Double) derives JsonEncoder
```

</TabItem>
</Tabs>

```
scala> Banana(0.5).toJson
val res: String = {"curvature":0.5}

Expand All @@ -94,21 +140,51 @@ val res: Either[String, Banana] = Left(.curvature(expected a number, got w))

Say we extend our data model to include more data types

<Tabs groupId="language">
<TabItem value="scala 2" label="Scala 2">

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

</TabItem>
<TabItem value="scala 3" label="Scala 3" default>

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

</TabItem>
</Tabs>

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

<Tabs groupId="language">
<TabItem value="scala 2" label="Scala 2">

```scala
object Fruit {
implicit val decoder: JsonDecoder[Fruit] = DeriveJsonDecoder.gen[Fruit]
implicit val encoder: JsonEncoder[Fruit] = DeriveJsonEncoder.gen[Fruit]
}
```

</TabItem>
<TabItem value="scala 3" label="Scala 3" default>

```scala
enum Fruit derives JsonCodec:
case Banana(curvature: Double)
case Apple (poison: Boolean)
```

</TabItem>
</Tabs>

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

```
Expand All @@ -121,6 +197,9 @@ 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.

<Tabs groupId="language">
<TabItem value="scala 2" label="Scala 2">

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

Expand All @@ -147,6 +226,32 @@ malformedJson.fromJson[Fruit]
List(Apple(false), Banana(0.4)).toJsonPretty
```

</TabItem>
<TabItem value="scala 3" label="Scala 3" default>

```scala
import zio.json.*

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

export 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
```

</TabItem>
</Tabs>

# How

Extreme **performance** is achieved by decoding JSON directly from the input source into business objects (inspired by [plokhotnyuk](https://github.com/plokhotnyuk/jsoniter-scala)). Although not a requirement, the latest advances in [Java Loom](https://wiki.openjdk.java.net/display/loom/Main) can be used to support arbitrarily large payloads with near-zero overhead.
Expand Down