All Downloads are FREE. Search and download functionalities are using the official Maven repository.

harness.zio.json.instances.scala Maven / Gradle / Ivy

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