2 * Copyright (c) 2010-2020 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 static enum PointType {
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)");
50 JsonObject obj = element.getAsJsonObject();
51 JsonElement value = obj.get("value");
53 UndefPoint point = ctxt.deserialize(obj, UndefPoint.class);
57 throw new JsonSyntaxException("unable to parse point WITHOUT a \"value\" element");
60 PointType pointType = PointType.UNDEFINED;
62 boolean valueIsPrimitive = value.isJsonPrimitive();
64 JsonElement rep = obj.get("rep");
65 if (rep != null && rep.isJsonPrimitive() && rep.getAsJsonPrimitive().isNumber()) {
67 * full point lists have a "rep" element so we know explicitly the point class
69 int repValue = rep.getAsInt();
71 pointType = PointType.STRING;
72 } else if (repValue < 4) {
73 pointType = valueIsPrimitive ? PointType.NUMBER : PointType.NESTED_NUMBER;
77 * refresh point lists do NOT have a "rep" element so try to infer the point
80 if (valueIsPrimitive) {
81 JsonPrimitive primitiveType = value.getAsJsonPrimitive();
82 pointType = primitiveType.isString() ? PointType.STRING : PointType.NUMBER;
84 pointType = PointType.NESTED_NUMBER;
90 point = ctxt.deserialize(obj, StringPoint.class);
97 point = ctxt.deserialize(obj, NestedNumberPoint.class);
104 point = ctxt.deserialize(obj, NumberPoint.class);
111 point = ctxt.deserialize(obj, UndefPoint.class);
117 throw new JsonSyntaxException("unable to parse point with a \"value\" element");