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.LocalDate;
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 LocalDate} objects.
33 * @author Rene Scherer - Initial Contribution
36 public class GsonLocalDateTypeAdapter implements JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> {
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;
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(LocalDate src, Type typeOfSrc, JsonSerializationContext context) {
58 return new JsonPrimitive(LOCAL_DATE_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 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);
85 return LOCAL_DATE_FORMATTER.parse(el, LocalDate::from);