Skip to content

Commit

Permalink
Changes wrt databind/3043 (SerializerProvider -> SerializationContext) (
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Nov 29, 2024
1 parent 50f57af commit 5fe4e2d
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
import tools.jackson.core.JsonParser;
import tools.jackson.core.JsonToken;

import tools.jackson.databind.BeanProperty;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.ValueSerializer;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.*;
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import tools.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor;
import tools.jackson.databind.jsonFormatVisitors.JsonValueFormat;
Expand Down Expand Up @@ -97,15 +93,15 @@ protected SerializationFeature getTimestampsFeature() {
}

@Override
public ValueSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property)
{
DurationSerializer ser = (DurationSerializer) super.createContextual(prov, property);
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
DurationSerializer ser = (DurationSerializer) super.createContextual(ctxt, property);
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
if (format != null && format.hasPattern()) {
final String pattern = format.getPattern();
DurationUnitConverter p = DurationUnitConverter.from(pattern);
if (p == null) {
prov.reportBadDefinition(handledType(),
ctxt.reportBadDefinition(handledType(),
String.format(
"Bad 'pattern' definition (\"%s\") for `Duration`: expected one of [%s]",
pattern, DurationUnitConverter.descForAllowed()));
Expand All @@ -116,15 +112,15 @@ public ValueSerializer<?> createContextual(SerializerProvider prov, BeanProperty
}

@Override
public void serialize(Duration duration, JsonGenerator generator, SerializerProvider provider)
public void serialize(Duration duration, JsonGenerator generator, SerializationContext ctxt)
throws JacksonException
{
if (useTimestamp(provider)) {
if (useTimestamp(ctxt)) {
// 03-Aug-2022, tatu: As per [modules-java8#224] need to consider
// Pattern first, and only then nano-seconds/millis difference
if (_durationUnitConverter != null) {
generator.writeNumber(_durationUnitConverter.convert(duration));
} else if (useNanoseconds(provider)) {
} else if (useNanoseconds(ctxt)) {
generator.writeNumber(_toNanos(duration));
} else {
generator.writeNumber(duration.toMillis());
Expand Down Expand Up @@ -157,8 +153,8 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp
JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
if (v2 != null) {
v2.numberType(JsonParser.NumberType.LONG);
SerializerProvider provider = visitor.getProvider();
if ((provider != null) && useNanoseconds(provider)) {
SerializationContext ctxt = visitor.getContext();
if ((ctxt != null) && useNanoseconds(ctxt)) {
// big number, no more specific qualifier to use...
} else { // otherwise good old Unix timestamp, in milliseconds
v2.format(JsonValueFormat.UTC_MILLISEC);
Expand All @@ -167,9 +163,9 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp
}

@Override
protected JsonToken serializationShape(SerializerProvider provider) {
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
protected JsonToken serializationShape(SerializationContext ctxt) {
if (useTimestamp(ctxt)) {
if (useNanoseconds(ctxt)) {
return JsonToken.VALUE_NUMBER_FLOAT;
}
return JsonToken.VALUE_NUMBER_INT;
Expand All @@ -183,7 +179,7 @@ protected JSR310FormattedSerializerBase<?> withFeatures(Boolean writeZoneId, Boo
}

@Override
protected DateTimeFormatter _useDateTimeFormatter(SerializerProvider prov, JsonFormat.Value format) {
protected DateTimeFormatter _useDateTimeFormatter(SerializationContext ctxt, JsonFormat.Value format) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import tools.jackson.core.JsonToken;
import tools.jackson.core.JsonParser.NumberType;

import tools.jackson.databind.JavaType;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.*;
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import tools.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor;
import tools.jackson.databind.jsonFormatVisitors.JsonNumberFormatVisitor;
Expand Down Expand Up @@ -83,11 +81,11 @@ protected abstract JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter
JsonFormat.Shape shape);

@Override
public void serialize(T value, JsonGenerator generator, SerializerProvider provider)
public void serialize(T value, JsonGenerator generator, SerializationContext ctxt)
throws JacksonException
{
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
if (useTimestamp(ctxt)) {
if (useNanoseconds(ctxt)) {
generator.writeNumber(DecimalUtils.toBigDecimal(
getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value)
));
Expand All @@ -97,14 +95,14 @@ public void serialize(T value, JsonGenerator generator, SerializerProvider provi
return;
}

generator.writeString(formatValue(value, provider));
generator.writeString(formatValue(value, ctxt));
}

// Overridden to ensure that our timestamp handling is as expected
@Override
protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
{
if (useNanoseconds(visitor.getProvider())) {
if (useNanoseconds(visitor.getContext())) {
JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
if (v2 != null) {
v2.numberType(NumberType.BIG_DECIMAL);
Expand All @@ -119,26 +117,26 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp
}

@Override
protected JsonToken serializationShape(SerializerProvider provider) {
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
protected JsonToken serializationShape(SerializationContext ctxt) {
if (useTimestamp(ctxt)) {
if (useNanoseconds(ctxt)) {
return JsonToken.VALUE_NUMBER_FLOAT;
}
return JsonToken.VALUE_NUMBER_INT;
}
return JsonToken.VALUE_STRING;
}

protected String formatValue(T value, SerializerProvider provider)
protected String formatValue(T value, SerializationContext ctxt)
{
DateTimeFormatter formatter = (_formatter != null) ? _formatter : defaultFormat;
if (formatter != null) {
if (formatter.getZone() == null) { // timezone set if annotated on property
// If the user specified to use the context TimeZone explicitly, and the formatter provided doesn't contain a TZ
// Then we use the TZ specified in the objectMapper
if (provider.getConfig().hasExplicitTimeZone()
&& provider.isEnabled(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)) {
formatter = formatter.withZone(provider.getTimeZone().toZoneId());
if (ctxt.getConfig().hasExplicitTimeZone()
&& ctxt.isEnabled(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)) {
formatter = formatter.withZone(ctxt.getTimeZone().toZoneId());
}
}
return formatter.format(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ protected JSR310FormattedSerializerBase<?> withFeatures(Boolean writeZoneId,
}

@Override
public ValueSerializer<?> createContextual(SerializerProvider prov,
public ValueSerializer<?> createContextual(SerializationContext ctxt,
BeanProperty property)
{
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
if (format != null) {
Boolean useTimestamp = null;

Expand All @@ -127,7 +127,7 @@ public ValueSerializer<?> createContextual(SerializerProvider prov,

// If not, do we have a pattern?
if (format.hasPattern()) {
dtf = _useDateTimeFormatter(prov, format);
dtf = _useDateTimeFormatter(ctxt, format);
}
JSR310FormattedSerializerBase<?> ser = this;
if ((shape != _shape) || (useTimestamp != _useTimestamp) || (dtf != _formatter)) {
Expand All @@ -150,7 +150,7 @@ public ValueSerializer<?> createContextual(SerializerProvider prov,
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
{
if (useTimestamp(visitor.getProvider())) {
if (useTimestamp(visitor.getContext())) {
_acceptTimestampVisitor(visitor, typeHint);
} else {
JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
Expand All @@ -164,16 +164,16 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp
{
// By default, most sub-types use JSON Array, so do this:
// 28-May-2019, tatu: serialized as a List<Integer>, presumably
JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(_integerListType(visitor.getProvider()));
JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(_integerListType(visitor.getContext()));
if (v2 != null) {
v2.itemsFormat(JsonFormatTypes.INTEGER);
}
}

protected JavaType _integerListType(SerializerProvider prov) {
protected JavaType _integerListType(SerializationContext ctxt) {
JavaType t = _integerListType;
if (t == null) {
t = prov.getTypeFactory()
t = ctxt.getTypeFactory()
.constructCollectionType(List.class, Integer.class);
_integerListType = t;
}
Expand All @@ -194,7 +194,7 @@ protected SerializationFeature getTimestampsFeature() {
return SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
}

protected boolean useTimestamp(SerializerProvider provider) {
protected boolean useTimestamp(SerializationContext ctxt) {
if (_useTimestamp != null) {
return _useTimestamp.booleanValue();
}
Expand All @@ -207,18 +207,17 @@ protected boolean useTimestamp(SerializerProvider provider) {
}
}
// assume that explicit formatter definition implies use of textual format
return (_formatter == null) && (provider != null)
&& provider.isEnabled(getTimestampsFeature());
return (_formatter == null) && (ctxt != null) && ctxt.isEnabled(getTimestampsFeature());
}

protected boolean _useTimestampExplicitOnly(SerializerProvider provider) {
protected boolean _useTimestampExplicitOnly(SerializationContext ctxt) {
if (_useTimestamp != null) {
return _useTimestamp.booleanValue();
}
return false;
}

protected boolean useNanoseconds(SerializerProvider provider) {
protected boolean useNanoseconds(SerializationContext ctxt) {
if (_useNanoseconds != null) {
return _useNanoseconds.booleanValue();
}
Expand All @@ -230,15 +229,15 @@ protected boolean useNanoseconds(SerializerProvider provider) {
return true;
}
}
return (provider != null)
&& provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
return (ctxt != null)
&& ctxt.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
}

// modules-java8#189: to be overridden by other formatters using this as base class
protected DateTimeFormatter _useDateTimeFormatter(SerializerProvider prov, JsonFormat.Value format) {
protected DateTimeFormatter _useDateTimeFormatter(SerializationContext ctxt, JsonFormat.Value format) {
DateTimeFormatter dtf;
final String pattern = format.getPattern();
final Locale locale = format.hasLocale() ? format.getLocale() : prov.getLocale();
final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
if (locale == null) {
dtf = DateTimeFormatter.ofPattern(pattern);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tools.jackson.core.JsonToken;
import tools.jackson.core.type.WritableTypeId;

import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.jsontype.TypeSerializer;
import tools.jackson.databind.ser.std.StdSerializer;

Expand All @@ -21,7 +21,7 @@ protected JSR310SerializerBase(Class<?> supportedType) {
}

@Override
public void serializeWithType(T value, JsonGenerator g, SerializerProvider ctxt,
public void serializeWithType(T value, JsonGenerator g, SerializationContext ctxt,
TypeSerializer typeSer)
throws JacksonException
{
Expand All @@ -36,5 +36,5 @@ public void serializeWithType(T value, JsonGenerator g, SerializerProvider ctxt,
* shape of value during serialization; needed to know how type id is to be
* serialized.
*/
protected abstract JsonToken serializationShape(SerializerProvider provider);
protected abstract JsonToken serializationShape(SerializationContext ctxt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ protected LocalDateSerializer withFormat(DateTimeFormatter dtf,
}

@Override
public void serialize(LocalDate date, JsonGenerator g, SerializerProvider provider)
public void serialize(LocalDate date, JsonGenerator g, SerializationContext ctxt)
throws JacksonException
{
if (useTimestamp(provider)) {
if (useTimestamp(ctxt)) {
if (_shape == JsonFormat.Shape.NUMBER_INT) {
g.writeNumber(date.toEpochDay());
} else {
g.writeStartArray();
_serializeAsArrayContents(date, g, provider);
_serializeAsArrayContents(date, g, ctxt);
g.writeEndArray();
}
} else {
Expand All @@ -78,7 +78,7 @@ public void serialize(LocalDate date, JsonGenerator g, SerializerProvider provid

@Override
public void serializeWithType(LocalDate value, JsonGenerator g,
SerializerProvider ctxt, TypeSerializer typeSer)
SerializationContext ctxt, TypeSerializer typeSer)
throws JacksonException
{
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
Expand All @@ -96,7 +96,7 @@ public void serializeWithType(LocalDate value, JsonGenerator g,
}

protected void _serializeAsArrayContents(LocalDate value, JsonGenerator g,
SerializerProvider provider)
SerializationContext ctxt)
throws JacksonException
{
g.writeNumber(value.getYear());
Expand All @@ -107,8 +107,8 @@ protected void _serializeAsArrayContents(LocalDate value, JsonGenerator g,
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
{
SerializerProvider provider = visitor.getProvider();
boolean useTimestamp = (provider != null) && useTimestamp(provider);
SerializationContext ctxt = visitor.getContext();
boolean useTimestamp = (ctxt != null) && useTimestamp(ctxt);
if (useTimestamp) {
_acceptTimestampVisitor(visitor, typeHint);
} else {
Expand All @@ -120,8 +120,8 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
}

@Override // since 2.9
protected JsonToken serializationShape(SerializerProvider provider) {
if (useTimestamp(provider)) {
protected JsonToken serializationShape(SerializationContext ctxt) {
if (useTimestamp(ctxt)) {
if (_shape == JsonFormat.Shape.NUMBER_INT) {
return JsonToken.VALUE_NUMBER_INT;
}
Expand Down
Loading

0 comments on commit 5fe4e2d

Please sign in to comment.