Search code examples
javajacksonjson-deserialization

Deserialize a JSON array partially into a HashMap with the help of jackson


I have the following JSON:

[
  {
    "a": 1,         
    "b": 2,                  
    "c": "one",
    "d": 3                     
  },
  {
    "a": 4,         
    "b": 5,                  
    "c": "two",
    "d": 6                     
  },
  {
    "a": 7,         
    "b": 8,                  
    "c": "three",
    "d": 9                     
  },
]

I want to cast this with the help of jackson into a HashMap<String,Integer> like this:

{
  "one": 2,
  "two": 5,
  "three": 8
}

I have tried a custom deserializer but I can't get it registered correctly and I don't really know if this is the correct way to do it. The next problem is that i don't have any nesting object as an actual basis for my JSON, means that I get it straight as a string like mentioned above, so I can't use any annotation like this:

@JsonDeserialize(using = CustomDeserializer.class).

So at the end I need to register the Deserializer in the ObjectMapper itself, but this won't work either with the help of a configuration like this:

ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(new TypeReference<HashMap<String,Integer>>(){}, new CustomDeserializer());

Am I missing something here? I hope someone can help me with this problem.


Solution

  • Maybe you have missed with adding module.addDeserializer

    HashMapValueDeserializer.class

    public class HashMapValueDeserializer extends JsonDeserializer<HashMap<String, String>> {
        @Override
        public HashMap<String, String> deserialize(JsonParser parser, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            HashMap<String, String> ret = new HashMap<String, String>();
    
            ObjectCodec codec = parser.getCodec();
            TreeNode node = codec.readTree(parser);
    
            for (JsonNode n : (ArrayNode)node){
                JsonNode c = n.get("c");
                JsonNode b = n.get("b");
                ret.put(c.asText(), b.asText());
            }
            return ret;
        }
    }
    

    Test.class

    public class Test {
      public static void main(String[] args) throws FileNotFoundException, MalformedURLException, JsonProcessingException {
        String jsonArray = "[{ \"a\" : \"1\", \"b\" : \"2\", \"c\" : \"one\", \"d\" : \"3\" }, { \"a\" : \"4\", \"b\" : \"5\", \"c\" : \"two\", \"d\" : \"6\" }, { \"a\" : \"7\", \"b\" : \"8\", \"c\" : \"three\", \"d\" : \"9\" }]";
    
        ObjectMapper mapper = new ObjectMapper();
    
        SimpleModule module =
            new SimpleModule("HashMapValueDeserializer", new Version(1, 0, 0, null, null, null));
        module.addDeserializer(HashMap.class, new HashMapValueDeserializer());
        mapper.registerModule(module);
    
        HashMap<String, Integer> result = mapper.readValue(jsonArray, HashMap.class);
        System.out.println(result);
      }
    
    }
    

    Output :

    {one=2, two=5, three=8}