Added KeyDeserializer class and test code (#1523)
* 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 * Jackson version update * keydeserializer code added * keydeserializer code added
This commit is contained in:
parent
6fe778979a
commit
361df07694
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.jackson.entities;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.serialization.MyPairDeserializer;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
|
||||||
|
public class ClassWithAMap {
|
||||||
|
|
||||||
|
@JsonProperty("map")
|
||||||
|
@JsonDeserialize(keyUsing = MyPairDeserializer.class)
|
||||||
|
private final Map<MyPair, String> map;
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public ClassWithAMap(Map<MyPair, String> map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<MyPair, String> getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.jackson.serialization;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.entities.MyPair;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.KeyDeserializer;
|
||||||
|
|
||||||
|
public class MyPairDeserializer extends KeyDeserializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyPair deserializeKey(String key, DeserializationContext ctxt)
|
||||||
|
throws IOException, JsonProcessingException {
|
||||||
|
|
||||||
|
return new MyPair(key);
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ import java.util.Map;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.entities.ClassWithAMap;
|
||||||
import com.baeldung.jackson.entities.MyPair;
|
import com.baeldung.jackson.entities.MyPair;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
@ -36,14 +37,21 @@ public class JacksonMapDeserializeTest {
|
|||||||
public void whenObjectStringMapDeserialize_thenCorrect()
|
public void whenObjectStringMapDeserialize_thenCorrect()
|
||||||
throws JsonParseException, JsonMappingException, IOException {
|
throws JsonParseException, JsonMappingException, IOException {
|
||||||
|
|
||||||
final String jsonInput = "{\"Abbott and Costello\" : \"Comedy\"}";
|
final String jsonInput = "{\"Abbott and Costello\":\"Comedy\"}";
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
TypeReference<HashMap<MyPair, String>> typeRef = new TypeReference<HashMap<MyPair, String>>() {
|
TypeReference<HashMap<MyPair, String>> typeRef = new TypeReference<HashMap<MyPair, String>>() {
|
||||||
};
|
};
|
||||||
|
|
||||||
map = mapper.readValue(jsonInput, typeRef);
|
map = mapper.readValue(jsonInput, typeRef);
|
||||||
|
|
||||||
Assert.assertEquals("Comedy", map.get(new MyPair("Abbott", "Costello")));
|
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")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user