Skip to content

Commit

Permalink
Fix #1073
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 9, 2016
1 parent cecd409 commit 70d961b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>,Object> en : StdJdkSerializers.all()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public static Collection<Map.Entry<Class<?>, 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 70d961b

Please sign in to comment.