Jsoniter Scala
structible-jsoniter-scala is available for Scala 2.12 and 2.13.
It provides derivations for JsonCodec instances.
Make sure to familiarize yourself with creating structible instances before looking at the examples below.
Dependency
- sbt
libraryDependencies += "com.github.cerst" % "structible-jsoniter-scala" % "0.6.4"
- Maven
<dependency> <groupId>com.github.cerst</groupId> <artifactId>structible-jsoniter-scala</artifactId> <version>0.6.4</version> </dependency>
- Gradle
dependencies { compile group: 'com.github.cerst', name: 'structible-jsoniter-scala', version: '0.6.4' }
Example
import com.github.cerst.structible.core.DefaultConstraints._
import com.github.cerst.structible.core._
import com.github.cerst.structible.jsoniterscala.ops._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros._
object JsonCodecExample {
final class UserId private (val value: Long) extends AnyVal
object UserId {
private val structible: Structible[Long, UserId] = Structible.structible(new UserId(_), _.value, c >= 0)
// jsoniter-scala uses a null value internally which you have to provide explicitly when using AnyVal due to a compiler bug:
// https://github.com/scala/bug/issues/8097
// otherwise, the created codec throws a NullPointerException at runtime
implicit val jsonCodecForUserId: JsonCodec[UserId] = structible.toJsonCodec(null.asInstanceOf[UserId])
}
final case class User(userId: UserId)
object User {
implicit val jsonCodecForPerson: JsonValueCodec[User] = JsonCodecMaker.make(CodecMakerConfig)
}
}
The source code for this page can be found here.