本文整理汇总了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
}
}
示例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)
示例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
}))