Skip to content

Commit 0b5aca5

Browse files
authored
Move date/time-related SerializationFeatures into DateTimeFeature (#5085)
1 parent e6f3789 commit 0b5aca5

File tree

56 files changed

+469
-429
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+469
-429
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Versions: 3.x (for earlier see VERSION-2.x)
88
3.0.0-rc3 (not yet released)
99

1010
#5065: Change default for `DateTimeFeature.ONE_BASED_MONTHS` to `true` in 3.0
11+
#5066: Move date/time `SerializationFeature`s into `DateTimeFeature` (3.0)
1112
- Branch rename "master" -> "3.x" [JSTEP-12]
1213

1314
3.0.0-rc3 (not yet released)

src/main/java/tools/jackson/databind/DeserializationFeature.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public enum DeserializationFeature implements ConfigFeature
458458
* and this setting <b>has no effect</b> on such types.
459459
*<p>
460460
* If disabled, standard millisecond timestamps are assumed.
461-
* This is the counterpart to {@link SerializationFeature#WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS}.
461+
* This is the counterpart to {@link DateTimeFeature#WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS}.
462462
*<p>
463463
* Feature is enabled by default, to support most accurate time values possible.
464464
*/

src/main/java/tools/jackson/databind/ObjectMapper.java

+9
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,15 @@ public boolean isEnabled(SerializationFeature f) {
547547
return _serializationConfig.isEnabled(f);
548548
}
549549

550+
/**
551+
* Method for checking whether given datatype-specific
552+
* feature is enabled.
553+
*/
554+
public boolean isEnabled(DatatypeFeature f) {
555+
// could call either config object:
556+
return _serializationConfig.isEnabled(f);
557+
}
558+
550559
/*
551560
/**********************************************************************
552561
/* Configuration, accessing module information

src/main/java/tools/jackson/databind/SerializationConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ public SerializationConfig with(DateFormat df) {
236236
SerializationConfig cfg = super.with(df);
237237
// Also need to toggle this feature based on existence of date format:
238238
if (df == null) {
239-
return cfg.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
239+
return cfg.with(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS);
240240
}
241-
return cfg.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
241+
return cfg.without(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS);
242242
}
243243

244244
/*

src/main/java/tools/jackson/databind/SerializationContext.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import tools.jackson.databind.cfg.ContextAttributes;
1818
import tools.jackson.databind.cfg.DatatypeFeature;
1919
import tools.jackson.databind.cfg.DatatypeFeatures;
20+
import tools.jackson.databind.cfg.DateTimeFeature;
2021
import tools.jackson.databind.cfg.GeneratorSettings;
2122
import tools.jackson.databind.exc.InvalidDefinitionException;
2223
import tools.jackson.databind.exc.InvalidTypeIdException;
@@ -1180,7 +1181,7 @@ public final void defaultSerializeProperty(String propertyName, Object value, Js
11801181
public final void defaultSerializeDateValue(long timestamp, JsonGenerator g)
11811182
throws JacksonException
11821183
{
1183-
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
1184+
if (isEnabled(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)) {
11841185
g.writeNumber(timestamp);
11851186
} else {
11861187
g.writeString(_dateFormat().format(new Date(timestamp)));
@@ -1197,7 +1198,7 @@ public final void defaultSerializeDateValue(long timestamp, JsonGenerator g)
11971198
public final void defaultSerializeDateValue(Date date, JsonGenerator g)
11981199
throws JacksonException
11991200
{
1200-
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
1201+
if (isEnabled(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)) {
12011202
g.writeNumber(date.getTime());
12021203
} else {
12031204
g.writeString(_dateFormat().format(date));
@@ -1206,13 +1207,13 @@ public final void defaultSerializeDateValue(Date date, JsonGenerator g)
12061207

12071208
/**
12081209
* Method that will handle serialization of Dates used as {@link java.util.Map} keys,
1209-
* based on {@link SerializationFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
1210+
* based on {@link DateTimeFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
12101211
* value (and if using textual representation, configured date format)
12111212
*/
12121213
public void defaultSerializeDateKey(long timestamp, JsonGenerator g)
12131214
throws JacksonException
12141215
{
1215-
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
1216+
if (isEnabled(DateTimeFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
12161217
g.writeName(String.valueOf(timestamp));
12171218
} else {
12181219
g.writeName(_dateFormat().format(new Date(timestamp)));
@@ -1221,12 +1222,12 @@ public void defaultSerializeDateKey(long timestamp, JsonGenerator g)
12211222

12221223
/**
12231224
* Method that will handle serialization of Dates used as {@link java.util.Map} keys,
1224-
* based on {@link SerializationFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
1225+
* based on {@link DateTimeFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
12251226
* value (and if using textual representation, configured date format)
12261227
*/
12271228
public void defaultSerializeDateKey(Date date, JsonGenerator g) throws JacksonException
12281229
{
1229-
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
1230+
if (isEnabled(DateTimeFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
12301231
g.writeName(String.valueOf(date.getTime()));
12311232
} else {
12321233
g.writeName(_dateFormat().format(date));

src/main/java/tools/jackson/databind/SerializationFeature.java

-104
Original file line numberDiff line numberDiff line change
@@ -172,110 +172,6 @@ public enum SerializationFeature implements ConfigFeature
172172
/**********************************************************************
173173
*/
174174

175-
/**
176-
* Feature that determines whether Date (and date/time) values
177-
* (and Date-based things like {@link java.util.Calendar}s) are to be
178-
* serialized as numeric time stamps (true; the default),
179-
* or as something else (usually textual representation).
180-
* If textual representation is used, the actual format depends on configuration
181-
* settings including possible per-property use of {@code @JsonFormat} annotation,
182-
* globally configured {@link java.text.DateFormat}.
183-
*<p>
184-
* For "classic" JDK date types ({@link java.util.Date}, {@link java.util.Calendar})
185-
* the default formatting is provided by {@link tools.jackson.databind.util.StdDateFormat},
186-
* and corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSX"
187-
* (see {@link java.text.DateFormat} for details of format Strings).
188-
* Whether this feature affects handling of other date-related
189-
* types depend on handlers of those types, although ideally they
190-
* should use this feature
191-
*<p>
192-
* Note: whether {@link java.util.Map} keys are serialized as Strings
193-
* or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS} instead of
194-
* this feature.
195-
*<p>
196-
* Feature is disabled by default as of Jackson 3.0 (in 2.x it was enabled),
197-
* so that date/time are by default serialized as textual values NOT timestamps.
198-
*/
199-
WRITE_DATES_AS_TIMESTAMPS(false),
200-
201-
/**
202-
* Feature that determines whether {@link java.util.Date}s
203-
* (and sub-types) used as {@link java.util.Map} keys are serialized
204-
* as time stamps or not (if not, will be serialized as textual values).
205-
*<p>
206-
* Default value is 'false', meaning that Date-valued Map keys are serialized
207-
* as textual (ISO-8601) values.
208-
*<p>
209-
* Feature is disabled by default.
210-
*/
211-
WRITE_DATE_KEYS_AS_TIMESTAMPS(false),
212-
213-
/**
214-
* Feature that controls whether numeric timestamp values are
215-
* to be written using nanosecond timestamps (enabled) or not (disabled);
216-
* <b>if and only if</b> datatype supports such resolution.
217-
* Only newer datatypes (such as Java8 Date/Time) support such resolution --
218-
* older types (pre-Java8 <b>java.util.Date</b> etc) and Joda do not --
219-
* and this setting <b>has no effect</b> on such types.
220-
*<p>
221-
* If disabled, standard millisecond timestamps are assumed.
222-
* This is the counterpart to {@link DeserializationFeature#READ_DATE_TIMESTAMPS_AS_NANOSECONDS}.
223-
*<p>
224-
* Feature is enabled by default, to support most accurate time values possible.
225-
*/
226-
WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS(true),
227-
228-
/**
229-
* Feature that determines whether date/date-time values should be serialized
230-
* so that they include timezone id, in cases where type itself contains
231-
* timezone information. Including this information may lead to compatibility
232-
* issues because ISO-8601 specification does not define formats that include
233-
* such information.
234-
*<p>
235-
* If enabled, Timezone id should be included using format specified
236-
* with Java 8 <code>DateTimeFormatter#ISO_ZONED_DATE_TIME</code> definition
237-
* (for example, '2011-12-03T10:15:30+01:00[Europe/Paris]').
238-
*<p>
239-
* Note: setting has no relevance if date/time values are serialized as timestamps.
240-
*<p>
241-
* Feature is disabled by default, so that zone id is NOT included; rather, timezone
242-
* offset is used for ISO-8601 compatibility (if any timezone information is
243-
* included in value).
244-
*/
245-
WRITE_DATES_WITH_ZONE_ID(false),
246-
247-
/**
248-
* Feature that determines whether timezone/offset included in zoned date/time
249-
* values (note: does NOT {@link java.util.Date} will be overridden if there
250-
* is an explicitly set context time zone.
251-
* If disabled, timezone/offset value is used-is; if enabled, context time zone
252-
* is used instead.
253-
*<p>
254-
* Note that this setting only affects "Zoned" date/time values of
255-
* {@code Java 8 date/time} types -- it will have no effect on old
256-
* {@link java.util} value handling (of which {@link java.util.Date} has no timezone
257-
* information and must use contextual timezone, implicit or explicit; and
258-
* {@link java.util.Calendar} which will always use timezone Calendar value has).
259-
* Setting is also ignored by Joda date/time values.
260-
*<p>
261-
* Feature is enabled by default for backwards-compatibility purposes
262-
* (from Jackson 2.x)
263-
*/
264-
WRITE_DATES_WITH_CONTEXT_TIME_ZONE(true),
265-
266-
/**
267-
* Feature that determines whether time values that represents time periods
268-
* (durations, periods, ranges) are to be serialized by default using
269-
* a numeric (true) or textual (false) representations. Note that numeric
270-
* representation may mean either simple number, or an array of numbers,
271-
* depending on type.
272-
*<p>
273-
* Feature is disabled by default as of Jackson 3.0 (in 2.x it was enabled),
274-
* so that period/duration are by default serialized as textual values,
275-
* NOT timestamps.
276-
*/
277-
WRITE_DURATIONS_AS_TIMESTAMPS(false),
278-
279175
/**
280176
* Feature that determines how type <code>char[]</code> is serialized:
281177
* when enabled, will be serialized as an explict JSON array (with

src/main/java/tools/jackson/databind/cfg/DateTimeFeature.java

+123-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tools.jackson.databind.cfg;
22

3+
import tools.jackson.databind.DeserializationFeature;
4+
35
/**
46
* Configurable on/off features to configure Date/Time handling.
57
* Mostly used to configure
@@ -58,8 +60,128 @@ public enum DateTimeFeature implements DatatypeFeature
5860
* Default setting is disabled, for backwards-compatibility with
5961
* Jackson 2.18.
6062
*/
61-
USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING(false)
63+
USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING(false),
64+
65+
/**
66+
* Feature that determines whether Date (and date/time) values
67+
* (and Date-based things like {@link java.util.Calendar}s) are to be
68+
* serialized as numeric time stamps (true; the default),
69+
* or as something else (usually textual representation).
70+
* If textual representation is used, the actual format depends on configuration
71+
* settings including possible per-property use of {@code @JsonFormat} annotation,
72+
* globally configured {@link java.text.DateFormat}.
73+
*<p>
74+
* For "classic" JDK date types ({@link java.util.Date}, {@link java.util.Calendar})
75+
* the default formatting is provided by {@link tools.jackson.databind.util.StdDateFormat},
76+
* and corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSX"
77+
* (see {@link java.text.DateFormat} for details of format Strings).
78+
* Whether this feature affects handling of other date-related
79+
* types depend on handlers of those types, although ideally they
80+
* should use this feature
81+
*<p>
82+
* Note: whether {@link java.util.Map} keys are serialized as Strings
83+
* or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS} instead of
84+
* this feature.
85+
*<p>
86+
* Feature used to be one of {@link tools.jackson.databind.SerializationFeature}s
87+
* in Jackson 2.x but was moved here in 3.0.
88+
*<p>
89+
* Feature is disabled by default as of Jackson 3.0 (in 2.x it was enabled),
90+
* so that date/time are by default serialized as textual values NOT timestamps.
91+
*/
92+
WRITE_DATES_AS_TIMESTAMPS(false),
6293

94+
/**
95+
* Feature that determines whether {@link java.util.Date}s
96+
* (and sub-types) used as {@link java.util.Map} keys are serialized
97+
* as time stamps or not (if not, will be serialized as textual values).
98+
*<p>
99+
* Feature used to be one of {@link tools.jackson.databind.SerializationFeature}s
100+
* in Jackson 2.x but was moved here in 3.0.
101+
*<p>
102+
* Feature is disabled by default, meaning that Date-valued Map keys are serialized
103+
* as textual (ISO-8601) values.
104+
*/
105+
WRITE_DATE_KEYS_AS_TIMESTAMPS(false),
106+
107+
/**
108+
* Feature that controls whether numeric timestamp values are
109+
* to be written using nanosecond timestamps (enabled) or not (disabled);
110+
* <b>if and only if</b> datatype supports such resolution.
111+
* Only newer datatypes (such as Java8 Date/Time) support such resolution --
112+
* older types (pre-Java8 <b>java.util.Date</b> etc) and Joda do not --
113+
* and this setting <b>has no effect</b> on such types.
114+
*<p>
115+
* If disabled, standard millisecond timestamps are assumed.
116+
* This is the counterpart to {@link DeserializationFeature#READ_DATE_TIMESTAMPS_AS_NANOSECONDS}.
117+
*<p>
118+
* Feature used to be one of {@link tools.jackson.databind.SerializationFeature}s
119+
* in Jackson 2.x but was moved here in 3.0.
120+
*<p>
121+
* Feature is enabled by default, to support most accurate time values possible.
122+
*/
123+
WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS(true),
124+
125+
/**
126+
* Feature that determines whether timezone/offset included in zoned date/time
127+
* values (note: does NOT {@link java.util.Date} will be overridden if there
128+
* is an explicitly set context time zone.
129+
* If disabled, timezone/offset value is used-is; if enabled, context time zone
130+
* is used instead.
131+
*<p>
132+
* Note that this setting only affects "Zoned" date/time values of
133+
* {@code Java 8 date/time} types -- it will have no effect on old
134+
* {@link java.util} value handling (of which {@link java.util.Date} has no timezone
135+
* information and must use contextual timezone, implicit or explicit; and
136+
* {@link java.util.Calendar} which will always use timezone Calendar value has).
137+
* Setting is also ignored by Joda date/time values.
138+
*<p>
139+
* Feature used to be one of {@link tools.jackson.databind.SerializationFeature}s
140+
* in Jackson 2.x but was moved here in 3.0.
141+
*<p>
142+
* Feature is enabled by default for backwards-compatibility purposes
143+
* (from Jackson 2.x)
144+
*/
145+
WRITE_DATES_WITH_CONTEXT_TIME_ZONE(true),
146+
147+
/**
148+
* Feature that determines whether date/date-time values should be serialized
149+
* so that they include timezone id, in cases where type itself contains
150+
* timezone information. Including this information may lead to compatibility
151+
* issues because ISO-8601 specification does not define formats that include
152+
* such information.
153+
*<p>
154+
* If enabled, Timezone id should be included using format specified
155+
* with Java 8 <code>DateTimeFormatter#ISO_ZONED_DATE_TIME</code> definition
156+
* (for example, '2011-12-03T10:15:30+01:00[Europe/Paris]').
157+
*<p>
158+
* Note: setting has no relevance if date/time values are serialized as timestamps.
159+
*<p>
160+
* Feature used to be one of {@link tools.jackson.databind.SerializationFeature}s
161+
* in Jackson 2.x but was moved here in 3.0.
162+
*<p>
163+
* Feature is disabled by default, so that zone id is NOT included; rather, timezone
164+
* offset is used for ISO-8601 compatibility (if any timezone information is
165+
* included in value).
166+
*/
167+
WRITE_DATES_WITH_ZONE_ID(false),
168+
169+
/**
170+
* Feature that determines whether time values that represents time periods
171+
* (durations, periods, ranges) are to be serialized by default using
172+
* a numeric (true) or textual (false) representations. Note that numeric
173+
* representation may mean either simple number, or an array of numbers,
174+
* depending on type.
175+
*<p>
176+
* Feature used to be one of {@link tools.jackson.databind.SerializationFeature}s
177+
* in Jackson 2.x but was moved here in 3.0.
178+
*<p>
179+
* Feature is disabled by default as of Jackson 3.0 (in 2.x it was enabled),
180+
* so that period/duration are by default serialized as textual values,
181+
* NOT timestamps.
182+
*/
183+
WRITE_DURATIONS_AS_TIMESTAMPS(false),
184+
63185
;
64186

65187
private final static int FEATURE_INDEX = DatatypeFeatures.FEATURE_INDEX_DATETIME;

src/main/java/tools/jackson/databind/cfg/MapperBuilder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -811,13 +811,13 @@ public B configureForJackson2() {
811811
return enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
812812
.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
813813
.enable(MapperFeature.USE_GETTERS_AS_SETTERS)
814-
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
815-
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
814+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES,
815+
DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
816816
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
817817
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
818818
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
819-
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
820-
.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
819+
.enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS,
820+
DateTimeFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
821821
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
822822
.disable(DateTimeFeature.ONE_BASED_MONTHS)
823823
;
@@ -1416,7 +1416,7 @@ public B addAbstractTypeResolver(AbstractTypeResolver resolver) {
14161416
*/
14171417
public B defaultDateFormat(DateFormat f) {
14181418
_baseSettings = _baseSettings.with(f);
1419-
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, (f == null));
1419+
configure(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS, (f == null));
14201420
return _this();
14211421
}
14221422

0 commit comments

Comments
 (0)