将Jackson转换为Play Json

时间:2015-05-25 13:21:55

标签: json scala playframework jackson

我正在尝试将带有Jackson注释的类转换为Play的JsObject。我知道,它应该通过将其转换为JsonNode然后转换为JsValue / JsObject来完成。

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

val jsNode: JsonNode = mapper.valueToTree(status)
val jsValue: JsObject = mapper.treeToValue(jsNode, classOf[JsObject])

但有例外:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "anticipatedClosing" (class play.api.libs.json.JsObject), not marked as ignorable (2 known properties: "fields", "value"])
 at [Source: N/A; line: -1, column: -1] (through reference chain: play.api.libs.json.JsObject["anticipatedClosing"])
...

做错了什么? 目前我正在转换杰克逊 - >字符串 - >玩Json,但看起来很难看

1 个答案:

答案 0 :(得分:0)

将PlayJsonModule添加到您的objectMapper

val mapper = new ObjectMapper().registerModule(PlayJsonModule)
val jsValue: JsObject = mapper.treeToValue(jsNode, classOf[JsObject])