Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/#116 - Removes the fraction part based on the SerializationFeature WRITE_TIMESTAMP_WITHOUT_FRACTION #164

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e837c02
Issue/#116 (#4)
sashikanthr Feb 27, 2020
82b62c8
...
cowtowncoder Mar 3, 2020
42e136e
[maven-release-plugin] prepare release jackson-modules-java8-2.10.3
cowtowncoder Mar 3, 2020
20dd1bf
[maven-release-plugin] prepare for next development iteration
cowtowncoder Mar 3, 2020
bd852c6
...
cowtowncoder Mar 3, 2020
29077a4
Merge branch '2.10' into 2.11
cowtowncoder Mar 3, 2020
ff084de
Merge branch '2.11'
cowtowncoder Mar 3, 2020
9c5180d
move to 2.11.0.rc1-SNAPSHOT
cowtowncoder Mar 20, 2020
a662a89
Merge branch '2.11'
cowtowncoder Mar 20, 2020
a5ae713
prepare for 2.11.0.rc1
cowtowncoder Mar 24, 2020
eea7cae
[maven-release-plugin] prepare release jackson-modules-java8-2.11.0.rc1
cowtowncoder Mar 24, 2020
0ab6bba
[maven-release-plugin] prepare for next development iteration
cowtowncoder Mar 24, 2020
1534c55
Merge branch '2.11'
cowtowncoder Mar 24, 2020
4248377
Prepare for 2.11.0 release
cowtowncoder Apr 26, 2020
d97c8b7
minor tweak to release notes
cowtowncoder Apr 26, 2020
4745094
[maven-release-plugin] prepare release jackson-modules-java8-2.11.0
cowtowncoder Apr 26, 2020
4c05007
[maven-release-plugin] prepare for next development iteration
cowtowncoder Apr 26, 2020
7ee62fc
Add 2.11 javadocs
cowtowncoder Apr 26, 2020
7e54da7
Merge branch '2.11'
cowtowncoder Apr 26, 2020
3ebb591
update release notes wrt #90 by adding CVE id (CVE-2018-1000873)
cowtowncoder Apr 26, 2020
defc104
Merge branch '2.9' into 2.10
cowtowncoder Apr 26, 2020
d44598e
Merge branch '2.10' into 2.11
cowtowncoder Apr 26, 2020
9c2f47c
Merge branch '2.11'
cowtowncoder Apr 26, 2020
1bc898d
Merge branch 'master' of github.com:sashikanthr/jackson-modules-java8
sashikanthr May 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public void serialize(Duration duration, JsonGenerator generator,
SerializerProvider provider) throws IOException
{
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
if (withoutFraction(provider)) {
generator.writeNumber(duration.toMillis()/1000);
} else if (useNanoseconds(provider)) {
generator.writeNumber(DecimalUtils.toBigDecimal(
duration.getSeconds(), duration.getNano()
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ protected abstract JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter
public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException
{
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
if (withoutFraction(provider)) {
generator.writeNumber(getEpochMillis.applyAsLong(value) / 1000);
return;
} else if (useNanoseconds(provider)) {
generator.writeNumber(DecimalUtils.toBigDecimal(
getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value)
));
return;
}
generator.writeNumber(getEpochMillis.applyAsLong(value));
generator.writeNumber(getEpochMillis.applyAsLong(value));
return;
}
String str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,9 @@ protected boolean useNanoseconds(SerializerProvider provider) {
return (provider != null)
&& provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
}

protected boolean withoutFraction(SerializerProvider provider) {
return (provider != null)
&& provider.isEnabled(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,24 @@ public void testSerializationWithTypeInfo03() throws Exception
assertEquals("The value is not correct.",
"[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value);
}

/**
* Tests the Serialization Feature 'WRITE_TIMESTAMPS_WITHOUT_FRACTION' for Duration Serializer.
* @throws Exception if there is an issue while processing the JSON
*/
@Test
public void testDurationSerializationWithFractionFeature() throws Exception
{
ObjectMapper mapper = newMapperBuilder().enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.enable(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION)
.addMixIn(TemporalAmount.class, MockObjectConfiguration.class)
.build();
Duration duration = Duration.ofSeconds(13498L, 8374);
String value = mapper.writeValueAsString(duration);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.",
"[\"" + Duration.class.getName() + "\"," + duration.getSeconds() + "]", value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.fasterxml.jackson.datatype.jsr310.ser;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
Expand Down Expand Up @@ -173,4 +176,58 @@ public void testSerializationWithTypeInfo03() throws Exception
assertEquals("The value is not correct.",
"[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value);
}

/**
* When {@link
* com.fasterxml.jackson.code.jackson-databind.WRITE_TIMESTAMPS_WITHOUT_FRACTION}
* is enabled no fraction is added to the serialized string.
*
* @throws Exception
*/
@Test
public void testSerializationWithFractionFlag() throws Exception
{
class TempClass {
@JsonProperty("registered_at")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant registeredAt;

public TempClass(long seconds, int nanos) {
this.registeredAt = Instant.ofEpochSecond(seconds, nanos);
}
}

String value = MAPPER.writer()
.with(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION)
.writeValueAsString(new TempClass(1420324047L, 123456));
assertEquals("The value should not include any decimals.", "{\"registered_at\":1420324047}", value);
}

/**
* When both WRITE_TIMESTAMPS_WITHOUT_FRACTION and
* WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS from {@link
* com.fasterxml.jackson.code.jackson-databind} are enabled the former one
* has priority.
*
* @throws Exception
*/
@Test
public void testSerializationFractionPriority() throws Exception
{
class TempClass {
@JsonProperty("registered_at")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant registeredAt;

public TempClass(long seconds, int nanos) {
this.registeredAt = Instant.ofEpochSecond(seconds, nanos);
}
}

String value = MAPPER.writer()
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.with(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION)
.writeValueAsString(new TempClass(1420324047L, 123456));
assertEquals("The value does not include any decimals.", "{\"registered_at\":1420324047}", value);
}
}
37 changes: 37 additions & 0 deletions docs/javadoc/datatypes/2.11/allclasses-frame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_162) on Sat Apr 25 18:50:04 PDT 2020 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>All Classes (Jackson datatype: jdk8 2.11.0 API)</title>
<meta name="date" content="2020-04-25">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1 class="bar">All&nbsp;Classes</h1>
<div class="indexContainer">
<ul>
<li><a href="com/fasterxml/jackson/datatype/jdk8/BaseScalarOptionalDeserializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">BaseScalarOptionalDeserializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/DoubleStreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">DoubleStreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/IntStreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">IntStreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8BeanSerializerModifier.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8BeanSerializerModifier</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8Deserializers.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8Deserializers</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8Module.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8Module</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8OptionalBeanPropertyWriter.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8OptionalBeanPropertyWriter</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8Serializers.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8Serializers</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8TypeModifier.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8TypeModifier</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8UnwrappingOptionalBeanPropertyWriter.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">Jdk8UnwrappingOptionalBeanPropertyWriter</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/LongStreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">LongStreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalDoubleSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">OptionalDoubleSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalIntDeserializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">OptionalIntDeserializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalLongDeserializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">OptionalLongDeserializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">OptionalSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/PackageVersion.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">PackageVersion</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/StreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">StreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/WrappedIOException.html" title="class in com.fasterxml.jackson.datatype.jdk8" target="classFrame">WrappedIOException</a></li>
</ul>
</div>
</body>
</html>
37 changes: 37 additions & 0 deletions docs/javadoc/datatypes/2.11/allclasses-noframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_162) on Sat Apr 25 18:50:04 PDT 2020 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>All Classes (Jackson datatype: jdk8 2.11.0 API)</title>
<meta name="date" content="2020-04-25">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1 class="bar">All&nbsp;Classes</h1>
<div class="indexContainer">
<ul>
<li><a href="com/fasterxml/jackson/datatype/jdk8/BaseScalarOptionalDeserializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">BaseScalarOptionalDeserializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/DoubleStreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">DoubleStreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/IntStreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">IntStreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8BeanSerializerModifier.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8BeanSerializerModifier</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8Deserializers.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8Deserializers</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8Module.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8Module</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8OptionalBeanPropertyWriter.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8OptionalBeanPropertyWriter</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8Serializers.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8Serializers</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8TypeModifier.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8TypeModifier</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/Jdk8UnwrappingOptionalBeanPropertyWriter.html" title="class in com.fasterxml.jackson.datatype.jdk8">Jdk8UnwrappingOptionalBeanPropertyWriter</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/LongStreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">LongStreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalDoubleSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">OptionalDoubleSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalIntDeserializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">OptionalIntDeserializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalLongDeserializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">OptionalLongDeserializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/OptionalSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">OptionalSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/PackageVersion.html" title="class in com.fasterxml.jackson.datatype.jdk8">PackageVersion</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/StreamSerializer.html" title="class in com.fasterxml.jackson.datatype.jdk8">StreamSerializer</a></li>
<li><a href="com/fasterxml/jackson/datatype/jdk8/WrappedIOException.html" title="class in com.fasterxml.jackson.datatype.jdk8">WrappedIOException</a></li>
</ul>
</div>
</body>
</html>
Loading