59

I have the following class:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

import java.io.Serializable;
import java.util.HashMap;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Theme implements Serializable {

    @JsonProperty
    private String themeName;

    @JsonProperty
    private boolean customized;

    @JsonProperty
    private HashMap<String, String> descriptor;

    //...getters and setters for the above properties
}

When I execute the following code:

    HashMap<String, Theme> test = new HashMap<String, Theme>();
    Theme t1 = new Theme();
    t1.setCustomized(false);
    t1.setThemeName("theme1");
    test.put("theme1", t1);

    Theme t2 = new Theme();
    t2.setCustomized(true);
    t2.setThemeName("theme2");
    t2.setDescriptor(new HashMap<String, String>());
    t2.getDescriptor().put("foo", "one");
    t2.getDescriptor().put("bar", "two");
    test.put("theme2", t2);
    String json = "";
    ObjectMapper mapper = objectMapperFactory.createObjectMapper();
    try {
        json = mapper.writeValueAsString(test);
    } catch (IOException e) {
        e.printStackTrace(); 
    }

The json string produced looks like this:

{
  "theme2": {
    "themeName": "theme2",
    "customized": true,
    "descriptor": {
      "foo": "one",
       "bar": "two"
    }
  },
  "theme1": {
    "themeName": "theme1",
    "customized": false,
    "descriptor": null
  }
}

My problem is getting the above json string to de-serizlize back into a

HashMap<String, Theme> 

object.

My de-serialization code looks like this:

HashMap<String, Themes> themes =
        objectMapperFactory.createObjectMapper().readValue(json, HashMap.class);

Which de-serializes into a HashMap with the correct keys, but does not create Theme objects for the values. I don't know what to specify instead of "HashMap.class" in the readValue() method.

Any help would be appreciated.

3 Answers 3

100

You should create specific Map type and provide it into deserialization process:

TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Theme.class);
HashMap<String, Theme> map = mapper.readValue(json, mapType);
4
  • Ok, we're definitely heading in the right direction, as it is de-serializing using the code above. However, The hashmap is only coming in with a single key/value (theme2). It does have the correct theme associated with the key, but theme1 isn't in the hashmap?
    – wbj
    Aug 5, 2013 at 14:17
  • Why not. For me, app prints this when I deserialize your JSON: '{theme1=Theme [themeName=theme1, customized=false, descriptor=null], theme2=Theme [themeName=theme2, customized=true, descriptor={foo=one, bar=two}]}'. Aug 5, 2013 at 16:40
  • Found the problem - poorly formed json string in my test data. So your solution works as advertised ;)
    – wbj
    Aug 5, 2013 at 16:55
  • 1
    I this also possible using Jackson annotations only? Jun 11, 2020 at 11:10
31

You can use TypeReference class which does the type casting for map with user defined types. More documentation at https://github.com/FasterXML/jackson-databind/

ObjectMapper mapper = new ObjectMapper();
Map<String,Theme> result =
  mapper.readValue(src, new TypeReference<Map<String,Theme>>() {});
3

You can make a POJO that extends a Map.

This is important for dealing with nested maps of objects.

{
  key1: { nestedKey1: { value: 'You did it!' } }
}

This can be deserialized via:

class Parent extends HashMap<String, Child> {}

class Child extends HashMap<String, MyCoolPojo> {}

class MyCoolPojo { public String value; }

Parent parent = new ObjectMapper().readValue(json, Parent.class);
parent.get("key1").get("nestedKey1").value; // "You did it!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.