Skip to content

jirihausner/zio-schema-jsoniter

Repository files navigation

zio-schema-jsoniter

zio-schema-jsoniter seamlessly integrates zio-schema with plokhotnyuk's jsoniter-scala, a safe and ultra-fast JSON library.

CI Badge Maven Central Version Scala Steward badge ZIO Schema Jsoniter

Why zio-schema-jsoniter?

  • Perfect for projects that already use jsoniter-scala 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 jsoniter-scala ecosystem.
  • Makes it easier to gradually migrate to zio-schema or incorporate its features into legacy stacks.

Installation

In order to use this library, you need to add one following line in your build.sbt file:

libraryDependencies += "io.github.jirihausner" %% "zio-schema-jsoniter-scala" % "0.1.0"

Example

import zio.schema.{DeriveSchema, Schema}

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

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

// derive `JsonValueCodec[A]` from implicit `Schema[A]`
import com.github.plokhotnyuk.jsoniter_scala.core.{JsonValueCodec, readFromString, writeToString}
import zio.schema.codec.jsoniter.JsoniterCodec

implicit val codec: JsonValueCodec[Person] = JsoniterCodec.schemaJsonValueCodec[Person]

readFromString[Person]("""{"name": "John", "age": 30}""") // Person("John", 30)
writeToString(Person("Adam", 24))                         // "{"name":"Adam","age":24}"

// derive `BinaryCodec[A]` from implicit `JsonValueCodec[A]`
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker
import zio.schema.codec.jsoniter.JsoniterCodec.jsonValueBinaryCodec

jsonValueBinaryCodec[Person](JsonCodecMaker.make[Person]) // zio.schema.codec.BinaryCodec[Person]

// derive `BinaryCodec[A]` backed by `JsonValueCodec[A]` from implicit `Schema[A]` directly
import zio.schema.codec.jsoniter.JsoniterCodec.schemaBasedBinaryCodec

schemaBasedBinaryCodec[Person] // zio.schema.codec.BinaryCodec[Person]

// derive `BinaryCodec[A]` backed by `JsonValueCodec[A]` from implicit `Schema[A]` directly with custom configuration
import zio.schema.NameFormat
import zio.schema.codec.circe.Configuration

val config = Configuration()
  .withEmptyCollectionsIgnored
  .withNullValuesIgnored
  .withDiscriminator("type", format = NameFormat.SnakeCase)
schemaBasedBinaryCodec[Person](config) // 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-jsoniter.

Disclaimer

zio-schema-jsoniter is not intended to compete with zio-schema-json. Instead, it serves as a complementary option for developers who prefer or already use jsoniter-scala 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

jsoniter-scala codecs for zio-schema

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages