Desserializando em um HashMap de objetos personalizados com jackson
Eu tenho a seguinte classe:
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
}
Quando eu executo o seguinte código:
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();
}
A string json produzida é assim:
{
"theme2": {
"themeName": "theme2",
"customized": true,
"descriptor": {
"foo": "one",
"bar": "two"
}
},
"theme1": {
"themeName": "theme1",
"customized": false,
"descriptor": null
}
}
Meu problema é pegar a string json acima para deselizá-la de volta para um
HashMap<String, Theme>
objeto.
Meu código de desserialização é assim:
HashMap<String, Themes> themes =
objectMapperFactory.createObjectMapper().readValue(json, HashMap.class);
Que de-serializa em um HashMap com as chaves corretas, mas não cria objetos Theme para os valores. Eu não sei o que especificar em vez de "HashMap.class" no método readValue ().
Qualquer ajuda seria apreciada.