2 * Copyright (c) 2017. Developed by Hedgecode.
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 org.hedgecode.snooker.gson;
19 import java.io.IOException;
20 import java.text.DateFormat;
21 import java.text.ParseException;
22 import java.text.ParsePosition;
23 import java.util.Date;
24 import java.util.Locale;
26 import com.google.gson.Gson;
27 import com.google.gson.JsonSyntaxException;
28 import com.google.gson.TypeAdapter;
29 import com.google.gson.TypeAdapterFactory;
30 import com.google.gson.internal.bind.util.ISO8601Utils;
31 import com.google.gson.reflect.TypeToken;
32 import com.google.gson.stream.JsonReader;
33 import com.google.gson.stream.JsonToken;
34 import com.google.gson.stream.JsonWriter;
37 * GSON Adapter for Date including a check for an empty string.
39 * @author Dmitry Samoshin aka gotty
41 public final class EmptyDateTypeAdapter extends TypeAdapter<Date> {
43 public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
44 @SuppressWarnings("unchecked")
46 public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
47 return typeToken.getRawType() == Date.class
48 ? (TypeAdapter<T>) new EmptyDateTypeAdapter()
53 private final DateFormat enUsFormat =
54 DateFormat.getDateTimeInstance(
55 DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US
57 private final DateFormat localFormat =
58 DateFormat.getDateTimeInstance(
59 DateFormat.DEFAULT, DateFormat.DEFAULT
63 public Date read(JsonReader in) throws IOException {
64 if (in.peek() == JsonToken.NULL) {
68 return deserializeToDate(in.nextString());
71 private synchronized Date deserializeToDate(String json) {
75 return localFormat.parse(json);
76 } catch (ParseException ignored) {
79 return enUsFormat.parse(json);
80 } catch (ParseException ignored) {
83 return ISO8601Utils.parse(json, new ParsePosition(0));
84 } catch (ParseException ignored) {
87 return new Date(Long.parseLong(json));
88 } catch (Exception e) {
89 throw new JsonSyntaxException(json, e);
94 public synchronized void write(JsonWriter out, Date value) throws IOException {
98 String dateFormatAsString = enUsFormat.format(value);
99 out.value(dateFormatAsString);