Ability to deserialize dictionary (HashMap) on top level

11 views
Skip to first unread message

EnterpriseJacksonBeansFrameworkCE

unread,
Aug 21, 2019, 1:02:58 PM8/21/19
to jackson-user

I am trying to map json dictionary of objects with known structure to POJO under jackson control:

import com.fasterxml.jackson.annotation.JsonProperty;

public class GetListOfRecordsResponse {
   @JsonProperty("recordsCount")
   private Integer recordsCount; // Works always

   @JsonProperty("records")
   private Map<String, GeneratedRecordObject> records = new HashMap<>(); // Should also work without "records" (see second json)
}

It works properly for following (fake) json response:

{
  "recordsCount": 2,
  "records": {
     "-1": {
       ...
     },
    "1": {
        ...
     }
}

How can I modify GetListOfRecordsResponse (JsonProperty attribute? Using another attribute?) to allow parsing following real world JSON response


{
  "recordsCount": 2,
   "-1": {
      ...
   },
   "1": {
      ...
   }
}


Removing  records does not work properly: the deserialized records is empty.

How can I solve this problem?

Tatu Saloranta

unread,
Aug 23, 2019, 5:58:36 PM8/23/19
to jackson-user
I am not 100% sure, but one thing that might work is to simply force
ignoral of `recordsCount` entry, something like:

public class GetListOfRecordsResponse {
@JsonIgnoreProperties({ "recordsCount" })
@JsonProperty("records")
private Map<String, GeneratedRecordObject> records;
}

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages