Search in sources :

Example 1 with ClassWithAMap

use of com.baeldung.jackson.entities.ClassWithAMap in project tutorials by eugenp.

the class JacksonMapDeserializeUnitTest method whenObjectStringMapDeserialize_thenCorrect.

@Test
public void whenObjectStringMapDeserialize_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
    final String jsonInput = "{\"Abbott and Costello\":\"Comedy\"}";
    TypeReference<HashMap<MyPair, String>> typeRef = new TypeReference<HashMap<MyPair, String>>() {
    };
    map = mapper.readValue(jsonInput, typeRef);
    Assert.assertEquals("Comedy", map.get(new MyPair("Abbott", "Costello")));
    ClassWithAMap classWithMap = mapper.readValue(jsonInput, ClassWithAMap.class);
    Assert.assertEquals("Comedy", classWithMap.getMap().get(new MyPair("Abbott", "Costello")));
}
Also used : MyPair(com.baeldung.jackson.entities.MyPair) HashMap(java.util.HashMap) ClassWithAMap(com.baeldung.jackson.entities.ClassWithAMap) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Aggregations

ClassWithAMap (com.baeldung.jackson.entities.ClassWithAMap)1 MyPair (com.baeldung.jackson.entities.MyPair)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1