简体   繁体   English

用Jackson解析Scala案例类

[英]Parsing scala case classes with Jackson

My understanding was that jackson's DefaultScalaModule is supposed to be able to read scala case classes out-of-the-box. 我的理解是,杰克逊的DefaultScalaModule应该能够立即读取scala case类。 But it seems to choke as long as I give it more than one field: 但是,只要我给它一个以上的领域,它似乎就会cho:

 val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
 scala> mapper.readValue("""{"foo": "bar", "bar": "foo"}""", classOf[Map[String, String]])
 res98: Map[String,String] = Map(foo -> bar, bar -> foo)

 class Foo(foo: String) 
 scala> mapper.readValue("""{"foo": "bar"}""", classOf[Foo])
 res101: Foo = Foo(bar)

 class Foo(foo: String, bar: String) 
 scala> mapper.readValue("""{"foo": "bar", "bar": "foo"}""", classOf[Foo])
 com.fasterxml.jackson.databind.JsonMappingException: Could not find creator property with name 'foo' (in class Foo)
 at [Source: {"foo": "bar", "bar": "foo"}; line: 1, column: 1]

Any ideas what's going on here? 有什么想法吗?

Ok, I've been using Jackson 2.5.4. 好的,我一直在使用Jackson 2.5.4。 Looks like upgrading to 2.6.6 fixes it. 看起来像升级到2.6.6可以修复它。

I tried upgrading to 2.7.3 at first, and it fixes that too, but seems to break something else (not sure what exactly, some of my tests failed, but I haven't figured out the exact cause, and just dropped it, since 2.6.6 seems to do what I need). 我最初尝试升级到2.7.3,它也解决了该问题,但似乎又破坏了其他内容(不确定到底是什么,我的某些测试失败了,但是我还没有找出确切的原因,只是放弃了,因为2.6.6似乎可以满足我的需求)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM