From 255f2e2847d133a751a2dd5a4bd16d048333cf75 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 5 Dec 2014 00:16:24 +0000 Subject: [PATCH] added a sample Date type adapter for UTC --- .../gson/typeadapters/UtcDateTypeAdapter.java | 63 +++++++++++++++++++ .../typeadapters/UtcDateTypeAdapterTest.java | 50 +++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java create mode 100644 extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java diff --git a/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java b/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java new file mode 100644 index 00000000..16e251fd --- /dev/null +++ b/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * 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 com.google.gson.typeadapters; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +public final class UtcDateTypeAdapter extends TypeAdapter { + private final DateFormat iso8601Format; + + public UtcDateTypeAdapter() { + this.iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US); + this.iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); + } + + // These methods need to be synchronized since JDK DateFormat classes are not thread-safe + // See issue 162 + @Override + public synchronized void write(JsonWriter out, Date date) throws IOException { + if (date == null) { + out.nullValue(); + } + out.value(iso8601Format.format(date)); + } + + // These methods need to be synchronized since JDK DateFormat classes are not thread-safe + // See issue 162 + @Override + public synchronized Date read(JsonReader in) throws IOException { + try { + switch (in.peek()) { + case NULL: return null; + default: return iso8601Format.parse(in.nextString()); + } + } catch (ParseException e) { + throw new JsonParseException(e); + } + } +} diff --git a/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java b/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java new file mode 100644 index 00000000..0ce7e8f7 --- /dev/null +++ b/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * 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 com.google.gson.typeadapters; + +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import junit.framework.TestCase; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public final class UtcDateTypeAdapterTest extends TestCase { + private final Gson gson = new GsonBuilder() + .registerTypeAdapter(Date.class, new UtcDateTypeAdapter()) + .create(); + + public void testLocalTimeZone() { + Date expected = new Date(); + String json = gson.toJson(expected); + Date actual = gson.fromJson(json, Date.class); + assertEquals(expected.getTime(), actual.getTime()); + } + + public void testDifferentTimeZones() { + for (String timeZone : TimeZone.getAvailableIDs()) { + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); + Date expected = cal.getTime(); + String json = gson.toJson(expected); + // System.out.println(json + ": " + timeZone); + Date actual = gson.fromJson(json, Date.class); + assertEquals(expected.getTime(), actual.getTime()); + } + } +}