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.netatmo.internal.deserialization;
15 import java.lang.reflect.ParameterizedType;
16 import java.lang.reflect.Type;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParseException;
29 * The {@link NAObjectMapDeserializer} is a specialized deserializer aimed to transform
30 * a list of `NAObjects` into a map identified by the object's id.
32 * @author Gaƫl L'hopital - Initial contribution
35 class NAObjectMapDeserializer implements JsonDeserializer<NAObjectMap<?>> {
37 public @Nullable NAObjectMap<?> deserialize(JsonElement json, Type clazz, JsonDeserializationContext context)
38 throws JsonParseException {
39 ParameterizedType parameterized = (ParameterizedType) clazz;
40 Type[] typeArguments = parameterized.getActualTypeArguments();
41 if (typeArguments.length > 0 && json instanceof JsonArray jsonArray) {
42 Type objectType = typeArguments[0];
43 NAObjectMap<NAObject> result = new NAObjectMap<>();
44 jsonArray.forEach(item -> {
45 result.put(context.deserialize(item, objectType));