2 * Copyright (c) 2010-2024 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.boschshc.internal.serialization;
15 import java.lang.reflect.Type;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
20 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Message;
21 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Scenario;
22 import org.openhab.binding.boschshc.internal.devices.bridge.dto.UserDefinedState;
23 import org.openhab.binding.boschshc.internal.services.dto.BoschSHCServiceState;
25 import com.google.gson.JsonDeserializationContext;
26 import com.google.gson.JsonDeserializer;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonParseException;
32 * Utility class for JSON deserialization of device data and triggered scenarios using Google Gson.
34 * @author Patrick Gell - Initial contribution
38 public class BoschServiceDataDeserializer implements JsonDeserializer<BoschSHCServiceState> {
42 public BoschSHCServiceState deserialize(JsonElement jsonElement, Type type,
43 JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
44 JsonObject jsonObject = jsonElement.getAsJsonObject();
45 JsonElement dataType = jsonObject.get("@type");
46 switch (dataType.getAsString()) {
47 case "DeviceServiceData" -> {
48 var deviceServiceData = new DeviceServiceData();
49 deviceServiceData.deviceId = jsonObject.get("deviceId").getAsString();
50 deviceServiceData.state = jsonObject.get("state");
51 deviceServiceData.id = jsonObject.get("id").getAsString();
52 deviceServiceData.path = jsonObject.get("path").getAsString();
53 return deviceServiceData;
55 case "scenarioTriggered" -> {
56 var scenario = new Scenario();
57 scenario.id = jsonObject.get("id").getAsString();
58 scenario.name = jsonObject.get("name").getAsString();
59 scenario.lastTimeTriggered = jsonObject.get("lastTimeTriggered").getAsString();
62 case "userDefinedState" -> {
63 var state = new UserDefinedState();
64 state.setId(jsonObject.get("id").getAsString());
65 state.setName(jsonObject.get("name").getAsString());
66 state.setState(jsonObject.get("state").getAsBoolean());
70 return GsonUtils.DEFAULT_GSON_INSTANCE.fromJson(jsonElement, Message.class);
73 return new BoschSHCServiceState(dataType.getAsString());