32

谁能用他们的 Scala 模块为 2.10 指出一个简单的 Jackson 序列化/反序列化示例?我正在寻找不需要逐字段注释或分配的基于反射的 JSON,它似乎可以做到这一点,但他们的文档不包含示例。

如果我有一个案例类:

case class Person(name:String, age:Int)
val person = Person("Fred", 65)

所以从他们的 github 自述文件中:

val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)

好的,现在……什么?如何将 p 转换为 JSON?

4

3 回答 3

46

试一试:

val person = Person("fred", 25)
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)    

val out = new StringWriter
mapper.writeValue(out, person)
val json = out.toString()
println(json)

val person2 = mapper.readValue(json, classOf[Person])
println(person2)

编辑

只要确保将该Person类声明为顶级,否则它将无法工作。

于 2013-06-06T16:31:08.067 回答
12

这是一个完整的例子:

package com.example.samples

import org.junit.Test
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.annotation.Bean
import java.io.File
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import java.io.StringWriter

class JacksonTest {

  @Test
  @throws[Exception] def jacksonTest(): Unit = {

    //case class Person(var name: String = "", var age: Int = 0)
    //case class Person(@Bean var name: String, @Bean var age: Int)
    case class Person( name: String, age: Int )

    val person = Person("fred", 25)
    val mapper = new ObjectMapper()
    mapper.registerModule(DefaultScalaModule)

    val out = new StringWriter
    mapper.writeValue(out, person)
    val json = out.toString()
    println(json)

    val person2 = mapper.readValue(json, classOf[Person])
    println(person2)
  }
}

但是,这在 mapper.readValue 处失败。

这是我的配置:

<!-- Jackson libraries for JSON marshalling and unmarshalling -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>

<!-- Jackson module for scala object marshalling and unmarshalling -->
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-scala_2.10</artifactId>
    <version>2.2.2</version>
</dependency>

<!-- Scala Compiler -->
<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-compiler</artifactId>
    <version>2.10.2</version>
</dependency>

任何想法为什么会失败?我看不出与工作示例有什么不同。

于 2013-10-04T02:05:04.067 回答
-1

我创建了一个通用函数来转换JSON String to Case Class/ObjectCase Class/Object to JSON String.

文件中所需的 SBT 依赖项build.sbt

name := "jackson-example"

scalaVersion := "2.12.11"

libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.10.1"
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.10.1"

JSON字符串到案例类/对象

def fromJson[T](json: String)(implicit m: Manifest[T]): Option[T] = {
    Try {
      lazy val mapper = new ObjectMapper() with ScalaObjectMapper
      mapper.registerModule(DefaultScalaModule)
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      mapper.readValue[T](json)
    } match {
      case Success(x) => Some(x)
      case Failure(err) => {
        logger.error("@@@@Got " + err.getMessage() + " while JSON to Object:--> " + json)
        None
      }
    }
  }

案例类/对象到 JSON 字符串

def toJson[T](obj: T)(implicit m: Manifest[T]): Option[String] = {
    Try {
      lazy val mapper = new ObjectMapper() with ScalaObjectMapper
      mapper.registerModule(DefaultScalaModule)
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      mapper.writeValueAsString(obj)
    } match {
      case Success(x) => Some(x)
      case Failure (err) => {
        logger.error("@@@@Got " + err.getMessage() + " while converting object  to JSON:--> " + obj)
        None
      }
    }
  }
于 2020-05-15T18:38:05.113 回答