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?

YaOg
  • 1,748
  • 5
  • 24
  • 43

1 Answers1

1

Scala will allow you to use keywords (and everything) to name things if you put them in ticks (`)

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

this will give you what you want.


Jerkson project is abandoned.

If this were jackson with scala module your code would be fine, @JsonProperty("type") would also do the job.

Rin malavi
  • 809
  • 1
  • 12
  • 25
  • back ticks don't help. JSON de-serialization fails – YaOg Nov 20 '13 at 09:46
  • also, scala module has other problems, see: https://github.com/FasterXML/jackson-module-scala/issues/111 – YaOg Nov 20 '13 at 09:47
  • Oh, I am sry, haven't even checked deserialization. Anyway point is Jerkson is abandoned, that is why your code doesn't work. Near to edge case issues or not, jackson with scala module would work, that or any other library, for that matter, is still better choice than a library which hasn't seen a commit in 7 months and is open about its obsoliteness – Rin malavi Nov 20 '13 at 16:30