forked from pippo-java/pippo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue pippo-java#556 - Improve GsonEngine and ContentType registration
- Loading branch information
Showing
7 changed files
with
365 additions
and
170 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
54 changes: 54 additions & 0 deletions
54
...ontent-type-parent/pippo-gson/src/main/java/ro/pippo/gson/ISO8601DateTimeTypeAdapter.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,54 @@ | ||
/* | ||
* Copyright (C) 2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ro.pippo.gson; | ||
|
||
import java.lang.reflect.Type; | ||
import java.text.ParseException; | ||
import java.text.ParsePosition; | ||
import java.util.Date; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.internal.bind.util.ISO8601Utils; | ||
|
||
public class ISO8601DateTimeTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> { | ||
|
||
public ISO8601DateTimeTypeAdapter() { | ||
} | ||
|
||
@Override | ||
public synchronized JsonElement serialize(Date date, Type type, | ||
JsonSerializationContext jsonSerializationContext) { | ||
String dateFormatAsString = ISO8601Utils.format(date); | ||
return new JsonPrimitive(dateFormatAsString); | ||
} | ||
|
||
@Override | ||
public synchronized Date deserialize(JsonElement jsonElement, Type type, | ||
JsonDeserializationContext jsonDeserializationContext) { | ||
try { | ||
return ISO8601Utils.parse(jsonElement.getAsString(), new ParsePosition(0)); | ||
} catch (ParseException e) { | ||
throw new JsonSyntaxException(jsonElement.getAsString(), e); | ||
} | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
pippo-content-type-parent/pippo-gson/src/main/java/ro/pippo/gson/ISO8601DateTypeAdapter.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,63 @@ | ||
/* | ||
* Copyright (C) 2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ro.pippo.gson; | ||
|
||
import java.lang.reflect.Type; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
public class ISO8601DateTypeAdapter implements JsonSerializer<java.sql.Date>, JsonDeserializer<java.sql.Date> { | ||
|
||
private final DateFormat dateFormat; | ||
|
||
public ISO8601DateTypeAdapter() { | ||
dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); | ||
} | ||
|
||
@Override | ||
public synchronized JsonElement serialize(java.sql.Date date, Type type, | ||
JsonSerializationContext jsonSerializationContext) { | ||
synchronized (dateFormat) { | ||
String dateFormatAsString = dateFormat.format(date); | ||
return new JsonPrimitive(dateFormatAsString); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized java.sql.Date deserialize(JsonElement jsonElement, Type type, | ||
JsonDeserializationContext jsonDeserializationContext) { | ||
try { | ||
synchronized (dateFormat) { | ||
Date date = dateFormat.parse(jsonElement.getAsString()); | ||
return new java.sql.Date(date.getTime()); | ||
} | ||
} catch (ParseException e) { | ||
throw new JsonSyntaxException(jsonElement.getAsString(), e); | ||
} | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
pippo-content-type-parent/pippo-gson/src/main/java/ro/pippo/gson/ISO8601TimeTypeAdapter.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,64 @@ | ||
/* | ||
* Copyright (C) 2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ro.pippo.gson; | ||
|
||
import java.lang.reflect.Type; | ||
import java.sql.Time; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
public class ISO8601TimeTypeAdapter implements JsonSerializer<Time>, JsonDeserializer<Time> { | ||
|
||
private final DateFormat timeFormat; | ||
|
||
public ISO8601TimeTypeAdapter() { | ||
timeFormat = new SimpleDateFormat("HH:mm:ssZ", Locale.US); | ||
} | ||
|
||
@Override | ||
public synchronized JsonElement serialize(Time time, Type type, | ||
JsonSerializationContext jsonSerializationContext) { | ||
synchronized (timeFormat) { | ||
String timeFormatAsString = timeFormat.format(time); | ||
return new JsonPrimitive(timeFormatAsString); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized Time deserialize(JsonElement jsonElement, Type type, | ||
JsonDeserializationContext jsonDeserializationContext) { | ||
try { | ||
synchronized (timeFormat) { | ||
Date date = timeFormat.parse(jsonElement.getAsString()); | ||
return new Time(date.getTime()); | ||
} | ||
} catch (ParseException e) { | ||
throw new JsonSyntaxException(jsonElement.getAsString(), e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.