2 * Copyright (c) 2010-2020 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.gardena.internal.model;
15 import java.lang.reflect.Type;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.gardena.internal.exception.GardenaException;
20 import org.openhab.binding.gardena.internal.model.dto.api.DataItem;
22 import com.google.gson.*;
25 * Custom deserializer for Gardena DataItems.
27 * @author Gerhard Riegler - Initial contribution
30 public class DataItemDeserializer implements JsonDeserializer<DataItem<?>> {
31 private static Gson gson = new GsonBuilder().create();
34 public @Nullable DataItem<?> deserialize(JsonElement element, Type type, JsonDeserializationContext ctx)
35 throws JsonParseException {
37 JsonObject jsonObj = element.getAsJsonObject();
38 return gson.fromJson(element, DataItemFactory.create(jsonObj.get("type").getAsString()));
39 } catch (GardenaException ex) {
40 throw new JsonParseException(ex.getMessage(), ex);