Skip to content

Commit 427077e

Browse files
nboseckerKevinGilmore
authored andcommitted
Jackson Map Serialize/Deserialize (eugenp#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
1 parent e6836c0 commit 427077e

4 files changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.jackson.entities;
2+
3+
import com.fasterxml.jackson.annotation.JsonValue;
4+
5+
public class MyPair {
6+
7+
private String first;
8+
private String second;
9+
10+
public MyPair(String first, String second) {
11+
this.first = first;
12+
this.second = second;
13+
}
14+
15+
public MyPair(String both) {
16+
String[] pairs = both.split("and");
17+
this.first = pairs[0].trim();
18+
this.second = pairs[1].trim();
19+
}
20+
21+
@Override
22+
@JsonValue
23+
public String toString() {
24+
return first + " and " + second;
25+
}
26+
27+
@Override
28+
public int hashCode() {
29+
final int prime = 31;
30+
int result = 1;
31+
result = prime * result + ((first == null) ? 0 : first.hashCode());
32+
result = prime * result + ((second == null) ? 0 : second.hashCode());
33+
return result;
34+
}
35+
36+
@Override
37+
public boolean equals(Object obj) {
38+
if (this == obj) {
39+
return true;
40+
}
41+
if (obj == null) {
42+
return false;
43+
}
44+
if (!(obj instanceof MyPair)) {
45+
return false;
46+
}
47+
MyPair other = (MyPair) obj;
48+
if (first == null) {
49+
if (other.first != null) {
50+
return false;
51+
}
52+
} else if (!first.equals(other.first)) {
53+
return false;
54+
}
55+
if (second == null) {
56+
if (other.second != null) {
57+
return false;
58+
}
59+
} else if (!second.equals(other.second)) {
60+
return false;
61+
}
62+
return true;
63+
}
64+
65+
public String getFirst() {
66+
return first;
67+
}
68+
69+
public void setFirst(String first) {
70+
this.first = first;
71+
}
72+
73+
public String getSecond() {
74+
return second;
75+
}
76+
77+
public void setSecond(String second) {
78+
this.second = second;
79+
}
80+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jackson.serialization;
2+
3+
import java.io.IOException;
4+
import java.io.StringWriter;
5+
6+
import com.baeldung.jackson.entities.MyPair;
7+
import com.fasterxml.jackson.core.JsonGenerator;
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.JsonSerializer;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.databind.SerializerProvider;
12+
13+
public class MyPairSerializer extends JsonSerializer<MyPair> {
14+
15+
private final ObjectMapper mapper = new ObjectMapper();
16+
17+
@Override
18+
public void serialize(MyPair value, JsonGenerator gen,
19+
SerializerProvider serializers) throws IOException,
20+
JsonProcessingException {
21+
StringWriter writer = new StringWriter();
22+
mapper.writeValue(writer, value);
23+
gen.writeFieldName(writer.toString());
24+
}
25+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.jackson.deserialization;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import com.baeldung.jackson.entities.MyPair;
11+
import com.fasterxml.jackson.core.JsonParseException;
12+
import com.fasterxml.jackson.core.type.TypeReference;
13+
import com.fasterxml.jackson.databind.JsonMappingException;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
16+
public class JacksonMapDeserializeTest {
17+
18+
private Map<MyPair, String> map;
19+
private Map<MyPair, MyPair> cmap;
20+
21+
@Test
22+
public void whenSimpleMapDeserialize_thenCorrect()
23+
throws JsonParseException, JsonMappingException, IOException {
24+
25+
final String jsonInput = "{\"key\": \"value\"}";
26+
final ObjectMapper mapper = new ObjectMapper();
27+
TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
28+
};
29+
30+
final Map<String, String> map = mapper.readValue(jsonInput, typeRef);
31+
32+
Assert.assertEquals("value", map.get("key"));
33+
}
34+
35+
@Test
36+
public void whenObjectStringMapDeserialize_thenCorrect()
37+
throws JsonParseException, JsonMappingException, IOException {
38+
39+
final String jsonInput = "{\"Abbott and Costello\" : \"Comedy\"}";
40+
final ObjectMapper mapper = new ObjectMapper();
41+
42+
TypeReference<HashMap<MyPair, String>> typeRef = new TypeReference<HashMap<MyPair, String>>() {
43+
};
44+
map = mapper.readValue(jsonInput, typeRef);
45+
46+
Assert.assertEquals("Comedy", map.get(new MyPair("Abbott", "Costello")));
47+
}
48+
49+
@Test
50+
public void whenObjectObjectMapDeserialize_thenCorrect()
51+
throws JsonParseException, JsonMappingException, IOException {
52+
53+
final String jsonInput = "{\"Abbott and Costello\" : \"Comedy and 1940s\"}";
54+
final ObjectMapper mapper = new ObjectMapper();
55+
TypeReference<HashMap<MyPair, MyPair>> typeRef = new TypeReference<HashMap<MyPair, MyPair>>() {
56+
};
57+
58+
cmap = mapper.readValue(jsonInput, typeRef);
59+
60+
Assert.assertEquals(new MyPair("Comedy", "1940s"),
61+
cmap.get(new MyPair("Abbott", "Costello")));
62+
}
63+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.jackson.serialization;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import com.baeldung.jackson.entities.MyPair;
10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
13+
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
14+
15+
public class JacksonMapSerializeTest {
16+
17+
@JsonSerialize(keyUsing = MyPairSerializer.class)
18+
private Map<MyPair, String> map;
19+
20+
@JsonSerialize(keyUsing = MapSerializer.class)
21+
private Map<MyPair, MyPair> cmap;
22+
23+
@JsonSerialize(keyUsing = MyPairSerializer.class)
24+
private MyPair mapKey;
25+
26+
@JsonSerialize(keyUsing = MyPairSerializer.class)
27+
private MyPair mapValue;
28+
29+
@Test
30+
public void whenSimpleMapSerialize_thenCorrect()
31+
throws JsonProcessingException {
32+
33+
Map<String, String> map = new HashMap<String, String>();
34+
map.put("key", "value");
35+
36+
final ObjectMapper mapper = new ObjectMapper();
37+
final String jsonResult = mapper.writeValueAsString(map);
38+
39+
Assert.assertEquals("{\"key\":\"value\"}", jsonResult);
40+
}
41+
42+
@Test
43+
public void whenCustomObjectStringMapSerialize_thenCorrect()
44+
throws JsonProcessingException {
45+
46+
map = new HashMap<MyPair, String>();
47+
MyPair key = new MyPair("Abbott", "Costello");
48+
map.put(key, "Comedy");
49+
50+
final ObjectMapper mapper = new ObjectMapper();
51+
final String jsonResult = mapper.writeValueAsString(map);
52+
53+
Assert.assertEquals("{\"Abbott and Costello\":\"Comedy\"}", jsonResult);
54+
}
55+
56+
@Test
57+
public void whenCustomObjectObjectMapSerialize_thenCorrect()
58+
throws JsonProcessingException {
59+
60+
cmap = new HashMap<MyPair, MyPair>();
61+
mapKey = new MyPair("Abbott", "Costello");
62+
mapValue = new MyPair("Comedy", "1940's");
63+
cmap.put(mapKey, mapValue);
64+
65+
final ObjectMapper mapper = new ObjectMapper();
66+
final String jsonResult = mapper.writeValueAsString(cmap);
67+
68+
Assert.assertEquals("{\"Abbott and Costello\":\"Comedy and 1940's\"}",
69+
jsonResult);
70+
}
71+
}

0 commit comments

Comments
 (0)