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.UpdateAction;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
26 * Adapter factory for update actions.
28 * @author Gregory Moyer - Initial contribution
31 public class UpdateActionTypeAdapterFactory extends CustomizedTypeAdapterFactory<UpdateAction> {
32 private static final String PROPERTY_PARAMETERS = "params";
33 private static final String PROPERTY_VALUE = "value";
35 public UpdateActionTypeAdapterFactory() {
36 super(UpdateAction.class);
40 protected void beforeWrite(UpdateAction source, JsonElement toSerialize) {
41 if (toSerialize == null || toSerialize.isJsonNull()) {
45 JsonObject actionObj = toSerialize.getAsJsonObject();
46 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()) {
53 JsonObject paramsObj = paramsElem.getAsJsonObject();
54 actionObj.remove(PROPERTY_PARAMETERS);
56 JsonObject newParamsObj = new JsonObject();
57 for (Entry<String, JsonElement> entry : paramsObj.entrySet()) {
58 newParamsObj.add(entry.getKey(), entry.getValue().getAsJsonObject().getAsJsonPrimitive(PROPERTY_VALUE));
60 actionObj.add(PROPERTY_PARAMETERS, newParamsObj);
65 protected void afterRead(@Nullable JsonElement deserialized) {
66 throw new UnsupportedOperationException(UpdateAction.class.getName() + " cannot be derialized");