]> git.basschouten.com Git - openhab-addons.git/blob
5da16d29e3025ecc76deb2737667890938b8f4db
[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.squeezebox.internal.dto;
14
15 import java.lang.reflect.Type;
16 import java.util.stream.Collectors;
17 import java.util.stream.StreamSupport;
18
19 import com.google.gson.JsonDeserializationContext;
20 import com.google.gson.JsonDeserializer;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParseException;
24
25 /**
26  * The {@link ButtonDTODeserializer} is responsible for deserializing a button object, which
27  * can either be an Integer, or a custom button specification.
28  *
29  * @author Mark Hilbush - Initial contribution
30  */
31 public class ButtonDTODeserializer implements JsonDeserializer<ButtonDTO> {
32
33     @Override
34     public ButtonDTO deserialize(JsonElement jsonElement, Type tyoeOfT, JsonDeserializationContext context)
35             throws JsonParseException {
36         ButtonDTO button = null;
37         if (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber()) {
38             Integer value = jsonElement.getAsInt();
39             button = new ButtonDTO();
40             button.custom = false;
41             button.enabled = value != 0;
42         } else if (jsonElement.isJsonObject()) {
43             JsonObject jsonObject = jsonElement.getAsJsonObject();
44             button = new ButtonDTO();
45             button.custom = true;
46             button.icon = jsonObject.get("icon").getAsString();
47             button.jiveStyle = jsonObject.get("jiveStyle").getAsString();
48             button.toolTip = jsonObject.get("tooltip").getAsString();
49             button.command = StreamSupport.stream(jsonObject.getAsJsonArray("command").spliterator(), false)
50                     .map(JsonElement::getAsString).collect(Collectors.joining(" "));
51         }
52         return button;
53     }
54 }