]> git.basschouten.com Git - openhab-addons.git/blob
b1b2b9d1ba6535407dff029e1ddbeea97dcb6d97
[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.binding.siemensrds.points;
14
15 import java.lang.reflect.Type;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19
20 import com.google.gson.JsonDeserializationContext;
21 import com.google.gson.JsonDeserializer;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParseException;
25 import com.google.gson.JsonPrimitive;
26 import com.google.gson.JsonSyntaxException;
27
28 /**
29  * private class a JSON de-serializer for the Data Point classes above
30  *
31  * @author Andrew Fiddian-Green - Initial contribution
32  */
33 @NonNullByDefault
34 public class PointDeserializer implements JsonDeserializer<BasePoint> {
35
36     private static enum PointType {
37         UNDEFINED,
38         STRING,
39         NESTED_NUMBER,
40         NUMBER
41     }
42
43     @Override
44     public @Nullable BasePoint deserialize(JsonElement element, Type guff, JsonDeserializationContext ctxt)
45             throws JsonParseException {
46         JsonObject obj = element.getAsJsonObject();
47         JsonElement value = obj.get("value");
48         if (value == null) {
49             UndefPoint point = ctxt.deserialize(obj, UndefPoint.class);
50             if (point != null) {
51                 return point;
52             }
53             throw new JsonSyntaxException("unable to parse point WITHOUT a \"value\" element");
54         }
55
56         PointType pointType = PointType.UNDEFINED;
57
58         boolean valueIsPrimitive = value.isJsonPrimitive();
59
60         JsonElement rep = obj.get("rep");
61         if (rep != null && rep.isJsonPrimitive() && rep.getAsJsonPrimitive().isNumber()) {
62             /*
63              * full point lists have a "rep" element so we know explicitly the point class
64              */
65             int repValue = rep.getAsInt();
66             if (repValue == 0) {
67                 pointType = PointType.STRING;
68             } else if (repValue < 4) {
69                 pointType = valueIsPrimitive ? PointType.NUMBER : PointType.NESTED_NUMBER;
70             }
71         } else {
72             /*
73              * refresh point lists do NOT have a "rep" element so try to infer the point
74              * class
75              */
76             if (valueIsPrimitive) {
77                 JsonPrimitive primitiveType = value.getAsJsonPrimitive();
78                 pointType = primitiveType.isString() ? PointType.STRING : PointType.NUMBER;
79             } else
80                 pointType = PointType.NESTED_NUMBER;
81         }
82
83         BasePoint point;
84         switch (pointType) {
85             case STRING: {
86                 point = ctxt.deserialize(obj, StringPoint.class);
87                 if (point != null) {
88                     return point;
89                 }
90                 break;
91             }
92             case NESTED_NUMBER: {
93                 point = ctxt.deserialize(obj, NestedNumberPoint.class);
94                 if (point != null) {
95                     return point;
96                 }
97                 break;
98             }
99             case NUMBER: {
100                 point = ctxt.deserialize(obj, NumberPoint.class);
101                 if (point != null) {
102                     return point;
103                 }
104                 break;
105             }
106             default: {
107                 point = ctxt.deserialize(obj, UndefPoint.class);
108                 if (point != null) {
109                     return point;
110                 }
111             }
112         }
113         throw new JsonSyntaxException("unable to parse point with a \"value\" element");
114     }
115 }