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

Commit c55b8e7

Browse files
committed
Fix #52
1 parent 71bed6b commit c55b8e7

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

release-notes/CREDITS

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ Alexey Kobyakov (akobiakov@github)
2020
* Reported #64: `@JsonUnwrapped` annotation is ignored when a field is an Optional
2121
(2.6.0)
2222

23+
Jose Thomas (josethomas@github)
24+
* Contributed #52: Guava collection types do not allow null values
25+
(2.6.2)

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Project: jackson-datatype-guava
66

77
2.6.2 (not yet released)
88

9+
#52: Guava collection types do not allow null values
10+
(contributed by Jose T)
911
#80: Relax OSGi version constraints for Guava dependency.
1012
(requested by Benson M)
1113
#82: Problem with polymorphic value types for `Optional`, with 2.6

src/main/java/com/fasterxml/jackson/datatype/guava/deser/GuavaImmutableMapDeserializer.java

+32-10
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,30 @@ abstract class GuavaImmutableMapDeserializer<T extends ImmutableMap<Object, Obje
2323
protected abstract ImmutableMap.Builder<Object, Object> createBuilder();
2424

2525
@Override
26-
protected T _deserializeEntries(JsonParser jp, DeserializationContext ctxt) throws IOException,
27-
JsonProcessingException {
26+
protected T _deserializeEntries(JsonParser p, DeserializationContext ctxt)
27+
throws IOException, JsonProcessingException
28+
{
2829
final KeyDeserializer keyDes = _keyDeserializer;
2930
final JsonDeserializer<?> valueDes = _valueDeserializer;
3031
final TypeDeserializer typeDeser = _typeDeserializerForValue;
3132

3233
ImmutableMap.Builder<Object, Object> builder = createBuilder();
33-
for (; jp.getCurrentToken() == JsonToken.FIELD_NAME; jp.nextToken()) {
34+
for (; p.getCurrentToken() == JsonToken.FIELD_NAME; p.nextToken()) {
3435
// Must point to field name now
35-
String fieldName = jp.getCurrentName();
36+
String fieldName = p.getCurrentName();
3637
Object key = (keyDes == null) ? fieldName : keyDes.deserializeKey(fieldName, ctxt);
3738
// And then the value...
38-
JsonToken t = jp.nextToken();
39+
JsonToken t = p.nextToken();
3940
// 28-Nov-2010, tatu: Should probably support "ignorable properties" in future...
4041
Object value;
4142
if (t == JsonToken.VALUE_NULL) {
42-
value = null;
43-
} else if (typeDeser == null) {
44-
value = valueDes.deserialize(jp, ctxt);
43+
_handleNull(ctxt, fieldName, _valueDeserializer, builder);
44+
continue;
45+
}
46+
if (typeDeser == null) {
47+
value = valueDes.deserialize(p, ctxt);
4548
} else {
46-
value = valueDes.deserializeWithType(jp, ctxt, typeDeser);
49+
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
4750
}
4851
builder.put(key, value);
4952
}
@@ -54,4 +57,23 @@ protected T _deserializeEntries(JsonParser jp, DeserializationContext ctxt) thro
5457
return map;
5558
}
5659

57-
}
60+
/**
61+
* Overridable helper method called when a JSON null value is encountered.
62+
* Since Guava Maps typically do not allow null values, special handling
63+
* is needed; default is to simply ignore and skip such values, but alternative
64+
* could be to throw an exception.
65+
*/
66+
protected void _handleNull(DeserializationContext ctxt, String fieldName,
67+
JsonDeserializer<?> valueDeser,
68+
ImmutableMap.Builder<Object, Object> builder) throws IOException
69+
{
70+
// 14-Sep-2015, tatu: As per [datatype-guava#52], avoid exception due to null
71+
// TODO: allow reporting problem via a feature, in future?
72+
73+
// Actually, first, see if there's an alternative to Java null
74+
Object nvl = valueDeser.getNullValue(ctxt);
75+
if (nvl != null) {
76+
builder.put(fieldName, nvl);
77+
}
78+
}
79+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ public void testImmutableMap() throws Exception
185185
map = MAPPER.readValue("{}", type);
186186
assertNotNull(map);
187187
assertEquals(0, map.size());
188+
189+
// and for [datatype-guava#52], verify allowance of JSON nulls
190+
map = MAPPER.readValue("{\"12\":true,\"4\":null}", type);
191+
assertEquals(1, map.size());
188192
}
189193

190194
public void testTypedImmutableMap() throws Exception

0 commit comments

Comments
 (0)