Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #355 #356

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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