diff options
| author | Kevin Sawicki | 2011-04-07 23:16:41 +0000 |
|---|---|---|
| committer | Kevin Sawicki | 2011-04-07 23:16:41 +0000 |
| commit | 7bb025dac9ba6c7275e9bd43c584bfb2ee4933f6 (patch) | |
| tree | 46ea8c75ee6219cf6068e3779cd613e8a8a19233 | |
| parent | f2c6f2944aa26064668497671b2e78ed9661890a (diff) | |
| download | egit-github-7bb025dac9ba6c7275e9bd43c584bfb2ee4933f6.tar.gz egit-github-7bb025dac9ba6c7275e9bd43c584bfb2ee4933f6.tar.xz egit-github-7bb025dac9ba6c7275e9bd43c584bfb2ee4933f6.zip | |
Add support for converting JSON dates to Java dates.
Change-Id: If328b7023e0b8cdc4f19f23fd473b4189b2ffe4e
Signed-off-by: Kevin Sawicki <kevin@github.com>
| -rw-r--r-- | org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java | 92 | ||||
| -rw-r--r-- | org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java | 6 |
2 files changed, 97 insertions, 1 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java new file mode 100644 index 00000000..de356f16 --- /dev/null +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.github.internal; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.TimeZone; + +/** + * Date formatter for multiple date formats present in the GitHub v2 API. + * + * @author Kevin Sawicki (kevin@github.com) + */ +public class DateFormatter implements JsonDeserializer<Date> { + + /** + * DATE_FORMAT1 + */ + public static final String DATE_FORMAT1 = "yyyy/MM/dd HH:mm:ss Z"; + + /** + * DATE_FORMAT2 + */ + public static final String DATE_FORMAT2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; + + /** + * DATE_FORMAT3 + */ + public static final String DATE_FORMAT3 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + /** + * DATE_FORMAT4 + */ + public static final String DATE_FORMAT4 = "yyyy-MM-dd'T'HH:mm:ss"; + + private static final String[] FORMATS = new String[] { DATE_FORMAT1, + DATE_FORMAT2, DATE_FORMAT3, DATE_FORMAT4 }; + + private List<DateFormat> formats; + + /** + * Create date formatter + */ + public DateFormatter() { + this.formats = new LinkedList<DateFormat>(); + TimeZone timeZone = TimeZone.getTimeZone("Zulu"); + for (String format : FORMATS) { + DateFormat dateFormat = new SimpleDateFormat(format); + dateFormat.setTimeZone(timeZone); + this.formats.add(dateFormat); + } + } + + /** + * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, + * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext) + */ + public Date deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + String string = json.getAsString(); + Exception exception = null; + for (DateFormat format : this.formats) { + try { + synchronized (format) { + return format.parse(string); + } + } catch (ParseException e) { + exception = e; + } + } + throw new JsonParseException(exception); + } + +} diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java index 17171f60..9a5b7438 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java @@ -13,6 +13,7 @@ package org.eclipse.mylyn.github.internal; import java.io.IOException; +import java.util.Date; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; @@ -29,6 +30,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; /** * Facility to perform API operations on a GitHub issue tracker. @@ -78,7 +80,9 @@ public class GitHubService { */ public GitHubService() { httpClient = new HttpClient(); - gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new DateFormatter()); + gson = builder.create(); } /** |
