1 /*
2  * Copyright (C) 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.google.gson.internal.bind;
18
19 import com.google.gson.Gson;
20 import com.google.gson.JsonSyntaxException;
21 import com.google.gson.TypeAdapter;
22 import com.google.gson.TypeAdapterFactory;
23 import com.google.gson.internal.JavaVersion;
24 import com.google.gson.internal.PreJava9DateFormatProvider;
25 import com.google.gson.internal.bind.util.ISO8601Utils;
26 import com.google.gson.reflect.TypeToken;
27 import com.google.gson.stream.JsonReader;
28 import com.google.gson.stream.JsonToken;
29 import com.google.gson.stream.JsonWriter;
30
31 import java.io.IOException;
32 import java.text.DateFormat;
33 import java.text.ParseException;
34 import java.text.ParsePosition;
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.Locale;
39
40 /**
41  * Adapter for Date. Although this class appears stateless, it is not.
42  * DateFormat captures its time zone and locale when it is created, which gives
43  * this class state. DateFormat isn't thread safe either, so this class has
44  * to synchronize its read and write methods.
45  */

46 public final class DateTypeAdapter extends TypeAdapter<Date> {
47   public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
48     @SuppressWarnings("unchecked"// we use a runtime check to make sure the 'T's equal
49     @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
50       return typeToken.getRawType() == Date.class ? (TypeAdapter<T>) new DateTypeAdapter() : null;
51     }
52   };
53
54   /**
55    * List of 1 or more different date formats used for de-serialization attempts.
56    * The first of them (default US format) is used for serialization as well.
57    */

58   private final List<DateFormat> dateFormats = new ArrayList<DateFormat>();
59
60   public DateTypeAdapter() {
61     dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US));
62     if (!Locale.getDefault().equals(Locale.US)) {
63       dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT));
64     }
65     if (JavaVersion.isJava9OrLater()) {
66       dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(DateFormat.DEFAULT, DateFormat.DEFAULT));
67     }
68   }
69
70   @Override public Date read(JsonReader in) throws IOException {
71     if (in.peek() == JsonToken.NULL) {
72       in.nextNull();
73       return null;
74     }
75     return deserializeToDate(in.nextString());
76   }
77
78   private synchronized Date deserializeToDate(String json) {
79     for (DateFormat dateFormat : dateFormats) {
80       try {
81         return dateFormat.parse(json);
82       } catch (ParseException ignored) {}
83     }
84     try {
85         return ISO8601Utils.parse(json, new ParsePosition(0));
86     } catch (ParseException e) {
87       throw new JsonSyntaxException(json, e);
88     }
89   }
90
91   @Override public synchronized void write(JsonWriter out, Date value) throws IOException {
92     if (value == null) {
93       out.nullValue();
94       return;
95     }
96     String dateFormatAsString = dateFormats.get(0).format(value);
97     out.value(dateFormatAsString);
98   }
99   
100   
101 }
102