From 5fe4e2d42aae34fdaa3eb85024233d57a799be39 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 29 Nov 2024 13:33:45 -0800 Subject: [PATCH] Changes wrt databind/3043 (SerializerProvider -> SerializationContext) (#331) --- .../jsr310/ser/DurationSerializer.java | 32 ++++++++----------- .../jsr310/ser/InstantSerializerBase.java | 28 ++++++++-------- .../ser/JSR310FormattedSerializerBase.java | 31 +++++++++--------- .../jsr310/ser/JSR310SerializerBase.java | 6 ++-- .../jsr310/ser/LocalDateSerializer.java | 18 +++++------ .../jsr310/ser/LocalDateTimeSerializer.java | 18 +++++------ .../jsr310/ser/LocalTimeSerializer.java | 20 ++++++------ .../jsr310/ser/MonthDaySerializer.java | 16 +++++----- .../jsr310/ser/OffsetTimeSerializer.java | 18 +++++------ .../jsr310/ser/OneBasedMonthSerializer.java | 2 +- .../jsr310/ser/YearMonthSerializer.java | 20 ++++++------ .../datatype/jsr310/ser/YearSerializer.java | 10 +++--- .../datatype/jsr310/ser/ZoneIdSerializer.java | 4 +-- .../jsr310/ser/ZonedDateTimeSerializer.java | 18 +++++------ .../ser/key/ZonedDateTimeKeySerializer.java | 18 +++++------ .../jsr310/misc/DateTimeSchemasTest.java | 22 ++++++------- 16 files changed, 137 insertions(+), 144 deletions(-) diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/DurationSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/DurationSerializer.java index ac7ba944..eba11e47 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/DurationSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/DurationSerializer.java @@ -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; @@ -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())); @@ -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()); @@ -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); @@ -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; @@ -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; } } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/InstantSerializerBase.java index f23a40a6..8d4941e3 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -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; @@ -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) )); @@ -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); @@ -119,9 +117,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; @@ -129,16 +127,16 @@ protected JsonToken serializationShape(SerializerProvider provider) { 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); diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java index 0758e992..7560cae8 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java @@ -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; @@ -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)) { @@ -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); @@ -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, 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; } @@ -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(); } @@ -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(); } @@ -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 { diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310SerializerBase.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310SerializerBase.java index 9dc7db4f..3d1c2d83 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310SerializerBase.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310SerializerBase.java @@ -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; @@ -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 { @@ -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); } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateSerializer.java index fa54ecc8..4f2ad469 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateSerializer.java @@ -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 { @@ -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, @@ -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()); @@ -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 { @@ -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; } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java index 12518312..17105f1f 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java @@ -25,7 +25,7 @@ import tools.jackson.core.JsonGenerator; 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; /** @@ -61,12 +61,12 @@ protected DateTimeFormatter _defaultFormatter() { } @Override - public void serialize(LocalDateTime value, JsonGenerator g, SerializerProvider provider) + public void serialize(LocalDateTime value, JsonGenerator g, SerializationContext ctxt) throws JacksonException { - if (useTimestamp(provider)) { + if (useTimestamp(ctxt)) { g.writeStartArray(); - _serializeAsArrayContents(value, g, provider); + _serializeAsArrayContents(value, g, ctxt); g.writeEndArray(); } else { DateTimeFormatter dtf = _formatter; @@ -78,7 +78,7 @@ public void serialize(LocalDateTime value, JsonGenerator g, SerializerProvider p } @Override - public void serializeWithType(LocalDateTime value, JsonGenerator g, SerializerProvider ctxt, + public void serializeWithType(LocalDateTime value, JsonGenerator g, SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException { @@ -99,7 +99,7 @@ public void serializeWithType(LocalDateTime value, JsonGenerator g, SerializerPr } private final void _serializeAsArrayContents(LocalDateTime value, JsonGenerator g, - SerializerProvider provider) + SerializationContext ctxt) throws JacksonException { g.writeNumber(value.getYear()); @@ -112,7 +112,7 @@ private final void _serializeAsArrayContents(LocalDateTime value, JsonGenerator if ((secs > 0) || (nanos > 0)) { g.writeNumber(secs); if (nanos > 0) { - if (useNanoseconds(provider)) { + if (useNanoseconds(ctxt)) { g.writeNumber(nanos); } else { g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND)); @@ -122,8 +122,8 @@ private final void _serializeAsArrayContents(LocalDateTime value, JsonGenerator } @Override - protected JsonToken serializationShape(SerializerProvider provider) { - return useTimestamp(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; + protected JsonToken serializationShape(SerializationContext ctxt) { + return useTimestamp(ctxt) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; } @Override diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalTimeSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalTimeSerializer.java index bd5acea3..2035fade 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalTimeSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/LocalTimeSerializer.java @@ -28,7 +28,7 @@ import tools.jackson.core.type.WritableTypeId; import tools.jackson.databind.JavaType; -import tools.jackson.databind.SerializerProvider; +import tools.jackson.databind.SerializationContext; import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; import tools.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor; import tools.jackson.databind.jsonFormatVisitors.JsonValueFormat; @@ -68,12 +68,12 @@ protected DateTimeFormatter _defaultFormatter() { } @Override - public void serialize(LocalTime value, JsonGenerator g, SerializerProvider provider) + public void serialize(LocalTime value, JsonGenerator g, SerializationContext ctxt) throws JacksonException { - if (useTimestamp(provider)) { + if (useTimestamp(ctxt)) { g.writeStartArray(); - _serializeAsArrayContents(value, g, provider); + _serializeAsArrayContents(value, g, ctxt); g.writeEndArray(); } else { DateTimeFormatter dtf = _formatter; @@ -86,7 +86,7 @@ public void serialize(LocalTime value, JsonGenerator g, SerializerProvider provi @Override public void serializeWithType(LocalTime value, JsonGenerator g, - SerializerProvider ctxt, TypeSerializer typeSer) + SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException { WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt, @@ -106,7 +106,7 @@ public void serializeWithType(LocalTime value, JsonGenerator g, } private final void _serializeAsArrayContents(LocalTime value, JsonGenerator g, - SerializerProvider provider) + SerializationContext ctxt) throws JacksonException { g.writeNumber(value.getHour()); @@ -117,7 +117,7 @@ private final void _serializeAsArrayContents(LocalTime value, JsonGenerator g, { g.writeNumber(secs); if (nanos > 0) { - if (useNanoseconds(provider)) { + if (useNanoseconds(ctxt)) { g.writeNumber(nanos); } else { g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND)); @@ -127,8 +127,8 @@ private final void _serializeAsArrayContents(LocalTime value, JsonGenerator g, } @Override - protected JsonToken serializationShape(SerializerProvider provider) { - return useTimestamp(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; + protected JsonToken serializationShape(SerializationContext ctxt) { + return useTimestamp(ctxt) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; } @Override @@ -141,7 +141,7 @@ protected JSR310FormattedSerializerBase withFeatures(Boolean writeZoneId, Boo @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); diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/MonthDaySerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/MonthDaySerializer.java index 8a6f8dcf..fbfc2bab 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/MonthDaySerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/MonthDaySerializer.java @@ -25,7 +25,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; /** @@ -60,12 +60,12 @@ protected MonthDaySerializer withFormat(DateTimeFormatter dtf, } @Override - public void serialize(MonthDay value, JsonGenerator g, SerializerProvider provider) + public void serialize(MonthDay value, JsonGenerator g, SerializationContext ctxt) throws JacksonException { - if (_useTimestampExplicitOnly(provider)) { + if (_useTimestampExplicitOnly(ctxt)) { g.writeStartArray(); - _serializeAsArrayContents(value, g, provider); + _serializeAsArrayContents(value, g, ctxt); g.writeEndArray(); } else { g.writeString((_formatter == null) ? value.toString() : value.format(_formatter)); @@ -74,7 +74,7 @@ public void serialize(MonthDay value, JsonGenerator g, SerializerProvider provid @Override public void serializeWithType(MonthDay value, JsonGenerator g, - SerializerProvider ctxt, TypeSerializer typeSer) + SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException { WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt, @@ -90,7 +90,7 @@ public void serializeWithType(MonthDay value, JsonGenerator g, } protected void _serializeAsArrayContents(MonthDay value, JsonGenerator g, - SerializerProvider provider) + SerializationContext ctxt) throws JacksonException { g.writeNumber(value.getMonthValue()); @@ -98,7 +98,7 @@ protected void _serializeAsArrayContents(MonthDay value, JsonGenerator g, } @Override - protected JsonToken serializationShape(SerializerProvider provider) { - return _useTimestampExplicitOnly(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; + protected JsonToken serializationShape(SerializationContext ctxt) { + return _useTimestampExplicitOnly(ctxt) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; } } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java index ce0c1fd1..3aec167c 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java @@ -25,7 +25,7 @@ import tools.jackson.core.JsonGenerator; 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; /** @@ -58,12 +58,12 @@ protected OffsetTimeSerializer withFormat(DateTimeFormatter dtf, } @Override - public void serialize(OffsetTime time, JsonGenerator g, SerializerProvider provider) + public void serialize(OffsetTime time, JsonGenerator g, SerializationContext ctxt) throws JacksonException { - if (useTimestamp(provider)) { + if (useTimestamp(ctxt)) { g.writeStartArray(); - _serializeAsArrayContents(time, g, provider); + _serializeAsArrayContents(time, g, ctxt); g.writeEndArray(); } else { String str = (_formatter == null) ? time.toString() : time.format(_formatter); @@ -72,7 +72,7 @@ public void serialize(OffsetTime time, JsonGenerator g, SerializerProvider provi } @Override - public void serializeWithType(OffsetTime value, JsonGenerator g, SerializerProvider ctxt, + public void serializeWithType(OffsetTime value, JsonGenerator g, SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException { @@ -90,7 +90,7 @@ public void serializeWithType(OffsetTime value, JsonGenerator g, SerializerProvi } private final void _serializeAsArrayContents(OffsetTime value, JsonGenerator g, - SerializerProvider provider) + SerializationContext ctxt) throws JacksonException { g.writeNumber(value.getHour()); @@ -100,7 +100,7 @@ private final void _serializeAsArrayContents(OffsetTime value, JsonGenerator g, if ((secs > 0) || (nanos > 0)) { g.writeNumber(secs); if (nanos > 0) { - if(useNanoseconds(provider)) { + if(useNanoseconds(ctxt)) { g.writeNumber(nanos); } else { g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND)); @@ -111,8 +111,8 @@ private final void _serializeAsArrayContents(OffsetTime value, JsonGenerator g, } @Override - protected JsonToken serializationShape(SerializerProvider provider) { - return useTimestamp(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; + protected JsonToken serializationShape(SerializationContext ctxt) { + return useTimestamp(ctxt) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; } @Override diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OneBasedMonthSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OneBasedMonthSerializer.java index cc7f44a3..08c4705f 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OneBasedMonthSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/OneBasedMonthSerializer.java @@ -19,7 +19,7 @@ public OneBasedMonthSerializer(ValueSerializer defaultSerializer) } @Override - public void serialize(Month value, JsonGenerator gen, SerializerProvider ctxt) + public void serialize(Month value, JsonGenerator gen, SerializationContext ctxt) { // 15-Jan-2024, tatu: [modules-java8#274] This is not really sufficient // (see `jackson-databind` `EnumSerializer` for full logic), but has to diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearMonthSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearMonthSerializer.java index 19bc3b93..50d47cbf 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearMonthSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearMonthSerializer.java @@ -27,7 +27,7 @@ import tools.jackson.core.type.WritableTypeId; import tools.jackson.databind.JavaType; -import tools.jackson.databind.SerializerProvider; +import tools.jackson.databind.SerializationContext; import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; import tools.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor; import tools.jackson.databind.jsonFormatVisitors.JsonValueFormat; @@ -63,12 +63,12 @@ protected YearMonthSerializer withFormat(DateTimeFormatter dtf, } @Override - public void serialize(YearMonth value, JsonGenerator g, SerializerProvider provider) + public void serialize(YearMonth value, JsonGenerator g, SerializationContext ctxt) throws JacksonException { - if (useTimestamp(provider)) { + if (useTimestamp(ctxt)) { g.writeStartArray(); - _serializeAsArrayContents(value, g, provider); + _serializeAsArrayContents(value, g, ctxt); g.writeEndArray(); return; } @@ -77,7 +77,7 @@ public void serialize(YearMonth value, JsonGenerator g, SerializerProvider provi @Override public void serializeWithType(YearMonth value, JsonGenerator g, - SerializerProvider ctxt, TypeSerializer typeSer) + SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException { WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt, @@ -93,7 +93,7 @@ public void serializeWithType(YearMonth value, JsonGenerator g, } protected void _serializeAsArrayContents(YearMonth value, JsonGenerator g, - SerializerProvider provider) + SerializationContext ctxt) throws JacksonException { g.writeNumber(value.getYear()); @@ -103,8 +103,8 @@ protected void _serializeAsArrayContents(YearMonth value, JsonGenerator g, @Override protected void _acceptTimestampVisitor(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) { super._acceptTimestampVisitor(visitor, typeHint); } else { @@ -116,7 +116,7 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp } @Override // since 2.9 - protected JsonToken serializationShape(SerializerProvider provider) { - return useTimestamp(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; + protected JsonToken serializationShape(SerializationContext ctxt) { + return useTimestamp(ctxt) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING; } } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearSerializer.java index e6ac60e5..03a74f05 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/YearSerializer.java @@ -26,7 +26,7 @@ import tools.jackson.core.JsonToken; import tools.jackson.databind.JavaType; -import tools.jackson.databind.SerializerProvider; +import tools.jackson.databind.SerializationContext; import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; import tools.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor; @@ -59,10 +59,10 @@ protected YearSerializer withFormat(DateTimeFormatter dtf, } @Override - public void serialize(Year year, JsonGenerator generator, SerializerProvider provider) + public void serialize(Year year, JsonGenerator generator, SerializationContext ctxt) throws JacksonException { - if (useTimestamp(provider)) { + if (useTimestamp(ctxt)) { generator.writeNumber(year.getValue()); } else { String str = (_formatter == null) ? year.toString() : year.format(_formatter); @@ -81,7 +81,7 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp } @Override // since 2.9 - protected JsonToken serializationShape(SerializerProvider provider) { - return useTimestamp(provider) ? JsonToken.VALUE_NUMBER_INT : JsonToken.VALUE_STRING; + protected JsonToken serializationShape(SerializationContext ctxt) { + return useTimestamp(ctxt) ? JsonToken.VALUE_NUMBER_INT : JsonToken.VALUE_STRING; } } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZoneIdSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZoneIdSerializer.java index ff56245b..093e9437 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZoneIdSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZoneIdSerializer.java @@ -6,7 +6,7 @@ import tools.jackson.core.JsonGenerator; 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.ToStringSerializerBase; @@ -16,7 +16,7 @@ public class ZoneIdSerializer extends ToStringSerializerBase @Override public void serializeWithType(Object value, JsonGenerator g, - SerializerProvider ctxt, TypeSerializer typeSer) + SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException { // Better ensure we don't use specific sub-classes: diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java index 8e110536..04d97115 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java @@ -8,8 +8,8 @@ import tools.jackson.core.JacksonException; import tools.jackson.core.JsonGenerator; import tools.jackson.core.JsonToken; +import tools.jackson.databind.SerializationContext; import tools.jackson.databind.SerializationFeature; -import tools.jackson.databind.SerializerProvider; public class ZonedDateTimeSerializer extends InstantSerializerBase { public static final ZonedDateTimeSerializer INSTANCE = new ZonedDateTimeSerializer(); @@ -62,29 +62,29 @@ protected JSR310FormattedSerializerBase withFeatures(Boolean writeZoneId, } @Override - public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider provider) + public void serialize(ZonedDateTime value, JsonGenerator g, SerializationContext ctxt) throws JacksonException { - if (!useTimestamp(provider)) { - if (shouldWriteWithZoneId(provider)) { + if (!useTimestamp(ctxt)) { + if (shouldWriteWithZoneId(ctxt)) { // write with zone g.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value)); return; } } - super.serialize(value, g, provider); + super.serialize(value, g, ctxt); } - public boolean shouldWriteWithZoneId(SerializerProvider ctxt) { + public boolean shouldWriteWithZoneId(SerializationContext ctxt) { return (_writeZoneId != null) ? _writeZoneId : ctxt.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); } @Override - protected JsonToken serializationShape(SerializerProvider provider) { - if (!useTimestamp(provider) && shouldWriteWithZoneId(provider)) { + protected JsonToken serializationShape(SerializationContext ctxt) { + if (!useTimestamp(ctxt) && shouldWriteWithZoneId(ctxt)) { return JsonToken.VALUE_STRING; } - return super.serializationShape(provider); + return super.serializationShape(ctxt); } } diff --git a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/key/ZonedDateTimeKeySerializer.java b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/key/ZonedDateTimeKeySerializer.java index 1f08fb37..d5297ca1 100644 --- a/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/key/ZonedDateTimeKeySerializer.java +++ b/datetime/src/main/java/tools/jackson/datatype/jsr310/ser/key/ZonedDateTimeKeySerializer.java @@ -8,8 +8,8 @@ import tools.jackson.databind.ValueSerializer; import tools.jackson.datatype.jsr310.util.DecimalUtils; +import tools.jackson.databind.SerializationContext; import tools.jackson.databind.SerializationFeature; -import tools.jackson.databind.SerializerProvider; public class ZonedDateTimeKeySerializer extends ValueSerializer { @@ -20,16 +20,16 @@ private ZonedDateTimeKeySerializer() { } @Override - public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider serializers) + public void serialize(ZonedDateTime value, JsonGenerator g, SerializationContext ctxt) throws JacksonException { /* [modules-java8#127]: Serialization of timezone data is disabled by default, but can be * turned on by enabling `SerializationFeature.WRITE_DATES_WITH_ZONE_ID` */ - if (serializers.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)) { + if (ctxt.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)) { g.writeName(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value)); - } else if (useTimestamps(serializers)) { - if (useNanos(serializers)) { + } else if (useTimestamps(ctxt)) { + if (useNanos(ctxt)) { g.writeName(DecimalUtils.toBigDecimal(value.toEpochSecond(), value.getNano()).toString()); } else { g.writeName(String.valueOf(value.toInstant().toEpochMilli())); @@ -39,11 +39,11 @@ public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider s } } - private static boolean useNanos(SerializerProvider serializers) { - return serializers.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); + private static boolean useNanos(SerializationContext ctxt) { + return ctxt.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); } - private static boolean useTimestamps(SerializerProvider serializers) { - return serializers.isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS); + private static boolean useTimestamps(SerializationContext ctxt) { + return ctxt.isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS); } } diff --git a/datetime/src/test/java/tools/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java b/datetime/src/test/java/tools/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java index 761937e8..a32d3d74 100644 --- a/datetime/src/test/java/tools/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java +++ b/datetime/src/test/java/tools/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java @@ -16,18 +16,18 @@ public class DateTimeSchemasTest extends ModuleTestBase { static class VisitorWrapper implements JsonFormatVisitorWrapper { - SerializerProvider serializerProvider; + SerializationContext serializationContext; final String baseName; final Map traversedProperties; - public VisitorWrapper(SerializerProvider serializerProvider, String baseName, Map traversedProperties) { - this.serializerProvider = serializerProvider; + public VisitorWrapper(SerializationContext ctxt, String baseName, Map traversedProperties) { + this.serializationContext = ctxt; this.baseName = baseName; this.traversedProperties = traversedProperties; } VisitorWrapper createSubtraverser(String bn) { - return new VisitorWrapper(getProvider(), bn, traversedProperties); + return new VisitorWrapper(getContext(), bn, traversedProperties); } public Map getTraversedProperties() { @@ -36,7 +36,7 @@ public Map getTraversedProperties() { @Override public JsonObjectFormatVisitor expectObjectFormat(JavaType type) { - return new JsonObjectFormatVisitor.Base(serializerProvider) { + return new JsonObjectFormatVisitor.Base(serializationContext) { @Override public void property(BeanProperty prop) { anyProperty(prop); @@ -50,7 +50,7 @@ public void optionalProperty(BeanProperty prop) { private void anyProperty(BeanProperty prop) { final String propertyName = prop.getFullName().toString(); traversedProperties.put(baseName + propertyName, ""); - serializerProvider.findPrimaryPropertySerializer(prop.getType(), prop) + serializationContext.findPrimaryPropertySerializer(prop.getType(), prop) .acceptJsonFormatVisitor(createSubtraverser(baseName + propertyName + "."), prop.getType()); } }; @@ -117,17 +117,17 @@ public JsonAnyFormatVisitor expectAnyFormat(JavaType type) { @Override public JsonMapFormatVisitor expectMapFormat(JavaType type) { traversedProperties.put(baseName, "MAP"); - return new JsonMapFormatVisitor.Base(serializerProvider); + return new JsonMapFormatVisitor.Base(serializationContext); } @Override - public SerializerProvider getProvider() { - return serializerProvider; + public SerializationContext getContext() { + return serializationContext; } @Override - public void setProvider(SerializerProvider provider) { - this.serializerProvider = provider; + public void setContext(SerializationContext ctxt) { + this.serializationContext = ctxt; } }