当前位置: 首页>>代码示例>>Scala>>正文


Scala Decoder类代码示例

本文整理汇总了Scala中io.circe.Decoder的典型用法代码示例。如果您正苦于以下问题:Scala Decoder类的具体用法?Scala Decoder怎么用?Scala Decoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Decoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: 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)
} 
开发者ID:travisbrown,项目名称:circe-spray,代码行数:20,代码来源:RootDecoder.scala

示例2: 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 
开发者ID:notxcain,项目名称:aecor,代码行数:27,代码来源:AnyValCirceEncoding.scala

示例3: 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)
} 
开发者ID:danielnixon,项目名称:progressive,代码行数:30,代码来源:FormSettings.scala

示例4: 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)
} 
开发者ID:danielnixon,项目名称:progressive,代码行数:23,代码来源:LinkSettings.scala

示例5: 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)
} 
开发者ID:danielnixon,项目名称:progressive,代码行数:21,代码来源:SubmitButtonSettings.scala

示例6: 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)
} 
开发者ID:Lasering,项目名称:http4s-akka,代码行数:21,代码来源:FrameFormatter.scala

示例7: 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 ) )
        }
    }
} 
开发者ID:Taig,项目名称:akka-http-phoenix,代码行数:26,代码来源:Event.scala

示例8: 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
        }
    }
} 
开发者ID:Taig,项目名称:akka-http-phoenix,代码行数:47,代码来源:Topic.scala

示例9: 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
  }
} 
开发者ID:lookout,项目名称:rate-limiting-strategy,代码行数:36,代码来源:RefusedByRateLimiterError.scala

示例10: 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
} 
开发者ID:fthomas,项目名称:kartograffel,代码行数:27,代码来源:Position.scala

示例11: 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
} 
开发者ID:fthomas,项目名称:kartograffel,代码行数:26,代码来源:Graffel.scala

示例12: Graph

//设置package包名称以及导入依赖的类
package com.outr.arango.managed

import java.util.concurrent.atomic.AtomicBoolean

import com.outr.arango.rest.LogEvent
import com.outr.arango.{Arango, ArangoCursor, ArangoDB, ArangoGraph, ArangoSession, Credentials, DocumentOption, Edge, Macros, Query, ReplicationMonitor}
import io.circe.Decoder
import io.youi.net.URL
import reactify.{Channel, Observable}

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.experimental.macros

class Graph(name: String,
            db: String = Arango.defaultDatabase,
            url: URL = Arango.defaultURL,
            credentials: Option[Credentials] = Arango.defaultCredentials,
            timeout: FiniteDuration = 15.seconds) {
  private[managed] lazy val arango: Arango = new Arango(url)
  private[managed] lazy val sessionFuture: Future[ArangoSession] = arango.session(credentials)
  private[managed] lazy val dbFuture: Future[ArangoDB] = sessionFuture.map(_.db(db))
  private[managed] lazy val graphFuture: Future[ArangoGraph] = dbFuture.map(_.graph(name))

  protected[managed] lazy val instance: ArangoGraph = Await.result[ArangoGraph](graphFuture, timeout)

  private[managed] var managedCollections = List.empty[AbstractCollection[_]]
  private[managed] lazy val monitor: ReplicationMonitor = instance.db.replication.monitor
  def collections: List[AbstractCollection[_]] = managedCollections

  val initialized: Channel[Boolean] = Channel[Boolean]
  lazy val realTime: RealTime = new RealTime(this)

  private val initCalled = new AtomicBoolean(false)

  
  def delete(dropCollections: Boolean = true): Future[Boolean] = graphFuture.flatMap { graph =>
    graph.delete(dropCollections).map(!_.error)
  }

  def cursor: ArangoCursor = instance.db.cursor
  def call[T](query: Query)(implicit decoder: Decoder[T]): Future[T] = instance.db.call[T](query)
  def first[T](query: Query)(implicit decoder: Decoder[T]): Future[Option[T]] = instance.db.first[T](query)
  def execute(query: Query): Future[Boolean] = instance.db.execute(query)

  def synchronous[T](future: Future[T], timeout: FiniteDuration = 10.seconds): T = Arango.synchronous(future, timeout)
} 
开发者ID:outr,项目名称:scarango,代码行数:49,代码来源:Graph.scala

示例13: PolymorphicVertexCollection

//设置package包名称以及导入依赖的类
package com.outr.arango.managed

import com.outr.arango.{DocumentOption, Macros}
import com.outr.arango.rest.CreateInfo
import io.circe.Decoder.Result
import io.circe.{Decoder, Encoder, HCursor, Json}

import scala.language.experimental.macros

