2 * Copyright 2017-2018 Gregory Moyer and contributors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openhab.binding.lametrictime.api.common.impl.typeadapters;
18 import java.util.Map.Entry;
20 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.imported.CustomizedTypeAdapterFactory;
21 import org.openhab.binding.lametrictime.api.local.model.Application;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
26 public class ApplicationTypeAdapterFactory extends CustomizedTypeAdapterFactory<Application>
28 private static final String PROPERTY_ID = "id";
29 private static final String PROPERTY_WIDGETS = "widgets";
30 private static final String PROPERTY_ACTIONS = "actions";
32 public ApplicationTypeAdapterFactory()
34 super(Application.class);
38 protected void beforeWrite(Application source, JsonElement toSerialize)
40 if (toSerialize == null || toSerialize.isJsonNull())
45 JsonObject appObj = toSerialize.getAsJsonObject();
46 if (appObj == null || appObj.isJsonNull())
52 JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
53 if (widgetsElem != null && !widgetsElem.isJsonNull())
55 for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet())
57 JsonElement widgetElem = entry.getValue();
58 if (widgetElem == null || widgetElem.isJsonNull())
62 widgetElem.getAsJsonObject().remove(PROPERTY_ID);
67 JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
68 if (actionsElem != null && !actionsElem.isJsonNull())
70 for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet())
72 JsonElement actionElem = entry.getValue();
73 if (actionElem == null || actionElem.isJsonNull())
77 actionElem.getAsJsonObject().remove(PROPERTY_ID);
83 protected void afterRead(JsonElement deserialized)
85 if (deserialized == null || deserialized.isJsonNull())
90 JsonObject appObj = deserialized.getAsJsonObject();
91 if (appObj == null || appObj.isJsonNull())
97 JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
98 if (widgetsElem != null && !widgetsElem.isJsonNull())
100 for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet())
102 JsonElement widgetElem = entry.getValue();
103 if (widgetElem == null || widgetElem.isJsonNull())
107 widgetElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
112 JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
113 if (actionsElem != null && !actionsElem.isJsonNull())
115 for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet())
117 JsonElement actionElem = entry.getValue();
118 if (actionElem == null || actionElem.isJsonNull())
122 actionElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());