]> git.basschouten.com Git - openhab-addons.git/blob
09c0ae0c9c0ff7211675f837a1304ca2950232d9
[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 NeeoMacros} class
32  *
33  * @author Tim Roberts - Initial contribution
34  */
35 @NonNullByDefault
36 public class NeeoMacrosDeserializer implements JsonDeserializer<@Nullable NeeoMacros> {
37     @Nullable
38     @Override
39     public NeeoMacros 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 jsonObject) {
45             final List<NeeoMacro> scenarios = new ArrayList<>();
46             for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
47                 final NeeoMacro macro = context.deserialize(entry.getValue(), NeeoMacro.class);
48                 scenarios.add(macro);
49             }
50
51             return new NeeoMacros(scenarios.toArray(new NeeoMacro[0]));
52         }
53         return null;
54     }
55 }