]> git.basschouten.com Git - openhab-addons.git/blob
7c512cfa04b03c1177e363a28c249473fba7e3cc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.gardena.internal.model;
14
15 import java.lang.reflect.Type;
16
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;
21
22 import com.google.gson.*;
23
24 /**
25  * Custom deserializer for Gardena DataItems.
26  *
27  * @author Gerhard Riegler - Initial contribution
28  */
29 @NonNullByDefault
30 public class DataItemDeserializer implements JsonDeserializer<DataItem<?>> {
31     private static Gson gson = new GsonBuilder().create();
32
33     @Override
34     public @Nullable DataItem<?> deserialize(JsonElement element, Type type, JsonDeserializationContext ctx)
35             throws JsonParseException {
36         try {
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);
41         }
42     }
43 }