]> git.basschouten.com Git - openhab-addons.git/blob
ea389e25261f7620dc01feebefbfacc1de2e83f9
[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.unifi.internal.api.util;
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.unifi.internal.api.dto.UniFiClient;
20 import org.openhab.binding.unifi.internal.api.dto.UniFiUnknownClient;
21 import org.openhab.binding.unifi.internal.api.dto.UniFiWiredClient;
22 import org.openhab.binding.unifi.internal.api.dto.UniFiWirelessClient;
23
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParseException;
29
30 /**
31  *
32  * The {@link UniFiClientDeserializer} is an implementation of {@link JsonDeserializer} that deserializes
33  * {@link UniFiClient} instances based on each client's <code>is_wired</code> property contained in the JSON output of
34  * the controller.
35  *
36  * @author Matthew Bowman - Initial contribution
37  */
38 @NonNullByDefault
39 public class UniFiClientDeserializer implements JsonDeserializer<UniFiClient> {
40
41     private static final String PROPERTY_IS_WIRED = "is_wired";
42
43     @Override
44     public @Nullable UniFiClient deserialize(final JsonElement json, final Type type,
45             final JsonDeserializationContext context) throws JsonParseException {
46         final JsonObject jsonObject = json.getAsJsonObject();
47         final JsonElement wiredElement = jsonObject.get(PROPERTY_IS_WIRED);
48         // mgb: if the "is_wired "property is missing, the client is unknown
49         if (wiredElement == null) {
50             return context.deserialize(json, UniFiUnknownClient.class);
51         }
52         if (wiredElement.getAsBoolean()) {
53             return context.deserialize(json, UniFiWiredClient.class);
54         }
55         return context.deserialize(json, UniFiWirelessClient.class);
56     }
57 }