2 * Copyright (C) 2008 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.google
.gson
;
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
;
29 * This type adapter supports three subclasses of date: Date, Timestamp, and
32 * @author Inderjeet Singh
35 final class DefaultDateTypeAdapter
implements JsonSerializer
<Date
>, JsonDeserializer
<Date
> {
37 // TODO: migrate to streaming adapter
39 private final DateFormat enUsFormat
;
40 private final DateFormat localFormat
;
41 private final DateFormat iso8601Format
;
43 DefaultDateTypeAdapter() {
44 this(DateFormat
.getDateTimeInstance(DateFormat
.DEFAULT
, DateFormat
.DEFAULT
, Locale
.US
),
45 DateFormat
.getDateTimeInstance(DateFormat
.DEFAULT
, DateFormat
.DEFAULT
));
48 DefaultDateTypeAdapter(String datePattern
) {
49 this(new SimpleDateFormat(datePattern
, Locale
.US
), new SimpleDateFormat(datePattern
));
52 DefaultDateTypeAdapter(int style
) {
53 this(DateFormat
.getDateInstance(style
, Locale
.US
), DateFormat
.getDateInstance(style
));
56 public DefaultDateTypeAdapter(int dateStyle
, int timeStyle
) {
57 this(DateFormat
.getDateTimeInstance(dateStyle
, timeStyle
, Locale
.US
),
58 DateFormat
.getDateTimeInstance(dateStyle
, timeStyle
));
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"));
68 // These methods need to be synchronized since JDK DateFormat classes are not thread-safe
70 public JsonElement
serialize(Date src
, Type typeOfSrc
, JsonSerializationContext context
) {
71 synchronized (localFormat
) {
72 String dateFormatAsString
= enUsFormat
.format(src
);
73 return new JsonPrimitive(dateFormatAsString
);
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");
82 Date date
= deserializeToDate(json
);
83 if (typeOfT
== Date
.class) {
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());
90 throw new IllegalArgumentException(getClass() + " cannot deserialize to " + typeOfT
);
94 private Date
deserializeToDate(JsonElement json
) {
95 synchronized (localFormat
) {
97 return localFormat
.parse(json
.getAsString());
98 } catch (ParseException ignored
) {
101 return enUsFormat
.parse(json
.getAsString());
102 } catch (ParseException ignored
) {
105 return iso8601Format
.parse(json
.getAsString());
106 } catch (ParseException e
) {
107 throw new JsonSyntaxException(json
.getAsString(), e
);
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();