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

Commit

Permalink
Merge branch '2.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 14, 2015
2 parents b6d2983 + 71bed6b commit 2781061
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project: jackson-datatype-guava

#80: Relax OSGi version constraints for Guava dependency.
(requested by Benson M)
#82: Problem with polymorphic value types for `Optional`, with 2.6

2.6.1 (09-Aug-2015)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
(provider.isEnabled(MapperFeature.USE_STATIC_TYPING)
|| _referredType.isFinal())) {
return withResolved(property,
provider.findPrimaryPropertySerializer(_referredType, property),
_findSerializer(provider, _referredType, _property),
_unwrapper);
}
} else {
Expand Down Expand Up @@ -130,6 +130,7 @@ public boolean isEmpty(SerializerProvider prov, Optional<?> value) {
return (value == null) || !value.isPresent();
}

@Override
public boolean isUnwrappingSerializer() {
return (_unwrapper != null);
}
Expand Down Expand Up @@ -212,12 +213,26 @@ protected final JsonSerializer<Object> _findSerializer(SerializerProvider provid
{
JsonSerializer<Object> ser = _dynamicSerializers.serializerFor(type);
if (ser == null) {
ser = provider.findPrimaryPropertySerializer(type, _property);
ser = _findSerializer(provider, type, _property);
if (_unwrapper != null) {
ser = ser.unwrappingSerializer(_unwrapper);
}
_dynamicSerializers = _dynamicSerializers.newWith(type, ser);
}
return ser;
}

private final JsonSerializer<Object> _findSerializer(SerializerProvider provider,
Class<?> type, BeanProperty prop) throws JsonMappingException
{
// Important: ask for TYPED serializer, in case polymorphic handling is needed!
return provider.findTypedValueSerializer(type, true, prop);
}

private final JsonSerializer<Object> _findSerializer(SerializerProvider provider,
JavaType type, BeanProperty prop) throws JsonMappingException
{
// Important: ask for TYPED serializer, in case polymorphic handling is needed!
return provider.findTypedValueSerializer(type, true, prop);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.*;

import com.google.common.base.Optional;

public class OptionalBasicTest extends ModuleTestBase
Expand Down Expand Up @@ -37,6 +38,20 @@ public void link(Unit u) {
}
}

// To test handling of polymorphic value types

public static class Container {
public Optional<Contained> contained;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(name = "ContainedImpl", value = ContainedImpl.class),
})
public static interface Contained { }

public static class ContainedImpl implements Contained { }

/*
/**********************************************************************
/* Test methods
Expand Down Expand Up @@ -228,9 +243,23 @@ public void testOptionalCollection() throws Exception {
}
}

// [Issue#48]
// [datatype-guava#48]
public void testDeserNull() throws Exception {
Optional<?> value = MAPPER.readValue("\"\"", new TypeReference<Optional<Integer>>() {});
assertFalse(value.isPresent());
}

// [datatype-guava#81]
public void testPolymorphic() throws Exception
{
final Container dto = new Container();
dto.contained = Optional.of((Contained) new ContainedImpl());

final String json = MAPPER.writeValueAsString(dto);

final Container fromJson = MAPPER.readValue(json, Container.class);
assertNotNull(fromJson.contained);
assertTrue(fromJson.contained.isPresent());
assertSame(ContainedImpl.class, fromJson.contained.get().getClass());
}
}

0 comments on commit 2781061

Please sign in to comment.