]> git.basschouten.com Git - openhab-addons.git/blob
06ef0d4516fbb8bf4f199e8107275854909f488f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.dto.response;
14
15 import java.lang.reflect.Type;
16
17 import com.google.gson.JsonElement;
18 import com.google.gson.JsonObject;
19 import com.google.gson.JsonSerializationContext;
20 import com.google.gson.JsonSerializer;
21
22 /**
23  * This response object is a bit complicated. The response is required to look like this:
24  *
25  * <pre>
26  * {
27  *   "success":{
28  *   "/1d49eeed-1fa7-434a-8e6c-70bb2cdc3e8f/lights/1/state/on": true
29  *   }
30  * }
31  * </pre>
32  *
33  * This object describes the right hand side of "success". The json key itself is the uri path
34  * and the value is either a boolean, number or string.
35  *
36  * This is done with a custom serializer that creates the proper {@link JsonObject}.
37  *
38  * @author David Graeff - Initial contribution
39  */
40 public class HueSuccessResponseStateChanged extends HueSuccessResponse {
41     private transient Object value;
42     private transient String relURI;
43
44     public HueSuccessResponseStateChanged(String relURI, Object value) {
45         this.relURI = relURI;
46         this.value = value;
47     }
48
49     public static class Serializer implements JsonSerializer<HueSuccessResponseStateChanged> {
50         @Override
51         public JsonElement serialize(HueSuccessResponseStateChanged product, Type type, JsonSerializationContext jsc) {
52             JsonObject jObj = new JsonObject();
53             if (product.value instanceof Float) {
54                 jObj.addProperty(product.relURI, (Float) product.value);
55             }
56             if (product.value instanceof Double) {
57                 jObj.addProperty(product.relURI, (Double) product.value);
58             }
59             if (product.value instanceof Integer) {
60                 jObj.addProperty(product.relURI, (Integer) product.value);
61             }
62             if (product.value instanceof Boolean) {
63                 jObj.addProperty(product.relURI, (Boolean) product.value);
64             }
65             if (product.value instanceof String) {
66                 jObj.addProperty(product.relURI, (String) product.value);
67             }
68             return jObj;
69         }
70     }
71 }