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

SqlDateSerializer does not obey SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS #219

Closed
BrentDouglas opened this issue May 13, 2013 · 9 comments
Milestone

Comments

@BrentDouglas
Copy link

This can result in inconsistent serialization of date depending on their origin.

@cowtowncoder
Copy link
Member

Oh. Now I remember what the issue was -- since originally regular java.util.Date default to numeric serialization, and java.sql.Date, there is/was no way to tell difference between "default" value and explicit forcing of numeric serialization.
So what we got here is a backwards-incompatibility in the making: I think the fix will need to go in 2.3, and can't be done in a patch.

There is also the additional question of formatting to use (default for sql.Date is to only use date part, no time); but that's probably lesser deal.

@cowtowncoder
Copy link
Member

One relevante note: as of Jackson 2.6, it is possible to use @JsonFormat(shape=JsonFormat.Shape.NUMBER) to force serialization of java.sql.Dates as timestamps. Plan is to also allow some way to define per-type format defaults, to allow overriding global defaults.

I am still not sure whether the global default itself should be changed, leaving this issue open.

@cowtowncoder
Copy link
Member

And with Jackson 2.8, it is possible to specify "config overrides", using ObjectMapper.configOverrides().setFormat().

But perhaps we could actually change defaults for 2.9, finally.

@cowtowncoder cowtowncoder added this to the 2.9.0 milestone Oct 12, 2016
@PatrickGotthard
Copy link

Hi, is this the correct config to restore the default behaviour of 2.8.x in 2.9.x?

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

@cowtowncoder
Copy link
Member

cowtowncoder commented Mar 6, 2018

@PatrickGotthard Yes, although use of shape does allow switching between number/String representation. Setting of pattern does implicitly also choose String so yes that should work.

@bkrieger
Copy link

@cowtowncoder mapper.configOverride(java.sql.Date.class).setFormat(JsonFormat.Value.forPattern("yyyy-MM-dd")) didn't work for me - java.sql.Date still serialized as a timestamp. 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. Do you have any thoughts?

@cowtowncoder
Copy link
Member

@bkrieger Sounds plausible. Could you please file a new issue, so it's easier to track fixes.

@bkrieger
Copy link

Done, #2064.

@ghost
Copy link

ghost commented Mar 28, 2019

Would this be considered a breaking change or incompatibility migrating from jackson-databind 2.8 to 2.9?

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

4 participants