2 * Copyright (c) 2010-2023 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.binding.siemensrds.points;
15 import java.lang.reflect.Type;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
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;
29 * private class a JSON de-serializer for the Data Point classes above
31 * @author Andrew Fiddian-Green - Initial contribution
34 public class PointDeserializer implements JsonDeserializer<BasePoint> {
36 private enum PointType {
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");
49 UndefPoint point = ctxt.deserialize(obj, UndefPoint.class);
53 throw new JsonSyntaxException("unable to parse point WITHOUT a \"value\" element");
56 PointType pointType = PointType.UNDEFINED;
58 boolean valueIsPrimitive = value.isJsonPrimitive();
60 JsonElement rep = obj.get("rep");
61 if (rep != null && rep.isJsonPrimitive() && rep.getAsJsonPrimitive().isNumber()) {
63 * full point lists have a "rep" element so we know explicitly the point class
65 int repValue = rep.getAsInt();
67 pointType = PointType.STRING;
68 } else if (repValue < 4) {
69 pointType = valueIsPrimitive ? PointType.NUMBER : PointType.NESTED_NUMBER;
73 * refresh point lists do NOT have a "rep" element so try to infer the point
76 if (valueIsPrimitive) {
77 JsonPrimitive primitiveType = value.getAsJsonPrimitive();
78 pointType = primitiveType.isString() ? PointType.STRING : PointType.NUMBER;
80 pointType = PointType.NESTED_NUMBER;
87 point = ctxt.deserialize(obj, StringPoint.class);
94 point = ctxt.deserialize(obj, NestedNumberPoint.class);
101 point = ctxt.deserialize(obj, NumberPoint.class);
108 point = ctxt.deserialize(obj, UndefPoint.class);
114 throw new JsonSyntaxException("unable to parse point with a \"value\" element");