]> git.basschouten.com Git - openhab-addons.git/blob
1b7e9080cf3191ad800aa7b5abaa600600d56336
[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.ecobee.internal.dto.thermostat;
14
15 import static org.openhab.binding.ecobee.internal.EcobeeBindingConstants.ECOBEE_DATETIME_FORMAT;
16
17 import java.lang.reflect.Type;
18 import java.time.LocalDateTime;
19 import java.time.format.DateTimeFormatter;
20 import java.time.format.DateTimeParseException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24
25 import com.google.gson.JsonDeserializationContext;
26 import com.google.gson.JsonDeserializer;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonParseException;
29
30 /**
31  * The {@link LocalDateTimeDeserializer} is responsible for handling the local dates returned from
32  * the Ecobee API service.
33  *
34  * @author Mark Hilbush - Initial contribution
35  */
36 @NonNullByDefault
37 public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> {
38
39     @Override
40     public @Nullable LocalDateTime deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2)
41             throws JsonParseException {
42         try {
43             DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ECOBEE_DATETIME_FORMAT);
44             return formatter.parse(element.getAsString(), LocalDateTime::from);
45         } catch (DateTimeParseException e) {
46             return null;
47         }
48     }
49 }