Skip to content
Open
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
68 changes: 51 additions & 17 deletions src/main/java/tools/jackson/databind/cfg/ConfigOverrides.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ConfigOverrides
implements java.io.Serializable,
Snapshottable<ConfigOverrides>
{
private static final long serialVersionUID = 3L;
private static final long serialVersionUID = 4L;

/**
* Convenience value used as the default root setting.
Expand Down Expand Up @@ -53,6 +53,11 @@ public class ConfigOverrides

protected Boolean _defaultLeniency;

/**
* @since 3.1
*/
protected JsonFormat.Value _defaultFormat;

/*
/**********************************************************************
/* Life cycle
Expand All @@ -64,20 +69,33 @@ public ConfigOverrides() {
INCLUDE_DEFAULT,
JsonSetter.Value.empty(),
DEFAULT_VISIBILITY_CHECKER,
null, null
null, null, JsonFormat.Value.empty()
);
}

protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
JsonInclude.Value defIncl, JsonSetter.Value defSetter,
VisibilityChecker defVisibility,
Boolean defMergeable, Boolean defLeniency) {
Boolean defMergeable, Boolean defLeniency, JsonFormat.Value defFormat) {
_overrides = overrides;
_defaultInclusion = defIncl;
_defaultNullHandling = defSetter;
_visibilityChecker = defVisibility;
_defaultMergeable = defMergeable;
_defaultLeniency = defLeniency;
_defaultFormat = defFormat;
}

/**
* @deprecated since 3.1
*/
@Deprecated
protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
JsonInclude.Value defIncl, JsonSetter.Value defSetter,
VisibilityChecker defVisibility,
Boolean defMergeable, Boolean defLeniency) {
this(overrides, defIncl, defSetter, defVisibility, defMergeable, defLeniency,
JsonFormat.Value.empty());
}

@Override
Expand All @@ -94,7 +112,7 @@ public ConfigOverrides snapshot()
}
return new ConfigOverrides(newOverrides,
_defaultInclusion, _defaultNullHandling, _visibilityChecker,
_defaultMergeable, _defaultLeniency);
_defaultMergeable, _defaultLeniency, _defaultFormat);
}

/*
Expand Down Expand Up @@ -128,26 +146,22 @@ public MutableConfigOverride findOrCreateOverride(Class<?> type) {
* override (if any).
*
* @return Default format settings for type; never null.
*
* @since 2.10
*/
public JsonFormat.Value findFormatDefaults(Class<?> type) {
JsonFormat.Value format = _defaultFormat;
if (_defaultLeniency != null) {
format = format.withLenient(_defaultLeniency);
}
if (_overrides != null) {
ConfigOverride override = _overrides.get(type);
if (override != null) {
JsonFormat.Value format = override.getFormat();
if (format != null) {
if (!format.hasLenient()) {
return format.withLenient(_defaultLeniency);
}
return format;
JsonFormat.Value formatOverride = override.getFormat();
if (formatOverride != null) {
format = format.withOverrides(formatOverride);
}
}
}
if (_defaultLeniency == null) {
return JsonFormat.Value.empty();
}
return JsonFormat.Value.forLeniency(_defaultLeniency);
return format;
}

/*
Expand Down Expand Up @@ -191,6 +205,17 @@ public VisibilityChecker getDefaultRecordVisibility() {
: _visibilityChecker;
}

/**
* Accessor for the global default {@code JsonFormat.Value} settings,
* not including possible per-type overrides (if you want to apply
* overrides, call {@link #findFormatDefaults} instead).
*
* @since 3.1
*/
public JsonFormat.Value getDefaultFormat() {
return _defaultFormat;
}

/*
/**********************************************************************
/* Global defaults mutators
Expand Down Expand Up @@ -227,6 +252,14 @@ public ConfigOverrides setDefaultVisibility(JsonAutoDetect.Value vis) {
return this;
}

/**
* @since 3.1
*/
public ConfigOverrides setDefaultFormat(JsonFormat.Value format) {
_defaultFormat = format;
return this;
}

/*
/**********************************************************************
/* Standard methods (for diagnostics)
Expand All @@ -241,6 +274,7 @@ public String toString() {
.append(", merge=").append(_defaultMergeable)
.append(", leniency=").append(_defaultLeniency)
.append(", visibility=").append(_visibilityChecker)
.append(", format=").append(_defaultFormat)
.append(", typed=")
;
if (_overrides == null) {
Expand All @@ -264,6 +298,6 @@ public String toString() {
*/

protected Map<Class<?>, MutableConfigOverride> _newMap() {
return new HashMap<Class<?>, MutableConfigOverride>();
return new HashMap<>();
}
}
11 changes: 11 additions & 0 deletions src/main/java/tools/jackson/databind/cfg/MapperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand Down Expand Up @@ -982,6 +983,16 @@ public B defaultLeniency(Boolean b) {
return _this();
}

/**
* Method for configuring default format settings to use for serialization and deserialization.
*
* @since 3.1
*/
public B defaultFormat(JsonFormat.Value format) {
_configOverrides.setDefaultFormat(format);
return _this();
}

