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