2 * Copyright (c) 2010-2021 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.io.hueemulation.internal.dto.response;
15 import java.lang.reflect.Type;
17 import com.google.gson.JsonElement;
18 import com.google.gson.JsonObject;
19 import com.google.gson.JsonSerializationContext;
20 import com.google.gson.JsonSerializer;
23 * This response object is a bit complicated. The response is required to look like this:
28 * "/1d49eeed-1fa7-434a-8e6c-70bb2cdc3e8f/lights/1/state/on": true
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.
36 * This is done with a custom serializer that creates the proper {@link JsonObject}.
38 * @author David Graeff - Initial contribution
40 public class HueSuccessResponseStateChanged extends HueSuccessResponse {
41 private transient Object value;
42 private transient String relURI;
44 public HueSuccessResponseStateChanged(String relURI, Object value) {
49 public static class Serializer implements JsonSerializer<HueSuccessResponseStateChanged> {
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);
56 if (product.value instanceof Double) {
57 jObj.addProperty(product.relURI, (Double) product.value);
59 if (product.value instanceof Integer) {
60 jObj.addProperty(product.relURI, (Integer) product.value);
62 if (product.value instanceof Boolean) {
63 jObj.addProperty(product.relURI, (Boolean) product.value);
65 if (product.value instanceof String) {
66 jObj.addProperty(product.relURI, (String) product.value);