]> git.basschouten.com Git - openhab-addons.git/blob
a7ab0a3637a8ee4e654405c3156544bbf435cc14
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openhab.binding.lametrictime.api.common.impl.typeadapters;
17
18 import java.util.Map.Entry;
19
20 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.imported.CustomizedTypeAdapterFactory;
21 import org.openhab.binding.lametrictime.api.local.model.UpdateAction;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25
26 public class UpdateActionTypeAdapterFactory extends CustomizedTypeAdapterFactory<UpdateAction>
27 {
28     private static final String PROPERTY_PARAMETERS = "params";
29     private static final String PROPERTY_VALUE = "value";
30
31     public UpdateActionTypeAdapterFactory()
32     {
33         super(UpdateAction.class);
34     }
35
36     @Override
37     protected void beforeWrite(UpdateAction source, JsonElement toSerialize)
38     {
39         if (toSerialize == null || toSerialize.isJsonNull())
40         {
41             return;
42         }
43
44         JsonObject actionObj = toSerialize.getAsJsonObject();
45         if (actionObj == null || actionObj.isJsonNull())
46         {
47             return;
48         }
49
50         // rewrite parameters map from {name => Parameter} to {name => value}
51         JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS);
52         if (paramsElem != null && !paramsElem.isJsonNull())
53         {
54             JsonObject paramsObj = paramsElem.getAsJsonObject();
55             actionObj.remove(PROPERTY_PARAMETERS);
56
57             JsonObject newParamsObj = new JsonObject();
58             for (Entry<String, JsonElement> entry : paramsObj.entrySet())
59             {
60                 newParamsObj.add(entry.getKey(),
61                                  entry.getValue()
62                                       .getAsJsonObject()
63                                       .getAsJsonPrimitive(PROPERTY_VALUE));
64             }
65             actionObj.add(PROPERTY_PARAMETERS, newParamsObj);
66         }
67     }
68
69     @Override
70     protected void afterRead(JsonElement deserialized)
71     {
72         throw new UnsupportedOperationException(UpdateAction.class.getName()
73                                                 + " cannot be derialized");
74     }
75 }