Skip to content

Commit 361df07

Browse files
nboseckerKevinGilmore
authored andcommitted
Added KeyDeserializer class and test code (eugenp#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
1 parent 6fe7789 commit 361df07

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.jackson.entities;
2+
3+
import java.util.Map;
4+
5+
import com.baeldung.jackson.serialization.MyPairDeserializer;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
9+
10+
public class ClassWithAMap {
11+
12+
@JsonProperty("map")
13+
@JsonDeserialize(keyUsing = MyPairDeserializer.class)
14+
private final Map<MyPair, String> map;
15+
16+
@JsonCreator
17+
public ClassWithAMap(Map<MyPair, String> map) {
18+
this.map = map;
19+
}
20+
21+
public Map<MyPair, String> getMap() {
22+
return map;
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.jackson.serialization;
2+
3+
import java.io.IOException;
4+
5+
import com.baeldung.jackson.entities.MyPair;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.DeserializationContext;
8+
import com.fasterxml.jackson.databind.KeyDeserializer;
9+
10+
public class MyPairDeserializer extends KeyDeserializer {
11+
12+
@Override
13+
public MyPair deserializeKey(String key, DeserializationContext ctxt)
14+
throws IOException, JsonProcessingException {
15+
16+
return new MyPair(key);
17+
}
18+
}

‎jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonMapDeserializeTest.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Assert;
88
import org.junit.Test;
99

10+
import com.baeldung.jackson.entities.ClassWithAMap;
1011
import com.baeldung.jackson.entities.MyPair;
1112
import com.fasterxml.jackson.core.JsonParseException;
1213
import com.fasterxml.jackson.core.type.TypeReference;
@@ -36,14 +37,21 @@ public void whenSimpleMapDeserialize_thenCorrect()
3637
public void whenObjectStringMapDeserialize_thenCorrect()
3738
throws JsonParseException, JsonMappingException, IOException {
3839

39-
final String jsonInput = "{\"Abbott and Costello\" : \"Comedy\"}";
40+
final String jsonInput = "{\"Abbott and Costello\":\"Comedy\"}";
4041
final ObjectMapper mapper = new ObjectMapper();
4142

4243
TypeReference<HashMap<MyPair, String>> typeRef = new TypeReference<HashMap<MyPair, String>>() {
4344
};
45+
4446
map = mapper.readValue(jsonInput, typeRef);
4547

4648
Assert.assertEquals("Comedy", map.get(new MyPair("Abbott", "Costello")));
49+
50+
ClassWithAMap classWithMap = mapper.readValue(jsonInput,
51+
ClassWithAMap.class);
52+
53+
Assert.assertEquals("Comedy",
54+
classWithMap.getMap().get(new MyPair("Abbott", "Costello")));
4755
}
4856

4957
@Test

0 commit comments

Comments
 (0)