本文整理汇总了Scala中org.json4s.JsonAST.JObject类的典型用法代码示例。如果您正苦于以下问题:Scala JObject类的具体用法?Scala JObject怎么用?Scala JObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MultiSubredditStub
//设置package包名称以及导入依赖的类
package com.orendain.sraw.model
import org.json4s.JsonAST.JObject
class MultiSubredditStub(val json: JObject) extends MultiSubreddit {
val icon_img = valString("icon_img")
val key_color = valString("icon_img")
val header_img = valOp[String]("header_img")
val user_is_banned = valBoolean("user_is_banned")
val user_is_contributor = valBoolean("user_is_contributor")
val user_is_moderator = valBoolean("user_is_moderator")
val header_size = valOp[List[Int]]("header_size")
val fullname = valString("name")
val icon_size = valOp[String]("icon_size")
val display_name = valString("display_name")
val name = (json \ "name").toString
}
示例2: Post
//设置package包名称以及导入依赖的类
package com.orendain.sraw.model
import org.json4s.JsonAST.JObject
import com.orendain.sraw.model.attribute.Created
import com.orendain.sraw.model.extract._
object Post extends HasExtractor[Post] {
val extractor = new ObjectExtractor[Post] {
def canExtract(json: JObject) = true
def extract(json: JObject) = {
if (PublicPost.extractor.canExtract(json)) PublicPost.extractor.extract(json)
else if (Message.extractor.canExtract(json)) Message.extractor.extract(json)
else throw new ExtractionException("Extraction failed. Input does not conform to a PublicPost.")
}
}
}
abstract class Post(override val json: JObject) extends RedditThing with Created {
// Shared by all
val kind = json.values("kind").toString
// Used by Message, Comment, Submission
val author = valString("author")
val distinguished = valOp[String]("distinguished")
val id = valString("id")
val name = valString("name")
val subreddit = valOp[String]("subreddit")
// Created
val created = valDouble("created")
val createdUTC = valDouble("created_utc")
}
示例3: 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
}
示例4: OptionCellSerializer
//设置package包名称以及导入依赖的类
package com.ntsdev.connect4.wire
import com.ntsdev.connect4.model.Cell
import org.json4s.CustomSerializer
import org.json4s.JsonAST.{JField, JObject, JString}
class OptionCellSerializer extends CustomSerializer[Option[Cell]](format => (
{
case JObject(JField("cell", JString(s)) :: Nil) =>
Cell.fromString(s)
},
{
case x: Option[_] =>
if(x.isDefined) {
val value = x.get.toString
JObject(JField("cell", JString(value)))
} else {
JObject(JField("cell", JString("[-]")))
}
}
))
示例5: User
//设置package包名称以及导入依赖的类
package io.corbel.sdk.iam
import org.json4s.JsonAST.JObject
case class User(
id: Option[String] = None,
domain: Option[String] = None,
username: Option[String] = None,
email: Option[String] = None,
password: Option[String] = None,
firstName: Option[String] = None,
lastName: Option[String] = None,
scopes: Option[Seq[String]] = None,
groups: Option[Seq[String]] = None,
properties: Option[JObject] = None,
phoneNumber: Option[String] = None
)
示例6: MongoQueryParser
//设置package包名称以及导入依赖的类
package service
import org.json4s.JsonAST.JBool
import org.json4s.JsonAST.JDecimal
import org.json4s.JsonAST.JDouble
import org.json4s.JsonAST.JInt
import org.json4s.JsonAST.JObject
import org.json4s.JsonAST.JString
import org.json4s.native.JsonMethods
import org.mongodb.scala.bson.collection.immutable.Document
class MongoQueryParser {
def parse(query: String): Document = {
val json = JsonMethods.parse(query, useBigDecimalForDouble = true)
val mongoQuery: List[Document] =
for {
JObject(child) <- json
(fieldName, value) <- child
} yield {
value match {
case JString(s) => Document(fieldName -> s)
case JDouble(num) => Document(fieldName -> num)
case JDecimal(num) => Document(fieldName -> num)
case JInt(num) => Document(fieldName -> num.intValue())
case JBool(bool) => Document(fieldName -> bool)
case _ => throw new IllegalArgumentException(f"Error when processing field '$fieldName%s': Unsupported type: '$value'")
}
}
mongoQuery.reduce(_ ++ _)
}
}
示例7: MqttSinkAppA
//设置package包名称以及导入依赖的类
package org.apress.prospark
import java.nio.charset.StandardCharsets
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.streaming.Seconds
import org.apache.spark.streaming.StreamingContext
import org.eclipse.paho.client.mqttv3.MqttClient
import org.eclipse.paho.client.mqttv3.MqttMessage
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import org.json4s.DefaultFormats
import org.json4s.JField
import org.json4s.JsonAST.JObject
import org.json4s.jvalue2extractable
import org.json4s.jvalue2monadic
import org.json4s.native.JsonMethods.parse
import org.json4s.string2JsonInput
object MqttSinkAppA {
def main(args: Array[String]) {
if (args.length != 3) {
System.err.println(
"Usage: MqttSinkApp <appname> <outputBrokerUrl> <topic>")
System.exit(1)
}
val Seq(appName, outputBrokerUrl, topic) = args.toSeq
val conf = new SparkConf()
.setAppName(appName)
.setJars(SparkContext.jarOfClass(this.getClass).toSeq)
val batchInterval = 10
val ssc = new StreamingContext(conf, Seconds(batchInterval))
HttpUtils.createStream(ssc, url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22IBM,GOOG,MSFT,AAPL,FB,ORCL,YHOO,TWTR,LNKD,INTC%22)%0A%09%09&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env",
interval = batchInterval)
.flatMap(rec => {
val query = parse(rec) \ "query"
((query \ "results" \ "quote").children).map(rec => JObject(JField("Timestamp", query \ "created")).merge(rec))
})
.map(rec => {
implicit val formats = DefaultFormats
rec.children.map(f => f.extract[String]) mkString ","
})
.foreachRDD { rdd =>
val client = new MqttClient(outputBrokerUrl, MqttClient.generateClientId(), new MemoryPersistence())
client.connect()
rdd.foreach(rec => client.publish(topic, new MqttMessage(rec.getBytes(StandardCharsets.UTF_8))))
client.disconnect()
client.close()
}
ssc.start()
ssc.awaitTermination()
}
}
示例8: UpdateSerializer
//设置package包名称以及导入依赖的类
package ru.finagram.api.json
import org.json4s.JsonAST.JObject
import org.json4s.JsonDSL._
import org.json4s.{ DefaultFormats, Extraction, Formats, JValue, Serializer, TypeInfo }
import ru.finagram.api.{ CallbackQueryUpdate, MessageUpdate, Update }
import ru.finagram.!!!
object UpdateSerializer extends Serializer[Update] {
private val UpdateClass = classOf[Update]
override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Update] = {
case (TypeInfo(UpdateClass, _), json: JObject) =>
json.values match {
case v if v.contains("message") =>
json.extract[MessageUpdate]
case v if v.contains("callbackQuery") =>
json.extract[CallbackQueryUpdate]
case v => !!!(s"Not implemented deserialization for $v")
}
}
override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case u: MessageUpdate =>
("update_id" -> u.updateId) ~~ ("message" -> json(u.message))
case u: CallbackQueryUpdate =>
("update_id" -> u.updateId) ~~ ("callback_query" -> json(u.callbackQuery))
}
private def json(obj: AnyRef): JValue = {
implicit val formats = DefaultFormats
Extraction.decompose(obj).snakizeKeys
}
}
示例9: TelegramResponseSerializer
//设置package包名称以及导入依赖的类
package ru.finagram.api.json
import org.json4s.JsonAST.JObject
import org.json4s.JsonDSL._
import org.json4s._
import ru.finagram.api._
object TelegramResponseSerializer extends Serializer[TelegramResponse] {
private val ResponseClass = classOf[TelegramResponse]
override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), TelegramResponse] = {
case (TypeInfo(ResponseClass, _), json: JObject) =>
val ok = (json \ "ok").extract[Boolean]
if (ok) {
json \ "result" match {
case _: JArray =>
json.extract[Updates]
case obj: JObject if obj.values.contains("fileId") =>
json.extract[FileResponse]
case obj: JObject if obj.values.contains("id") =>
json.extract[MeResponse]
case _ => ???
}
} else {
json.extract[TelegramException]
}
}
override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case me: MeResponse =>
("ok" -> true) ~~ ("result" -> json(me.result))
case u: Updates =>
("ok" -> true) ~~ ("result" -> json(u.result))
case u: FileResponse =>
("ok" -> true) ~~ ("result" -> json(u.result))
case e: TelegramException =>
if (e.errorCode.isEmpty)
("ok" -> false) ~ ("description" -> e.description)
else
("ok" -> false) ~ ("description" -> e.description) ~ ("errorCode" -> e.errorCode.get)
}
private def json(obj: AnyRef): JValue = {
import ru.finagram.api.json.Implicit.formats
Extraction.decompose(obj)
}
}
示例10: KeyboardMarkupSerializer
//设置package包名称以及导入依赖的类
package ru.finagram.api.json
import org.json4s.JsonAST.JObject
import org.json4s.{ Formats, JValue, Serializer, TypeInfo }
import ru.finagram.api.{ InlineKeyboardMarkup, KeyboardMarkup, ReplyKeyboardMarkup }
object KeyboardMarkupSerializer extends Serializer[KeyboardMarkup] {
private val KeyboardMarkupClass = classOf[KeyboardMarkup]
override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), KeyboardMarkup] = {
case (TypeInfo(KeyboardMarkupClass, _), json: JObject) =>
json.values match {
case v if v.contains("keyboard") =>
json.extract[ReplyKeyboardMarkup]
case v if v.contains("inline_keyboard") =>
json.extract[InlineKeyboardMarkup]
}
}
override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = PartialFunction.empty
}
示例11: EchoEndpoint
//设置package包名称以及导入依赖的类
package streaming
import java.time.Instant
import javax.websocket.server.ServerEndpoint
import javax.websocket.{OnMessage, Session}
import akka.actor.ActorSystem
import akka.testkit.{TestActorRef, TestKitBase, TestProbe}
import co.coinsmith.kafka.cryptocoin.streaming.{Connect, WebsocketActor}
import org.glassfish.tyrus.test.tools.TestContainer
import org.json4s.JsonAST.JObject
import org.json4s.JsonDSL.WithBigDecimal._
import org.json4s.jackson.JsonMethods._
import org.scalatest.FlatSpecLike
@ServerEndpoint("/WebsocketActorSpec/echoEndpoint")
class EchoEndpoint {
@OnMessage
def onMessage(message: String, session: Session) = message
}
class WebsocketActorSpec extends TestContainer with TestKitBase with FlatSpecLike {
implicit lazy val system = ActorSystem("WebsocketActorSpecSystem")
val serverEndpoint = new EchoEndpoint
val uri = getURI(serverEndpoint.getClass)
"WebsocketActor" should "forward received messages to receiver" in {
val server = startServer(serverEndpoint.getClass)
val actorRef = TestActorRef(WebsocketActor.props(uri))
val probe = TestProbe("probe")
val json = List.empty
val expected = JObject(json)
actorRef ! probe.ref
actorRef ! Connect
actorRef ! compact(render(json))
probe.expectMsgPF() {
case (_: Instant, expected) => true
case _ => false
}
stopServer(server)
}
}
示例12: 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)
}
}
示例13: FieldNamesMappingUtils
//设置package包名称以及导入依赖的类
package com.datawizards.sparklocal.impl.scala.dataset.io
import com.datawizards.dmg.dialects.Dialect
import com.datawizards.dmg.metadata.MetaDataExtractor
import com.datawizards.sparklocal.dataset.io.JSONDialect
import org.json4s.JValue
import org.json4s.JsonAST.JObject
import scala.reflect.runtime.universe.TypeTag
object FieldNamesMappingUtils {
def constructFieldNameMapping[T: TypeTag](dialect: Dialect, fromOriginal: Boolean=true): Map[String, String] = {
val classTypeMetaData = MetaDataExtractor.extractClassMetaDataForDialect[T](dialect)
classTypeMetaData
.fields
.map{ f =>
if(fromOriginal) f.originalFieldName -> f.fieldName
else f.fieldName -> f.originalFieldName
}
.toMap
}
def changeJsonObjectFieldNames[T: TypeTag](value: JValue, fromOriginal: Boolean): JValue = value match {
case JObject(fields) =>
val fieldNameMapping = FieldNamesMappingUtils.constructFieldNameMapping[T](JSONDialect, fromOriginal)
JObject(
fields.map{case (fieldName, fieldValue) =>
(fieldNameMapping.getOrElse(fieldName, fieldName), fieldValue)
}
)
case other => other
}
}
示例14: KilometerClient
//设置package包名称以及导入依赖的类
package io.kilometer.client
import java.net.URL
import io.kilometer.client.response.ResponseLogger
import io.kilometer.collector._
import org.json4s.Extraction
import org.json4s.JsonAST.JObject
class KilometerClient[T](collector: Collector[T]) extends ResponseLogger {
implicit val formats = org.json4s.DefaultFormats
private def urlBuilder(path: String) = new URL("https", collector.hostAddress, collector.hostPort, path)
private def changeUserProperty(user: String, property: String, value: Long, stateChange: String = "increase") = {
collector.post(urlBuilder(s"/users/$user/properties/$property/$stateChange/$value"), JObject())
}
def addEvent(event: KilometerEvent) = {
collector.post(urlBuilder("/events"), Extraction.decompose(event))
}
def updateUserProperties(user: String, properties: Map[String, String]) = {
collector.put(urlBuilder(s"/users/$user/properties"), Extraction.decompose(properties))
}
def increaseUserProperty(user: String, property: String, value: Long) = {
changeUserProperty(user, property, value)
}
def decreaseUserProperty(user: String, property: String, value: Long) = {
changeUserProperty(user, property, value, "decrease")
}
}
case class AnonEvent(event_name: String, event_properties: Map[String, String]) extends KilometerEvent
case class IdentifiedEvent(user_id: String, event_name: String, event_properties: Map[String, String]) extends IdentifiedKilometerEvent
示例15: KafkaEventBus
//设置package包名称以及导入依赖的类
package io.corbel.event.kafka
import akka.NotUsed
import akka.actor.ActorSystem
import akka.kafka.scaladsl.{Consumer, Producer}
import akka.kafka.{ConsumerSettings, ProducerMessage, ProducerSettings, Subscriptions}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import grizzled.slf4j.Logging
import io.corbel.event.{Event, EventBus, SubscriptionProperties}
import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.serialization.{ByteArrayDeserializer, ByteArraySerializer, StringDeserializer, StringSerializer}
import org.json4s.DefaultFormats
import org.json4s.JsonAST.JObject
import org.json4s.native.Serialization._
class KafkaEventBus(kafkaServers: String)(implicit system: ActorSystem) extends EventBus with Logging {
implicit val materializer = ActorMaterializer()
implicit val format = DefaultFormats
val producerSettings = ProducerSettings(system, new ByteArraySerializer, new StringSerializer)
.withBootstrapServers(kafkaServers)
val consumerSettings = ConsumerSettings(system, new ByteArrayDeserializer, new StringDeserializer)
.withBootstrapServers(kafkaServers)
.withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest")
override def dispatcher(event: Event): Sink[JObject, NotUsed] = {
Flow[JObject]
.map(e => ProducerMessage.Message(new ProducerRecord[Array[Byte], String](event.id, write(e)), e))
.via(Producer.flow(producerSettings))
.map(result => {
val record = result.message.record
println(s"${record.topic}/${record.partition} ${result.offset}: ${record.value} (${result.message.passThrough}")
result
})
.recover({
case e: Throwable => e.printStackTrace()
})
.to(Sink.last)
}
override def subscribe(event: Event, props: SubscriptionProperties): Source[(Event, JObject), _] = {
val settings = props.toConsumerSettings(consumerSettings)
val subscription = Subscriptions.topics(event.id)
Consumer.atMostOnceSource(settings, subscription).map(message => (event, deserialize(message.value)))
}
def deserialize(data:String): JObject = {
read[JObject](data)
}
}