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