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.energidataservice.internal.api.serialization;
15 import java.lang.reflect.Type;
16 import java.time.LocalDateTime;
17 import java.time.format.DateTimeParseException;
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;
28 * The {@link LocalDateTimeDeserializer} converts a formatted string to {@link LocalDateTime}.
30 * @author Jacob Laursen - Initial contribution
33 public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> {
36 public @Nullable LocalDateTime deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2)
37 throws JsonParseException {
39 return LocalDateTime.parse(element.getAsString());
40 } catch (DateTimeParseException e) {
41 throw new JsonParseException("Could not parse as LocalDateTime: " + element.getAsString(), e);