TestInstantSerialization.java
/*
* Copyright 2013 FasterXML.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package com.fasterxml.jackson.datatype.jsr310.old;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
import static org.junit.jupiter.api.Assertions.*;
public class TestInstantSerialization extends ModuleTestBase
{
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_INSTANT;
private ObjectMapper mapper;
@BeforeEach
public void setUp()
{
this.mapper = newMapper();
}
@Test
public void testSerializationAsTimestamp01Nanoseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("0.0", value);
}
@Test
public void testSerializationAsTimestamp01Milliseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("0", value);
}
@Test
public void testSerializationAsTimestamp02Nanoseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("123456789.183917322", value);
}
@Test
public void testSerializationAsTimestamp02Milliseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("123456789183", value);
}
@Test
public void testSerializationAsTimestamp03Nanoseconds() throws Exception
{
Instant date = Instant.now();
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals(DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), value);
}
@Test
public void testSerializationAsTimestamp03Milliseconds() throws Exception
{
Instant date = Instant.now();
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals(Long.toString(date.toEpochMilli()), value);
}
@Test
public void testSerializationAsString01() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals('"' + FORMATTER.format(date) + '"', value);
}
@Test
public void testSerializationAsString02() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals('"' + FORMATTER.format(date) + '"', value);
}
@Test
public void testSerializationAsString03() throws Exception
{
Instant date = Instant.now();
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals('"' + FORMATTER.format(date) + '"', value);
}
@Test
public void testSerializationWithTypeInfo01() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("[\"" + Instant.class.getName() + "\",123456789.183917322]", value);
}
@Test
public void testSerializationWithTypeInfo02() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("[\"" + Instant.class.getName() + "\",123456789183]", value);
}
@Test
public void testSerializationWithTypeInfo03() throws Exception
{
Instant date = Instant.now();
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
String value = this.mapper.writeValueAsString(date);
assertNotNull(value);
assertEquals("[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value);
}
@Test
public void testDeserializationAsFloat01() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
Instant value = this.mapper.readValue("0.000000000", Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsFloat02() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
Instant value = this.mapper.readValue("123456789.183917322", Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsFloat03() throws Exception
{
Instant date = Instant.now();
Instant value = this.mapper.readValue(
DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), Instant.class
);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsInt01Nanoseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
Instant value = this.mapper.readValue("0", Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsInt01Milliseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
Instant value = this.mapper.readValue("0", Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsInt02Nanoseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 0);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
Instant value = this.mapper.readValue("123456789", Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsInt02Milliseconds() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 422000000);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
Instant value = this.mapper.readValue("123456789422", Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsInt03Nanoseconds() throws Exception
{
Instant date = Instant.now();
date = date.minus(date.getNano(), ChronoUnit.NANOS);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
Instant value = this.mapper.readValue(Long.toString(date.getEpochSecond()), Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsInt03Milliseconds() throws Exception
{
Instant date = Instant.now();
date = date.minus(date.getNano(), ChronoUnit.NANOS);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
Instant value = this.mapper.readValue(Long.toString(date.toEpochMilli()), Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsString01() throws Exception
{
Instant date = Instant.ofEpochSecond(0L);
Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsString02() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationAsString03() throws Exception
{
Instant date = Instant.now();
Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class);
assertNotNull(value);
assertEquals(date, value);
}
@Test
public void testDeserializationWithTypeInfo01() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 183917322);
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
Temporal value = this.mapper.readValue(
"[\"" + Instant.class.getName() + "\",123456789.183917322]", Temporal.class
);
assertNotNull(value);
assertInstanceOf(Instant.class, value, "The value should be an Instant.");
assertEquals(date, value);
}
@Test
public void testDeserializationWithTypeInfo02() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 0);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
Temporal value = this.mapper.readValue(
"[\"" + Instant.class.getName() + "\",123456789]", Temporal.class
);
assertNotNull(value);
assertInstanceOf(Instant.class, value, "The value should be an Instant.");
assertEquals(date, value);
}
@Test
public void testDeserializationWithTypeInfo03() throws Exception
{
Instant date = Instant.ofEpochSecond(123456789L, 422000000);
this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
Temporal value = this.mapper.readValue(
"[\"" + Instant.class.getName() + "\",123456789422]", Temporal.class
);
assertNotNull(value);
assertInstanceOf(Instant.class, value, "The value should be an Instant.");
assertEquals(date, value);
}
@Test
public void testDeserializationWithTypeInfo04() throws Exception
{
Instant date = Instant.now();
this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class);
Temporal value = this.mapper.readValue(
"[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class
);
assertNotNull(value);
assertInstanceOf(Instant.class, value, "The value should be an Instant.");
assertEquals(date, value);
}
}