Search in sources :

Example 1 with MyPair

use of com.baeldung.jackson.entities.MyPair 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)

Example 2 with MyPair

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

the class JacksonMapDeserializeUnitTest method whenObjectObjectMapDeserialize_thenCorrect.

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

Example 3 with MyPair

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

the class JacksonMapSerializeUnitTest method whenCustomObjectObjectMapSerialize_thenCorrect.

@Test
public void whenCustomObjectObjectMapSerialize_thenCorrect() throws JsonProcessingException {
    cmap = new HashMap<>();
    mapKey = new MyPair("Abbott", "Costello");
    mapValue = new MyPair("Comedy", "1940's");
    cmap.put(mapKey, mapValue);
    final String jsonResult = mapper.writeValueAsString(cmap);
    Assert.assertEquals("{\"Abbott and Costello\":\"Comedy and 1940's\"}", jsonResult);
}
Also used : MyPair(com.baeldung.jackson.entities.MyPair) Test(org.junit.Test)

Example 4 with MyPair

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

the class JacksonMapSerializeUnitTest method whenCustomObjectStringMapSerialize_thenCorrect.

@Test
public void whenCustomObjectStringMapSerialize_thenCorrect() throws JsonProcessingException {
    map = new HashMap<>();
    MyPair key = new MyPair("Abbott", "Costello");
    map.put(key, "Comedy");
    final String jsonResult = mapper.writeValueAsString(map);
    Assert.assertEquals("{\"Abbott and Costello\":\"Comedy\"}", jsonResult);
}
Also used : MyPair(com.baeldung.jackson.entities.MyPair) Test(org.junit.Test)

Aggregations

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