This can result in inconsistent serialization of date depending on their origin.
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.
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.
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.
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"));
@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.
@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?
@bkrieger Sounds plausible. Could you please file a new issue, so it's easier to track fixes.
Would this be considered a breaking change or incompatibility migrating from jackson-databind 2.8 to 2.9?
Most helpful comment
Would this be considered a breaking change or incompatibility migrating from jackson-databind 2.8 to 2.9?