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


Scala _类代码示例

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


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

示例1: LocaleCountrySerializer

//设置package包名称以及导入依赖的类
package com.wix.pay.stripe


import java.util.Locale
import org.json4s.JsonAST.JString
import org.json4s.reflect.TypeInfo
import org.json4s.{Formats, JsonDSL, MappingException, Serializer, _}


class LocaleCountrySerializer extends Serializer[Locale] {
  private val LocaleClass = classOf[Locale]

  def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Locale] = {
    case (TypeInfo(LocaleClass, _), json) => json match {
      case JString(country) =>
        new Locale("", country)
      case x => throw new MappingException("Can't convert " + x + " to LocaleClass")
    }
  }

  def serialize(implicit formats: Formats): PartialFunction[Any, JValue] = {
    case x: Locale =>
      import JsonDSL._
      x.getCountry
  }
} 
开发者ID:wix,项目名称:libpay-stripe,代码行数:27,代码来源:LocaleCountrySerializer.scala

示例2: negotiateAction

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

import org.json4s.{Extraction, _}
import play.api.mvc._

import scala.concurrent.{ExecutionContext, Future}
import org.json4s._
import org.json4s.Xml.toXml
import org.json4s.native.JsonMethods.{render => jsonRender, _}
import play.api.http.ContentTypes


trait ContentNegotiation extends AcceptExtractors with Rendering with Results {
  import ExecutionContext.Implicits.global

  def negotiateAction[M](block: Request[AnyContent] => Future[Content[M]])
                        (implicit formats: Formats): Action[AnyContent] = Action.async { implicit request =>
    block(request) map { result =>
      val repr: JValue = serialize(result.model)
      render {
        case Accepts.Xml() => xmlResult(repr, result.status)
        case Accepts.Json() => jsonResult(repr, result.status)
      }
    }
  }

  private def xmlResult(repr: JValue, status: Int)
               (implicit request: Request[AnyContent]) =
    Results.Status(status)(toXml(repr).toString).as(ContentTypes.XML)

  private def jsonResult(repr: JValue, status: Int)
               (implicit request: Request[AnyContent]) =
    Results.Status(status)(compact(jsonRender(repr))).as(ContentTypes.JSON)

  private def serialize[M](model: M)(implicit formats: Formats): JValue =
    Extraction.decompose(model)
}

final case class Content[M](model: M, status: Int = 200) 
开发者ID:seglo,项目名称:play-content-negotiation,代码行数:40,代码来源:ContentNegotiation.scala

示例3: HistorySerializer

//设置package包名称以及导入依赖的类
package daniel.zolnai.marathon.serializer

import daniel.zolnai.marathon.entity.{History, TriggerHistory}
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.json4s.JsonDSL._
import org.json4s.{CustomSerializer, _}


class HistorySerializer extends CustomSerializer[History](format => ( {
  case json: JValue =>
    implicit val formats = format
    val history = new History()
    history.lastSaved = (json \ "lastSaved").extractOpt[DateTime]
    history.triggerHistories = (json \ "triggerHistories").extract[List[TriggerHistory]]
    history
}, {
  case history: History =>
    implicit val formats = format
    val outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    def _fromDate(date: Option[DateTime]): Option[String] = {
      if (date.isEmpty) {
        None
      } else {
        Some(outputFormat.print(date.get))
      }
    }
    val result = ("lastSaved" -> _fromDate(history.lastSaved)) ~
      ("triggerHistories" -> Extraction.decompose(history.triggerHistories))
    result
})) 
开发者ID:dzolnai,项目名称:marathon-email-notifier,代码行数:32,代码来源:HistorySerializer.scala


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