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

Cannot set custom format for SqlDateSerializer globally #2064

Closed
bkrieger opened this issue Jun 12, 2018 · 1 comment
Closed

Cannot set custom format for SqlDateSerializer globally #2064

bkrieger opened this issue Jun 12, 2018 · 1 comment
Milestone

Comments

@bkrieger
Copy link

Version: 2.9.5

After #219 was fixed, the default format for java.sql.Date serialization switched from string to numeric, following the default value of WRITE_DATES_AS_TIMESTAMPS.

In order to prevent breaks, I want java.sql.Date to serialize as a string, without changing behavior for java.util.Date (which has always serialized as a number by default).

According to #219 (comment), I should be able to revert the behavior for java.sql.Date only with

final ObjectMapper mapper = new ObjectMapper();
mapper.configOverride(java.sql.Date.class).setFormat(JsonFormat.Value.forPattern("yyyy-MM-dd"));

This doesn't seem to do anything, though. Looking at the code, it looks like it's because the custom format isn't actually added to SqlDateSerializer except in the createContextual method (https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/ser/std/DateTimeSerializerBase.java#L59).

For now, I've reverted this behavior with

mapper.registerModule(new SimpleModule() {
            {
                addSerializer(
                        java.sql.Date.class,
                        new SqlDateSerializer().withFormat(false, new SimpleDateFormat("yyyy-MM-dd"))
                );
            }
        });

but it seems pretty hacky so I'd prefer the other method if possible.

@cowtowncoder
Copy link
Member

Quick note: timing here is bit unfortunate, as I just released 2.9.6. But of course, with fixes, better late than never. :)
Thank you for reporting the issue.

@cowtowncoder cowtowncoder added this to the 2.9.7 milestone Aug 31, 2018
@cowtowncoder cowtowncoder changed the title Cannot set custom format for SqlDateSerializer globally Cannot set custom format for SqlDateSerializer globally Aug 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants