Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 465d5b9

Browse files
committed
Merge pull request #74 from soldierkam/master
Multimap serializer ignores _valueTypeSerializer
2 parents e47ad79 + ec0571f commit 465d5b9

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed

src/main/java/com/fasterxml/jackson/datatype/guava/ser/MultimapSerializer.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,11 @@ private final void serializeFields(Multimap<?, ?> mmap, JsonGenerator gen, Seria
316316
serializers = _dynamicValueSerializers;
317317
}
318318
}
319-
valueSer.serialize(vv, gen, provider);
319+
if (_valueTypeSerializer == null) {
320+
valueSer.serialize(vv, gen, provider);
321+
} else {
322+
valueSer.serializeWithType(vv, gen, provider, _valueTypeSerializer);
323+
}
320324
}
321325
gen.writeEndArray();
322326
}

src/test/java/com/fasterxml/jackson/datatype/guava/TestMultimaps.java

+53
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.fasterxml.jackson.annotation.*;
44
import com.fasterxml.jackson.core.type.TypeReference;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.datatype.guava.pojo.AddOp;
7+
import com.fasterxml.jackson.datatype.guava.pojo.MathOp;
8+
import com.fasterxml.jackson.datatype.guava.pojo.MulOp;
69

710
import com.google.common.collect.*;
811

@@ -11,6 +14,7 @@
1114
import java.util.Map;
1215

1316
import static com.google.common.collect.TreeMultimap.create;
17+
import static junit.framework.TestCase.assertEquals;
1418

1519
/**
1620
* Unit tests to verify handling of various {@link Multimap}s.
@@ -254,4 +258,53 @@ public void testIssue67() throws IOException
254258
assertEquals(Maps.immutableEntry("a", 7), iterator.next());
255259
assertEquals(Maps.immutableEntry("a", 8), iterator.next());
256260
}
261+
262+
public void testPolymorphicValue() throws IOException {
263+
ImmutableMultimapWrapper input = new ImmutableMultimapWrapper(ImmutableMultimap.of("add", new AddOp(3, 2), "mul", new MulOp(4, 6)));
264+
265+
String json = MAPPER.writeValueAsString(input);
266+
267+
ImmutableMultimapWrapper output = MAPPER.readValue(json, ImmutableMultimapWrapper.class);
268+
assertEquals(input, output);
269+
}
270+
271+
public static class ImmutableMultimapWrapper {
272+
273+
private ImmutableMultimap<String, MathOp> multimap;
274+
275+
public ImmutableMultimapWrapper() {
276+
}
277+
278+
public ImmutableMultimapWrapper(ImmutableMultimap<String, MathOp> f) {
279+
this.multimap = f;
280+
}
281+
282+
public ImmutableMultimap<String, MathOp> getMultimap() {
283+
return multimap;
284+
}
285+
286+
public void setMultimap(ImmutableMultimap<String, MathOp> multimap) {
287+
this.multimap = multimap;
288+
}
289+
290+
@Override
291+
public int hashCode() {
292+
int hash = 7;
293+
hash = 97 * hash + (this.multimap != null ? this.multimap.hashCode() : 0);
294+
return hash;
295+
}
296+
297+
@Override
298+
public boolean equals(Object obj) {
299+
if (obj == null) {
300+
return false;
301+
}
302+
if (getClass() != obj.getClass()) {
303+
return false;
304+
}
305+
final ImmutableMultimapWrapper other = (ImmutableMultimapWrapper) obj;
306+
return !(this.multimap != other.multimap && (this.multimap == null || !this.multimap.equals(other.multimap)));
307+
}
308+
309+
}
257310
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fasterxml.jackson.datatype.guava.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
/**
7+
*
8+
* @author Marcin Kamionowski
9+
*/
10+
public class AddOp implements MathOp {
11+
12+
private final int left;
13+
private final int right;
14+
15+
@JsonCreator
16+
public AddOp(
17+
@JsonProperty("left") int left,
18+
@JsonProperty("right") int right) {
19+
this.left = left;
20+
this.right = right;
21+
}
22+
23+
public int getLeft() {
24+
return left;
25+
}
26+
27+
public int getRight() {
28+
return right;
29+
}
30+
31+
public int value() {
32+
return left + right;
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
int hash = 3;
38+
hash = 47 * hash + this.left;
39+
hash = 47 * hash + this.right;
40+
return hash;
41+
}
42+
43+
@Override
44+
public boolean equals(Object obj) {
45+
if (obj == null) {
46+
return false;
47+
}
48+
if (getClass() != obj.getClass()) {
49+
return false;
50+
}
51+
final AddOp other = (AddOp) obj;
52+
if (this.left != other.left) {
53+
return false;
54+
}
55+
return this.right == other.right;
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fasterxml.jackson.datatype.guava.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
7+
/**
8+
*
9+
* @author Marcin Kamionowski
10+
*/
11+
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="_t")
12+
@JsonSubTypes({
13+
@Type(name="add", value=AddOp.class),
14+
@Type(name="mul", value=MulOp.class)})
15+
public interface MathOp {
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fasterxml.jackson.datatype.guava.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
/**
7+
*
8+
* @author Marcin Kamionowski
9+
*/
10+
public class MulOp implements MathOp {
11+
12+
private final int left;
13+
private final int right;
14+
15+
@JsonCreator
16+
public MulOp(
17+
@JsonProperty("left") int left,
18+
@JsonProperty("right") int right) {
19+
this.left = left;
20+
this.right = right;
21+
}
22+
23+
public int getLeft() {
24+
return left;
25+
}
26+
27+
public int getRight() {
28+
return right;
29+
}
30+
31+
public int value() {
32+
return left * right;
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
int hash = 7;
38+
hash = 67 * hash + this.left;
39+
hash = 67 * hash + this.right;
40+
return hash;
41+
}
42+
43+
@Override
44+
public boolean equals(Object obj) {
45+
if (obj == null) {
46+
return false;
47+
}
48+
if (getClass() != obj.getClass()) {
49+
return false;
50+
}
51+
final MulOp other = (MulOp) obj;
52+
if (this.left != other.left) {
53+
return false;
54+
}
55+
return this.right == other.right;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)