Jackson Scala模块的小例子?

Gre*_*reg 30 json scala jackson

任何人都可以指出我用他们的Scala模块2.10进行杰克逊序列化/反序列化的简单例子吗?我正在寻找基于反射的JSON,不需要逐个字段的注释或赋值,它似乎可以做到这一点,但他们的文档不包含任何示例.

如果我有一个案例类:

case class Person(name:String, age:Int)
val person = Person("Fred", 65)
Run Code Online (Sandbox Code Playgroud)

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

val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
Run Code Online (Sandbox Code Playgroud)

好的,现在是什么......?如何将p转换为/从JSON转换?

cmb*_*ter 38

试一试:

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)
Run Code Online (Sandbox Code Playgroud)

编辑

请务必将该Person类声明为顶级,否则将无效.

  • 我只是尝试了这个,但没有运气 - readValue 调用失败,并显示: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize Class com.example.JacksonTest$Person$3 (of type local/anonymous) as a Bean。我缺少什么?Person 可以完全声明为: case class Person( name:String,age:Int ) 吗? (2认同)
  • @Blankman,如果我错了,请纠正我,但是 Java 中的运行时注释是通过反射提供的,因此使用它们不会帮助您避免反射。有一些基于宏的 scala json 框架可以通过宏生成 ser/deser 代码并避免反射。我认为 Play 和 Scala Pickling 都有基于宏的 json 处理,而且可能还有更多。 (2认同)
  • @DeanHiller,解决方案工作正常.我的猜测是user48956,我唯一可以看到有问题的,没有将Person case类声明为顶级类,而是将其嵌套在另一个类/对象中.你不能这样做,因为我不相信需要访问的东西(构造函数).如果你宣布Person为最高级别的东西工作得很好(或者至少他们在一年前我发布这个答案时做得很好).不确定这真的值得投票...... (2认同)

use*_*956 9

这是一个完整的例子:

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,这在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>
Run Code Online (Sandbox Code Playgroud)

任何想法为什么失败?我看不出与工作实例的区别.

  • 好吧,我不得不将我的case类移到我的类和其他函数之外,所以它是一个完整的顶级类而且由于某种原因修复了这个问题. (4认同)
  • 将case类移到外面的原因是因为在测试中定义case类时,它实际上是一个内部类.这意味着它需要引用父类,这就是事情变得复杂的问题(在java中使用内部类时存在类似的问题).您发现将其作为顶级课程的解决方案是一种解决方案.另一个是将case类放在一个对象中,这使得它成为一个静态类,一切都很好. (2认同)