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

New configuration for Guava Range default bound type. #79

Merged
merged 1 commit into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
public class GuavaDeserializers
extends Deserializers.Base
{
private BoundType _defaultBoundType;

public GuavaDeserializers(BoundType defaultBoundType) {
_defaultBoundType = defaultBoundType;
}

/**
* We have plenty of collection types to support...
*/
Expand Down Expand Up @@ -251,7 +257,7 @@ public JsonDeserializer<?> findBeanDeserializer(final JavaType type, Deserializa
return new GuavaOptionalDeserializer(type, refType, typeDeser, valueDeser);
}
if (raw == Range.class) {
return new RangeDeserializer(type);
return new RangeDeserializer(_defaultBoundType, type);
}
if (raw == HostAndPort.class) {
return HostAndPortDeserializer.std;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.fasterxml.jackson.core.Version;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.PackageVersion;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok this caused the bug -- PackageVersion must not come from version of another module, like jackson-databind.
It is generated by Maven build. I removed this which fixes the unit test fail.

import com.fasterxml.jackson.datatype.guava.ser.GuavaBeanSerializerModifier;
import com.google.common.collect.BoundType;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Basic Jackson {@link Module} that adds support for Guava types.
Expand Down Expand Up @@ -37,6 +41,7 @@ public class GuavaModule extends Module // can't use just SimpleModule, due to g
* changes after registration will have no effect.
*/
protected boolean _cfgHandleAbsentAsNull = true;
protected BoundType _defaultBoundType;

public GuavaModule() {
super();
Expand All @@ -48,7 +53,7 @@ public GuavaModule() {
@Override
public void setupModule(SetupContext context)
{
context.addDeserializers(new GuavaDeserializers());
context.addDeserializers(new GuavaDeserializers(_defaultBoundType));
context.addSerializers(new GuavaSerializers());
context.addTypeModifier(new GuavaTypeModifier());

Expand All @@ -75,6 +80,24 @@ public GuavaModule configureAbsentsAsNulls(boolean state) {
_cfgHandleAbsentAsNull = state;
return this;
}

/**
* Configuration method that may be used to change the {@link BoundType} to be used
* when deserializing {@link com.google.common.collect.Range} objects. This configuration
* will is used when the object to be deserialied has no bound type attribute.
* The default {@link BoundType} is CLOSED.
*
* @param boundType {@link BoundType}
*
* @return This module instance, useful for chaining calls
*
* @since 2.6.1 ? FIXME
*/
public GuavaModule defaultBoundType(BoundType boundType) {
checkNotNull(boundType);
_defaultBoundType = boundType;
return this;
}

@Override
public int hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ public class RangeDeserializer

protected final JsonDeserializer<Object> _endpointDeserializer;

private BoundType _defaultBoundType;

/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/

public RangeDeserializer(JavaType rangeType) {
public RangeDeserializer(BoundType defaultBoundType, JavaType rangeType) {
this(rangeType, null);
this._defaultBoundType = defaultBoundType;
}

@SuppressWarnings("unchecked")
Expand All @@ -51,6 +54,15 @@ public RangeDeserializer(JavaType rangeType, JsonDeserializer<?> endpointDeser)
_endpointDeserializer = (JsonDeserializer<Object>) endpointDeser;
}

@SuppressWarnings("unchecked")
public RangeDeserializer(JavaType rangeType, JsonDeserializer<?> endpointDeser, BoundType defaultBoundType)
{
super(rangeType);
_rangeType = rangeType;
_endpointDeserializer = (JsonDeserializer<Object>) endpointDeser;
_defaultBoundType = defaultBoundType;
}

@Override
public JavaType getValueType() { return _rangeType; }

Expand All @@ -64,7 +76,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
endpointType = TypeFactory.unknownType();
}
JsonDeserializer<Object> deser = ctxt.findContextualValueDeserializer(endpointType, property);
return new RangeDeserializer(_rangeType, deser);
return new RangeDeserializer(_rangeType, deser, _defaultBoundType);
}
return this;
}
Expand Down Expand Up @@ -126,6 +138,12 @@ public Range<?> deserialize(JsonParser parser, DeserializationContext context)
}
}

if (lowerBoundType == null)
lowerBoundType = _defaultBoundType;

if (upperBoundType == null)
upperBoundType = _defaultBoundType;

try {
if ((lowerEndpoint != null) && (upperEndpoint != null)) {
Preconditions.checkState(lowerEndpoint.getClass() == upperEndpoint.getClass(),
Expand Down
146 changes: 146 additions & 0 deletions src/test/java/com/fasterxml/jackson/datatype/guava/TestRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.deser.util.RangeFactory;

import com.google.common.collect.BoundType;
import com.google.common.collect.Range;

import java.io.IOException;
Expand Down Expand Up @@ -103,4 +105,148 @@ public void testUntyped() throws Exception
assertNotNull(out);
assertEquals(Range.class, out.range.getClass());
}

public void testDefaultBoundTypeNoBoundTypeInformed() throws Exception
{
String json = "{\"lowerEndpoint\": 2, \"upperEndpoint\": 3}";

try {
MAPPER.readValue(json, Range.class);
fail("Should have failed");
} catch (JsonMappingException e) {
verifyException(e, "'lowerEndpoint' field found, but not 'lowerBoundType'");
}
}

public void testDefaultBoundTypeNoBoundTypeInformedWithClosedConfigured() throws Exception
{
String json = "{\"lowerEndpoint\": 2, \"upperEndpoint\": 3}";

GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED);
ObjectMapper mapper = new ObjectMapper().registerModule(mod);

@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class);

assertEquals(Integer.valueOf(2), r.lowerEndpoint());
assertEquals(Integer.valueOf(3), r.upperEndpoint());
assertEquals(BoundType.CLOSED, r.lowerBoundType());
assertEquals(BoundType.CLOSED, r.upperBoundType());
}

