Skip to content

Commit

Permalink
Merge branch '2.18' into 2.19
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 31, 2024
2 parents 0659f2e + a46f82c commit 0bb8d01
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

public class InstantSerializationTest extends JodaTestBase
{
private final ObjectMapper MAPPER = jodaMapper();
{
MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
MAPPER.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
}
private final ObjectMapper MAPPER = mapperWithModuleBuilder()
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.build();

public void testInstantSer() throws Exception {
Instant instant = new Instant(0L);
Expand All @@ -24,7 +23,7 @@ public void testInstantSer() throws Exception {
assertEquals("0", MAPPER.writeValueAsString(instant));

// but if re-configured, as regular ISO-8601 string
assertEquals(quote("1970-01-01T00:00:00.000Z"), MAPPER.writer()
assertEquals(q("1970-01-01T00:00:00.000Z"), MAPPER.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(instant));
}
Expand All @@ -34,24 +33,22 @@ public void testCustomFormatInstantSer() throws Exception
final String json = MAPPER.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(new FormattedInstant(new Instant(0L)));
assertEquals(aposToQuotes(
assertEquals(a2q(
"{'value':'01/01/1970 00_00_00_000'}"), json);
}

// [datatype-joda#60]
public void testInstantConversion() throws Exception
{
final ObjectMapper mapper = jodaMapper();

// Configure Date Formatting
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
final ObjectMapper mapper = mapperWithModuleBuilder()
// Configure Date Formatting
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));

// Create an instant and serialize and additionally serialize the instant as DateTime to demonstrate the difference
org.joda.time.Instant now = new DateTime(1431498572205L).toInstant();

String instantString = mapper.writeValueAsString(now);

assertEquals("\"2015-05-13T06:29:32.205Z\"", instantString);
assertEquals("\"2015-05-13T06:29:32.205Z\"", mapper.writeValueAsString(now));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fasterxml.jackson.datatype.joda.ser;

import java.io.IOException;

import org.joda.time.DateTimeZone;
import org.joda.time.Interval;

Expand All @@ -28,31 +26,32 @@ static class FormattedInterval
/**********************************************************
*/

private final ObjectMapper MAPPER = jodaMapper();
{
MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
MAPPER.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
}
private final ObjectMapper MAPPER = mapperWithModuleBuilder()
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.build();

private final ObjectWriter WRITER = MAPPER.writer();

public void testIntervalSerBasic() throws IOException
public void testIntervalSerBasic() throws Exception
{
Interval interval = new Interval(1396439982, 1396440001);
assertEquals(quote("1396439982-1396440001"), MAPPER.writeValueAsString(interval));
assertEquals(q("1396439982-1396440001"), MAPPER.writeValueAsString(interval));

// related to #48
assertEquals(quote("1970-01-17T03:53:59.982Z/1970-01-17T03:54:00.001Z"),
assertEquals(q("1970-01-17T03:53:59.982Z/1970-01-17T03:54:00.001Z"),
WRITER.without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.writeValueAsString(interval));
}

public void testIntervalSerWithTypeInfo() throws IOException
public void testIntervalSerWithTypeInfo() throws Exception
{
Interval interval = new Interval(1396439982, 1396440001);

ObjectMapper mapper = jodaMapper();
mapper.addMixIn(Interval.class, ObjectConfiguration.class);
assertEquals("[\"org.joda.time.Interval\"," + quote("1396439982-1396440001") + "]",
ObjectMapper mapper = mapperWithModuleBuilder()
.addMixIn(Interval.class, ObjectConfiguration.class)
.build();
assertEquals("[\"org.joda.time.Interval\"," + q("1396439982-1396440001") + "]",
mapper.writeValueAsString(interval));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ public T getContents() {
}
}

private final ObjectMapper MAPPER = jodaMapper();
{
MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
MAPPER.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
}
private final ObjectMapper MAPPER = mapperWithModuleBuilder()
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.build();

private final ObjectWriter WRITER = MAPPER.writer();

Expand Down Expand Up @@ -74,9 +73,10 @@ public void testLocalDateSerWithTypeInfo() throws IOException

// but we can force it to be a String as well (note: here we assume this is
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.addMixIn(LocalDate.class, ObjectConfiguration.class);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
ObjectMapper mapper = mapperWithModuleBuilder()
.addMixIn(LocalDate.class, ObjectConfiguration.class)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
assertEquals("[\"org.joda.time.LocalDate\",\"2001-05-25\"]", mapper.writeValueAsString(date));
}

Expand All @@ -94,8 +94,9 @@ public void testLocalTimeSer() throws IOException

// but we can force it to be a String as well (note: here we assume this is
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
ObjectMapper mapper = mapperWithModuleBuilder()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
assertEquals(q("13:20:54.000"), mapper.writeValueAsString(date));

}
Expand Down Expand Up @@ -127,9 +128,10 @@ public void testLocalTimeSerWithTypeInfo() throws IOException

// but we can force it to be a String as well (note: here we assume this is
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.addMixIn(LocalTime.class, ObjectConfiguration.class);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
ObjectMapper mapper = mapperWithModuleBuilder()
.addMixIn(LocalTime.class, ObjectConfiguration.class)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
assertEquals("[\"org.joda.time.LocalTime\",\"13:20:54.000\"]",
mapper.writeValueAsString(date));
}
Expand All @@ -148,8 +150,9 @@ public void testLocalDateTimeSer() throws IOException
assertEquals("[2001,5,25,10,15,30,37]", MAPPER.writeValueAsString(date));
// but we can force it to be a String as well (note: here we assume this is
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
ObjectMapper mapper = mapperWithModuleBuilder()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
assertEquals(q("2001-05-25T10:15:30.037"), mapper.writeValueAsString(date));
}

Expand All @@ -161,10 +164,12 @@ public void testLocalDateTimeSerWithTypeInfo() throws IOException
assertEquals("[2001,5,25,10,15,30,37]", MAPPER.writeValueAsString(date));
// but we can force it to be a String as well (note: here we assume this is
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.addMixIn(LocalDateTime.class, ObjectConfiguration.class);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals("[\"org.joda.time.LocalDateTime\",\"2001-05-25T10:15:30.037\"]", mapper.writeValueAsString(date));
ObjectMapper mapper = mapperWithModuleBuilder()
.addMixIn(LocalDateTime.class, ObjectConfiguration.class)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
assertEquals("[\"org.joda.time.LocalDateTime\",\"2001-05-25T10:15:30.037\"]",
mapper.writeValueAsString(date));
}

