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

scala-sttp.additionalTypeSerializers.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
package {{invokerPackage}}

import java.net.{ URI, URISyntaxException }

{{#json4s}}
object AdditionalTypeSerializers {
    import org.json4s.{Serializer, CustomSerializer, JNull, MappingException}
    import org.json4s.JsonAST.JString
      case object URISerializer extends CustomSerializer[URI]( _ => ( {
        case JString(s) => 
          try new URI(s)
          catch {
            case _: URISyntaxException =>
              throw new MappingException("String could not be parsed as a URI reference, it violates RFC 2396.")
            case _: NullPointerException =>
               throw new MappingException("String is null.")
          }
        case JNull => null
      }, {
        case uri: URI =>
         JString(uri.toString())
      }))

  def all: Seq[Serializer[_]] = Seq[Serializer[_]]() :+ URISerializer
}
{{/json4s}}
{{#circe}}
trait AdditionalTypeSerializers {
    import io.circe._

    implicit final lazy val URIDecoder: Decoder[URI] = Decoder.decodeString.emap(string =>
      try Right(new URI(string))
      catch {
        case _: URISyntaxException =>
          Left("String could not be parsed as a URI reference, it violates RFC 2396.")
        case _: NullPointerException =>
          Left("String is null.")
      }
    )

     implicit final lazy val URIEncoder: Encoder[URI] = new Encoder[URI] {
      final def apply(a: URI): Json = Json.fromString(a.toString)
  }
}
{{/circe}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy