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

Scala parse类代码示例

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

本文整理汇总了Scala中org.json4s.jackson.JsonMethods.parse的典型用法代码示例。如果您正苦于以下问题:Scala parse类的具体用法?Scala parse怎么用?Scala parse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了parse类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: JsonExtraction

//设置package包名称以及导入依赖的类
package uk.gov.hmrc.utils.json

import java.net.{URI, URL}

import org.json4s.JsonAST.JString
import org.json4s.jackson.JsonMethods.parse
import org.json4s.{CustomSerializer, DefaultFormats, Formats, JNull}

object JsonExtraction {

  val formats = DefaultFormats + json4sDateTimeSerializer + json4sLocalDateSerializer + UriSerializer + UrlSerializer + Json4sIntToBigDecimalSerializer

  def apply[A](body: String)(implicit m: Manifest[A], formats: Formats = formats): A = extractResponse[A](body)

  private def extractResponse[A](body: String)(implicit m: Manifest[A], format: Formats = formats): A = Option(body) match {
    case Some(b) if b.length > 0 =>
      try {
        parse(b, useBigDecimalForDouble = true).extract
      } catch {
        case t: Throwable =>
          throw t
      }
    case _ => throw new IllegalArgumentException("A string value is required for transformation")
  }

  case object UriSerializer extends CustomSerializer[URI](format => ( {
    case JString(uri) => URI.create(uri)
    case JNull => null
  }, {
    case uri: URI => JString(uri.toString)
  }
  ))

  case object UrlSerializer extends CustomSerializer[URL](format => ( {
    case JString(url) => new URL(url)
    case JNull => null
  }, {
    case url: URL => JString(url.toString)
  }
  ))

} 
开发者ID:danewatts,项目名称:zap-automation,代码行数:43,代码来源:JsonExtraction.scala


示例2: 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)
  }
} 
开发者ID:zhranklin,项目名称:notice_crawler,代码行数:40,代码来源:Script.scala


示例3: JSON

//设置package包名称以及导入依赖的类
package se.joham.funrts.util

import org.json4s.{CustomSerializer, Extraction, ShortTypeHints}
import se.joham.funrts.model._
import Extraction.{decompose, extract}
import org.json4s.JsonAST.JValue
import se.gigurra.scalego.serialization.KnownSubTypes
import se.gigurra.scalego.serialization.json.JsonSerializer
import se.joham.funrts.model.FunRtsECS.{ECS, IdTypes}
import org.json4s.jackson.JsonMethods.{compact, parse}
import org.json4s.jackson.JsonMethods.{pretty => prty}


object JSON {

  def writeAst(ecs: ECS): JValue = ecsSerializer.SerializableOps(ecs).toJsonAst
  def    write(ecs: ECS, pretty: Boolean): String = ecsSerializer.SerializableOps(ecs).toJson(pretty)
  def writeAst(terrain: Terrain): JValue = decompose(terrain)
  def    write(terrain: Terrain, pretty: Boolean): String = if (pretty) prty(writeAst(terrain)) else compact(writeAst(terrain))

  def     readEcsAst(ecs: ECS, json: JValue): Unit = ecsSerializer.SerializableOps(ecs).appendJsonAst(json)
  def        readEcs(ecs: ECS, json: String): Unit = ecsSerializer.SerializableOps(ecs).appendJson(json)
  def readTerrainAst(json: JValue): Terrain = extract[Terrain](json)
  def    readTerrain(json: String): Terrain = readTerrainAst(parse(json))


  //////////////////////////////////////////////////////////////////////////////////////

  object TerrainSerializer extends CustomSerializer[Terrain](_ => ({
    case json => extract[TerrainSerializable](json).toTerrain },{
    case terrain: Terrain => decompose(new TerrainSerializable(terrain))
  }))

  case class TerrainSerializable(nx: Int, ny: Int, base64Tiles: String) {
    def this(terrain: Terrain) = this(terrain.nx, terrain.ny, Base64.encodeString(terrain.tiles))
    def toTerrain: Terrain = Terrain(nx, ny, Base64.decodeBinary(base64Tiles))
  }

  implicit lazy val jsonFormats = org.json4s.DefaultFormats + TerrainSerializer + ShortTypeHints(Action.classes)
  val ecsSerializer = new JsonSerializer[IdTypes](
    knownSubtypes = KnownSubTypes.fromShortClassName(types = Action.classes:_*),
    jsonFormats = jsonFormats
  )

} 
开发者ID:GiGurra,项目名称:fun-rts,代码行数:46,代码来源:JSON.scala


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

} 
开发者ID:uts-cic,项目名称:athanor-server,代码行数:60,代码来源:Athanor.scala


