0

Using Jerkson version:

<dependency>
    <groupId>com.cloudphysics</groupId>
    <artifactId>jerkson_2.10</artifactId>
    <version>0.6.3</version>
</dependency>

I have this case class:

case class Parameter(val name:String, val value:String, @(JsonProperty@field)("type") val aType:String, val restriction:String, val defaultValue:String, val required:Boolean, val description:String)

The Json ouptut contains a field named 'type'. Obviusly this is a problem in Scala as 'type' is a key word. Although it looks like JsonProperty sold be supported this seems to be broken.

In a test I have this code:

val p = Parameter("name", "value", "string", "restricted", "myDefault", true, "desc")
println(Json.generate(p))

It prints:

{"name":"name","value":"value","aType":"string","restriction":"restricted","defaultValue":"myDefault","required":true,"description":"desc"}

'aType' and not 'type'

Any ideas what am I doing wrong?

4

1 回答 1

1

Scala 将允许您使用关键字(和所有内容)来命名事物,如果您将它们放在记号 (`) 中

case class Parameter( ..., `type`: String,  ....)

这会给你你想要的。


Jerkson 项目被放弃。

如果这是带有scala 模块的杰克逊,您的代码会很好,@JsonProperty("type")也可以完成这项工作。

于 2013-11-19T23:19:58.553 回答