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.ZonedDateTime;
17 import java.time.format.DateTimeFormatter;
18 import java.time.format.DateTimeParseException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
23 import com.google.gson.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonPrimitive;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
32 * GSON serialiser/deserialiser for converting {@link ZonedDateTime} objects.
34 * @author Rene Scherer - Initial Contribution
37 public class GsonZonedDateTimeTypeAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
39 private static final DateTimeFormatter ZONED_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx");
42 * Gson invokes this call-back method during serialization when it encounters a field of the
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).
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.
57 public JsonElement serialize(ZonedDateTime src, Type typeOfSrc, JsonSerializationContext context) {
58 return new JsonPrimitive(ZONED_FORMATTER.format(src));
62 * Gson invokes this call-back method during deserialization when it encounters a field of the
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).
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}
78 public @Nullable ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
79 throws JsonParseException {
80 String content = json.getAsString();
82 return ZonedDateTime.parse(content);
83 } catch (DateTimeParseException e) {
84 throw new JsonParseException("Could not parse as ZonedDateTime: " + content, e);