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

poppet.codec.jackson.instances.JacksonCodecInstancesBinCompat.scala Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
package poppet.codec.jackson.instances

import cats.Applicative
import cats.Functor
import cats.implicits.*
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import poppet.*

trait JacksonCodecInstancesBinCompat {
    protected class MacroTypeReference[A] extends TypeReference[A]

    implicit inline def jacksonFromJsonCodec[A](implicit inline om: ObjectMapper): Codec[JsonNode, A] =
        new Codec[JsonNode, A] {
            override def apply(a: JsonNode): Either[CodecFailure[JsonNode], A] = {
                try Right(om.readValue(om.treeAsTokens(a), new MacroTypeReference[A] {}))
                catch { case e: Exception => Left(new CodecFailure(e.getMessage, a, e)) }
            }
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy