diff --git a/docs/codecs/bson.md b/docs/codecs/bson.md index 31102d71b..284ce88d1 100644 --- a/docs/codecs/bson.md +++ b/docs/codecs/bson.md @@ -1,6 +1,7 @@ --- id: bson title: "Bson Codecs" +sidebar_label: "BSON" --- ## Introduction diff --git a/docs/codecs/json.md b/docs/codecs/json.md new file mode 100644 index 000000000..d45d8174b --- /dev/null +++ b/docs/codecs/json.md @@ -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. + + + + diff --git a/docs/sidebars.js b/docs/sidebars.js index 86e263726..ddad391e8 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -33,7 +33,8 @@ const sidebars = { link: { type: "doc", id: "codecs/index" }, items: [ "codecs/apache-avro", - "codecs/bson" + "codecs/bson", + "codecs/json" ], }, "dynamic-data-representation",