Skip to content

Commit 6290eef

Browse files
committed
Fixed #691
1 parent 960e49e commit 6290eef

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@ Zoltan Farkas (zolyfarkas@github)
203203
Ludevik@github:
204204
* Reported #682: Class<?>-valued Map keys not serialized properly
205205
(2.5.1)
206+
207+
Antibrumm@github:
208+
* Reported #691: Jackson 2.5.0. NullSerializer for MapProperty failing
209+
(2.5.2)

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.5.2 (not yet released)
8+
9+
#691: Jackson 2.5.0. NullSerializer for MapProperty failing
10+
(reported by Antibrumm@github)
11+
712
2.5.1 (06-Feb-2015)
813

914
#667: Problem with bogus conflict between single-arg-String vs `CharSequence` constructor

src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,6 @@ protected void serializeTypedFields(Map<?,?> value, JsonGenerator gen, Serialize
772772
continue;
773773
}
774774
valueSer = provider.getDefaultNullValueSerializer();
775-
keySerializer.serialize(keyElem, gen, provider);
776-
provider.defaultSerializeNull(gen);
777775
} else {
778776
valueSer = _valueSerializer;
779777
Class<?> cc = valueElem.getClass();

src/main/java/com/fasterxml/jackson/databind/ser/std/NullSerializer.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.*;
88
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
99
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
10+
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1011

1112
/**
1213
* This is a simple dummy serializer that will just output literal
@@ -25,8 +26,22 @@ public class NullSerializer
2526
private NullSerializer() { super(Object.class); }
2627

2728
@Override
28-
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
29-
jgen.writeNull();
29+
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
30+
gen.writeNull();
31+
}
32+
33+
/**
34+
* Although this method should rarely get called, for convenience we should override
35+
* it, and handle it same way as "natural" types: by serializing exactly as is,
36+
* without type decorations. The most common possible use case is that of delegation
37+
* by JSON filter; caller can not know what kind of serializer it gets handed.
38+
*/
39+
@Override
40+
public void serializeWithType(Object value, JsonGenerator gen, SerializerProvider serializers,
41+
TypeSerializer typeSer)
42+
throws IOException
43+
{
44+
gen.writeNull();
3045
}
3146

3247
@Override

src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public NoEmptyStringsMap add(String key, String value) {
125125
@JsonTypeName("mymap")
126126
static class MapWithTypedValues extends LinkedHashMap<String,String> { }
127127

128+
@JsonTypeInfo(use = Id.CLASS)
129+
public static class Mixin691 { }
130+
128131
/*
129132
/**********************************************************
130133
/* Test methods
@@ -276,4 +279,16 @@ public void testNullJsonMapping691() throws Exception
276279
assertEquals(aposToQuotes("{'@type':'mymap','id':'Test','NULL':null}"),
277280
json);
278281
}
282+
283+
// [databind#691]
284+
public void testNullJsonInTypedMap691() throws Exception {
285+
Map<String, String> map = new HashMap<String, String>();
286+
map.put("NULL", null);
287+
288+
ObjectMapper mapper = new ObjectMapper();
289+
mapper.addMixIn(Object.class, Mixin691.class);
290+
String json = mapper.writeValueAsString(map);
291+
assertEquals("{\"@class\":\"java.util.HashMap\",\"NULL\":null}", json);
292+
}
279293
}
294+

0 commit comments

Comments
 (0)