本文整理汇总了Scala中org.json4s.jackson.Serialization.write类的典型用法代码示例。如果您正苦于以下问题:Scala write类的具体用法?Scala write怎么用?Scala write使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了write类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Item
//设置package包名称以及导入依赖的类
package com.github.vladminzatu.surfer.persist
import com.github.vladminzatu.surfer.Score
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{HttpClientBuilder}
import org.apache.spark.rdd.RDD
import org.json4s.jackson.Serialization.write
case class Item(item:String, score:Double)
class RestPersister extends Persister {
val url = "http://localhost:8080/items"
override def persist(scores: RDD[(String, Score)]): Unit = {
implicit val formats = org.json4s.DefaultFormats
val payload = write(scores.collect().sortWith((a,b) => a._2.value > b._2.value).map(x => Item(x._1, x._2.value)))
val client = HttpClientBuilder.create().build();
client.execute(postRequest(payload))
}
private def postRequest(payload: String): HttpPost = {
val post = new HttpPost(url)
post.setEntity(new StringEntity(payload))
post
}
}
示例2: Hello
//设置package包名称以及导入依赖的类
import java.io._
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.write
object Hello extends App {
println("Hello")
//case class Person(name: String, age: Option[Int])
//case class Finance(month: String, Income:String, Expenses: String, Profit: String)
case class Box(
env: String,
name: String,
domain: String,
layer: String,
service: String,
admin: String,
comments: String = "" )
// This line is neccesary for "write" to work ???
implicit val formats = DefaultFormats
//val john = Person("John", None)
//println(write(john))
val bufferedSource = io.Source.fromFile("./boxes.csv")
val jsonWriter = new PrintWriter(new File("/home/francisco/DevScala/workspace/funWithJson/boxes.json"))
var counter = 1
for (line <- bufferedSource.getLines) {
val cols = line.split(";").map(_.trim)
val box = cols.length match {
case 6 => Box(cols(0),cols(1),cols(2),cols(3),cols(4),cols(5))
case 7 => Box(cols(0),cols(1),cols(2),cols(3),cols(4),cols(5),cols(6))
}
jsonWriter.write("{\"index\":{\"_id\": \"" + counter + "\"}}" + "\n")
jsonWriter.write(write(box)+"\n")
counter += 1
println(write(box))
}
jsonWriter.close
bufferedSource.close
}
示例3: Script
//设置package包名称以及导入依赖的类
package com.zhranklin.notice.script
import com.zhranklin.notice.Logging
import org.json4s.jackson.JsonMethods.parse
import org.json4s.jackson.Serialization.{write, writePretty}
import scala.util.{Try, _}
object Script extends MessageJsonFormats with Logging {
if (System.getProperties.getProperty("logpath") != null){
val pwd = System.getenv("PWD")
log.i(s"logpath???, ??????, ?: $pwd")
}
val pretty = java.lang.Boolean.parseBoolean(System.getProperty("pretty", "true"))
log.i(s"pretty is set to: $pretty")
val respWrite: (Response) ? String = if (pretty) writePretty[Response] else write[Response]
val response = respWrite andThen println
def main(args: Array[String]): Unit = try {
work()
} catch {
case e: Exception ?
log.e("unknown_error", e)
response(err.UNKNOWN_ERROR)
}
def work() = {
val lines = Iterator.continually(Console.in.readLine).takeWhile(_ != null)
lines map handleCommand foreach response
}
def handleCommand(jsonStr: String): Response = Try(parse(jsonStr)).map(_.extract[Request]) match {
case Success(r) ? r.handle
case Failure(e) ?
log.e(e.getMessage, e)
err.JSON_FORMAT_ERR(e)
}
}
示例4: makePairs
//设置package包名称以及导入依赖的类
package ifttt
import ifttt.Scheduler.makeOrderedPair
lazy val pairs: Set[(String, String)] = {
def makePairs(emails: List[String]): List[(String, String)] = {
emails match {
case hd :: tl => tl.map(makeOrderedPair(hd, _)) ::: makePairs(tl)
case Nil => List()
}
}
// By sorting the pairs, we ensure that if two people are both part of two distinct teams, the pairs that contains
// their emails will be in the same order in both teams.
makePairs(members.toList).toSet
}
def toJson: String = {
import org.json4s._
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write
implicit val formats = Serialization.formats(NoTypeHints)
write(this)
}
}
示例5: HBaseWriterActivity
//设置package包名称以及导入依赖的类
package yumi.pipeline.activities.connectors.hbase
import org.apache.spark.sql.execution.datasources.hbase.{HBaseRelation, HBaseTableCatalog}
import org.json4s.NoTypeHints
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write
import yumi.pipeline.{Activity, Parameters, SessionData, YumiContext}
class HBaseWriterActivity (val parameters: Parameters) extends Activity{
val format = "org.apache.spark.sql.execution.datasources.hbase"
val mode = parameters
.getOptionAs[String]("mode")
.getOrElse("append")
val table = parameters.getAs[String]("table")
val as = parameters.getAs[String]("as")
val namespace = parameters.getAs[String]("namespace")
val rowKey = parameters.getAs[String]("rowKey")
val columns = parameters.getAs[List[Map[Any, Any]]]("columns")
val zookeeperQuorum = parameters.getAs[String]("zookeeper.quorum")
val zookeeperZnodeParent = parameters.getAs[String]("zookeeper.znode.parent")
override protected[this] def onInvoke(sessionData: SessionData)
(implicit yumiContext: YumiContext): SessionData = {
import yumiContext._
implicit val formats = Serialization.formats(NoTypeHints)
val columnString = columns.map(x=> write(x).stripPrefix("{").stripSuffix("}")).mkString(",")
val catalog = s"""{
|"table":{"namespace":"$namespace", "name":"$as"},
|"rowkey":"$rowKey",
|"columns":{$columnString}
|}""".stripMargin
val connection = s"""{
"hbase.zookeeper.quorum":"$zookeeperQuorum",
"zookeeper.znode.parent":"$zookeeperZnodeParent"
}
"""
dataFrameWriter.write(
dataFrame = sparkSession.table(table),
format = format,
mode = mode,
options = Map(HBaseTableCatalog.tableCatalog -> catalog,
HBaseRelation.HBASE_CONFIGURATION -> connection),
path = format
)
sessionData
}
}
示例6: HBaseReaderActivity
//设置package包名称以及导入依赖的类
package yumi.pipeline.activities.connectors.hbase
import org.apache.spark.sql.execution.datasources.hbase.{HBaseRelation, HBaseTableCatalog}
import org.json4s.NoTypeHints
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write
import yumi.pipeline.{Activity, Parameters, SessionData, YumiContext}
class HBaseReaderActivity(val parameters: Parameters) extends Activity {
val table = parameters.getString("table")
val asView = parameters.getString("as")
val namespace = parameters.getString("namespace")
val rowKey = parameters.getString("rowKey")
val columns = parameters.getAs[List[Map[Any, Any]]]("columns")
val zookeeperQuorum = parameters.getString("zookeeper.quorum")
val zookeeperZnodeParent = parameters.getString("zookeeper.znode.parent")
override protected[this] def onInvoke(sessionData: SessionData)
(implicit yumiContext: YumiContext): SessionData = {
import yumiContext._
implicit val formats = Serialization.formats(NoTypeHints)
val columnString = columns.map(x=> write(x).stripPrefix("{").stripSuffix("}")).mkString(",")
val catalog = s"""{
|"table":{"namespace":"$namespace", "name":"$table"},
|"rowkey":"$rowKey",
|"columns":{$columnString}
|}""".stripMargin
val connection = s"""{
"hbase.zookeeper.quorum":"$zookeeperQuorum",
"zookeeper.znode.parent":"$zookeeperZnodeParent"
}
"""
sparkSession
.read
.options(Map(HBaseTableCatalog.tableCatalog -> catalog,
HBaseRelation.HBASE_CONFIGURATION -> connection))
.format("org.apache.spark.sql.execution.datasources.hbase")
.load()
.createTempView(asView)
sessionData
}
}
示例7: Segment
//设置package包名称以及导入依赖的类
package com.crystal
package models
// Kinesis
import stores.KinesisStream
// JSON
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.{read, write}
// Rules
import rule_engine.rules.Rule
case class Segment(val name: String, rule: Rule) {
import Segment._
def containsUser(user: User): Boolean = {
rule.fulfilledBy(user.toMap)
}
def alreadyContainedUser(user: User): Boolean = {
val userWithoutNewEvents = User(user.id, user.persistedActions)
rule.fulfilledBy(userWithoutNewEvents.toMap)
}
def publishUserEntrance(user: User) = {
implicit val formats = Serialization.formats(ShortTypeHints(List(classOf[EnterSegmentEvent])))
val event = EnterSegmentEvent(user.id, name)
stream.put(user.id, write(event).getBytes("UTF-8"))
}
def publishUserExit(user: User) = {
implicit val formats = Serialization.formats(ShortTypeHints(List(classOf[ExitSegmentEvent])))
val event = ExitSegmentEvent(user.id, name)
stream.put(user.id, write(event).getBytes("UTF-8"))
}
}
object Segment {
val config = AppConfig.load().get
val stream = KinesisStream(config.outStreamName)
case class EnterSegmentEvent(user_id: String, segment_name: String)
case class ExitSegmentEvent(user_id: String, segment_name: String)
}
示例8: TaskStatus
//设置package包名称以及导入依赖的类
package com.pagerduty.scheduler.model
import java.time.Instant
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization.{read, write}
case class TaskStatus(numberOfAttempts: Int, completionResult: CompletionResult, nextAttemptAt: Option[Instant]) {
def isComplete = completionResult.isComplete
implicit val formats = DefaultFormats + new TaskKeyTimeSerializer + new CompletionResultSerializer
def toJson: String = write(this)
}
object TaskStatus {
val Dropped: TaskStatus = {
TaskStatus(numberOfAttempts = 0, CompletionResult.Dropped, nextAttemptAt = None)
}
val NeverAttempted: TaskStatus = {
TaskStatus(numberOfAttempts = 0, CompletionResult.Incomplete, nextAttemptAt = None)
}
def successful(numberOfAttempts: Int): TaskStatus = {
TaskStatus(numberOfAttempts, CompletionResult.Success, nextAttemptAt = None)
}
def failed(numberOfAttempts: Int): TaskStatus = {
TaskStatus(numberOfAttempts, CompletionResult.Failure, nextAttemptAt = None)
}
implicit val formats = DefaultFormats + new TaskKeyTimeSerializer + new CompletionResultSerializer
def fromJson(taskStatus: String): TaskStatus = read[TaskStatus](taskStatus)
}
示例9: TaskAttempt
//设置package包名称以及导入依赖的类
package com.pagerduty.scheduler.model
import java.time.Instant
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization.{read, write}
case class TaskAttempt(
attemptNumber: Int,
startedAt: Instant,
finishedAt: Instant,
taskResult: CompletionResult,
taskResultUpdatedAt: Instant,
exceptionClass: Option[String],
exceptionMessage: Option[String],
exceptionStackTrace: Option[String]) {
implicit val formats = DefaultFormats + new TaskKeyTimeSerializer + new CompletionResultSerializer
def toJson: String = write(this)
}
object TaskAttempt {
def apply(
attemptNumber: Int,
startedAt: Instant,
finishedAt: Instant,
taskResult: CompletionResult,
taskResultUpdatedAt: Instant,
exception: Option[Throwable]
): TaskAttempt = {
TaskAttempt(
attemptNumber,
startedAt,
finishedAt,
taskResult,
taskResultUpdatedAt,
exceptionClass = exception.map(_.getClass.getName),
exceptionMessage = exception.map(_.getMessage),
exceptionStackTrace = exception.map(_.getStackTraceString)
)
}
implicit val formats = DefaultFormats + new TaskKeyTimeSerializer + new CompletionResultSerializer
def fromJson(taskAttempt: String): TaskAttempt = read[TaskAttempt](taskAttempt)
}
示例10: OrderTaggingEventAdapter
//设置package包名称以及导入依赖的类
package poc.persistence.write
import java.nio.charset.Charset
import akka.actor.ExtendedActorSystem
import akka.event.Logging
import akka.persistence.journal.{Tagged, WriteEventAdapter}
import org.json4s.DefaultFormats
import poc.persistence.events.{OrderCancelled, OrderInitialized}
class OrderTaggingEventAdapter(actorSystem: ExtendedActorSystem) extends WriteEventAdapter {
private val log = Logging.getLogger(actorSystem, this)
override def toJournal(event: Any): Any = event match {
case e: OrderInitialized =>
log.debug("tagging OrderInitialized event")
Tagged(e, Set("UserEvent"))
case e: OrderCancelled =>
log.debug("tagged OrderCancelled event")
Tagged(e, Set("UserEvent"))
}
override def manifest(event: Any): String = ""
}
import akka.serialization.Serializer
class EventSerialization(actorSystem: ExtendedActorSystem) extends Serializer {
import org.json4s.jackson.Serialization.{read, write}
private val log = Logging.getLogger(actorSystem, this)
val UTF8: Charset = Charset.forName("UTF-8")
implicit val formats = DefaultFormats
// Completely unique value to identify this implementation of Serializer, used to optimize network traffic.
// Values from 0 to 16 are reserved for Akka internal usage.
// Make sure this does not conflict with any other kind of serializer or you will have problems
override def identifier: Int = 90020001
override def includeManifest = true
override def fromBinary(bytes: Array[Byte], manifestOpt: Option[Class[_]]): AnyRef = {
implicit val manifest = manifestOpt match {
case Some(x) => Manifest.classType(x)
case None => Manifest.AnyRef
}
val str = new String(bytes, UTF8)
val result = read(str)
result
}
override def toBinary(o: AnyRef): Array[Byte] = {
val jsonString = write(o)
val dat = write(o).getBytes(UTF8)
dat
}
}
示例11: Athanor
//设置package包名称以及导入依赖的类
package au.edu.utscic.athanorserver.athanor
import au.edu.utscic.athanorserver.data.RhetoricalImplicits
import au.edu.utscic.athanorserver.data.RhetoricalTypes._
import com.typesafe.config.{Config, ConfigFactory}
import com.xerox.jatanor.JAtanor
import org.json4s.JsonAST.JValue
import org.json4s.NoTypeHints
import org.json4s.jackson.JsonMethods.parse
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write
import scala.io.Source
object Athanor {
lazy val config: Config = ConfigFactory.load()
lazy val path: String = config.getString("app.path")
lazy val athanor = new JAtanor
lazy val handler = athanor.LoadProgram(program,"")
lazy val program: String = fullPath("apply.kif")
lazy val testSentence: String = fullPath("sentence.json")
lazy val demoFile:String = Source.fromFile(testSentence).getLines.mkString
def fullPath(file:String): String = {
s"$path/scripts/$file"
}
def parseJsonSentence(sent:String):ParsedSentence = {
import RhetoricalImplicits._
val json:JValue = parse(sent)
val lexNodes:LexicalNodes = json(0)
val constTree:ConstituentTree = json(1).extract[ConstituentTree]
val deps:Dependencies = json(2).extract[Dependencies]
(lexNodes,constTree,deps)
}
def parsedSentenceToJsonString(parsedSent:ParsedSentence):String = {
implicit val formats = Serialization.formats(NoTypeHints)
val l = write(parsedSent._1)
val c = write(parsedSent._2).replaceAll("""(\"(?=[0-9]))|((?<=[0-9])\")""","") //remove quotes around Ints for json
val d = write(parsedSent._3)
s"[$l,$c,$d]"
}
def analyseParsedSentence(parsed:ParsedSentence):List[String] = {
val jsonStr:String = parsedSentenceToJsonString(parsed)
this.analyseJson(jsonStr)
}
def analyseJson(json:String):List[String] = {
athanor.ExecuteFunctionArray(handler,"Apply",List(json).toArray).toList
}
}
示例12: PostDesc
//设置package包名称以及导入依赖的类
package com.biosimilarity.evaluator.importer.models
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.write
case class PostDesc(
src: String,
trgts: List[String],
label: String,
uid: String,
value: String
) {
implicit val formats = DefaultFormats
def toJson = write(this)
}
object PostDesc {
implicit val formats = DefaultFormats
def fromJson(json: String) = parse(json).extract[PostDesc]
}
示例13: DataSetDesc
//设置package包名称以及导入依赖的类
package com.biosimilarity.evaluator.importer.models
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.write
case class DataSetDesc(
agents: List[AgentDesc],
labels: Option[List[JObject]],
cnxns: Option[List[ConnectionDesc]],
posts: Option[List[PostDesc]]
) {
implicit val formats = DefaultFormats
def toJson = write(this)
}
object DataSetDesc {
implicit val formats = DefaultFormats
def fromJson(json: String) = parse(json).extract[DataSetDesc]
}
示例14: ExportImportResource
//设置package包名称以及导入依赖的类
package cjp.catalogue.resource
import cjp.catalogue.model.PersistedCatalogueProduct
import cjp.catalogue.service._
import cjp.catalogue.utils.Logging
import org.json4s.FieldSerializer
import org.json4s.FieldSerializer.ignore
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write
import play.api.http.MimeTypes
import play.api.mvc.{Action, Controller}
class ExportImportResource(exportImportService: ExportImportService, catalogueImportValidator: CatalogueImportValidator) extends Controller with Logging {
val productSerializer = FieldSerializer[PersistedCatalogueProduct](
ignore("_id") orElse ignore("name") orElse ignore("version") orElse ignore("created") orElse ignore("lastModified")
)
implicit val formats = CjpFormats.formats + productSerializer
def export = Action {
Ok(write(exportImportService.export)).as(MimeTypes.JSON)
}
def importCatalogue = Action(parse.tolerantText(parse.UNLIMITED)) {
implicit request =>
try {
val catalogueExport: CatalogueData = Serialization.read[CatalogueData](request.body)
val importValidation = catalogueImportValidator.validate(catalogueExport)
if (importValidation.isValid) {
exportImportService.importCatalogue(catalogueExport)
NoContent
} else {
BadRequest(write(importValidation))
}
} catch {
case e => {
error("error importing catalogue", e)
InternalServerError(e.getMessage)
}
}
}
}