2 * Copyright (c) 2010-2023 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.squeezebox.internal.dto;
15 import java.lang.reflect.Type;
16 import java.util.stream.Collectors;
17 import java.util.stream.StreamSupport;
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;
26 * The {@link ButtonDTODeserializer} is responsible for deserializing a button object, which
27 * can either be an Integer, or a custom button specification.
29 * @author Mark Hilbush - Initial contribution
31 public class ButtonDTODeserializer implements JsonDeserializer<ButtonDTO> {
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();
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(" "));