harness.zio.json.instances.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 cats.data.NonEmptyList
import cats.syntax.either.*
import harness.core.*
import zio.json.*
implicit def catsNelJsonCodec[A: JsonCodec]: JsonCodec[NonEmptyList[A]] =
JsonCodec
.list[A]
.transformOrFail(
{
case head :: tail => NonEmptyList(head, tail).asRight
case Nil => "NonEmptyList has 0 elements".asLeft
},
_.toList,
)
implicit def fieldDecoderFromStringDecoder[T](implicit sd: StringDecoder[T]): JsonFieldDecoder[T] =
JsonFieldDecoder.string.mapOrFail(sd.decode)
implicit def fieldEncoderFromStringEncoder[T](implicit se: StringEncoder[T]): JsonFieldEncoder[T] =
JsonFieldEncoder.string.contramap(se.encode)
implicit val throwableJsonCodec: JsonCodec[Throwable] =
EncodedThrowable.throwableJsonCodec
© 2015 - 2025 Weber Informatics LLC | Privacy Policy