本文整理汇总了Scala中io.circe.Encoder类的典型用法代码示例。如果您正苦于以下问题:Scala Encoder类的具体用法?Scala Encoder怎么用?Scala Encoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Encoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: route
//设置package包名称以及导入依赖的类
package com.ulasakdeniz.hakker
import akka.http.scaladsl.marshalling.{Marshal, ToEntityMarshaller}
import akka.http.scaladsl.model.{HttpHeader, HttpResponse, ResponseEntity, StatusCode}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import com.typesafe.config.ConfigFactory
import com.ulasakdeniz.hakker.template.Render
import de.heikoseeberger.akkahttpcirce.CirceSupport._
import io.circe.{Encoder, Json}
import io.circe.syntax._
import scala.collection.immutable
import scala.concurrent.ExecutionContext
trait Controller extends Render {
override lazy val config = ConfigFactory.load()
val StatusCodes = akka.http.scaladsl.model.StatusCodes
def route: Route
def apply(): Route = {
get {
// render frontend files
pathPrefix("js") {
renderDir("js")
}
} ~ route
}
def send(statusCode: StatusCode): Route = complete(statusCode)
def send[T](statusCode: StatusCode, content: T, headers: immutable.Seq[HttpHeader] = Nil)(
implicit marshaller: ToEntityMarshaller[T],
ec: ExecutionContext): Route = {
val response = Marshal(content)
.to[ResponseEntity](marshaller, ec)
.map(entity => {
HttpResponse(statusCode, headers = headers).withEntity(entity)
})
complete(response)
}
def sendJson[T](statusCode: StatusCode, content: T)(implicit encoder: Encoder[T],
ec: ExecutionContext): Route = {
sendJson(statusCode, content.asJson)
}
def sendJson[T](content: T)(implicit encoder: Encoder[T], ec: ExecutionContext): Route = {
sendJson(StatusCodes.OK, content)
}
def sendJson(statusCode: StatusCode, json: Json)(implicit ec: ExecutionContext): Route = {
send(statusCode, Option(json.noSpaces))
}
}
示例2: RootDecoder
//设置package包名称以及导入依赖的类
package io.circe.spray
import io.circe.{ Decoder, Encoder, RootEncoder }
case class RootDecoder[A](underlying: Decoder[A])
final object RootDecoder extends LowPriorityRootDecoders {
implicit def rootDecoderWithRootEncoder[A](implicit decoder: Decoder[A], encoder: RootEncoder[A]): RootDecoder[A] =
RootDecoder(decoder)
}
private[spray] sealed class LowPriorityRootDecoders {
implicit def rootDecoderBlocker1[A](implicit decoder: Decoder[A], encoder: Encoder[A]): RootDecoder[A] =
RootDecoder(decoder)
implicit def rootDecoderBlocker2[A](implicit decoder: Decoder[A], encoder: Encoder[A]): RootDecoder[A] =
RootDecoder(decoder)
implicit def rootDecoder[A](implicit decoder: Decoder[A]): RootDecoder[A] = RootDecoder(decoder)
}
示例3: anyValEncoder
//设置package包名称以及导入依赖的类
package aecor.example
import io.circe.{ Decoder, Encoder }
import shapeless.Unwrapped
trait AnyValCirceEncoding {
implicit def anyValEncoder[V, U](implicit ev: V <:< AnyVal,
V: Unwrapped.Aux[V, U],
encoder: Encoder[U]): Encoder[V] = {
val _ = ev
encoder.contramap(V.unwrap)
}
implicit def anyValDecoder[V, U](implicit ev: V <:< AnyVal,
V: Unwrapped.Aux[V, U],
decoder: Decoder[U]): Decoder[V] = {
val _ = ev
decoder.map(V.wrap)
}
}
object AnyValCirceEncoding extends AnyValCirceEncoding
object CirceSupport
extends de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
with AnyValCirceEncoding
示例4: FormSettings
//设置package包名称以及导入依赖的类
package org.danielnixon.progressive.shared.api
import io.circe.{ Decoder, Encoder }
import io.circe.generic.semiauto.{ deriveDecoder, deriveEncoder }
import org.danielnixon.progressive.shared.Wart
final case class FormSettings(
ajax: Boolean = true,
target: Option[Target] = None,
refreshTarget: Option[Target] = Some(Target.ClosestRefresh),
busyMessage: Option[String] = None,
focusTarget: Boolean = true,
reloadPage: Boolean = false,
confirmMessage: Option[String] = None,
confirmedAction: Option[String] = None,
ajaxAction: Option[String] = None,
remove: Boolean = false,
resetForm: Boolean = false
)
@SuppressWarnings(Array(Wart.Nothing))
object FormSettings {
implicit val decoder: Decoder[FormSettings] = deriveDecoder[FormSettings]
implicit val encoder: Encoder[FormSettings] = deriveEncoder[FormSettings]
def asJson(target: FormSettings): String = Json.asJson(target)
def fromJson(json: String): Option[FormSettings] = Json.fromJson(json)
}
示例5: LinkSettings
//设置package包名称以及导入依赖的类
package org.danielnixon.progressive.shared.api
import io.circe.{ Decoder, Encoder }
import io.circe.generic.semiauto.{ deriveDecoder, deriveEncoder }
import org.danielnixon.progressive.shared.Wart
final case class LinkSettings(
target: Target,
busyMessage: Option[String] = None,
focusTarget: Boolean = true,
href: Option[String] = None
)
@SuppressWarnings(Array(Wart.AsInstanceOf, Wart.Nothing))
object LinkSettings {
implicit val decoder: Decoder[LinkSettings] = deriveDecoder[LinkSettings]
implicit val encoder: Encoder[LinkSettings] = deriveEncoder[LinkSettings]
def asJson(target: LinkSettings): String = Json.asJson(target)
def fromJson(json: String): Option[LinkSettings] = Json.fromJson(json)
}
示例6: SubmitButtonSettings
//设置package包名称以及导入依赖的类
package org.danielnixon.progressive.shared.api
import io.circe.{ Decoder, Encoder }
import io.circe.generic.semiauto.{ deriveDecoder, deriveEncoder }
import org.danielnixon.progressive.shared.Wart
final case class SubmitButtonSettings(
target: Option[Target] = None,
busyMessage: Option[String] = None
)
@SuppressWarnings(Array(Wart.Nothing))
object SubmitButtonSettings {
implicit val decoder: Decoder[SubmitButtonSettings] = deriveDecoder[SubmitButtonSettings]
implicit val encoder: Encoder[SubmitButtonSettings] = deriveEncoder[SubmitButtonSettings]
def asJson(target: SubmitButtonSettings): String = Json.asJson(target)
def fromJson(json: String): Option[SubmitButtonSettings] = Json.fromJson(json)
}
示例7: circeableFrameFormat
//设置package包名称以及导入依赖的类
package org.http4s.akka
import io.circe.{Decoder, Encoder, Json}
import io.circe.parser._
import org.http4s.{Charset, DefaultCharset, UrlForm}
import org.http4s.websocket.WebsocketBits._
import play.twirl.api._
implicit def circeableFrameFormat[A: Encoder : Decoder]: FrameFormatter[A] = jsonFrameFormatter.transform[A](
Encoder[A].apply(_),
Decoder[A].decodeJson(_).toTry.get
)
//=== TWIRL ===
implicit val htmlFrameFormatter: FrameFormatter[Html] = stringFrameFormatter.transform(_.body, Html.apply)
implicit val xmlFrameFormatter: FrameFormatter[Xml] = stringFrameFormatter.transform(_.body, Xml.apply)
implicit val txtFrameFormatter: FrameFormatter[Txt] = stringFrameFormatter.transform(_.body, Txt.apply)
implicit val javaScriptFrameFormatter: FrameFormatter[JavaScript] = stringFrameFormatter.transform(_.body, JavaScript.apply)
}
示例8: yaml
//设置package包名称以及导入依赖的类
package swaggerblocks.extensions
import io.circe.yaml._
import io.circe.Encoder
import io.circe.syntax._
import swaggerblocks.internal.models.{ApiObjectSchema, ApiSchemaDefinition}
object yaml {
implicit class ExampleExtension(schemaDef: ApiSchemaDefinition) {
private val printer = Printer.spaces2.copy(
preserveOrder = true,
dropNullKeys = true
)
def withExample[T](obj: T)(implicit encoder: Encoder[T]): ApiSchemaDefinition =
schemaDef.schema match {
case s: ApiObjectSchema =>
schemaDef.copy(
schema = s.copy(
example = Some(printer.pretty(obj.asJson))
)
)
case _ =>
schemaDef
}
}
}
示例9: apply
//设置package包名称以及导入依赖的类
package swaggerblocks.rendering.yaml
import io.circe.yaml.parser.parse
import io.circe.{Encoder, Json}
import swaggerblocks.internal.specModels._
import cats.syntax.either._
trait SpecEncoders {
implicit val encodeSpecExample = new Encoder[SpecExample] {
def apply(se: SpecExample): Json = {
// Since we control the serialization this should be always valid
parse(se.value).valueOr(throw _)
}
}
implicit val encodeSpecValue = new Encoder[SpecValue] {
def apply(sv: SpecValue): Json = sv match {
case SpecValue.StringValue(v) => Json.fromString(v)
case SpecValue.IntValue(v) => Json.fromInt(v)
case SpecValue.BooleanValue(v) => Json.fromBoolean(v)
case SpecValue.ListValue(list) => Json.fromValues(list.map(apply))
case SpecValue.MapValue(mapping) => Json.fromFields(mapping.mapValues(apply))
}
}
}
示例10: Event
//设置package包名称以及导入依赖的类
package io.taig.akka.http.phoenix
import io.circe.{ Decoder, Encoder }
sealed case class Event( name: String )
object Event {
object Close extends Event( "phx_close" )
object Error extends Event( "phx_error" )
object Join extends Event( "phx_join" )
object Reply extends Event( "phx_reply" )
object Leave extends Event( "phx_leave" )
val all = Close :: Error :: Join :: Reply :: Leave :: Nil
implicit val encoderEvent: Encoder[Event] = {
Encoder[String].contramap( _.name )
}
implicit val decoderEvent: Decoder[Event] = {
Decoder[String].map { name ?
all.find( _.name == name ).getOrElse( Event( name ) )
}
}
}
示例11: Topic
//设置package包名称以及导入依赖的类
package io.taig.akka.http.phoenix
import io.circe.{ Decoder, Encoder }
import cats.implicits._
case class Topic( name: String, identifier: Option[String] ) {
def isSubscribedTo( topic: Topic ): Boolean = topic match {
case Topic( `name`, `identifier` ) ? true
case Topic( `name`, None ) ? true
case _ ? false
}
def serialize = name + identifier.map( ":" + _ ).getOrElse( "" )
override def toString = s"Topic($serialize)"
}
object Topic {
implicit val encoderTopic: Encoder[Topic] = {
Encoder[String].contramap( _.serialize )
}
implicit val decoderTopic: Decoder[Topic] = {
Decoder[String].emap { topic ?
Either.fromOption( parse( topic ), "Invalid format" )
}
}
val Phoenix = Topic( "phoenix" )
val Pattern = "(\\w+)(?::(\\w+))?".r
def apply( name: String, identifier: String ): Topic = {
Topic( name, Some( identifier ) )
}
def apply( name: String ): Topic = Topic( name, None )
def parse( topic: String ): Option[Topic] = {
topic match {
case Pattern( name, identifier ) ?
Some( Topic( name, Option( identifier ) ) )
case _ ? None
}
}
}
示例12: RefusedByRateLimiterError
//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter
import com.twitter.finagle.http.{Response, Status}
import com.twitter.finagle.RefusedByRateLimiter
import com.twitter.io.Buf
import com.twitter.logging.Logger
import io.circe.{Decoder, Encoder}
import io.circe.syntax._
final case class RefusedByRateLimiterError(
message: String
) extends Exception(message) {
def toResponse: Response = RefusedByRateLimiterError.toResponse(this)
}
object RefusedByRateLimiterError {
val LOG = Logger.get(getClass)
implicit val errorEncoder: Encoder[RefusedByRateLimiterError] =
Encoder.forProduct1("message") { err => err.message }
implicit val errorDecoder: Decoder[RefusedByRateLimiterError] =
Decoder.forProduct1[String, RefusedByRateLimiterError]("message") {
case (message: String) => RefusedByRateLimiterError(message)
}
def toResponse(error: RefusedByRateLimiterError): Response = {
val response = Response(Status.TooManyRequests)
val content = error.asJson.noSpaces
LOG.info(content)
response.content = Buf.Utf8(content)
response.contentType = "application/json"
response
}
}
示例13: Position
//设置package包名称以及导入依赖的类
package kartograffel.shared.model
import eu.timepit.refined.W
import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Interval
import io.circe.generic.semiauto._
import io.circe.{Decoder, Encoder}
import io.circe.refined._
import kartograffel.shared.model.Position.{Latitude, Longitude}
final case class Position(
latitude: Latitude,
longitude: Longitude
)
object Position {
type Latitude = Double Refined Interval.Closed[W.`-90.0`.T, W.`90.0`.T]
type Longitude = Double Refined Interval.Closed[W.`-180.0`.T, W.`180.0`.T]
implicit val positionDecoder: Decoder[Position] =
deriveDecoder
implicit val positionEncoder: Encoder[Position] =
deriveEncoder
}
示例14: Graffel
//设置package包名称以及导入依赖的类
package kartograffel.shared.model
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
final case class Graffel(
id: Graffel.Id,
position: Position
)
object Graffel {
final case class Id(value: String) extends AnyVal
implicit val graffelIdDecoder: Decoder[Id] =
Decoder.decodeString.map(Id.apply)
implicit val graffelIdEncoder: Encoder[Id] =
Encoder.encodeString.contramap(_.value)
implicit val graffelDecoder: Decoder[Graffel] =
deriveDecoder
implicit val graffelEncoder: Encoder[Graffel] =
deriveEncoder
}
示例15: EdgeCollection
//设置package包名称以及导入依赖的类
package com.outr.arango.managed
import com.outr.arango.{ArangoCode, ArangoEdge, ArangoException, DocumentOption, Edge}
import com.outr.arango.rest.{CreateInfo, GraphResponse}
import io.circe.Encoder
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
abstract class EdgeCollection[T <: Edge with DocumentOption](override val graph: Graph,
override val name: String,
from: List[String],
to: List[String]) extends AbstractCollection[T] {
private lazy val edge: ArangoEdge = graph.instance.edge(name)
override def create(waitForSync: Boolean = false): Future[GraphResponse] = edge.create(from, to, waitForSync)
override def delete(): Future[GraphResponse] = edge.delete(dropCollection = true)
override def get(key: String): Future[Option[T]] = edge[T](key).map(_.edge).recover {
case t: ArangoException if t.error.errorCode == ArangoCode.ArangoDocumentNotFound => None
}
override protected def insertInternal(document: T): Future[CreateInfo] = {
edge.insert[T](document).map(_.edge)
}
override protected def updateInternal[M](key: String, modification: M)
(implicit encoder: Encoder[M]): Future[CreateInfo] = {
edge.modify[M](key, modification).map(_.edge)
}
override protected def replaceInternal(currentKey: String, document: T): Future[Unit] = {
edge.replace[T](currentKey, document).map(_ => ())
}
override protected def deleteInternal(key: String): Future[Boolean] = edge.delete(key).map(_.removed)
}