2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.surepetcare.internal.utils;
15 import java.lang.reflect.Type;
16 import java.time.LocalTime;
17 import java.time.format.DateTimeFormatter;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
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;
31 * GSON serialiser/deserialiser for converting {@link LocalTime} objects.
33 * @author Rene Scherer - Initial Contribution
36 public class GsonLocalTimeTypeAdapter implements JsonSerializer<LocalTime>, JsonDeserializer<LocalTime> {
38 private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
41 * Gson invokes this call-back method during serialization when it encounters a field of the
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).
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.
56 public JsonElement serialize(LocalTime src, Type typeOfSrc, JsonSerializationContext context) {
57 return new JsonPrimitive(TIME_FORMATTER.format(src));
61 * Gson invokes this call-back method during deserialization when it encounters a field of the
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).
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}
77 public @Nullable LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
78 throws JsonParseException {
79 return TIME_FORMATTER.parse(json.getAsString(), LocalTime::from);