]> git.basschouten.com Git - openhab-addons.git/blob
467a05682aedaad2fc4a5678fe576d945ba26844
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 BasePoint deserialize(@Nullable JsonElement element, @Nullable Type guff,
45             @Nullable JsonDeserializationContext ctxt) throws JsonParseException {
46         if (element == null || ctxt == null) {
47             throw new JsonParseException("method called with null argument(s)");
48         }
49
50         JsonObject obj = element.getAsJsonObject();
51         JsonElement value = obj.get("value");
52         if (value == null) {
53             UndefPoint point = ctxt.deserialize(obj, UndefPoint.class);
54             if (point != null) {
55                 return point;
56             }
57             throw new JsonSyntaxException("unable to parse point WITHOUT a \"value\" element");
58         }
59
60         PointType pointType = PointType.UNDEFINED;
61
62         boolean valueIsPrimitive = value.isJsonPrimitive();
63
64         JsonElement rep = obj.get("rep");
65         if (rep != null && rep.isJsonPrimitive() && rep.getAsJsonPrimitive().isNumber()) {
66             /*
67              * full point lists have a "rep" element so we know explicitly the point class
68              */
69             int repValue = rep.getAsInt();
70             if (repValue == 0) {
71                 pointType = PointType.STRING;
72             } else if (repValue < 4) {
73                 pointType = valueIsPrimitive ? PointType.NUMBER : PointType.NESTED_NUMBER;
74             }
75         } else {
76             /*
77              * refresh point lists do NOT have a "rep" element so try to infer the point
78              * class
79              */
80             if (valueIsPrimitive) {
81                 JsonPrimitive primitiveType = value.getAsJsonPrimitive();
82                 pointType = primitiveType.isString() ? PointType.STRING : PointType.NUMBER;
83             } else
84                 pointType = PointType.NESTED_NUMBER;
85         }
86
87         BasePoint point;
88         switch (pointType) {
89             case STRING: {
90                 point = ctxt.deserialize(obj, StringPoint.class);
91                 if (point != null) {
92                     return point;
93                 }
94                 break;
95             }
96             case NESTED_NUMBER: {
97                 point = ctxt.deserialize(obj, NestedNumberPoint.class);
98                 if (point != null) {
99                     return point;
100                 }
101                 break;
102             }
103             case NUMBER: {
104                 point = ctxt.deserialize(obj, NumberPoint.class);
105                 if (point != null) {
106                     return point;
107                 }
108                 break;
109             }
110             default: {
111                 point = ctxt.deserialize(obj, UndefPoint.class);
112                 if (point != null) {
113                     return point;
114                 }
115             }
116         }
117         throw new JsonSyntaxException("unable to parse point with a \"value\" element");
118     }
119 }