示例5: Hi

//设置package包名称以及导入依赖的类
import scalaj.http._
import org.json4s._
import org.json4s.jackson.JsonMethods.parse

object Hi {

  

  def getOrganizationEvents(org: String): HttpResponse[String] = {
    Http("https://api.github.com/orgs/"+org+"/events").method("GET").asString
  }

  def getLinks(gitHubResp: HttpResponse[String]) = {
    val links = gitHubResp.headers("Link")(0).split(",")
    (links(0), links(1))
  }

  def asJson(json: String) = parse(json)

  def main(args: Array[String]) = {
    println("Hi!")
    val response = getOrganizationEvents("Telefonica")

    println(">>>>>>>>>> "+response.headers)
    println()
    println("b:::"+getLinks(response).toString())
    println()
    println()
    implicit val formats = org.json4s.DefaultFormats
    //println(parse(response.body).extract[Map[String, Any]])

    val activity = asJson(response.body)

    println(activity(0))
    println()
    println()
    println(activity(1))


  }
} 
开发者ID:javierarilos,项目名称:git_activity_monitor_scala,代码行数:42,代码来源:hw.scala


示例6: StreamRecord

//设置package包名称以及导入依赖的类
package com.bwsw.tstreamstransactionserver.netty.server.streamService


import com.sun.xml.internal.messaging.saaj.util.ByteInputStream
import com.bwsw.tstreamstransactionserver.rpc
import com.bwsw.tstreamstransactionserver.netty.server.streamService
import org.json4s._
import org.json4s.jackson.JsonMethods.{parse, pretty, render}

case class StreamRecord(key: StreamKey, stream: streamService.StreamValue)
  extends rpc.Stream
{
  override def id: Int = key.id
  override def name: String = stream.name
  override def partitions: Int = stream.partitions
  override def ttl: Long = stream.ttl
  override def description: Option[String] = stream.description
  override def zkPath: String = stream.zkPath.get

  def toBinaryJson: Array[Byte] = {
    implicit val formats = DefaultFormats
    pretty(render(Extraction.decompose(this))).getBytes
  }
}

object StreamRecord {
  def fromBinaryJson(bytes: Array[Byte]): StreamRecord = {
    implicit val formats = DefaultFormats
    parse(new ByteInputStream(bytes, bytes.length))
      .extract[StreamRecord]
  }
} 
开发者ID:bwsw,项目名称:tstreams-transaction-server,代码行数:33,代码来源:StreamRecord.scala


示例7: JsonSerializer

//设置package包名称以及导入依赖的类
package com.github.gigurra.scalego.serialization.json

import org.json4s.JsonAST.JValue
import org.json4s.{DefaultFormats, Extraction, Formats}
import org.json4s.jackson.JsonMethods.{compact, parse}
import org.json4s.jackson.JsonMethods.{pretty => prty}
import Extraction.{decompose, extract}
import com.github.gigurra.scalego.core.{ECS, IdTypes}
import com.github.gigurra.scalego.serialization.{ECSSerializer, IdTypeMapper, KnownSubTypes}
import ECSSerializer._


case class JsonSerializer[T_IdTypes <: IdTypes](knownSubtypes: KnownSubTypes = KnownSubTypes.empty,
                                                jsonFormats: Formats = DefaultFormats)
                                               (implicit systemIdMapper: IdTypeMapper[T_IdTypes#SystemId], entityIdMapper: IdTypeMapper[T_IdTypes#EntityId]) {

  private val serializer = new ECSSerializer[JValue, T_IdTypes](new JsonMapper[T_IdTypes](jsonFormats), knownSubtypes)

  implicit class SerializableOps(ecs: ECS[T_IdTypes]) {

    def toJsonAst: JValue = {
      val map = serializer.SerializableECSOpsWrite(ecs).toSerializable
      decompose(map)(jsonFormats)
    }

    def toJson(pretty: Boolean = false): String = {
      if (pretty) prty(toJsonAst)
      else compact(toJsonAst)
    }

    def appendJson(json: String): Unit = {
      appendJsonAst(parse(json))
    }

    def appendJsonAst(json: JValue): Unit = {
      val intermediaryFormat = extract[SerializableEcs[JValue]](json)(jsonFormats, implicitly[Manifest[SerializableEcs[JValue]]])
      serializer.SerializableECSOpsRead(ecs).append(intermediaryFormat)
    }
  }
} 
开发者ID:GiGurra,项目名称:scalego,代码行数:41,代码来源:JsonSerializer.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala ByteStreams类代码示例发布时间:2022-05-23
下一篇:
Scala ArrayBlockingQueue类代码示例发布时间: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