/*
/**********************************************************************
/* Changing settings, coercion config
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/tools/jackson/databind/cfg/MapperConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,17 @@ public JsonInclude.Value getDefaultInclusion(Class<?> baseType,
return result;
}


/**
* Accessor for global default format settings to apply for serialization and
* deserialization.
* The format obtained from this accessor should have the lowest precedence,
* overridable by per-type and/or per-property format overrides.
*
* @since 3.1
*/
public abstract JsonFormat.Value getDefaultFormat();

/**
* Accessor for default format settings to use for serialization (and, to a degree
* deserialization), considering baseline settings and per-type defaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ public final JsonInclude.Value getDefaultInclusion(Class<?> baseType,
return def.withOverrides(v);
}

@Override // @since 3.1
public JsonFormat.Value getDefaultFormat() {
return _configOverrides.getDefaultFormat();
}

@Override
public final JsonFormat.Value getDefaultPropertyFormat(Class<?> type) {
return _configOverrides.findFormatDefaults(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ public Byte deserialize(JsonParser p, DeserializationContext ctxt) throws Jackso
return _parseByte(p, ctxt);
}


/**
* @since 3.1
*/
@Override
public ValueDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
{
return RadixDeserializerFactory.createRadixStringDeserializer(this,
findFormatOverrides(ctxt, property, handledType()));
}

protected Byte _parseByte(JsonParser p, DeserializationContext ctxt)
throws JacksonException
{
Expand Down Expand Up @@ -347,6 +358,17 @@ public ShortDeserializer(Class<Short> cls, Short nvl)
super(cls, LogicalType.Integer, nvl, (short)0);
}

/**
* @since 3.1
*/
@Override
public ValueDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
{
return RadixDeserializerFactory.createRadixStringDeserializer(this,
findFormatOverrides(ctxt, property, handledType()));
}

@Override
public Short deserialize(JsonParser p, DeserializationContext ctxt)
throws JacksonException
Expand Down Expand Up @@ -528,6 +550,17 @@ public IntegerDeserializer(Class<Integer> cls, Integer nvl) {
@Override
public boolean isCachable() { return true; }

/**
* @since 3.1
*/
@Override
public ValueDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
{
return RadixDeserializerFactory.createRadixStringDeserializer(this,
findFormatOverrides(ctxt, property, handledType()));
}

@Override
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException {
if (p.isExpectedNumberIntToken()) {
Expand Down Expand Up @@ -569,8 +602,21 @@ public LongDeserializer(Class<Long> cls, Long nvl) {
@Override
public boolean isCachable() { return true; }

/**
* @since 3.1
*/
@Override
public Long deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException {
public ValueDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
{
return RadixDeserializerFactory.createRadixStringDeserializer(this,
findFormatOverrides(ctxt, property, handledType()));
}

@Override
public Long deserialize(JsonParser p, DeserializationContext ctxt)
throws JacksonException
{
if (p.isExpectedNumberIntToken()) {
return p.getLongValue();
}
Expand All @@ -593,7 +639,8 @@ public FloatDeserializer(Class<Float> cls, Float nvl) {
}

@Override
public Float deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException
public Float deserialize(JsonParser p, DeserializationContext ctxt)
throws JacksonException
{
if (p.hasToken(JsonToken.VALUE_NUMBER_FLOAT)) {
return p.getFloatValue();
Expand Down Expand Up @@ -939,6 +986,17 @@ public final LogicalType logicalType() {
return LogicalType.Integer;
}

/**
* @since 3.1
*/
@Override
public ValueDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
{
return RadixDeserializerFactory.createRadixStringDeserializer(this,
findFormatOverrides(ctxt, property, handledType()));
}

@Override
public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tools.jackson.databind.deser.jdk;

import com.fasterxml.jackson.annotation.JsonFormat;

import tools.jackson.databind.deser.std.FromStringWithRadixToNumberDeserializer;
import tools.jackson.databind.deser.std.StdDeserializer;
import tools.jackson.databind.deser.std.StdScalarDeserializer;

/**
* Factory class for {@link FromStringWithRadixToNumberDeserializer} for deserializers in {@link tools.jackson.databind.deser.jdk.NumberDeserializers}
*
* @since 3.1
*/
public class RadixDeserializerFactory
{
public static StdDeserializer<? extends Number> createRadixStringDeserializer(
StdScalarDeserializer<? extends Number> initialDeser,
JsonFormat.Value formatOverrides)
{
if (formatOverrides != null && formatOverrides.getShape() == JsonFormat.Shape.STRING) {
if (isSerializeWithRadixOverride(formatOverrides)) {
int radix = formatOverrides.getRadix();
return new FromStringWithRadixToNumberDeserializer(initialDeser, radix);
}
}
return initialDeser;
}

/**
* Check if we have a proper {@link JsonFormat} annotation for serializing a number
* using an alternative radix specified in the annotation.
*/
private static boolean isSerializeWithRadixOverride(JsonFormat.Value format) {
return format.hasNonDefaultRadix();
}
}
Loading