Skip to content

Commit

Permalink
Fix #75
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 13, 2018
1 parent bf2e8dc commit 275e054
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 34 deletions.
2 changes: 1 addition & 1 deletion datatypes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<version>2.9.7-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion datetime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<version>2.9.7-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@
* </pre>
*<p>
* Note that as of 2.x, if auto-registering modules, this package will register
* legacy version, {@link JSR310Module}, and NOT this module. 3.x will change the efaults.
* legacy version, {@link JSR310Module}, and NOT this module. 3.x will change the default.
* Legacy version has the same functionality, but slightly different default configuration:
* see {@link com.fasterxml.jackson.datatype.jsr310.JSR310Module} for details.
*<p>
* Most {@code java.time} types are serialized as numbers (integers or decimals as appropriate) if the
* {@link com.fasterxml.jackson.databind.SerializationFeature#WRITE_DATES_AS_TIMESTAMPS} feature is enabled, and otherwise are serialized in
* standard <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">ISO-8601</a> string representation. ISO-8601 specifies formats
* for representing offset dates and times, zoned dates and times, local dates and times, periods, durations, zones, and more. All
* {@code java.time} types have built-in translation to and from ISO-8601 formats.
* {@link com.fasterxml.jackson.databind.SerializationFeature#WRITE_DATES_AS_TIMESTAMPS} feature is enabled
* (or, for {@link Duration}, {@link com.fasterxml.jackson.databind.SerializationFeature#WRITE_DURATIONS_AS_TIMESTAMPS}),
* and otherwise are serialized in standard
* <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">ISO-8601</a> string representation.
* ISO-8601 specifies formats for representing offset dates and times, zoned dates and times,
* local dates and times, periods, durations, zones, and more. All {@code java.time} types
* have built-in translation to and from ISO-8601 formats.
* <p>
* Granularity of timestamps is controlled through the companion features
* {@link com.fasterxml.jackson.databind.SerializationFeature#WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS} and
Expand Down Expand Up @@ -155,7 +158,6 @@ public JavaTimeModule()
addDeserializer(ZoneId.class, JSR310StringParsableDeserializer.ZONE_ID);
addDeserializer(ZoneOffset.class, JSR310StringParsableDeserializer.ZONE_OFFSET);


// then serializers:
addSerializer(Duration.class, DurationSerializer.INSTANCE);
addSerializer(Instant.class, InstantSerializer.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor;
Expand All @@ -34,6 +36,11 @@

/**
* Serializer for Java 8 temporal {@link Duration}s.
*<p>
* NOTE: since 2.10, {@link SerializationFeature#WRITE_DURATIONS_AS_TIMESTAMPS}
* determines global default used for determining if serialization should use
* numeric (timestamps) or textual representation. Before this,
* {@link SerializationFeature#WRITE_DATES_AS_TIMESTAMPS} was used.
*
* @author Nick Williams
* @since 2.2
Expand Down Expand Up @@ -62,7 +69,13 @@ protected DurationSerializer(DurationSerializer base,
protected DurationSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) {
return new DurationSerializer(this, useTimestamp, dtf);
}


// @since 2.10
@Override
protected SerializationFeature getTimestampsFeature() {
return SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS;
}

@Override
public void serialize(Duration duration, JsonGenerator generator, SerializerProvider provider) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaTyp
}
}

/**
* Overridable method that determines {@link SerializationFeature} that is used as
* the global default in determining if date/time value serialized should use numeric
* format ("timestamp") or not.
*<p>
* Note that this feature is just the baseline setting and may be overridden on per-type
* or per-property basis.
*
* @since 2.10
*/
protected SerializationFeature getTimestampsFeature() {
return SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
}

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

protected boolean _useTimestampExplicitOnly(SerializerProvider provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testSerializationAsTimestampNanoseconds01() throws Exception
{
Duration duration = Duration.ofSeconds(60L, 0);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -40,7 +40,7 @@ public void testSerializationAsTimestampNanoseconds02() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -53,7 +53,7 @@ public void testSerializationAsTimestampMilliseconds01() throws Exception
{
Duration duration = Duration.ofSeconds(60L, 0);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -66,7 +66,7 @@ public void testSerializationAsTimestampMilliseconds02() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -79,7 +79,7 @@ public void testSerializationAsTimestampMilliseconds03() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 837481723);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -92,7 +92,7 @@ public void testSerializationAsString01() throws Exception
{
Duration duration = Duration.ofSeconds(60L, 0);
String value = WRITER
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.writeValueAsString(duration);

assertNotNull("The value should not be null.", value);
Expand All @@ -104,7 +104,7 @@ public void testSerializationAsString02() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = WRITER
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.writeValueAsString(duration);

assertNotNull("The value should not be null.", value);
Expand All @@ -115,7 +115,7 @@ public void testSerializationAsString02() throws Exception
public void testSerializationWithTypeInfo01() throws Exception
{
ObjectMapper mapper = newMapper(); // need new to add mix-ins:
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class);
Duration duration = Duration.ofSeconds(13498L, 8374);
Expand All @@ -130,7 +130,7 @@ public void testSerializationWithTypeInfo01() throws Exception
public void testSerializationWithTypeInfo02() throws Exception
{
ObjectMapper mapper = newMapper(); // need new to add mix-ins:
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class);
Duration duration = Duration.ofSeconds(13498L, 837481723);
Expand All @@ -145,7 +145,7 @@ public void testSerializationWithTypeInfo02() throws Exception
public void testSerializationWithTypeInfo03() throws Exception
{
ObjectMapper mapper = newMapper(); // need new to add mix-ins:
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class);
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = mapper.writeValueAsString(duration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.Test;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeParseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testSerializationAsTimestampNanoseconds01() throws Exception
{
Duration duration = Duration.ofSeconds(60L, 0);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -41,7 +41,7 @@ public void testSerializationAsTimestampNanoseconds02() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -54,7 +54,7 @@ public void testSerializationAsTimestampMilliseconds01() throws Exception
{
Duration duration = Duration.ofSeconds(60L, 0);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -67,7 +67,7 @@ public void testSerializationAsTimestampMilliseconds02() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -80,7 +80,7 @@ public void testSerializationAsTimestampMilliseconds03() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 837481723);
String value = WRITER
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(duration);

Expand All @@ -93,7 +93,7 @@ public void testSerializationAsString01() throws Exception
{
Duration duration = Duration.ofSeconds(60L, 0);
String value = WRITER
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.writeValueAsString(duration);

assertNotNull("The value should not be null.", value);
Expand All @@ -105,7 +105,7 @@ public void testSerializationAsString02() throws Exception
{
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = WRITER
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.writeValueAsString(duration);

assertNotNull("The value should not be null.", value);
Expand All @@ -116,7 +116,7 @@ public void testSerializationAsString02() throws Exception
public void testSerializationWithTypeInfo01() throws Exception
{
ObjectMapper mapper = newMapper(); // need new to add mix-ins:
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class);
Duration duration = Duration.ofSeconds(13498L, 8374);
Expand All @@ -131,7 +131,7 @@ public void testSerializationWithTypeInfo01() throws Exception
public void testSerializationWithTypeInfo02() throws Exception
{
ObjectMapper mapper = newMapper(); // need new to add mix-ins:
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, true);
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class);
Duration duration = Duration.ofSeconds(13498L, 837481723);
Expand All @@ -146,7 +146,7 @@ public void testSerializationWithTypeInfo02() throws Exception
public void testSerializationWithTypeInfo03() throws Exception
{
ObjectMapper mapper = newMapper(); // need new to add mix-ins:
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class);
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = mapper.writeValueAsString(duration);
Expand Down
2 changes: 1 addition & 1 deletion parameter-names/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<version>2.9.7-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>
<artifactId>jackson-module-parameter-names</artifactId>
<name>Jackson-module-parameter-names</name>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<parent>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-base</artifactId>
<version>2.9.6</version>
<version>2.10.0-SNAPSHOT</version>
</parent>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<name>Jackson modules: Java 8</name>
<version>2.9.7-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>Parent pom for Jackson modules needed to support Java 8 features and types
</description>
Expand Down

0 comments on commit 275e054

Please sign in to comment.