-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make dependency on java.sql optional
- Loading branch information
1 parent
361292f
commit 380c4ec
Showing
11 changed files
with
274 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
gson/src/main/java/com/google/gson/internal/sql/SqlTimestampTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.google.gson.internal.sql; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.sql.Timestamp; | ||
import java.util.Date; | ||
|
||
class SqlTimestampTypeAdapter extends TypeAdapter<Timestamp> { | ||
static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { | ||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal | ||
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { | ||
if (typeToken.getRawType() == Timestamp.class) { | ||
final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class); | ||
return (TypeAdapter<T>) new SqlTimestampTypeAdapter(dateTypeAdapter); | ||
} else { | ||
return null; | ||
} | ||
} | ||
}; | ||
|
||
private final TypeAdapter<Date> dateTypeAdapter; | ||
|
||
private SqlTimestampTypeAdapter(TypeAdapter<Date> dateTypeAdapter) { | ||
this.dateTypeAdapter = dateTypeAdapter; | ||
} | ||
|
||
@Override | ||
public Timestamp read(JsonReader in) throws IOException { | ||
Date date = dateTypeAdapter.read(in); | ||
return date != null ? new Timestamp(date.getTime()) : null; | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, Timestamp value) throws IOException { | ||
dateTypeAdapter.write(out, value); | ||
} | ||
} |
Oops, something went wrong.