本文整理汇总了Scala中org.json4s.JsonAST.JArray类的典型用法代码示例。如果您正苦于以下问题:Scala JArray类的具体用法?Scala JArray怎么用?Scala JArray使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JArray类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: RedditExtractor
//设置package包名称以及导入依赖的类
package com.orendain.sraw.util
import org.json4s.JsonAST.JValue
import org.json4s.JsonAST.JArray
import com.orendain.sraw.model._
import com.orendain.sraw.model.extract._
object RedditExtractor extends HasExtractor[RedditEntity] {
val extractor = new Extractor[RedditEntity] {
def canExtract(json: JValue) = true
def extract(json: JValue) =
extractors.map(_.extractor).filter(_.canExtract(json)).head.extract(json)
}
// TODO: fix
val extractors = Seq(
Comment,
LoggedInUser,
User,
Submission,
Message,
Subreddit,
Trophy,
GenericObject,
GenericEntity
)
}
object RedditExtractors {
object CommentTree extends HasExtractor[Listing[Comment]] {
val extractor = new Extractor[Listing[Comment]] {
def canExtract(json: JValue) = RedditJson.unwrap(json) match {
case JArray(arr) => Listing.extractor(Comment).extractor.canExtract(arr.tail.head)
}
def extract(json: JValue) = RedditJson.unwrap(json) match {
case JArray(arr) => Listing.extractor(Comment).extractor.extract(arr.tail.head)
}
}
}
}
示例2: SearchableLanguageValueSerializer
//设置package包名称以及导入依赖的类
package no.ndla.articleapi.model.search
import org.json4s.JsonAST.{JArray, JField, JObject, JString}
import org.json4s.{CustomSerializer, MappingException}
import no.ndla.articleapi.model.domain.Language.UnknownLanguage
class SearchableLanguageValueSerializer extends CustomSerializer[SearchableLanguageValues](format => ( {
case JObject(items) => SearchableLanguageValues(items.map {
case JField(name, JString(value)) => LanguageValue(Some(name), value)
})
}, {
case x: SearchableLanguageValues =>
JObject(x.languageValues.map(languageValue => JField(languageValue.lang.getOrElse(UnknownLanguage), JString(languageValue.value))).toList)
}))
class SearchableLanguageListSerializer extends CustomSerializer[SearchableLanguageList](format => ( {
case JObject(items) => {
SearchableLanguageList(items.map{
case JField(name, JArray(fieldItems)) => LanguageValue(Some(name), fieldItems.map{
case JString(value) => value
case x => throw new MappingException(s"Cannot convert $x to SearchableLanguageList")
}.to[Seq])
})
}
}, {
case x: SearchableLanguageList =>
JObject(x.languageValues.map(languageValue => JField(languageValue.lang.getOrElse(UnknownLanguage), JArray(languageValue.value.map(lv => JString(lv)).toList))).toList)
}))
object SearchableLanguageFormats {
val JSonFormats = org.json4s.DefaultFormats + new SearchableLanguageValueSerializer + new SearchableLanguageListSerializer
}
示例3: FieldDoesNotExistError
//设置package包名称以及导入依赖的类
package com.github.rcoh.query.render
import org.json4s.JsonAST.{JArray, JObject, JValue}
import com.github.rcoh.query.lang.Query
import com.github.rcoh.query.loaders.{ConcreteLoadable, ExposeAlways, Loadable, LoadableField}
trait RenderError
case class FieldDoesNotExistError(field: String) extends Exception(s"Field $field did not exist in object.")
case class ObjectNotIndexable(field: String, obj: JValue) extends Exception(s"Requested $field but $obj is not indexable.")
object Renderer {
def render[A: Loadable](obj: A, query: Query): JValue = {
val concreteLoadable = Loadable.toConcreteLoadable(obj)
val loadResult = concreteLoadable.load
val fieldMap = loadResult match {
case Right(jValue) if query.fields.nonEmpty => throw ObjectNotIndexable(query.fields.head._1, jValue)
case Right(jValue) => return jValue
case Left(fields) => fields
}
val requestedFields = query.fields.map { case (fieldName, subQuery) =>
val field = fieldMap.getOrElse(fieldName, throw FieldDoesNotExistError(fieldName))
field.loader() match {
case Left(loadable) => fieldName -> render(loadable, subQuery)
case Right(loadables) => fieldName -> renderList(loadables, subQuery)
}
}
val alwaysExposed = (fieldMap -- query.fields.keySet).collect {
case (fieldName, LoadableField(ExposeAlways, loader)) => loader() match {
case Left(loadable) => fieldName -> render(loadable, Query.NoQuery)
case Right(loadables) => fieldName -> renderList(loadables, Query.NoQuery)
}
}
val resultingMap = requestedFields ++ alwaysExposed
JObject(resultingMap.toList: _*)
}
private def renderList(loadables: Iterable[ConcreteLoadable], query: Query): JArray = {
val paged = loadables.drop(query.paging.offset).take(query.paging.max)
val renderedObjects = paged.map(render(_, query))
JArray(renderedObjects.toList)
}
}
示例4: BoardConfig
//设置package包名称以及导入依赖的类
package com.criteo.slab.app
import com.criteo.slab.core.{Box, Layout}
import com.criteo.slab.utils.Jsonable
import org.json4s.JsonAST.{JArray, JString}
import org.json4s.{CustomSerializer, Serializer}
private[slab] case class BoardConfig(
title: String,
layout: Layout,
links: Seq[(Box[_], Box[_])] = Seq.empty
)
object BoardConfig {
implicit object ToJSON extends Jsonable[BoardConfig] {
override val serializers: Seq[Serializer[_]] =
implicitly[Jsonable[Box[_]]].serializers ++
implicitly[Jsonable[Layout]].serializers :+
LinkSer
object LinkSer extends CustomSerializer[Box[_] Tuple2 Box[_]](_ => ( {
case _ => throw new NotImplementedError("Not deserializable")
}, {
case (Box(title1, _, _, _, _), Box(title2, _, _, _, _)) => JArray(List(JString(title1), JString(title2)))
}
))
}
}
示例5: DataPoint
//设置package包名称以及导入依赖的类
package com.criteo.slab.lib.graphite
import com.criteo.slab.utils.Jsonable
import org.json4s.JsonAST.{JArray, JDouble, JInt, JNull}
import org.json4s.{CustomSerializer, Serializer}
private[slab] case class DataPoint(value: Option[Double], timestamp: Long)
object DataPoint {
implicit object ToJSON extends Jsonable[DataPoint] {
override val serializers: Seq[Serializer[_]] = List(Ser)
object Ser extends CustomSerializer[DataPoint](_ => ( {
case JArray(JDouble(value) :: JInt(date) :: Nil) =>
DataPoint(Some(value), date.toLong)
case JArray(JNull :: JInt(date) :: Nil) =>
DataPoint(None, date.toLong)
}, {
case DataPoint(value, date) =>
val v = value match {
case Some(v) => JDouble(v)
case None => JNull
}
JArray(
List(
v,
JInt(date)
)
)
}
))
}
}
private[slab] case class GraphiteMetric(
target: String,
datapoints: List[DataPoint]
)
object GraphiteMetric {
implicit object ToJSON extends Jsonable[GraphiteMetric] {
override val serializers: Seq[Serializer[_]] = implicitly[Jsonable[DataPoint]].serializers
}
}
示例6: SearchableLanguageValueSerializer
//设置package包名称以及导入依赖的类
package no.ndla.listingapi.model.domain.search
import no.ndla.listingapi.model.domain.Label
import no.ndla.listingapi.model.domain.search.Language.UnknownLanguage
import org.json4s.{CustomSerializer, MappingException}
import org.json4s.JsonAST.{JArray, JField, JObject, JString}
class SearchableLanguageValueSerializer extends CustomSerializer[SearchableLanguageValues](_ => ( {
case JObject(items) => SearchableLanguageValues(items.map {
case JField(name, JString(value)) => LanguageValue(Some(name), value)
})
}, {
case x: SearchableLanguageValues =>
JObject(x.languageValues.map(languageValue => JField(languageValue.lang.getOrElse(UnknownLanguage), JString(languageValue.value))).toList)
}))
class SearchableLanguageListSerializer extends CustomSerializer[SearchableLanguageList](format => ( {
case JObject(items) => {
val res = items.map {
case JField(name, JArray(fieldItems)) => LanguageValue(Some(name), fieldItems.map {
case JObject(Seq(JField("type", JString(type_)), JField("labels", JArray(x)))) =>
val labels = x.map {
case JString(label) => label
case unknownType => throw new MappingException(s"Cannot convert $unknownType to Label")
}
Label(Some(type_), labels)
case JObject(Seq(JField("labels", JArray(x)))) =>
val labels = x.map {
case JString(label) => label
case unknownType => throw new MappingException(s"Cannot convert $unknownType to Label")
}
Label(None, labels)
case x => throw new MappingException(s"Cannot convert $x to SearchableLanguageList")
}.to[Seq])
}
SearchableLanguageList(res)
}
}, {
case x: SearchableLanguageList =>
JObject(x.languageValues.map(languageValue =>
JField(languageValue.lang.getOrElse(UnknownLanguage), JArray(languageValue.value.map(lv => {
lv.`type` match {
case Some(typ) => JObject(JField("type", JString(typ)), JField("labels", JArray(lv.labels.map(l => JString(l)).toList)))
case None => JObject(JField("labels", JArray(lv.labels.map(l => JString(l)).toList)))
}
}).toList))
).toList)
}))
object SearchableLanguageFormats {
val JSonFormats = org.json4s.DefaultFormats + new SearchableLanguageValueSerializer + new SearchableLanguageListSerializer
}