]> git.basschouten.com Git - openhab-addons.git/blob
7ebb793d112ea2e9cdc35cc4c232305f4038eb40
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 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
84         BasePoint point;
85         switch (pointType) {
86             case STRING: {
87                 point = ctxt.deserialize(obj, StringPoint.class);
88                 if (point != null) {
89                     return point;
90                 }
91                 break;
92             }
93             case NESTED_NUMBER: {
94                 point = ctxt.deserialize(obj, NestedNumberPoint.class);
95                 if (point != null) {
96                     return point;
97                 }
98                 break;
99             }
100             case NUMBER: {
101                 point = ctxt.deserialize(obj, NumberPoint.class);
102                 if (point != null) {
103                     return point;
104                 }
105                 break;
106             }
107             default: {
108                 point = ctxt.deserialize(obj, UndefPoint.class);
109                 if (point != null) {
110                     return point;
111                 }
112             }
113         }
114         throw new JsonSyntaxException("unable to parse point with a \"value\" element");
115     }
116 }