Scala 2.10 + Json序列化和反序列化 [英] Scala 2.10 + Json serialization and deserialization

查看:537
本文介绍了Scala 2.10 + Json序列化和反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala 2.10似乎已经破坏了一些旧库(至少暂时是这样),例如Jerkson和lift-json.

目标可用性如下:

case class Person(name: String, height: String, attributes: Map[String, String], friends: List[String])

//to serialize
val person = Person("Name", ....)
val json = serialize(person)

//to deserialize
val sameperson = deserialize[Person](json)

但是我很难找到与Scala 2.10兼容的现有的生成和反序列化Json的良好方法.

Scala 2.10中是否有最佳实践来做到这一点?

解决方案

Jackson 是要处理的Java库JSON快速. Jerkson项目包装了Jackson,但似乎被放弃了.我已切换到Jackson的 Scala模块,以便对本机Scala数据结构进行序列化和反序列化. /p>

要获取它,请在您的build.sbt中添加以下内容:

libraryDependencies ++= Seq(
  "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.1.3",
   ...
)

然后您的示例将与以下Jackson包装程序一起使用(我从jackson-module-scala测试文件中提取了该示例):

import java.lang.reflect.{Type, ParameterizedType}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.`type`.TypeReference;

object JacksonWrapper {
  val mapper = new ObjectMapper()
  mapper.registerModule(DefaultScalaModule)

  def serialize(value: Any): String = {
    import java.io.StringWriter
    val writer = new StringWriter()
    mapper.writeValue(writer, value)
    writer.toString
  }

  def deserialize[T: Manifest](value: String) : T =
    mapper.readValue(value, typeReference[T])

  private [this] def typeReference[T: Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  private [this] def typeFromManifest(m: Manifest[_]): Type = {
    if (m.typeArguments.isEmpty) { m.erasure }
    else new ParameterizedType {
      def getRawType = m.erasure
      def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
      def getOwnerType = null
    }
  }
}

其他Scala 2.10 JSON选项包括基于《编程Scala》书的Twitter的 scala-json 这很简单,但要以性能为代价.还有 spray-json ,它使用播放的JSON处理看起来不错,但它并不容易与播放项目.

Scala 2.10 seems to have broken some of the old libraries (at least for the time being) like Jerkson and lift-json.

The target usability is as follows:

case class Person(name: String, height: String, attributes: Map[String, String], friends: List[String])

//to serialize
val person = Person("Name", ....)
val json = serialize(person)

//to deserialize
val sameperson = deserialize[Person](json)

But I'm having trouble finding good existing ways of generating and deserializing Json that work with Scala 2.10.

Are there best practice ways of doing this in Scala 2.10?

解决方案

Jackson is a Java library to process JSON fast. The Jerkson project wraps Jackson, but appears to be abandoned. I've switched to Jackson's Scala Module for serialization and deserialization to native Scala data structures.

To get it, include the following in your build.sbt:

libraryDependencies ++= Seq(
  "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.1.3",
   ...
)

Then your examples will work verbatim with the following Jackson wrapper (I extracted it from jackson-module-scala test files):

import java.lang.reflect.{Type, ParameterizedType}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.`type`.TypeReference;

object JacksonWrapper {
  val mapper = new ObjectMapper()
  mapper.registerModule(DefaultScalaModule)

  def serialize(value: Any): String = {
    import java.io.StringWriter
    val writer = new StringWriter()
    mapper.writeValue(writer, value)
    writer.toString
  }

  def deserialize[T: Manifest](value: String) : T =
    mapper.readValue(value, typeReference[T])

  private [this] def typeReference[T: Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  private [this] def typeFromManifest(m: Manifest[_]): Type = {
    if (m.typeArguments.isEmpty) { m.erasure }
    else new ParameterizedType {
      def getRawType = m.erasure
      def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
      def getOwnerType = null
    }
  }
}

Other Scala 2.10 JSON options include Twitter's scala-json based on the Programming Scala book--it's simple, at the cost of performance. There is also spray-json, which uses parboiled for parsing. Finally, Play's JSON handling looks nice, but it does not easily decouple from the Play project.

这篇关于Scala 2.10 + Json序列化和反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