LocalDateTimeSerTest.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 tools.jackson.databind.ext.javatime.ser;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.Temporal;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.JsonFormat;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.ext.javatime.DateTimeTestBase;
import tools.jackson.databind.ext.javatime.MockObjectConfiguration;
import static org.junit.jupiter.api.Assertions.*;
public class LocalDateTimeSerTest
extends DateTimeTestBase
{
static class LDTWrapper {
@JsonFormat(pattern="yyyy-MM-dd'A'HH:mm:ss")
public LocalDateTime value;
public LDTWrapper(LocalDateTime v) { value = v; }
}
// 05-Feb-2025, tatu: Use Jackson 2.x defaults wrt as-timestamps
// serialization
private final static ObjectMapper MAPPER = mapperBuilder()
.enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
@Test
public void testSerializationAsTimestamp01() throws Exception
{
LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43);
assertEquals("[1986,1,17,15,43]",
MAPPER.writeValueAsString(time));
}
@Test
public void testSerializationAsTimestamp02() throws Exception
{
LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57);
String value = MAPPER.writeValueAsString(time);
assertEquals("[2013,8,21,9,22,57]", value);
}
@Test
public void testSerializationAsTimestamp03Nanosecond() throws Exception
{
LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57);
String value = MAPPER.writer()
.with(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(DateTimeFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(time);
assertEquals("[2013,8,21,9,22,0,57]", value);
}
@Test
public void testSerializationAsTimestamp03Millisecond() throws Exception
{
LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57);
String value = MAPPER.writer()
.without(DateTimeFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(time);
assertEquals("[2013,8,21,9,22,0,0]", value);
}
@Test
public void testSerializationAsTimestamp04Nanosecond() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837);
String value = MAPPER.writer()
.with(DateTimeFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(time);
assertEquals("[2005,11,5,22,31,5,829837]", value);
}
@Test
public void testSerializationAsTimestamp04Millisecond() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837);
String value = MAPPER.writer()
.without(DateTimeFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(time);
assertEquals("[2005,11,5,22,31,5,422]", value);
}
@Test
public void testSerializationAsString01() throws Exception
{
LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43, 05);
final ObjectMapper m = newMapperBuilder()
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
assertEquals("\"1986-01-17T15:43:05\"", m.writeValueAsString(time));
}
@Test
public void testSerializationAsString02() throws Exception
{
LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57);
final ObjectMapper m = newMapperBuilder()
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
String value = m.writeValueAsString(time);
assertEquals('"' + time.toString() + '"', value);
}
@Test
public void testSerializationAsString03() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837);
final ObjectMapper m = newMapperBuilder()
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
String value = m.writeValueAsString(time);
assertEquals('"' + time.toString() + '"', value);
}
@Test
public void testSerializationWithFormatOverride() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 999000);
assertEquals(a2q("{'value':'2005-11-05A22:31:05'}"),
MAPPER.writeValueAsString(new LDTWrapper(time)));
ObjectMapper m = mapperBuilder().withConfigOverride(LocalDateTime.class,
cfg -> cfg.setFormat(JsonFormat.Value.forPattern("yyyy-MM-dd'X'HH:mm")))
.build();
assertEquals(a2q("'2005-11-05X22:31'"), m.writeValueAsString(time));
}
@Test
public void testSerializationWithTypeInfo01() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837);
final ObjectMapper m = newMapperBuilder()
.configure(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS, true)
.configure(DateTimeFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true)
.addMixIn(Temporal.class, MockObjectConfiguration.class)
.build();
String value = m.writeValueAsString(time);
assertEquals("[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value);
}
@Test
public void testSerializationWithTypeInfo02() throws Exception
{
final ObjectMapper m = newMapperBuilder()
.configure(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS, true)
.configure(DateTimeFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.addMixIn(Temporal.class, MockObjectConfiguration.class)
.build();
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837);
String value = m.writeValueAsString(time);
assertEquals("[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value);
}
@Test
public void testSerializationWithTypeInfo03() throws Exception
{
final ObjectMapper m = newMapperBuilder()
.configure(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.addMixIn(Temporal.class, MockObjectConfiguration.class)
.build();
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837);
String value = m.writeValueAsString(time);
assertEquals("[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value);
}
}