]> git.basschouten.com Git - openhab-addons.git/blob
87317802bb78ce42f91ab900e605f4fa5209755b
[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.LocalTime;
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 LocalTime} objects.
32  *
33  * @author Rene Scherer - Initial Contribution
34  */
35 @NonNullByDefault
36 public class GsonLocalTimeTypeAdapter implements JsonSerializer<LocalTime>, JsonDeserializer<LocalTime> {
37
38     private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
39
40     /**
41      * Gson invokes this call-back method during serialization when it encounters a field of the
42      * specified type.
43      * <p>
44      *
45      * In the implementation of this call-back method, you should consider invoking
46      * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
47      * non-trivial field of the {@code src} object. However, you should never invoke it on the
48      * {@code src} object itself since that will cause an infinite loop (Gson will call your
49      * call-back method again).
50      *
51      * @param src the object that needs to be converted to Json.
52      * @param typeOfSrc the actual type (fully genericized version) of the source object.
53      * @return a JsonElement corresponding to the specified object.
54      */
55     @Override
56     public JsonElement serialize(LocalTime src, Type typeOfSrc, JsonSerializationContext context) {
57         return new JsonPrimitive(TIME_FORMATTER.format(src));
58     }
59
60     /**
61      * Gson invokes this call-back method during deserialization when it encounters a field of the
62      * specified type.
63      * <p>
64      *
65      * In the implementation of this call-back method, you should consider invoking
66      * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects
67      * for any non-trivial field of the returned object. However, you should never invoke it on the
68      * the same type passing {@code json} since that will cause an infinite loop (Gson will call your
69      * call-back method again).
70      *
71      * @param json The Json data being deserialized
72      * @param typeOfT The type of the Object to deserialize to
73      * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
74      * @throws JsonParseException if json is not in the expected format of {@code typeOfT}
75      */
76     @Override
77     public @Nullable LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
78             throws JsonParseException {
79         return TIME_FORMATTER.parse(json.getAsString(), LocalTime::from);
80     }
81 }