Skip to content

Commit 53eed8e

Browse files
committed
Refactor deser side too
1 parent 41753dd commit 53eed8e

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ protected InstantDeserializer(Class<T> supportedType,
200200
*/
201201
@Deprecated()
202202
protected InstantDeserializer(Class<T> supportedType,
203-
DateTimeFormatter formatter,
204-
Function<TemporalAccessor, T> parsedToValue,
205-
Function<FromIntegerArguments, T> fromMilliseconds,
206-
Function<FromDecimalArguments, T> fromNanoseconds,
207-
BiFunction<T, ZoneId, T> adjust,
208-
boolean replaceZeroOffsetAsZ
203+
DateTimeFormatter formatter,
204+
Function<TemporalAccessor, T> parsedToValue,
205+
Function<FromIntegerArguments, T> fromMilliseconds,
206+
Function<FromDecimalArguments, T> fromNanoseconds,
207+
BiFunction<T, ZoneId, T> adjust,
208+
boolean replaceZeroOffsetAsZ
209209
) {
210210
this(supportedType, formatter, parsedToValue, fromMilliseconds, fromNanoseconds,
211211
adjust, replaceZeroOffsetAsZ,
@@ -300,23 +300,10 @@ protected InstantDeserializer(InstantDeserializer<T> base,
300300
}
301301

302302
/**
303-
* Factory method to create a new deserializer instance with a custom {@link DateTimeFormatter}.
304-
* This is primarily intended for {@link OffsetDateTime} and {@link ZonedDateTime} deserialization,
305-
* allowing customization of parsing behavior (e.g., defaulting offset values or controlling nano-second precision).
306-
*
307-
* @param base Base deserializer to copy settings from (typically one of the static instances like
308-
* {@link #OFFSET_DATE_TIME}, {@link #ZONED_DATE_TIME}, or {@link #INSTANT})
309-
* @param formatter Custom {@link DateTimeFormatter} to use for parsing
310-
* @return New deserializer instance with the custom formatter
311-
* @since 2.19
303+
* NOTE: {@code public} since 2.21
312304
*/
313-
public static <T extends Temporal> InstantDeserializer<T> withCustomFormatter(
314-
InstantDeserializer<T> base, DateTimeFormatter formatter) {
315-
return new InstantDeserializer<>(base, formatter);
316-
}
317-
318305
@Override
319-
protected InstantDeserializer<T> withDateFormat(DateTimeFormatter dtf) {
306+
public InstantDeserializer<T> withDateFormat(DateTimeFormatter dtf) {
320307
if (dtf == _formatter) {
321308
return this;
322309
}

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public OffsetDateTimeSerializer(OffsetDateTimeSerializer base, Boolean useTimest
3636
}
3737

3838
/**
39+
* Method for constructing a new {@code OffsetDateTimeSerializer} with settings
40+
* of this serializer but with custom {@link DateTimeFormatter} overrides.
41+
* Commonly used on {@code INSTANCE} like so:
42+
*<pre>
43+
* DateTimeFormatter dtf = new DateTimeFormatterBuilder()
44+
* .append(DateTimeFormatter.ISO_LOCAL_DATE)
45+
* .appendLiteral('T')
46+
* // and so on
47+
* .toFormatter();
48+
* OffsetDateTimeSerializer ser = OffsetDateTimeSerializer.INSTANCE
49+
* .withFormatter(dtf);
50+
* // register via Module
51+
*</pre>
52+
*
3953
* @since 2.21
4054
*/
4155
public OffsetDateTimeSerializer withFormatter(DateTimeFormatter formatter)

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
import com.fasterxml.jackson.annotation.JsonFormat;
1515
import com.fasterxml.jackson.annotation.JsonFormat.Feature;
16+
1617
import com.fasterxml.jackson.core.type.TypeReference;
17-
import com.fasterxml.jackson.databind.DeserializationFeature;
18-
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import com.fasterxml.jackson.databind.ObjectReader;
20-
import com.fasterxml.jackson.databind.SerializationFeature;
21-
import com.fasterxml.jackson.databind.json.JsonMapper;
18+
19+
import com.fasterxml.jackson.databind.*;
20+
import com.fasterxml.jackson.databind.module.SimpleModule;
2221
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
2322
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
2423
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
@@ -876,9 +875,9 @@ private static String offsetWithoutColon(String string){
876875
}
877876

878877
/*
879-
/**********************************************************
880-
/* Tests for custom formatter (#376)
881-
/**********************************************************
878+
/**********************************************************************
879+
/* Tests for custom formatter (modules-java8#376)
880+
/**********************************************************************
882881
*/
883882

884883
@Test
@@ -897,12 +896,11 @@ public void testDeserializationWithCustomFormatter() throws Exception
897896

898897
// Create custom deserializer with the custom formatter
899898
InstantDeserializer<OffsetDateTime> customDeserializer =
900-
InstantDeserializer.withCustomFormatter(InstantDeserializer.OFFSET_DATE_TIME, customFormatter);
899+
InstantDeserializer.OFFSET_DATE_TIME.withDateFormat(customFormatter);
901900

902901
// Create a custom module to override the default deserializer
903-
com.fasterxml.jackson.databind.module.SimpleModule customModule =
904-
new com.fasterxml.jackson.databind.module.SimpleModule("CustomOffsetDateTimeModule");
905-
customModule.addDeserializer(OffsetDateTime.class, customDeserializer);
902+
SimpleModule customModule = new SimpleModule("CustomOffsetDateTimeModule")
903+
.addDeserializer(OffsetDateTime.class, customDeserializer);
906904

907905
// Add both JavaTimeModule (for other types) and our custom module
908906
// The custom module will override OffsetDateTime deserialization
@@ -949,11 +947,9 @@ public void testDeserializationWithCustomFormatterRoundTrip() throws Exception
949947
.toFormatter();
950948

951949
InstantDeserializer<OffsetDateTime> customDeserializer =
952-
InstantDeserializer.withCustomFormatter(InstantDeserializer.OFFSET_DATE_TIME, customFormatter);
953-
954-
com.fasterxml.jackson.databind.module.SimpleModule customModule =
955-
new com.fasterxml.jackson.databind.module.SimpleModule();
956-
customModule.addDeserializer(OffsetDateTime.class, customDeserializer);
950+
InstantDeserializer.OFFSET_DATE_TIME.withDateFormat(customFormatter);
951+
SimpleModule customModule = new SimpleModule("CustomOffsetDateTimeModule")
952+
.addDeserializer(OffsetDateTime.class, customDeserializer);
957953

958954
ObjectMapper mapper = mapperBuilder()
959955
.addModule(customModule)

0 commit comments

Comments
 (0)