import org.slf4j.Logger;\r
import org.slf4j.LoggerFactory;\r
\r
+import com.gitblit.utils.JsonUtils;\r
import com.gitblit.utils.StringUtils;\r
-import com.google.gson.Gson;\r
-import com.google.gson.GsonBuilder;\r
\r
/**\r
* Servlet class for interpreting json requests.\r
return null;\r
}\r
\r
- Gson gson = new Gson();\r
- X object = gson.fromJson(json.toString(), clazz);\r
+ X object = JsonUtils.fromJsonString(json.toString(), clazz);\r
return object;\r
}\r
\r
return null;\r
}\r
\r
- Gson gson = new Gson();\r
- X object = gson.fromJson(json.toString(), type);\r
+ X object = JsonUtils.fromJsonString(json.toString(), type);\r
return object;\r
}\r
\r
protected void serialize(HttpServletResponse response, Object o) throws IOException {\r
if (o != null) {\r
// Send JSON response\r
- Gson gson = new GsonBuilder().setPrettyPrinting().create();\r
- String json = gson.toJson(o);\r
+ String json = JsonUtils.toJsonString(o);\r
response.getWriter().append(json);\r
}\r
}\r
import java.security.SecureRandom;\r
import java.security.cert.CertificateException;\r
import java.security.cert.X509Certificate;\r
+import java.text.DateFormat;\r
+import java.text.ParseException;\r
+import java.text.SimpleDateFormat;\r
import java.util.Collection;\r
+import java.util.Date;\r
+import java.util.Locale;\r
import java.util.Map;\r
+import java.util.TimeZone;\r
\r
import javax.net.ssl.HostnameVerifier;\r
import javax.net.ssl.HttpsURLConnection;\r
import com.gitblit.models.UserModel;\r
import com.google.gson.Gson;\r
import com.google.gson.GsonBuilder;\r
+import com.google.gson.JsonDeserializationContext;\r
+import com.google.gson.JsonDeserializer;\r
+import com.google.gson.JsonElement;\r
+import com.google.gson.JsonPrimitive;\r
+import com.google.gson.JsonSerializationContext;\r
+import com.google.gson.JsonSerializer;\r
+import com.google.gson.JsonSyntaxException;\r
import com.google.gson.reflect.TypeToken;\r
\r
/**\r
* @return json\r
*/\r
public static String toJsonString(Object o) {\r
- Gson gson = new GsonBuilder().setPrettyPrinting().create();\r
- String json = gson.toJson(o);\r
+ String json = gson().toJson(o);\r
return json;\r
}\r
\r
* @return an object\r
*/\r
public static <X> X fromJsonString(String json, Class<X> clazz) {\r
- Gson gson = new Gson();\r
- return gson.fromJson(json, clazz);\r
+ return gson().fromJson(json, clazz);\r
}\r
\r
/**\r
* @return an object\r
*/\r
public static <X> X fromJsonString(String json, Type type) {\r
- Gson gson = new Gson();\r
- return gson.fromJson(json, type);\r
+ return gson().fromJson(json, type);\r
}\r
\r
/**\r
if (StringUtils.isEmpty(json)) {\r
return null;\r
}\r
- Gson gson = new Gson();\r
- return gson.fromJson(json, type);\r
+ return gson().fromJson(json, type);\r
}\r
\r
/**\r
}\r
}\r
\r
+ // build custom gson instance with GMT date serializer/deserializer\r
+ // http://code.google.com/p/google-gson/issues/detail?id=281\r
+ private static Gson gson() {\r
+ GsonBuilder builder = new GsonBuilder();\r
+ builder.registerTypeAdapter(Date.class, new GmtDateTypeAdapter());\r
+ builder.setPrettyPrinting();\r
+ return builder.create();\r
+ }\r
+\r
+ private static class GmtDateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {\r
+ private final DateFormat dateFormat;\r
+\r
+ private GmtDateTypeAdapter() {\r
+ dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);\r
+ dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));\r
+ }\r
+\r
+ @Override\r
+ public synchronized JsonElement serialize(Date date, Type type,\r
+ JsonSerializationContext jsonSerializationContext) {\r
+ synchronized (dateFormat) {\r
+ String dateFormatAsString = dateFormat.format(date);\r
+ return new JsonPrimitive(dateFormatAsString);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public synchronized Date deserialize(JsonElement jsonElement, Type type,\r
+ JsonDeserializationContext jsonDeserializationContext) {\r
+ try {\r
+ synchronized (dateFormat) {\r
+ return dateFormat.parse(jsonElement.getAsString());\r
+ }\r
+ } catch (ParseException e) {\r
+ throw new JsonSyntaxException(jsonElement.getAsString(), e);\r
+ }\r
+ }\r
+ }\r
+\r
/**\r
* DummyTrustManager trusts all certificates.\r
*/\r