]> git.basschouten.com Git - openhab-addons.git/blob
cb9f7811805918fb3433e2bf512191bfaae67693
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.surepetcare.internal.utils;
14
15 import java.lang.reflect.Type;
16 import java.time.LocalDate;
17 import java.time.format.DateTimeFormatter;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 import com.google.gson.JsonDeserializationContext;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParseException;
26 import com.google.gson.JsonPrimitive;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29
30 /**
31  * GSON serialiser/deserialiser for converting {@link LocalDate} objects.
32  *
33  * @author Rene Scherer - Initial Contribution
34  */
35 @NonNullByDefault
36 public class GsonLocalDateTypeAdapter implements JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> {
37
38     private static final DateTimeFormatter LOCAL_DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;
39     private static final DateTimeFormatter ZONED_DATETIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
40
41     /**
42      * Gson invokes this call-back method during serialization when it encounters a field of the
43      * specified type.
44      * <p>
45      *
46      * In the implementation of this call-back method, you should consider invoking
47      * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
48      * non-trivial field of the {@code src} object. However, you should never invoke it on the
49      * {@code src} object itself since that will cause an infinite loop (Gson will call your
50      * call-back method again).
51      *
52      * @param src the object that needs to be converted to Json.
53      * @param typeOfSrc the actual type (fully genericized version) of the source object.
54      * @return a JsonElement corresponding to the specified object.
55      */
56     @Override
57     public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) {
58         return new JsonPrimitive(LOCAL_DATE_FORMATTER.format(src));
59     }
60
61     /**
62      * Gson invokes this call-back method during deserialization when it encounters a field of the
63      * specified type.
64      * <p>
65      *
66      * In the implementation of this call-back method, you should consider invoking
67      * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects
68      * for any non-trivial field of the returned object. However, you should never invoke it on the
69      * the same type passing {@code json} since that will cause an infinite loop (Gson will call your
70      * call-back method again).
71      *
72      * @param json The Json data being deserialized
73      * @param typeOfT The type of the Object to deserialize to
74      * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
75      * @throws JsonParseException if json is not in the expected format of {@code typeOfT}
76      */
77     @Override
78     public @Nullable LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
79             throws JsonParseException {
80         String el = json.getAsString();
81         // we allow dates to be represented as dates only or with time and timezone offset
82         if (el.length() > 10) {
83             return ZONED_DATETIME_FORMATTER.parse(el, LocalDate::from);
84         } else {
85             return LOCAL_DATE_FORMATTER.parse(el, LocalDate::from);
86         }
87     }
88 }