当前位置: 首页>>代码示例>>Scala>>正文


Scala compact类代码示例

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


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

示例1: 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

示例2: 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.compact类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。