Skip to content

Commit

Permalink
Fix #355 (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Feb 6, 2025
1 parent d3d2571 commit f478801
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ protected boolean useTimestamp(SerializationContext ctxt) {
}
}
// assume that explicit formatter definition implies use of textual format
return (_formatter == null) && (ctxt != null) && ctxt.isEnabled(getTimestampsFeature());
return (_formatter == null) && useTimestampFromGlobalDefaults(ctxt);
}

// @since 2.19
protected boolean useTimestampFromGlobalDefaults(SerializationContext ctxt) {
return (ctxt != null)
&& ctxt.isEnabled(getTimestampsFeature());
}

protected boolean _useTimestampExplicitOnly(SerializationContext ctxt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ protected YearSerializer withFormat(DateTimeFormatter dtf,
return new YearSerializer(this, dtf, useTimestamp);
}

// Need to ensure Year still defaults to numeric ("timestamp") regardless
// of general global setting (since that defaults to textual in Jackson 3.x)
@Override
protected boolean useTimestampFromGlobalDefaults(SerializationContext ctxt) {
return true;
}

@Override
public void serialize(Year year, JsonGenerator generator, SerializationContext ctxt)
throws JacksonException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonFormat;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.datatype.jsr310.MockObjectConfiguration;
import tools.jackson.datatype.jsr310.ModuleTestBase;
Expand All @@ -29,6 +31,17 @@

public class YearSerTest extends ModuleTestBase
{
final static class YearAsStringWrapper {
@JsonFormat(shape = JsonFormat.Shape.STRING)
public Year value;

public YearAsStringWrapper(Year value) {
this.value = value;
}
}

// Defaults fine: year only serialized as String with explicit
// overrides
private final ObjectMapper MAPPER = newMapper();

@Test
Expand All @@ -40,6 +53,25 @@ public void testDefaultSerialization() throws Exception
MAPPER.writeValueAsString(Year.of(2013)));
}

@Test
public void testAsStringSerializationViaAnnotation() throws Exception
{
assertEquals(a2q("{'value':'1972'}"),
MAPPER.writeValueAsString(new YearAsStringWrapper(Year.of(1972))));
}

@Test
public void testAsStringSerializationViaFormatConfig() throws Exception
{
final ObjectMapper asStringMapper = mapperBuilder()
.withConfigOverride(Year.class, o -> o.setFormat(
JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
.build();

assertEquals(q("2025"),
asStringMapper.writeValueAsString(Year.of(2025)));
}

@Test
public void testSerializationWithTypeInfo() throws Exception
{
Expand Down

0 comments on commit f478801

Please sign in to comment.