class PolymorphicVertexCollection[T <: PolymorphicDocumentOption]
                                    (graph: Graph, name: String, val types: List[PolymorphicType[T]])
                                    extends VertexCollection[T](graph, name) {
  private lazy val typeMap: Map[String, PolymorphicType[T]] = types.map(t => t.value -> t).toMap
  override implicit val encoder: Encoder[T] = new Encoder[T] {
    override def apply(a: T): Json = typeMap(a._type).encoder(a)
  }
  override implicit val decoder: Decoder[T] = new Decoder[T] {
    override def apply(c: HCursor): Result[T] = {
      val decoder = for {
        t <- Decoder[String].prepare(_.downField("_type"))
        d <- typeMap.get(t).fold(
          Decoder.failedWithMessage[T]("_type not found in polymorphic document")
        )(_.decoder)
      } yield d
      decoder(c)
    }
  }

  override protected def updateDocument(document: T, info: CreateInfo): T = typeMap(document._type).updateDocument(document, info)
}

trait PolymorphicDocumentOption extends DocumentOption {
  def _type: String
}

trait PolymorphicType[T] {
  def value: String
  def encoder: Encoder[T]
  def decoder: Decoder[T]
  def updateDocument(document: T, info: CreateInfo): T
} 
开发者ID:outr,项目名称:scarango,代码行数:42,代码来源:PolymorphicVertexCollection.scala

示例14: ArangoCursor

//设置package包名称以及导入依赖的类
package com.outr.arango

import com.outr.arango.rest.{QueryRequest, QueryRequestOptions, QueryResponse}
import io.circe.{Decoder, Json}
import io.circe.generic.auto._
import io.youi.http.Method

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class ArangoCursor(arangoDB: ArangoDB) {
  def apply[T](query: Query,
               count: Boolean = false,
               batchSize: Option[Int] = None,
               cache: Option[Boolean] = None,
               memoryLimit: Option[Long] = None,
               ttl: Option[Int] = None,
               options: QueryRequestOptions = QueryRequestOptions())
              (implicit decoder: Decoder[T]): Future[QueryResponse[T]] = {
    val bindVars = Json.obj(query.args.map {
      case (key, value) => {
        val argValue: Json = value match {
          case Value.Null => Json.Null
          case StringValue(s) => Json.fromString(s)
          case IntValue(i) => Json.fromInt(i)
          case LongValue(l) => Json.fromLong(l)
          case DoubleValue(d) => Json.fromDoubleOrNull(d)
        }
        key -> argValue
      }
    }.toSeq: _*)
    val request = QueryRequest(
      query = query.value,
      bindVars = bindVars,
      count = count,
      batchSize = batchSize,
      cache = cache,
      memoryLimit = memoryLimit,
      ttl = ttl,
      options = options
    )
    arangoDB.restful[QueryRequest, QueryResponse[T]]("cursor", request)
  }

  def paged[T](query: Query,
               batchSize: Int = 100,
               cache: Option[Boolean] = None,
               memoryLimit: Option[Long] = None,
               ttl: Option[Int] = None,
               options: QueryRequestOptions = QueryRequestOptions())
              (implicit decoder: Decoder[T]): Future[QueryResponsePagination[T]] = {
    val count = true
    apply[T](query, count, Some(batchSize), cache, memoryLimit, ttl, options).map(r => QueryResponsePagination[T](this, r))
  }

  def get[T](id: String)
            (implicit decoder: Decoder[T]): Future[QueryResponse[T]] = {
    arangoDB.call[QueryResponse[T]](s"cursor/$id", Method.Put)
  }
} 
开发者ID:outr,项目名称:scarango,代码行数:61,代码来源:ArangoCursor.scala

示例15: QueryResponsePagination

//设置package包名称以及导入依赖的类
package com.outr.arango

import com.outr.arango.rest.QueryResponse
import io.circe.Decoder

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

case class QueryResponsePagination[T](cursor: ArangoCursor,
                                      response: QueryResponse[T],
                                      offset: Int = 0)(implicit decoder: Decoder[T]) extends Iterable[T] {
  lazy val start: Int = offset
  lazy val end: Int = math.max(offset, offset + response.result.size - 1)
  def results: List[T] = response.result
  def total: Int = response.count.get
  def hasNext: Boolean = response.hasMore
  def next(): Future[QueryResponsePagination[T]] = if (response.hasMore) {
    cursor.get[T](response.id.get).map(r => copy(response = r, offset = end + 1))
  } else {
    Future.failed(throw new RuntimeException("No more results."))
  }

  override def iterator: Iterator[T] = new QueryResponseIterator[T](this)
} 
开发者ID:outr,项目名称:scarango,代码行数:25,代码来源:QueryResponsePagination.scala


注:本文中的io.circe.Decoder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。