]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2008 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; | |
18 | ||
19 | import java.lang.reflect.Type; | |
20 | import java.sql.Timestamp; | |
21 | import java.text.DateFormat; | |
22 | import java.text.ParseException; | |
23 | import java.text.SimpleDateFormat; | |
24 | import java.util.Date; | |
25 | import java.util.Locale; | |
26 | import java.util.TimeZone; | |
27 | ||
28 | /** | |
29 | * This type adapter supports three subclasses of date: Date, Timestamp, and | |
30 | * java.sql.Date. | |
31 | * | |
32 | * @author Inderjeet Singh | |
33 | * @author Joel Leitch | |
34 | */ | |
35 | final class DefaultDateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> { | |
36 | ||
37 | // TODO: migrate to streaming adapter | |
38 | ||
39 | private final DateFormat enUsFormat; | |
40 | private final DateFormat localFormat; | |
41 | private final DateFormat iso8601Format; | |
42 | ||
43 | DefaultDateTypeAdapter() { | |
44 | this(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US), | |
45 | DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT)); | |
46 | } | |
47 | ||
48 | DefaultDateTypeAdapter(String datePattern) { | |
49 | this(new SimpleDateFormat(datePattern, Locale.US), new SimpleDateFormat(datePattern)); | |
50 | } | |
51 | ||
52 | DefaultDateTypeAdapter(int style) { | |
53 | this(DateFormat.getDateInstance(style, Locale.US), DateFormat.getDateInstance(style)); | |
54 | } | |
55 | ||
56 | public DefaultDateTypeAdapter(int dateStyle, int timeStyle) { | |
57 | this(DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US), | |
58 | DateFormat.getDateTimeInstance(dateStyle, timeStyle)); | |
59 | } | |
60 | ||
61 | DefaultDateTypeAdapter(DateFormat enUsFormat, DateFormat localFormat) { | |
62 | this.enUsFormat = enUsFormat; | |
63 | this.localFormat = localFormat; | |
64 | this.iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); | |
65 | this.iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); | |
66 | } | |
67 | ||
68 | // These methods need to be synchronized since JDK DateFormat classes are not thread-safe | |
69 | // See issue 162 | |
70 | public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { | |
71 | synchronized (localFormat) { | |
72 | String dateFormatAsString = enUsFormat.format(src); | |
73 | return new JsonPrimitive(dateFormatAsString); | |
74 | } | |
75 | } | |
76 | ||
77 | public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | |
78 | throws JsonParseException { | |
79 | if (!(json instanceof JsonPrimitive)) { | |
80 | throw new JsonParseException("The date should be a string value"); | |
81 | } | |
82 | Date date = deserializeToDate(json); | |
83 | if (typeOfT == Date.class) { | |
84 | return date; | |
85 | } else if (typeOfT == Timestamp.class) { | |
86 | return new Timestamp(date.getTime()); | |
87 | } else if (typeOfT == java.sql.Date.class) { | |
88 | return new java.sql.Date(date.getTime()); | |
89 | } else { | |
90 | throw new IllegalArgumentException(getClass() + " cannot deserialize to " + typeOfT); | |
91 | } | |
92 | } | |
93 | ||
94 | private Date deserializeToDate(JsonElement json) { | |
95 | synchronized (localFormat) { | |
96 | try { | |
97 | return localFormat.parse(json.getAsString()); | |
98 | } catch (ParseException ignored) { | |
99 | } | |
100 | try { | |
101 | return enUsFormat.parse(json.getAsString()); | |
102 | } catch (ParseException ignored) { | |
103 | } | |
104 | try { | |
105 | return iso8601Format.parse(json.getAsString()); | |
106 | } catch (ParseException e) { | |
107 | throw new JsonSyntaxException(json.getAsString(), e); | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | @Override | |
113 | public String toString() { | |
114 | StringBuilder sb = new StringBuilder(); | |
115 | sb.append(DefaultDateTypeAdapter.class.getSimpleName()); | |
116 | sb.append('(').append(localFormat.getClass().getSimpleName()).append(')'); | |
117 | return sb.toString(); | |
118 | } | |
119 | } |