0

I have a simple Map and I would like to deserialize it to a Pojo with some internal nested classes.

Example for map:

Map = ["total_errs"=20, "err_1_day"=3, "err_2_day"=1, ..., "err_30_day=5"]

I would like to deserialize this into a nice Pojo as follow:

class CustomPojo {
   @JsonProperty("total_errs")
   private long totalErrors;

   private List<ErrorByDay> errByDays;

   static class ErrorByDay {
      private long age; // age is the key of each map entry
      private long count; // count is the value of each map entry
    }
}

Calling code:

CustomPojo pojo = new ObjectMapper().convert(map, CustomPojo.class);

Is there a nice way to solve this using annotations only? (I know I can write a custom Deserializer for that).

Thank you!

Shvalb
  • 1,835
  • 2
  • 30
  • 60
  • I recommend doing this by yourself manually, thus avoiding having to jump through all the hoops Jackson has in store for you. On the other hand, if you have a gazillion of types to deserialize it's worth digging into the minutiae of Jackson. – Hans Westerbeek Oct 22 '18 at 14:57
  • See https://stackoverflow.com/questions/16428817/convert-a-mapstring-string-to-a-pojo – Beno Oct 22 '18 at 14:59
  • notice, it's not a simple map to pojo conversion, the pojo has a list of items and keys of the map are strings which would need to translate into index. – Shvalb Oct 22 '18 at 15:02
  • If the index appears at the end of key such as `err_day_1`, `err_day_2`, then it can be deserialized to a list by using `@JsonAnySetter` in `Jackson`. – LHCHIN Nov 25 '19 at 12:26

0 Answers0