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

Commit

Permalink
Fix #52
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 14, 2015
1 parent 71bed6b commit c55b8e7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
3 changes: 3 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ Alexey Kobyakov (akobiakov@github)
* Reported #64: `@JsonUnwrapped` annotation is ignored when a field is an Optional
(2.6.0)

Jose Thomas (josethomas@github)
* Contributed #52: Guava collection types do not allow null values
(2.6.2)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project: jackson-datatype-guava

2.6.2 (not yet released)

#52: Guava collection types do not allow null values
(contributed by Jose T)
#80: Relax OSGi version constraints for Guava dependency.
(requested by Benson M)
#82: Problem with polymorphic value types for `Optional`, with 2.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,30 @@ abstract class GuavaImmutableMapDeserializer<T extends ImmutableMap<Object, Obje
protected abstract ImmutableMap.Builder<Object, Object> createBuilder();

@Override
protected T _deserializeEntries(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
protected T _deserializeEntries(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
final KeyDeserializer keyDes = _keyDeserializer;
final JsonDeserializer<?> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _typeDeserializerForValue;

ImmutableMap.Builder<Object, Object> builder = createBuilder();
for (; jp.getCurrentToken() == JsonToken.FIELD_NAME; jp.nextToken()) {
for (; p.getCurrentToken() == JsonToken.FIELD_NAME; p.nextToken()) {
// Must point to field name now
String fieldName = jp.getCurrentName();
String fieldName = p.getCurrentName();
Object key = (keyDes == null) ? fieldName : keyDes.deserializeKey(fieldName, ctxt);
// And then the value...
JsonToken t = jp.nextToken();
JsonToken t = p.nextToken();
// 28-Nov-2010, tatu: Should probably support "ignorable properties" in future...
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
_handleNull(ctxt, fieldName, _valueDeserializer, builder);
continue;
}
if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(jp, ctxt, typeDeser);
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
builder.put(key, value);
}
Expand All @@ -54,4 +57,23 @@ protected T _deserializeEntries(JsonParser jp, DeserializationContext ctxt) thro
return map;
}

}
/**
* Overridable helper method called when a JSON null value is encountered.
* Since Guava Maps typically do not allow null values, special handling
* is needed; default is to simply ignore and skip such values, but alternative
* could be to throw an exception.
*/
protected void _handleNull(DeserializationContext ctxt, String fieldName,
JsonDeserializer<?> valueDeser,
ImmutableMap.Builder<Object, Object> builder) throws IOException
{
// 14-Sep-2015, tatu: As per [datatype-guava#52], avoid exception due to null
// TODO: allow reporting problem via a feature, in future?

// Actually, first, see if there's an alternative to Java null
Object nvl = valueDeser.getNullValue(ctxt);
if (nvl != null) {
builder.put(fieldName, nvl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ public void testImmutableMap() throws Exception
map = MAPPER.readValue("{}", type);
assertNotNull(map);
assertEquals(0, map.size());

// and for [datatype-guava#52], verify allowance of JSON nulls
map = MAPPER.readValue("{\"12\":true,\"4\":null}", type);
assertEquals(1, map.size());
}

public void testTypedImmutableMap() throws Exception
Expand Down

0 comments on commit c55b8e7

Please sign in to comment.