From 70d961bfb281498489eeb7d318bc5c96fa7f92e6 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 9 Jan 2016 14:18:23 -0800 Subject: [PATCH] Fix #1073 --- release-notes/VERSION | 2 ++ .../databind/ser/BasicSerializerFactory.java | 9 +-------- .../databind/ser/std/SqlTimeSerializer.java | 4 ++-- .../databind/ser/std/StdJdkSerializers.java | 13 +++++++++++++ .../databind/ser/TestDateSerialization.java | 15 +++++++++++++++ 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/release-notes/VERSION b/release-notes/VERSION index 00320ffe25..2bce07ee8c 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -7,6 +7,8 @@ Project: jackson-databind 2.5.5-1 (not released) #1051: Problem with Object Id and Type Id as Wrapper Object (regression in 2.5.1) +#1073: Add try-catch around `java.sql` type serializers + (suggested by claudemt@github) 2.5.5 (07-Dec-2015) diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java index d485fc21f8..f69cd255d0 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java @@ -82,14 +82,7 @@ public abstract class BasicSerializerFactory // Other discrete non-container types: // First, Date/Time zoo: _concrete.put(Calendar.class.getName(), CalendarSerializer.instance); - DateSerializer dateSer = DateSerializer.instance; - _concrete.put(java.util.Date.class.getName(), dateSer); - // note: timestamps are very similar to java.util.Date, thus serialized as such - _concrete.put(java.sql.Timestamp.class.getName(), dateSer); - - // leave some of less commonly used ones as lazy, no point in proactive construction - _concreteLazy.put(java.sql.Date.class.getName(), SqlDateSerializer.class); - _concreteLazy.put(java.sql.Time.class.getName(), SqlTimeSerializer.class); + _concrete.put(java.util.Date.class.getName(), DateSerializer.instance); // And then other standard non-structured JDK types for (Map.Entry,Object> en : StdJdkSerializers.all()) { diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlTimeSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlTimeSerializer.java index 0444302a0f..e6422b47ff 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlTimeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlTimeSerializer.java @@ -21,9 +21,9 @@ public class SqlTimeSerializer public SqlTimeSerializer() { super(java.sql.Time.class); } @Override - public void serialize(java.sql.Time value, JsonGenerator jgen, SerializerProvider provider) throws IOException + public void serialize(java.sql.Time value, JsonGenerator g, SerializerProvider provider) throws IOException { - jgen.writeString(value.toString()); + g.writeString(value.toString()); } @Override diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java index f8f446844a..2d4cc21de7 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java @@ -55,6 +55,19 @@ public static Collection, Object>> all() sers.put(Void.class, NullSerializer.instance); sers.put(Void.TYPE, NullSerializer.instance); + // 09-Jan-2015, tatu: As per [databind#1073], let's try to guard against possibility + // of some environments missing `java.sql.` types + try { + // note: timestamps are very similar to java.util.Date, thus serialized as such + sers.put(java.sql.Timestamp.class, DateSerializer.instance); + + // leave some of less commonly used ones as lazy, no point in proactive construction + sers.put(java.sql.Date.class, SqlDateSerializer.class); + sers.put(java.sql.Time.class, SqlTimeSerializer.class); + } catch (NoClassDefFoundError e) { + // nothing much we can do here; could log, but probably not useful for now. + } + return sers.entrySet(); } diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestDateSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestDateSerialization.java index 0420022412..205d1283fa 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/TestDateSerialization.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestDateSerialization.java @@ -109,6 +109,21 @@ public void testSqlDate() throws IOException assertEquals(aposToQuotes("{'date':0}"), MAPPER.writeValueAsString(new SqlDateAsNumberBean(0L))); } + public void testSqlTimestamp() throws IOException + { + java.sql.Timestamp input = new java.sql.Timestamp(0L); + // just should produce same output as standard `java.util.Date`: + Date altTnput = new Date(0L); + assertEquals(MAPPER.writeValueAsString(altTnput), + MAPPER.writeValueAsString(input)); + } + + public void testSqlTime() throws IOException + { + java.sql.Time input = new java.sql.Time(0L); + assertEquals(quote(input.toString()), MAPPER.writeValueAsString(input)); + } + public void testTimeZone() throws IOException { TimeZone input = TimeZone.getTimeZone("PST");