Jsoniter Support

This module provides refined types codec instances for Jsoniter Scala.

Dependency

SBT:

libraryDependencies += "io.github.iltotore" %% "iron-jsoniter" % "version"

Mill:

mvn"io.github.iltotore::iron-jsoniter:version"

Following examples' dependencies

SBT:

libraryDependencies += "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.19.1"
libraryDependencies += "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.19.1"

Mill:

mvn"com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-core:2.19.1"
mvn"com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-macros:2.19.1"

Encoder/Decoder instances

Given JsonValueCodec for Iron enables using refined types for JSON serialization/deserialization:

import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.github.plokhotnyuk.jsoniter_scala.core.*

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.jsoniter.given

case class User(name: String :| Alphanumeric, age: Int :| Positive)

given JsonValueCodec[User] = JsonCodecMaker.make

//Encoding
writeToString[User](User("totore", 18))

//Decoding
readFromString[User]("""{"name":"totore","age":18}""")
readFromString[User]("""{"name":"totore","age":-18}""") //Error: "Should be strictly positive"

Key codec instances

Given JsonKeyCodec for Iron enables using refined types as Map keys. The JsonKeyCodec of the underlying type is required:

import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.github.plokhotnyuk.jsoniter_scala.core.*

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.jsoniter.given

given JsonKeyCodec[Int] = new:
  def decodeKey(in: JsonReader): Int = in.readKeyAsInt()
  def encodeKey(x: Int, out: JsonWriter): Unit = out.writeKey(x)

given JsonValueCodec[Map[Int :| Positive, String]] = JsonCodecMaker.make

//Encoding
val one: Int :| Positive = 1
writeToString[Map[Int :| Positive, String]](Map(one -> "one"))

//Decoding
readFromString[Map[Int :| Positive, String]]("""{"1":"one"}""")
readFromString[Map[Int :| Positive, String]]("""{"-1":"minus one"}""") //Error: "Should be strictly positive"