]> git.basschouten.com Git - openhab-addons.git/blob
1a776ab8f093dfccb9c5d90f6a85418d2befcc28
[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 NeeoRooms} class
32  *
33  * @author Tim Roberts - Initial contribution
34  */
35 @NonNullByDefault
36 public class NeeoRoomsDeserializer implements JsonDeserializer<@Nullable NeeoRooms> {
37     @Nullable
38     @Override
39     public NeeoRooms 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         if (jsonElement instanceof JsonObject) {
44             final List<NeeoRoom> recipes = new ArrayList<>();
45             for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) {
46                 final NeeoRoom room = context.deserialize(entry.getValue(), NeeoRoom.class);
47                 recipes.add(room);
48             }
49
50             return new NeeoRooms(recipes.toArray(new NeeoRoom[0]));
51         }
52         return null;
53     }
54 }