Skip to content

Commit

Permalink
Merge branch '2.17'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 3, 2024
2 parents d551852 + 2445f78 commit 3dc559a
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 14 deletions.
3 changes: 3 additions & 0 deletions datatypes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ String withEmailJson = mapper.writeValueAsString(withEmail);
// prints: {"name":"Example Co.","email":"info@example.com"}
System.out.println(withEmailJson);
```
## More

See [Wiki](https://github.com/FasterXML/jackson-modules-java8/wiki) for more information (javadocs, downloads).
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ public void serializeWithType(LocalDate value, JsonGenerator g,
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
typeSer.typeId(value, serializationShape(ctxt)));
// need to write out to avoid double-writing array markers
switch (typeIdDef.valueShape) {
case START_ARRAY:
JsonToken shape = (typeIdDef == null) ? null : typeIdDef.valueShape;
if (shape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, ctxt);
break;
case VALUE_NUMBER_INT:
} else if (shape == JsonToken.VALUE_NUMBER_INT) {
g.writeNumber(value.toEpochDay());
break;
default:
} else {
g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
}
typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void serializeWithType(LocalDateTime value, JsonGenerator g, SerializerPr
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
typeSer.typeId(value, serializationShape(ctxt)));
// need to write out to avoid double-writing array markers
if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
if ((typeIdDef != null)
&& typeIdDef.valueShape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, ctxt);
} else {
DateTimeFormatter dtf = _formatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public void serializeWithType(LocalTime value, JsonGenerator g,
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
typeSer.typeId(value, serializationShape(ctxt)));
// need to write out to avoid double-writing array markers
if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
if ((typeIdDef != null)
&& typeIdDef.valueShape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, ctxt);
} else {
DateTimeFormatter dtf = _formatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public void serializeWithType(MonthDay value, JsonGenerator g,
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
typeSer.typeId(value, serializationShape(ctxt)));
// need to write out to avoid double-writing array markers
if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
if ((typeIdDef != null)
&& typeIdDef.valueShape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, ctxt);
} else {
g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public void serializeWithType(OffsetTime value, JsonGenerator g, SerializerProvi
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
typeSer.typeId(value, serializationShape(ctxt)));
// need to write out to avoid double-writing array markers
if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
if ((typeIdDef != null)
&& typeIdDef.valueShape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, ctxt);
} else {
String str = (_formatter == null) ? value.toString() : value.format(_formatter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public void serializeWithType(YearMonth value, JsonGenerator g,
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
typeSer.typeId(value, serializationShape(ctxt)));
// need to write out to avoid double-writing array markers
if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
if ((typeIdDef != null)
&& typeIdDef.valueShape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, ctxt);
} else {
g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import java.util.regex.Matcher;

import org.junit.Test;

import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.exc.MismatchedInputException;
import tools.jackson.datatype.jsr310.MockObjectConfiguration;
Expand All @@ -20,10 +22,7 @@
import tools.jackson.databind.ObjectReader;
import tools.jackson.databind.SerializationFeature;

import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assume.assumeTrue;

import static tools.jackson.datatype.jsr310.deser.InstantDeserializer.ISO8601_COLONLESS_OFFSET_REGEX;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package tools.jackson.datatype.jsr310.misc;

import java.time.*;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature;

import tools.jackson.datatype.jsr310.ModuleTestBase;

// for [modules-java8#296]: problem with `JsonTypeInfo.Id.DEDUCTION`
public class DeductionTypeSerialization296Test extends ModuleTestBase
{
static class Wrapper {
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
public Object value;

public Wrapper(Object value) {
this.value = value;
}
}

private final ObjectMapper MAPPER = mapperBuilder()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();

@Test
public void testLocalDate() throws Exception
{
LocalDate date = LocalDate.of(1986, Month.JANUARY, 17);
Assert.assertEquals(a2q("{'value':'1986-01-17'}"),
MAPPER.writeValueAsString(new Wrapper(date)));
}

@Test
public void testLocalDateTime() throws Exception
{
LocalDateTime datetime = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57);
Assert.assertEquals(a2q("{'value':'2013-08-21T09:22:00.000000057'}"),
MAPPER.writeValueAsString(new Wrapper(datetime)));
}

@Test
public void testLocalTime() throws Exception
{
LocalTime time = LocalTime.of(9, 22, 57);
Assert.assertEquals(a2q("{'value':'09:22:57'}"),
MAPPER.writeValueAsString(new Wrapper(time)));
}

@Test
public void testMonthDate() throws Exception
{
MonthDay date = MonthDay.of(Month.JANUARY, 17);
Assert.assertEquals(a2q("{'value':'--01-17'}"),
MAPPER.writeValueAsString(new Wrapper(date)));
}

@Test
public void testOffsetTime() throws Exception
{
OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300"));
Assert.assertEquals(a2q("{'value':'15:43+03:00'}"),
MAPPER.writeValueAsString(new Wrapper(time)));
}

@Test
public void testYearMonth() throws Exception
{
YearMonth date = YearMonth.of(1986, Month.JANUARY);
Assert.assertEquals(a2q("{'value':'1986-01'}"),
MAPPER.writeValueAsString(new Wrapper(date)));
}

@Test
public void testZoneId() throws Exception
{
ZoneId zone = ZoneId.of("America/Denver");
Assert.assertEquals(a2q("{'value':'America/Denver'}"),
MAPPER.writeValueAsString(new Wrapper(zone)));
}
}
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Modules:

-

2.16.2 (not yet released)

#296: NPE when serializing a `LocalDate` or `LocalDateTime` using `AsDeductionTypeSerializer`
(reported by @mike-reynolds-savient)

2.16.1 (24-Dec-2023)

#286: Breaking change in `InstantDeserializer API between 2.15 and 2.16
Expand Down

0 comments on commit 3dc559a

Please sign in to comment.