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