harness.zio.json.JsonCodecOps.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of harness-zio-json_3 Show documentation
Show all versions of harness-zio-json_3 Show documentation
Miscellaneous libraries/utilities for Scala.
The newest version!
package harness.zio.json
import harness.core.*
import scala.reflect.ClassTag
import zio.json.*
implicit class JsonEncoderOps(self: JsonEncoder.type) {
def fromHarnessStringEncoder[T](implicit encoder: StringEncoder[T]): JsonEncoder[T] =
JsonEncoder.string.contramap(encoder.encode)
}
implicit class JsonDecoderOps(self: JsonDecoder.type) {
def fromHarnessStringDecoder[T](implicit decoder: StringDecoder[T]): JsonDecoder[T] =
JsonDecoder.string.mapOrFail(decoder.decode)
}
implicit class JsonCodecOps(self: JsonCodec.type) {
def `enum`[E <: Enum[E], Enc](implicit ec: JsonCodec[Enc], ewe: Enum.WithEnc[E, Enc], ct: ClassTag[E]): JsonCodec[E] =
ec.transformOrFail(
e => ewe.decode(e).toRight(s"Invalid ${ct.runtimeClass.getSimpleName}: $e"),
ewe.encode,
)
def fromHarnessStringEncoderAndDecoder[T](implicit encoder: StringEncoder[T], decoder: StringDecoder[T]): JsonCodec[T] =
JsonCodec(JsonEncoder.fromHarnessStringEncoder[T], JsonDecoder.fromHarnessStringDecoder[T])
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy