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


Scala Format类代码示例

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


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

示例1: PlayJsonSnapshotMatcherSpec

//设置package包名称以及导入依赖的类
package com.commodityvectors.snapshotmatchers.playJson

import java.io.File

import com.commodityvectors.snapshotmatchers.{SnapshotMatcher, SnapshotSerializer}
import org.apache.commons.io.FileUtils
import org.scalatest.{BeforeAndAfterEach, Matchers, fixture}
import play.api.libs.json.{Format, JsValue, Json}

import scala.util.Try

class PlayJsonSnapshotMatcherSpec extends fixture.WordSpec with Matchers with SnapshotMatcher with PlayJsonSnapshotMatcher with BeforeAndAfterEach {
  case class Test(value: Int)
  implicit lazy val jsonFormat: Format[Test] = Json.format[Test]

  val snapshotFolder: String = "scalatest-snapshot-matcher-play-json/src/test/__snapshots__"
  val currentSpecPath: String = s"$snapshotFolder/com/commodityvectors/snapshotmatchers/playJson/PlayJsonSnapshotMatcherSpec"

  override def afterEach(): Unit = {
    Try(FileUtils.deleteDirectory(new File(snapshotFolder)))
  }

  "PlayJsonSnapshotMatcherSpec" should {
    "pretty print json" in { implicit test =>
      val instance = Test(1)
      SnapshotSerializer.serialize(Json.toJson(instance)) shouldEqual
        s"""{
           |  "value" : 1
           |}""".stripMargin
    }

    "generate json snapshot file" in { implicit test =>
      val instance = Test(1)
      Json.toJson(instance) should matchSnapshot[JsValue]("customId")
      FileUtils.readFileToString(
        new File(s"$currentSpecPath/customId.snap")
      ) shouldEqual
        s"""{
           |  "value" : 1
           |}""".stripMargin
    }

    "allow deserialization" in { implicit test =>
      val instance = Test(1)
      Json.toJson(instance) should matchSnapshot[JsValue]("anotherId")
      "anotherId" should deserializeAs(instance)
    }
  }
} 
开发者ID:commodityvectors,项目名称:scalatest-snapshot-matchers,代码行数:50,代码来源:PlayJsonSnapshotMatcherSpec.scala

示例2: ContentType

//设置package包名称以及导入依赖的类
package io.soheila.cms.entities

import io.soheila.commons.formats.EnumFormat
import play.api.libs.json.Format

object ContentType extends Enumeration {
  type ContentType = Value

  val Html = Value("Html")
  val Text = Value("Text")
  val Markup = Value("Markup")
  val Wysiwyg = Value("Wysiwyg")
}

case class Content(rawText: String, text: String, contentType: ContentType.Value)

object Content {
  import play.api.libs.json.Json

  implicit val contentTypeFormat: Format[ContentType.Value] = EnumFormat.enumFormat(ContentType)
  implicit val jsonFormat = Json.format[Content]
} 
开发者ID:esfand-r,项目名称:soheila-cm,代码行数:23,代码来源:Content.scala

示例3: UnmarshallingDDirectivesSpec

//设置package包名称以及导入依赖的类
package akka.http.documenteddsl

import java.time.LocalDate

import akka.http.documenteddsl.directives.UnmarshallingDDirectives._
import akka.http.documenteddsl.documentation.OutDocumentation._
import akka.http.documenteddsl.documentation.{JsonSchema, OutDocumentation, RouteDocumentation}
import akka.http.scaladsl.model.{ContentTypes, StatusCodes}
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.MustMatchers._
import org.scalatest.WordSpec
import play.api.libs.json.{Format, Json}

class UnmarshallingDDirectivesSpec extends WordSpec with DDirectivesSpec with ScalatestRouteTest {
  import UnmarshallingDDirectivesSpec._

  "Out" must {
    val now = LocalDate.now()

    "be applied to route documentation" in {
      Out[TestOut].describe(RouteDocumentation()).out mustBe Some(OutDocumentation(
        success = List(
          Payload.Success(
            status = Status(StatusCodes.OK),
            contentType = "application/json",
            schema = JsonSchema.resolveSchema[TestOut],
            example = None))))
    }
    "be applied to route documentation (concatenated)" in {
      val out = Out(StatusCodes.Created, TestOut("id", Some("name"), now)) & Out(StatusCodes.NotFound, "entity not found")
      out.describe(RouteDocumentation()).out mustBe Some(OutDocumentation(
        failure = List(
          Payload.Failure(
            status = Status(StatusCodes.NotFound),
            contentType = None,
            description = Some("entity not found"))),
        success = List(
          Payload.Success(
            status = Status(StatusCodes.Created),
            contentType = "application/json",
            schema = JsonSchema.resolveSchema[TestOut],
            example = Some(Json toJson TestOut("id", Some("name"), now))))))
    }
  }

}

object UnmarshallingDDirectivesSpec {
  case class TestOut(id: String, name: Option[String], createdAt: LocalDate)
  implicit val testInFormat: Format[TestOut] = Json.format[TestOut]
} 
开发者ID:evolution-gaming,项目名称:akka-http-documenteddsl,代码行数:52,代码来源:UnmarshallingDDirectivesSpec.scala

示例4: descriptor

//设置package包名称以及导入依赖的类
package sample.helloworldconsumer.api

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.{Service, ServiceCall}
import com.lightbend.lagom.scaladsl.api.transport.Method
import play.api.libs.json.{Format, Json}

trait HelloConsumerService extends Service {

  override def descriptor = {
    import Service._

    named("wordCounts").withCalls(
      restCall(Method.GET, "/api/wordcount", findTopHundredWordCounts _),
      restCall(Method.GET, "/api/foo", foo)
    ).withAutoAcl(true)
  }

  def findTopHundredWordCounts(): ServiceCall[NotUsed, Map[String, Int]]

  def foo(): ServiceCall[NotUsed, String]

  case class WordDetails(word: String, count: String)

  object WordDetails {
    implicit val format: Format[WordDetails] = Json.format
  }
} 
开发者ID:knoldus,项目名称:lagom-scala-wordcount.g8,代码行数:29,代码来源:HelloConsumerService.scala

示例5: AccountState

//设置package包名称以及导入依赖的类
package org.ioreskovic.greatmaterialcontinuum.impl.stt.acc

import org.ioreskovic.greatmaterialcontinuum.api.Account
import play.api.libs.json.{Format, Json}

object AccountState {
  implicit val format: Format[AccountState] = Json.format
}

case class AccountState(maybeAccount: Option[Account]) {
  def deactivate: AccountState = withActiveStatus(false)

  def activate: AccountState = withActiveStatus(true)

  def withActiveStatus(status: Boolean): AccountState = maybeAccount match {
    case Some(acc) => AccountState(Some(Account(acc.username, acc.password, active = status)))
    case _ => AccountState(None)
  }
} 
开发者ID:ioreskovic,项目名称:great-material-continuum,代码行数:20,代码来源:AccountState.scala

示例6: createPlayer

//设置package包名称以及导入依赖的类
package com.chriswk.gameranker.player.api

import java.util.UUID

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.{Service, ServiceCall}
import play.api.libs.json.{Format, Json}

trait PlayerService extends Service {
  def createPlayer: ServiceCall[CreatePlayer, Player]

  def getPlayer(playerId: UUID): ServiceCall[NotUsed, Player]

  def getPlayers: ServiceCall[NotUsed, Seq[Player]]

  override def descriptor = {
    import Service._
    named("player").withCalls(
      pathCall("/api/player", createPlayer),
      pathCall("/api/player/:id", getPlayer _),
      pathCall("/api/player", getPlayers)
    )
  }
}

case class Player(id: UUID, name: String)

object Player {
  implicit val format: Format[Player] = Json.format
}

case class CreatePlayer(name: String)

object CreatePlayer {
  implicit val format: Format[CreatePlayer] = Json.format
} 
开发者ID:chriswk,项目名称:gameranker,代码行数:37,代码来源:PlayerService.scala

示例7: CoreMonitorStatus

//设置package包名称以及导入依赖的类
package org.wex.cmsfs.config.api

import play.api.libs.json.{Format, Json}

case class CoreMonitorStatus(id: Int, category: String, name: String, metric: String,
                             collect: Option[CoreMonitorStageStatus] = None,
                             analyze: Option[CoreMonitorStageStatus] = None,
                             alarm: Option[CoreMonitorStageStatus] = None)

object CoreMonitorStatus extends ((Int, String, String, String,
  Option[CoreMonitorStageStatus], Option[CoreMonitorStageStatus], Option[CoreMonitorStageStatus]) => CoreMonitorStatus) {
  implicit val format: Format[CoreMonitorStatus] = Json.format
}

case class CoreMonitorStageStatus(state: Boolean, timestamp: String, result: String)

object CoreMonitorStageStatus {
  implicit val format: Format[CoreMonitorStageStatus] = Json.format
} 
开发者ID:shinhwagk,项目名称:cmsfs,代码行数:20,代码来源:CoreMonitorStatus.scala

示例8: Status

//设置package包名称以及导入依赖的类
package domain

import play.api.libs.json.{Format, JsString, JsSuccess, JsValue}

object Status extends Enumeration {

  type Status = Value
  val Inactive = Value(1, "Inactive")
  val Active = Value(2, "Active")
  val Deleted = Value(3, "Deleted")

