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

Commit 7573622

Browse files
committed
Fixed #41
1 parent 8230d54 commit 7573622

File tree

4 files changed

+76
-21
lines changed

4 files changed

+76
-21
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ Guava (http://code.google.com/p/guava-libraries/) types (currently mostly just c
2121
<tag>HEAD</tag>
2222
</scm>
2323

24+
<contributors>
25+
<contributor>
26+
<name>Steven Schlansker</name>
27+
<email>[email protected]</email>
28+
</contributor>
29+
</contributors>
30+
2431
<properties>
2532
<version.jackson>2.3.2</version.jackson>
2633

release-notes/VERSION

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
Project: jackson-datatype-guava
2-
Version: 2.3.2 (01-Mar-2014)
2+
Version: 2.3.3 (xx-xxx-2014)
33

4-
#36: Improve Range deserializer to work with older Guava versions (10-)
5-
(contribtued by ispringer@github)
4+
#41: `Multimap` serializer does not honor @JsonInclude(JsonInclude.Include.NON_EMPTY)
5+
(reported by Olve S-H)
66

77
------------------------------------------------------------------------
88
=== History: ===
99
------------------------------------------------------------------------
1010

11+
2.3.2 (01-Mar-2014)
12+
13+
#36: Improve Range deserializer to work with older Guava versions (10-)
14+
(contribtued by ispringer@github)
15+
1116
2.3.1 (28-Dec-2013)
1217

1318
#33: Add support for `Range` values

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

+37-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
import com.fasterxml.jackson.databind.*;
1010
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
11+
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
1112
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
1213
import com.fasterxml.jackson.databind.type.MapLikeType;
1314

1415
import com.google.common.collect.Lists;
1516
import com.google.common.collect.Multimap;
1617

1718
public class MultimapSerializer
18-
extends JsonSerializer<Multimap<?, ?>>
19+
extends ContainerSerializer<Multimap<?, ?>>
1920
implements ContextualSerializer
2021
{
2122
private final MapLikeType _type;
@@ -31,6 +32,7 @@ public MultimapSerializer(SerializationConfig config,
3132
TypeSerializer valueTypeSerializer,
3233
JsonSerializer<Object> valueSerializer)
3334
{
35+
super(type.getRawClass(), false);
3436
_type = type;
3537
_property = null;
3638
_keySerializer = keySerializer;
@@ -43,6 +45,7 @@ protected MultimapSerializer(MultimapSerializer src, BeanProperty property,
4345
JsonSerializer<?> keySerializer,
4446
TypeSerializer valueTypeSerializer, JsonSerializer<?> valueSerializer)
4547
{
48+
super(src);
4649
_type = src._type;
4750
_property = property;
4851
_keySerializer = (JsonSerializer<Object>) keySerializer;
@@ -56,6 +59,12 @@ protected MultimapSerializer withResolved(BeanProperty property,
5659
{
5760
return new MultimapSerializer(this, property, keySer, vts, valueSer);
5861
}
62+
63+
@Override
64+
protected ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer typeSer) {
65+
return new MultimapSerializer(this, _property, _keySerializer,
66+
typeSer, _valueSerializer);
67+
}
5968

6069
/*
6170
/**********************************************************
@@ -92,7 +101,33 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
92101

93102
/*
94103
/**********************************************************
95-
/* JsonSerializer implementation
104+
/* Accessors for ContainerSerializer
105+
/**********************************************************
106+
*/
107+
108+
@Override
109+
public JsonSerializer<?> getContentSerializer() {
110+
return _valueSerializer;
111+
}
112+
113+
@Override
114+
public JavaType getContentType() {
115+
return _type.getContentType();
116+
}
117+
118+
@Override
119+
public boolean hasSingleElement(Multimap<?,?> map) {
120+
return map.size() == 1;
121+
}
122+
123+
@Override
124+
public boolean isEmpty(Multimap<?,?> map) {
125+
return map.isEmpty();
126+
}
127+
128+
/*
129+
/**********************************************************
130+
/* Post-processing (contextualization)
96131
/**********************************************************
97132
*/
98133

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package com.fasterxml.jackson.datatype.guava;
22

3+
import com.fasterxml.jackson.annotation.*;
34
import com.fasterxml.jackson.core.type.TypeReference;
45
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.google.common.collect.ArrayListMultimap;
6-
import com.google.common.collect.HashMultimap;
7-
import com.google.common.collect.ImmutableMultimap;
8-
import com.google.common.collect.LinkedHashMultimap;
9-
import com.google.common.collect.LinkedListMultimap;
10-
import com.google.common.collect.ListMultimap;
11-
import com.google.common.collect.Multimap;
12-
import com.google.common.collect.SetMultimap;
13-
import com.google.common.collect.TreeMultimap;
6+
7+
import com.google.common.collect.*;
148

159
import java.io.IOException;
1610
import java.util.Map;
@@ -24,10 +18,23 @@
2418
*/
2519
public class TestMultimaps extends BaseTest
2620
{
21+
// Test for issue #13 on github, provided by stevenschlansker
22+
public static enum MyEnum {
23+
YAY,
24+
BOO
25+
}
26+
27+
// [Issue#41]
28+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
29+
static class MultiMapWrapper {
30+
@JsonProperty
31+
Multimap<String, String> map = ArrayListMultimap.create();
32+
}
33+
2734
private static final String StringStringMultimap =
2835
"{\"first\":[\"abc\",\"abc\",\"foo\"]," + "\"second\":[\"bar\"]}";
2936

30-
private final ObjectMapper MAPPER = mapperWithModule();
37+
private final ObjectMapper MAPPER = mapperWithModule();
3138

3239
public void testMultimap() throws Exception
3340
{
@@ -98,12 +105,6 @@ public void testMultimapIssue3() throws Exception
98105
javaMap = o.readValue(t4, Map.class);
99106
assertEquals(2, javaMap.size());
100107
}
101-
102-
// Test for issue #13 on github, provided by stevenschlansker
103-
public static enum MyEnum {
104-
YAY,
105-
BOO
106-
}
107108

108109
public void testEnumKey() throws Exception
109110
{
@@ -119,6 +120,13 @@ public void testEnumKey() throws Exception
119120
assertEquals(map, MAPPER.readValue(serializedForm, type));
120121
}
121122

123+
// [Issue#41]
124+
public void testEmptyMapExclusion() throws Exception
125+
{
126+
String json = MAPPER.writeValueAsString(new MultiMapWrapper());
127+
assertEquals("{}", json);
128+
}
129+
122130
/*
123131
/**********************************************************************
124132
/* Unit tests for set-based multimaps

0 commit comments

Comments
 (0)