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.UpdateAction;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
26 public class UpdateActionTypeAdapterFactory extends CustomizedTypeAdapterFactory<UpdateAction>
28 private static final String PROPERTY_PARAMETERS = "params";
29 private static final String PROPERTY_VALUE = "value";
31 public UpdateActionTypeAdapterFactory()
33 super(UpdateAction.class);
37 protected void beforeWrite(UpdateAction source, JsonElement toSerialize)
39 if (toSerialize == null || toSerialize.isJsonNull())
44 JsonObject actionObj = toSerialize.getAsJsonObject();
45 if (actionObj == null || actionObj.isJsonNull())
50 // rewrite parameters map from {name => Parameter} to {name => value}
51 JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS);
52 if (paramsElem != null && !paramsElem.isJsonNull())
54 JsonObject paramsObj = paramsElem.getAsJsonObject();
55 actionObj.remove(PROPERTY_PARAMETERS);
57 JsonObject newParamsObj = new JsonObject();
58 for (Entry<String, JsonElement> entry : paramsObj.entrySet())
60 newParamsObj.add(entry.getKey(),
63 .getAsJsonPrimitive(PROPERTY_VALUE));
65 actionObj.add(PROPERTY_PARAMETERS, newParamsObj);
70 protected void afterRead(JsonElement deserialized)
72 throw new UnsupportedOperationException(UpdateAction.class.getName()
73 + " cannot be derialized");