  implicit val myEnumFormat = new Format[Status] {
    def reads(json: JsValue) = JsSuccess(Status.withName(json.as[String]))
    def writes(myEnum: Status) = JsString(myEnum.toString)
  }
} 
开发者ID:dtambussi,项目名称:pedidos,代码行数:17,代码来源:Status.scala

示例9: EstadoPedido

//设置package包名称以及导入依赖的类
package domain

import play.api.libs.json.{Format, JsString, JsSuccess, JsValue}

object EstadoPedido extends Enumeration {

  type EstadoPedido = Value
  val Generado = Value(1, "Generado")
  val Pendiente = Value(2, "Pendiente")
  val Confeccionado = Value(3, "Confeccionado")
  val Entregado = Value(4, "Entregado")
  val Cancelado = Value(5, "Cancelado")

  implicit val myEnumFormat = new Format[EstadoPedido] {
    def reads(json: JsValue) = JsSuccess(EstadoPedido.withName(json.as[String]))
    def writes(myEnum: EstadoPedido) = JsString(myEnum.toString)
  }
} 
开发者ID:dtambussi,项目名称:pedidos,代码行数:19,代码来源:EstadoPedido.scala

示例10: Categoria

//设置package包名称以及导入依赖的类
package domain

import play.api.libs.json.{Format, JsString, JsSuccess, JsValue}

object Categoria extends Enumeration {

  type Categoria = Value
  val Entradas = Value(1, "Entradas")
  val PlatosPrincipales = Value(2, "PlatosPrincipales")
  val Postres = Value(3, "Postres")
  val Bebidas = Value(4, "Bebidas")
  val Cafeteria = Value(5, "Cafeteria")

  implicit val myEnumFormat = new Format[Categoria] {
    def reads(json: JsValue) = JsSuccess(Categoria.withName(json.as[String]))
    def writes(myEnum: Categoria) = JsString(myEnum.toString)
  }
} 
开发者ID:dtambussi,项目名称:pedidos,代码行数:19,代码来源:Categoria.scala

示例11: Item

//设置package包名称以及导入依赖的类
package demo.api.basket

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.api.{Descriptor, Service, ServiceCall}
import com.lightbend.lagom.scaladsl.api.transport._
import demo.api.basket.ExtraTransportExceptions.CustomExceptionSerializer
import play.api.Environment
import play.api.libs.json.{Format, Json}

case class Item(name: String, price: Int)
object Item {
  implicit val itemFormat: Format[Item] = Json.format
}

case class Basket(items: Seq[Item], total: Int)
object Basket {
  implicit val basketFormat: Format[Basket] = Json.format
}

case class OrderPlaced(basketId: String, basket: Basket)
object OrderPlaced {
  implicit val orderPlacedFormat: Format[OrderPlaced] = Json.format
}

trait BasketService extends Service {
  def getBasket(basketId: String): ServiceCall[NotUsed, Basket]
  def getTotal(basketId: String): ServiceCall[NotUsed, Int]
  def addItem(basketId: String): ServiceCall[Item, NotUsed]
  def clearAll(basketId: String): ServiceCall[NotUsed, NotUsed]
  def placeOrder(basketId: String): ServiceCall[NotUsed, NotUsed]
  def placedOrders: Topic[OrderPlaced]

  override def descriptor: Descriptor = {
    import Method._
    import Service._

    named("basket").withCalls(
      restCall(GET, "/basket/:basketId", getBasket _),
      restCall(GET, "/basket/:basketId/total", getTotal _),
      restCall(POST, "/basket/:basketId/items", addItem _),
      restCall(DELETE, "/basket/:basketId/items", clearAll _),
      restCall(POST, "/basket/:basketId/order", placeOrder _)
    ).withTopics(
      topic("placed-orders", placedOrders)
    ).withExceptionSerializer(new CustomExceptionSerializer(Environment.simple()))
  }
} 
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:49,代码来源:BasketService.scala

示例12: Item

//设置package包名称以及导入依赖的类
package demo.api.basket

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.api.{Descriptor, Service, ServiceCall}
import com.lightbend.lagom.scaladsl.api.transport._
import play.api.libs.json.{Format, Json}

case class Item(name: String, price: Int)
object Item {
  implicit val itemFormat: Format[Item] = Json.format
}

case class Basket(items: Seq[Item], total: Int)
object Basket {
  implicit val basketFormat: Format[Basket] = Json.format
}

case class OrderPlaced(basketId: String, basket: Basket)
object OrderPlaced {
  implicit val orderPlacedFormat: Format[OrderPlaced] = Json.format
}