public void testDefaultBoundTypeOnlyLowerBoundTypeInformed() throws Exception
{
String json = "{\"lowerEndpoint\": 2, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3}";

try {
MAPPER.readValue(json, Range.class);
fail("Should have failed");
} catch (JsonMappingException e) {
verifyException(e, "'upperEndpoint' field found, but not 'upperBoundType'");
}
}

public void testDefaultBoundTypeOnlyLowerBoundTypeInformedWithClosedConfigured() throws Exception
{
String json = "{\"lowerEndpoint\": 2, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3}";

GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED);
ObjectMapper mapper = new ObjectMapper().registerModule(mod);

@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class);

assertEquals(Integer.valueOf(2), r.lowerEndpoint());
assertEquals(Integer.valueOf(3), r.upperEndpoint());
assertEquals(BoundType.OPEN, r.lowerBoundType());
assertEquals(BoundType.CLOSED, r.upperBoundType());
}

public void testDefaultBoundTypeOnlyUpperBoundTypeInformed() throws Exception
{
String json = "{\"lowerEndpoint\": 2, \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}";

try {
MAPPER.readValue(json, Range.class);
fail("Should have failed");
} catch (JsonMappingException e) {
verifyException(e, "'lowerEndpoint' field found, but not 'lowerBoundType'");
}
}

public void testDefaultBoundTypeOnlyUpperBoundTypeInformedWithClosedConfigured() throws Exception
{
String json = "{\"lowerEndpoint\": 1, \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}";

GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED);
ObjectMapper mapper = new ObjectMapper().registerModule(mod);

@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class);

assertEquals(Integer.valueOf(1), r.lowerEndpoint());
assertEquals(Integer.valueOf(3), r.upperEndpoint());
assertEquals(BoundType.CLOSED, r.lowerBoundType());
assertEquals(BoundType.OPEN, r.upperBoundType());
}

public void testDefaultBoundTypeBothBoundTypesOpen() throws Exception
{
String json = "{\"lowerEndpoint\": 2, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}";
@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) MAPPER.readValue(json, Range.class);

assertEquals(Integer.valueOf(2), r.lowerEndpoint());
assertEquals(Integer.valueOf(3), r.upperEndpoint());

assertEquals(BoundType.OPEN, r.lowerBoundType());
assertEquals(BoundType.OPEN, r.upperBoundType());
}

public void testDefaultBoundTypeBothBoundTypesOpenWithClosedConfigured() throws Exception
{
String json = "{\"lowerEndpoint\": 1, \"lowerBoundType\": \"OPEN\", \"upperEndpoint\": 3, \"upperBoundType\": \"OPEN\"}";

GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED);
ObjectMapper mapper = new ObjectMapper().registerModule(mod);

@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class);

assertEquals(Integer.valueOf(1), r.lowerEndpoint());
assertEquals(Integer.valueOf(3), r.upperEndpoint());

assertEquals(BoundType.OPEN, r.lowerBoundType());
assertEquals(BoundType.OPEN, r.upperBoundType());
}

public void testDefaultBoundTypeBothBoundTypesClosed() throws Exception
{
String json = "{\"lowerEndpoint\": 1, \"lowerBoundType\": \"CLOSED\", \"upperEndpoint\": 3, \"upperBoundType\": \"CLOSED\"}";
@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) MAPPER.readValue(json, Range.class);

assertEquals(Integer.valueOf(1), r.lowerEndpoint());
assertEquals(Integer.valueOf(3), r.upperEndpoint());

assertEquals(BoundType.CLOSED, r.lowerBoundType());
assertEquals(BoundType.CLOSED, r.upperBoundType());
}

public void testDefaultBoundTypeBothBoundTypesClosedWithOpenConfigured() throws Exception
{
String json = "{\"lowerEndpoint\": 12, \"lowerBoundType\": \"CLOSED\", \"upperEndpoint\": 33, \"upperBoundType\": \"CLOSED\"}";

GuavaModule mod = new GuavaModule().defaultBoundType(BoundType.CLOSED);
ObjectMapper mapper = new ObjectMapper().registerModule(mod);

@SuppressWarnings("unchecked")
Range<Integer> r = (Range<Integer>) mapper.readValue(json, Range.class);

assertEquals(Integer.valueOf(12), r.lowerEndpoint());
assertEquals(Integer.valueOf(33), r.upperEndpoint());

assertEquals(BoundType.CLOSED, r.lowerBoundType());
assertEquals(BoundType.CLOSED, r.upperBoundType());
}
}