Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GsonEngine and ContentType registration #557

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pippo-content-type-parent/pippo-gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<description>Google Gson integration</description>

<properties>
<gson.version>2.3.1</gson.version>
<gson.version>2.8.6</gson.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -46,4 +46,24 @@
</dependency>
</dependencies>

<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
</testResource>

<testResource>
<directory>src/test/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<filtering>false</filtering>
</testResource>
</testResources>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,14 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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 org.kohsuke.MetaInfServices;
import ro.pippo.core.Application;
import ro.pippo.core.ContentTypeEngine;
import ro.pippo.core.HttpConstants;

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;

/**
* A JsonEngine based on Gson.
Expand All @@ -49,11 +38,7 @@ public class GsonEngine implements ContentTypeEngine {

@Override
public void init(Application application) {
this.gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new ISO8601DateTimeTypeAdapter())
.registerTypeAdapter(Time.class, new ISO8601TimeTypeAdapter())
.registerTypeAdapter(java.sql.Date.class, new ISO8601DateTypeAdapter())
.create();
this.gson = buildGson(application);
}

@Override
Expand All @@ -71,93 +56,12 @@ public <T> T fromString(String content, Class<T> classOfT) {
return gson.fromJson(content, classOfT);
}

public static 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() / 1000) * 1000);
}
} catch (ParseException e) {
throw new JsonSyntaxException(jsonElement.getAsString(), e);
}
}
}

public static 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() / 1000) * 1000);
}
} catch (ParseException e) {
throw new JsonSyntaxException(jsonElement.getAsString(), e);
}
}
protected Gson buildGson(Application application) {
return new GsonBuilder()
.registerTypeAdapter(Date.class, new ISO8601DateTimeTypeAdapter())
.registerTypeAdapter(Time.class, new ISO8601TimeTypeAdapter())
.registerTypeAdapter(java.sql.Date.class, new ISO8601DateTypeAdapter())
.create();
}

public static class ISO8601DateTimeTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
private final DateFormat dateTimeFormat;

public ISO8601DateTimeTypeAdapter() {
dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
}

@Override
public synchronized JsonElement serialize(Date date, Type type,
JsonSerializationContext jsonSerializationContext) {
synchronized (dateTimeFormat) {
String dateFormatAsString = dateTimeFormat.format(date);
return new JsonPrimitive(dateFormatAsString);
}
}

@Override
public synchronized Date deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) {
try {
synchronized (dateTimeFormat) {
Date date = dateTimeFormat.parse(jsonElement.getAsString());
return new Date((date.getTime() / 1000) * 1000);
}
} catch (ParseException e) {
throw new JsonSyntaxException(jsonElement.getAsString(), e);
}
}
}
}
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);
}
}

}
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);
}
}

}
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);
}
}

}
Loading