trait BasketService extends Service {
  def getBasket(basketId: String): ServiceCall[NotUsed, Basket]
  def getTotal(basketId: String): ServiceCall[NotUsed, Int]
  def addItem(basketId: String): ServiceCall[Item, NotUsed]
  def clearAll(basketId: String): ServiceCall[NotUsed, NotUsed]
  def placeOrder(basketId: String): ServiceCall[NotUsed, NotUsed]
  def placedOrders: Topic[OrderPlaced]

  override def descriptor: Descriptor = {
    import Method._
    import Service._

    named("basket").withCalls(
      restCall(GET, "/basket/:basketId", getBasket _),
      restCall(GET, "/basket/:basketId/total", getTotal _),
      restCall(POST, "/basket/:basketId/items", addItem _),
      restCall(DELETE, "/basket/:basketId/items", clearAll _),
      restCall(POST, "/basket/:basketId/order", placeOrder _)
    ).withTopics(
      topic("placed-orders", placedOrders)
    )
  }
} 
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:47,代码来源:BasketService.scala

示例13: OrderEntityFormats

//设置package包名称以及导入依赖的类
package demo.impl.order

import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.ReplyType
import com.lightbend.lagom.scaladsl.playjson.JsonSerializer
import play.api.libs.json.{Format, Json}

object OrderEntityFormats {
  implicit val orderItemsFormat: Format[OrderItems] = Json.format
  implicit val createOrderFormat: Format[CreateOrder] = Json.format
  implicit val getOrderFormat = JsonSerializer.emptySingletonFormat(GetOrder)
  implicit val orderInitialisedFormat: Format[OrderInitialized] = Json.format
  implicit val orderStateFormat: Format[OrderState] = Json.format
}

case class OrderItems (items: Seq[String])

trait OrderCommand
case class CreateOrder(items: Seq[String]) extends OrderCommand with ReplyType[Done]
case object GetOrder extends OrderCommand with ReplyType[OrderItems]

trait OrderEvent
case class OrderInitialized(items: Seq[String]) extends OrderEvent
case class OrderState(items: Seq[String])

object OrderState {
  def empty = OrderState(Seq())
}

class Order extends PersistentEntity {
  override type Command = OrderCommand
  override type Event = OrderEvent
  override type State = OrderState

  override def initialState = OrderState.empty

  override def behavior: Behavior = Actions()
    .onCommand[CreateOrder, Done] {
      case (CreateOrder(items), ctx, state) => {
        ctx.thenPersist(OrderInitialized(items)){
          evt => ctx.reply(Done)
        }
      }
    }
    .onReadOnlyCommand[GetOrder.type, OrderItems] {
      case (GetOrder, ctx, state) => ctx.reply(OrderItems(state.items))
    }
    .onEvent{
      case (OrderInitialized(items), state) => OrderState(items)
    }
} 
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:53,代码来源:Order.scala

示例14: Item

//设置package包名称以及导入依赖的类
package demo.api.basket

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.{Descriptor, Service, ServiceCall}
import com.lightbend.lagom.scaladsl.api.transport._
import play.api.libs.json.{Format, Json}

case class Item(name: String, price: Int)
object Item {
  implicit val itemFormat: Format[Item] = Json.format
}

case class Basket(items: Seq[Item], total: Int)
object Basket {
  implicit val basketFormat: Format[Basket] = Json.format
}

trait BasketService extends Service {
  def getBasket(basketId: String): ServiceCall[NotUsed, Basket]
  def getTotal(basketId: String): ServiceCall[NotUsed, Int]
  def addItem(basketId: String): ServiceCall[Item, NotUsed]

  override def descriptor: Descriptor = {
    import Method._
    import Service._

    named("basket").withCalls(
      restCall(GET, "/basket/:basketId", getBasket _),
      restCall(GET, "/basket/:basketId/total", getTotal _),
      restCall(POST, "/basket/:basketId/items", addItem _)
    )
  }
} 
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:34,代码来源:BasketService.scala

示例15: Item

//设置package包名称以及导入依赖的类
package demo.api.basket

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.{Descriptor, Service, ServiceCall}
import com.lightbend.lagom.scaladsl.api.transport._
import play.api.libs.json.{Format, Json}

case class Item(name: String, price: Int)
object Item {
  implicit val itemFormat: Format[Item] = Json.format
}

case class Basket(items: Seq[Item], total: Int)
object Basket {
  implicit val basketFormat: Format[Basket] = Json.format
}

trait BasketService extends Service {
  def getBasket(basketId: String): ServiceCall[NotUsed, Basket]
  def addItem(basketId: String): ServiceCall[Item, NotUsed]

  override def descriptor: Descriptor = {
    import Method._
    import Service._

    named("basket").withCalls(
      restCall(GET, "/basket/:basketId", getBasket _),
      restCall(POST, "/basket/:basketId/items", addItem _)
    )
  }
} 
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:32,代码来源:BasketService.scala


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