• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala JArray类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

} 
开发者ID:orendain,项目名称:sraw,代码行数:45,代码来源:RedditExtractor.scala


示例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
} 
开发者ID:NDLANO,项目名称:article-api,代码行数:34,代码来源:SearchableLanguageFormats.scala


示例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)
  }
} 
开发者ID:rcoh,项目名称:lasic,代码行数:46,代码来源:Renderer.scala


示例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)))
    }
    ))

  }
} 
开发者ID:criteo,项目名称:slab,代码行数:31,代码来源:BoardConfig.scala


示例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
  }

} 
开发者ID:criteo,项目名称:slab,代码行数:50,代码来源:GraphiteMetric.scala


示例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
} 
开发者ID:NDLANO,项目名称:listing-api,代码行数:56,代码来源:SearchableLanguageFormats.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala ByteArrayDeserializer类代码示例发布时间:2022-05-23
下一篇:
Scala ConfigValueFactory类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap