]> git.basschouten.com Git - openhab-addons.git/blob
e519e8608e21d65b45bc70ebf58e84b27d2793c3
[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.ArrayList;
19 import java.util.List;
20 import java.util.Map.Entry;
21
22 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.imported.CustomizedTypeAdapterFactory;
23 import org.openhab.binding.lametrictime.api.local.model.Action;
24
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27
28 public class ActionTypeAdapterFactory extends CustomizedTypeAdapterFactory<Action>
29 {
30     private static final String PROPERTY_ID = "id";
31     private static final String PROPERTY_PARAMETERS = "params";
32
33     public ActionTypeAdapterFactory()
34     {
35         super(Action.class);
36     }
37
38     @Override
39     protected void beforeWrite(Action source, JsonElement toSerialize)
40     {
41         if (toSerialize == null || toSerialize.isJsonNull())
42         {
43             return;
44         }
45
46         JsonObject actionObj = toSerialize.getAsJsonObject();
47         if (actionObj == null || actionObj.isJsonNull())
48         {
49             return;
50         }
51
52         // rewrite parameters from a nested object (map) to properties on the action
53         JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS);
54         if (paramsElem != null && !paramsElem.isJsonNull())
55         {
56             JsonObject paramsObj = paramsElem.getAsJsonObject();
57             actionObj.remove(PROPERTY_PARAMETERS);
58
59             for (Entry<String, JsonElement> entry : paramsObj.entrySet())
60             {
61                 actionObj.add(entry.getKey(), entry.getValue());
62             }
63         }
64     }
65
66     @Override
67     protected void afterRead(JsonElement deserialized)
68     {
69         if (deserialized == null || deserialized.isJsonNull())
70         {
71             return;
72         }
73
74         JsonObject actionObj = deserialized.getAsJsonObject();
75         if (actionObj == null || actionObj.isJsonNull())
76         {
77             return;
78         }
79
80         if (actionObj.has(PROPERTY_PARAMETERS))
81         {
82             throw new IllegalArgumentException("Attempting to deserialize Action that contains a colliding "
83                                                + PROPERTY_PARAMETERS
84                                                + " property");
85         }
86
87         // temporary list of field names
88         List<String> fields = new ArrayList<>();
89
90         // rewrite parameters to a nested object (map)
91         JsonObject paramsObj = new JsonObject();
92         for (Entry<String, JsonElement> entry : actionObj.entrySet())
93         {
94             // skip ID field
95             if (PROPERTY_ID.equals(entry.getKey()))
96             {
97                 continue;
98             }
99
100             String paramId = entry.getKey();
101             fields.add(paramId); // to be removed later
102
103             paramsObj.add(paramId, entry.getValue());
104         }
105         actionObj.add(PROPERTY_PARAMETERS, paramsObj);
106
107         // remove all fields other than the list
108         fields.forEach(field -> actionObj.remove(field));
109     }
110 }