-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
id: bson | ||
title: "Bson Codecs" | ||
sidebar_label: "BSON" | ||
--- | ||
|
||
## Introduction | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
id: json | ||
title: "JSON Codecs" | ||
sidebar_label: "JSON" | ||
--- | ||
|
||
## Introduction | ||
|
||
JSON (JavaScript Object Notation) is a widely used data interchange format for transmitting and storing data. ZIO Schema provides `zio-schema-json` module which has functionality to derive JSON codecs from a ZIO Schema. JSON codecs allow us to easily serialize and deserialize data in JSON format. In this article, we will explore how derive JSON codecs using the ZIO Schema. | ||
|
||
## Installation | ||
|
||
To derive JSON codecs from a ZIO Schema, we need to add the following dependency to our `build.sbt` file: | ||
|
||
```scala | ||
libraryDependencies += "dev.zio" %% "zio-schema-json" % @VERSION@ | ||
``` | ||
|
||
## JsonCodec | ||
|
||
The `JsonCodec` object inside the `zio.schema.codec` package provides the `jsonCodec` operator which allows us to derive JSON codecs from a ZIO Schema: | ||
|
||
```scala | ||
object JsonCodec { | ||
def jsonCodec[A](schema: Schema[A]): zio.json.JsonCodec[A] = ??? | ||
} | ||
``` | ||
|
||
Let's try an example to see how it works: | ||
|
||
```scala mdoc:compile-only | ||
import zio._ | ||
import zio.json._ | ||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
case class Person(name: String, age: Int) | ||
|
||
object Person { | ||
implicit val schema: Schema[Person] = | ||
DeriveSchema.gen | ||
implicit val jsonCodec: zio.json.JsonCodec[Person] = | ||
zio.schema.codec.JsonCodec.jsonCodec(schema) | ||
} | ||
|
||
object Main extends ZIOAppDefault { | ||
def run = for { | ||
_ <- ZIO.debug("JSON Codec Example:") | ||
person: Person = Person("John", 42) | ||
encoded: String = person.toJson | ||
_ <- ZIO.debug(s"person object encoded to JSON string: $encoded") | ||
decoded <- ZIO.fromEither(Person.jsonCodec.decodeJson(encoded)) | ||
_ <- ZIO.debug(s"JSON object decoded to Person class: $decoded") | ||
} yield () | ||
} | ||
``` | ||
|
||
## BinaryCodec | ||
|
||
We can also derive a binary codec from a ZIO Schema using the `schemaBasedBinaryCodec`: | ||
|
||
```scala | ||
object JsonCodec { | ||
implicit def schemaBasedBinaryCodec[A](implicit schema: Schema[A]): BinaryCodec[A] = ??? | ||
} | ||
``` | ||
|
||
Let's try an example: | ||
|
||
```scala mdoc:compile-only | ||
import zio._ | ||
import zio.schema.codec.BinaryCodec | ||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
case class Person(name: String, age: Int) | ||
|
||
object Person { | ||
implicit val schema: Schema[Person] = | ||
DeriveSchema.gen | ||
implicit val jsonBinaryCodec: BinaryCodec[Person] = | ||
zio.schema.codec.JsonCodec.schemaBasedBinaryCodec(schema) | ||
} | ||
|
||
object Main extends ZIOAppDefault { | ||
def run = for { | ||
_ <- ZIO.debug("JSON Codec Example:") | ||
person: Person = Person("John", 42) | ||
encoded: Chunk[Byte] = Person.jsonBinaryCodec.encode(person) | ||
_ <- ZIO.debug(s"person object encoded to Binary JSON: ${toHex(encoded)}") | ||
decoded <- ZIO.fromEither(Person.jsonBinaryCodec.decode(encoded)) | ||
_ <- ZIO.debug(s"JSON object decoded to Person class: $decoded") | ||
} yield () | ||
|
||
def toHex(bytes: Chunk[Byte]): String = | ||
bytes.map("%02x".format(_)).mkString(" ") | ||
} | ||
``` | ||
|
||
The output of the above program is: | ||
|
||
```scala | ||
JSON Codec Example: | ||
person object encoded to JSON string: 7b 22 6e 61 6d 65 22 3a 22 4a 6f 68 6e 22 2c 22 61 67 65 22 3a 34 32 7d | ||
JSON object decoded to Person class: Person(John,42) | ||
``` | ||
|
||
By utilizing JSON codecs derived from ZIO Schema, developers can easily serialize and deserialize data in JSON format without writing boilerplate code. This enhances productivity and simplifies data handling in Scala applications. | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters