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

morphir.sdk.maybe.Codec.scala Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
package morphir.sdk.maybe

import io.circe.{ Decoder, Encoder, HCursor, Json }
import morphir.sdk.Maybe
import morphir.sdk.Maybe.Maybe

object Codec {
  implicit def encodeMaybe[A](implicit encodeA: Encoder[A]): Encoder[Maybe[A]] =
    (maybeVal: Maybe[A]) =>
      maybeVal match {
        case Maybe.Just(value) => encodeA(value)
        case Maybe.Nothing     => Json.Null
      }

  implicit def decodeMaybe[A](implicit decodeA: Decoder[A]): Decoder[Maybe[A]] =
    (c: HCursor) =>
      c.focus match {
        case None            => Right(Maybe.Nothing)
        case Some(Json.Null) => Right(Maybe.Nothing)
        case Some(_)         => decodeA(c).map(Maybe.Just(_))
      }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy