Jackson Map Serialize/Deserialize (#1511)

* Solr w Apache SolrJ

* Solr w Apache SolrJ

* updated test names and moved add to @before method

* create apache-solrj module, moved code from spring-data-solr

* More examples for indexing,delete,and query for solrj

* More examples for indexing,delete,and query for solrj

* Jackson Map Serialize/Deserialize

* Jackson Map Serialize/Deserialize
This commit is contained in:
Nancy Bosecker
2017-03-27 05:46:58 -07:00
committed by KevinGilmore
parent e6836c01c7
commit 427077ebfa
4 changed files with 239 additions and 0 deletions
@@ -0,0 +1,63 @@
package com.baeldung.jackson.deserialization;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.jackson.entities.MyPair;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonMapDeserializeTest {
private Map<MyPair, String> map;
private Map<MyPair, MyPair> cmap;
@Test
public void whenSimpleMapDeserialize_thenCorrect()
throws JsonParseException, JsonMappingException, IOException {
final String jsonInput = "{\"key\": \"value\"}";
final ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
};
final Map<String, String> map = mapper.readValue(jsonInput, typeRef);
Assert.assertEquals("value", map.get("key"));
}
@Test
public void whenObjectStringMapDeserialize_thenCorrect()
throws JsonParseException, JsonMappingException, IOException {
final String jsonInput = "{\"Abbott and Costello\" : \"Comedy\"}";
final ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<MyPair, String>> typeRef = new TypeReference<HashMap<MyPair, String>>() {
};
map = mapper.readValue(jsonInput, typeRef);
Assert.assertEquals("Comedy", map.get(new MyPair("Abbott", "Costello")));
}
@Test
public void whenObjectObjectMapDeserialize_thenCorrect()
throws JsonParseException, JsonMappingException, IOException {
final String jsonInput = "{\"Abbott and Costello\" : \"Comedy and 1940s\"}";
final ObjectMapper mapper = new ObjectMapper();
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")));
}
}
@@ -0,0 +1,71 @@
package com.baeldung.jackson.serialization;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.jackson.entities.MyPair;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
public class JacksonMapSerializeTest {
@JsonSerialize(keyUsing = MyPairSerializer.class)
private Map<MyPair, String> map;
@JsonSerialize(keyUsing = MapSerializer.class)
private Map<MyPair, MyPair> cmap;
@JsonSerialize(keyUsing = MyPairSerializer.class)
private MyPair mapKey;
@JsonSerialize(keyUsing = MyPairSerializer.class)
private MyPair mapValue;
@Test
public void whenSimpleMapSerialize_thenCorrect()
throws JsonProcessingException {
Map<String, String> map = new HashMap<String, String>();
map.put("key", "value");
final ObjectMapper mapper = new ObjectMapper();
final String jsonResult = mapper.writeValueAsString(map);
Assert.assertEquals("{\"key\":\"value\"}", jsonResult);
}
@Test
public void whenCustomObjectStringMapSerialize_thenCorrect()
throws JsonProcessingException {
map = new HashMap<MyPair, String>();
MyPair key = new MyPair("Abbott", "Costello");
map.put(key, "Comedy");
final ObjectMapper mapper = new ObjectMapper();
final String jsonResult = mapper.writeValueAsString(map);
Assert.assertEquals("{\"Abbott and Costello\":\"Comedy\"}", jsonResult);
}
@Test
public void whenCustomObjectObjectMapSerialize_thenCorrect()
throws JsonProcessingException {
cmap = new HashMap<MyPair, MyPair>();
mapKey = new MyPair("Abbott", "Costello");
mapValue = new MyPair("Comedy", "1940's");
cmap.put(mapKey, mapValue);
final ObjectMapper mapper = new ObjectMapper();
final String jsonResult = mapper.writeValueAsString(cmap);
Assert.assertEquals("{\"Abbott and Costello\":\"Comedy and 1940's\"}",
jsonResult);
}
}