3

This is what I want to do: Jackson deserialize extra fields as map, but I haven't gotten it to work.

This is the class I want to deserialize:

class MyClass(
           @JsonProperty val name: String,
           @JsonProperty val age: Int,
           @JsonIgnore val other: mutable.HashMap[String, Any] = mutable.HashMap()
         ) {

  @JsonAnyGetter def getOther: mutable.HashMap[String, Any] = other

  @JsonAnySetter def setOther(key: String, value: Any): Unit = {
    other.put(key, value)
  }
}

Now the input might be this:

{
    "name": "Lisa",
    "age": 42
}

And everyting is well. But the input might also be this:

{
    "name": "Lisa",
    "age": 42
    "extraProperty": 123
}

And in that case I would like to store everything other than name/age (i.e. extraProperty here) into the map, so that I can pass it on to the next service. But it's not working: Nothing appears in the other map. I've also tried doing @(JsonAnyGetter@field) val other: mutable.HashMap[String, Any] = mutable.HashMap() (which I found here: https://github.com/FasterXML/jackson-module-scala/issues/308), but it didn't work.

Has anyone gotten this to work with Scalatra?

L42
  • 3,052
  • 4
  • 28
  • 49

0 Answers0