]> git.basschouten.com Git - openhab-addons.git/blob
945bb58b5b080ee56927b6aec93fc2bc18b1bfaf
[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.neeo.internal.models;
14
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Objects;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParseException;
29
30 /**
31  * The implementation of {@link JsonDeserializer} to deserialize a {@link NeeoDevices} class
32  *
33  * @author Tim Roberts - Initial contribution
34  */
35 @NonNullByDefault
36 public class NeeoDevicesDeserializer implements JsonDeserializer<@Nullable NeeoDevices> {
37     @Nullable
38     @Override
39     public NeeoDevices deserialize(@Nullable JsonElement jsonElement, @Nullable Type type,
40             @Nullable JsonDeserializationContext context) throws JsonParseException {
41         Objects.requireNonNull(jsonElement, "jsonElement cannot be null");
42         Objects.requireNonNull(context, "context cannot be null");
43
44         if (jsonElement instanceof JsonObject) {
45             final List<NeeoDevice> scenarios = new ArrayList<>();
46             for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) {
47                 final NeeoDevice device = context.deserialize(entry.getValue(), NeeoDevice.class);
48                 scenarios.add(device);
49             }
50
51             return new NeeoDevices(scenarios.toArray(new NeeoDevice[0]));
52         }
53         return null;
54     }
55 }