/*
Expand All @@ -182,8 +187,9 @@ public void testPeriodSer() throws IOException
public void testPeriodSerWithTypeInfo() throws IOException
{
Period in = new Period(1, 2, 3, 4);
ObjectMapper mapper = jodaMapper();
mapper.addMixIn(Period.class, ObjectConfiguration.class);
ObjectMapper mapper = mapperWithModuleBuilder()
.addMixIn(Period.class, ObjectConfiguration.class)
.build();
assertEquals("[\"org.joda.time.Period\",\"PT1H2M3.004S\"]", mapper.writeValueAsString(in));
}

Expand All @@ -207,42 +213,39 @@ public void testDurationSer() throws IOException
public void testDurationSerWithTypeInfo() throws IOException
{
Duration d = new Duration(3123422);
ObjectMapper mapper = jodaMapper();
mapper.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
mapper.addMixIn(Duration.class, ObjectConfiguration.class);
ObjectMapper mapper = mapperWithModuleBuilder()
.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.addMixIn(Duration.class, ObjectConfiguration.class)
.build();
String json = mapper.writeValueAsString(d);
assertEquals("[\"org.joda.time.Duration\",3123422]", json);
}

public void testMonthDaySer() throws Exception
{
MonthDay monthDay = new MonthDay(7, 23);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(monthDay);
String json = MAPPER.writeValueAsString(monthDay);
assertEquals(q("--07-23"), json);
}

public void testCustomMonthDaySer() throws Exception
{
MonthDay monthDay = new MonthDay(7, 23);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(new FormattedMonthDay(monthDay));
String json = MAPPER.writeValueAsString(new FormattedMonthDay(monthDay));
assertEquals(a2q("{'value':'07:23'}"), json);
}

public void testYearMonthSer() throws Exception
{
YearMonth yearMonth = new YearMonth(2013, 8);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(yearMonth);
String json = MAPPER.writeValueAsString(yearMonth);
assertEquals(q("2013-08"), json);
}

public void testCustomYearMonthSer() throws Exception
{
YearMonth yearMonth = new YearMonth(2013, 8);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(new FormattedYearMonth(yearMonth));
String json = MAPPER.writeValueAsString(new FormattedYearMonth(yearMonth));
assertEquals(a2q("{'value':'2013/08'}"), json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DummyClassWithDate(DateTime date) {

public void testJacksonAnnotatedPOJOWithDateWithTimezoneToJson() throws Exception
{
ObjectMapper mapper = jodaMapper();
final ObjectMapper mapper = jodaMapper();
String ZONE_ID = "Asia/Krasnoyarsk";

DummyClassWithDate input = new DummyClassWithDate(new DateTime(2015, 11, 23, 22, 06, 39,
Expand Down

0 comments on commit 0bb8d01

Please sign in to comment.