io.prestosql.jdbc.$internal.jackson.datatype.guava.GuavaModule Maven / Gradle / Ivy
package io.prestosql.jdbc.$internal.jackson.datatype.guava;
import io.prestosql.jdbc.$internal.jackson.core.Version;
import io.prestosql.jdbc.$internal.jackson.databind.*;
import io.prestosql.jdbc.$internal.jackson.datatype.guava.ser.GuavaBeanSerializerModifier;
import io.prestosql.jdbc.$internal.guava.collect.BoundType;
import static io.prestosql.jdbc.$internal.guava.base.Preconditions.checkNotNull;
/**
* Basic Jackson {@link Module} that adds support for Guava types.
*
* Current configurability includes:
*
* configureAbsentsAsNulls
(default: false
):
* Determines whether inclusion strategy of NON_NULL
should additionally consider
* Optional.absent()
values (as POJO properties) to be excluded; if true, they will
* be excluded, if false, they will be included.
* Note that the defaults for other "Optional" types are different; Guava setting is chosen solely
* for backwards compatibility.
*
*
*/
public class GuavaModule extends io.prestosql.jdbc.$internal.jackson.databind.Module // can't use just SimpleModule, due to generic types
{
private final String NAME = "GuavaModule";
/**
* Configuration setting that determines whether `Optional.absent()` is
* considered "same as null" for serialization purposes; that is, to be
* filtered same as nulls are.
* If enabled, absent values are treated like nulls; if disabled, they are not.
* In either case, absent values are always considered "empty".
*
* Default value is `true` for backwards compatibility (2.5 and prior
* only had this behavior).
*
* Note that this setting MUST be changed BEFORE registering the module:
* changes after registration will have no effect.
*/
protected boolean _cfgHandleAbsentAsNull = true;
protected BoundType _defaultBoundType;
public GuavaModule() {
super();
}
@Override public String getModuleName() { return NAME; }
@Override public Version version() { return PackageVersion.VERSION; }
@Override
public void setupModule(SetupContext context)
{
context.addDeserializers(new GuavaDeserializers(_defaultBoundType));
context.addSerializers(new GuavaSerializers());
context.addTypeModifier(new GuavaTypeModifier());
// 28-Apr-2015, tatu: Allow disabling "treat Optional.absent() like Java nulls"
if (_cfgHandleAbsentAsNull) {
context.addBeanSerializerModifier(new GuavaBeanSerializerModifier());
}
}
/**
* Configuration method that may be used to change configuration setting
* _cfgHandleAbsentAsNull
: enabling means that `Optional.absent()` values
* are handled like Java nulls (wrt filtering on serialization); disabling that
* they are only treated as "empty" values, but not like native Java nulls.
* Recommended setting for this value is `false`, for compatibility with other
* "optional" values (like JDK 8 optionals); but the default is `true` for
* backwards compatibility.
*
* @return This module instance, useful for chaining calls
*
* @since 2.6
*/
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 io.prestosql.jdbc.$internal.guava.collect.Range} objects. This configuration
* will is used when the object to be deserialized 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.7
*/
public GuavaModule defaultBoundType(BoundType boundType) {
checkNotNull(boundType);
_defaultBoundType = boundType;
return this;
}
@Override
public int hashCode() {
return NAME.hashCode();
}
@Override
public boolean equals(Object o) {
return this == o;
}
}