Skip to content

Play JSON integration into ZIO Schema ecosystem.

License

Notifications You must be signed in to change notification settings

jirihausner/zio-schema-play-json

Repository files navigation

zio-schema-play-json

zio-schema-play-json seamlessly integrates zio-schema with the widely used Play JSON JSON library developed by Play team.

CI Badge Maven Central Version Scala Steward badge ZIO Schema Play JSON

Why zio-schema-play-json?

  • Perfect for projects that already use Play JSON that want to take advantage of the type-safe schema definitions of zio-schema.
  • Provides an alternative to zio-schema-json, catering to teams already invested in Play JSON ecosystem.
  • Makes it easier to gradually migrate to zio-schema or incorporate its features into legacy stacks.

Installation

In order to use this library, we need to add one of the following lines in your build.sbt file:

libraryDependencies += "io.github.jirihausner" %% "zio-schema-play-json"     % "0.1.0" // play-json 3.0.+
libraryDependencies += "io.github.jirihausner" %% "zio-schema-play-json-210" % "0.1.0" // play-json 2.10.+
libraryDependencies += "io.github.jirihausner" %% "zio-schema-play-json-27"  % "0.1.0" // play-json 2.7.+
libraryDependencies += "io.github.jirihausner" %% "zio-schema-play-json-26"  % "0.1.0" // play-json 2.6.+

zio-schema-play-json also publishes artifacts for ScalaJS.

Example

import play.api.libs.json.Format
import zio.schema.codec.play.json.PlayJsonCodec
import zio.schema.{DeriveSchema, Schema}

case class Person(name: String, age: Int)

object Person {
  implicit val schema: Schema[Person] = DeriveSchema.gen
}

// derive Play JSON format from Schema
import play.api.libs.json.Json
implicit val codec: Format[Person] = PlayJsonCodec.schemaFormat(Person.schema)

Json.parse("""{"name": "John", "age": 30}""").as[Person] // Person("John", 30)
Json.stringify(Json.toJson(Person("Adam", 24))) // {"Adam": 24}

// use existing Play JSON format as BinaryCodec
import zio.schema.codec.play.json.PlayJsonCodec.playJsonBinaryCodec

playJsonBinaryCodec[Person](Json.format[Person]) // zio.schema.codec.BinaryCodec[Person]

// derive Play JSON BinaryCodec from schema
import zio.schema.codec.play.json.PlayJsonCodec.schemaBasedBinaryCodec

schemaBasedBinaryCodec[Person](PlayJsonCodec.Config.default) // zio.schema.codec.BinaryCodec[Person]

Acknowledgements

This library was heavily inspired by zio-schema-json. Huge thanks to its original contributors for laying foundational ideas and implementation, which greatly influenced zio-schema-play-json.

Disclaimer

zio-schema-play-json is not intended to compete with zio-schema-json. Instead, it serves as a complementary option for developers who prefer or already use Play JSON in their stack.


Contributions are welcome! If you have suggestions, improvements, or feature requests, feel free to open an issue or a pull request.

About

Play JSON integration into ZIO Schema ecosystem.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages