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.lametrictime.internal.api.common.impl.typeadapters;
15 import java.util.Map.Entry;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lametrictime.internal.api.common.impl.typeadapters.imported.CustomizedTypeAdapterFactory;
20 import org.openhab.binding.lametrictime.internal.api.local.dto.Application;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
26 * Adapter factory for applications.
28 * @author Gregory Moyer - Initial contribution
31 public class ApplicationTypeAdapterFactory extends CustomizedTypeAdapterFactory<Application> {
32 private static final String PROPERTY_ID = "id";
33 private static final String PROPERTY_WIDGETS = "widgets";
34 private static final String PROPERTY_ACTIONS = "actions";
36 public ApplicationTypeAdapterFactory() {
37 super(Application.class);
41 protected void beforeWrite(Application source, JsonElement toSerialize) {
42 if (toSerialize == null || toSerialize.isJsonNull()) {
46 JsonObject appObj = toSerialize.getAsJsonObject();
47 if (appObj == null || appObj.isJsonNull()) {
52 JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
53 if (widgetsElem != null && !widgetsElem.isJsonNull()) {
54 for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) {
55 JsonElement widgetElem = entry.getValue();
56 if (widgetElem == null || widgetElem.isJsonNull()) {
59 widgetElem.getAsJsonObject().remove(PROPERTY_ID);
64 JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
65 if (actionsElem != null && !actionsElem.isJsonNull()) {
66 for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) {
67 JsonElement actionElem = entry.getValue();
68 if (actionElem == null || actionElem.isJsonNull()) {
71 actionElem.getAsJsonObject().remove(PROPERTY_ID);
77 protected void afterRead(@Nullable JsonElement deserialized) {
78 if (deserialized == null || deserialized.isJsonNull()) {
82 JsonObject appObj = deserialized.getAsJsonObject();
83 if (appObj == null || appObj.isJsonNull()) {
88 JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
89 if (widgetsElem != null && !widgetsElem.isJsonNull()) {
90 for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) {
91 JsonElement widgetElem = entry.getValue();
92 if (widgetElem == null || widgetElem.isJsonNull()) {
95 widgetElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
100 JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
101 if (actionsElem != null && !actionsElem.isJsonNull()) {
102 for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) {
103 JsonElement actionElem = entry.getValue();
104 if (actionElem == null || actionElem.isJsonNull()) {
107 actionElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());