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.neeo.internal.models;
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.List;
19 import java.util.Objects;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
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;
31 * The implementation of {@link JsonDeserializer} to deserialize a {@link NeeoScenarios} class
33 * @author Tim Roberts - Initial contribution
36 public class NeeoScenariosDeserializer implements JsonDeserializer<@Nullable NeeoScenarios> {
39 public NeeoScenarios deserialize(@Nullable JsonElement jsonElement, @Nullable Type jtype,
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<NeeoScenario> scenarios = new ArrayList<>();
45 for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) {
46 final NeeoScenario scenario = context.deserialize(entry.getValue(), NeeoScenario.class);
47 scenarios.add(scenario);
50 return new NeeoScenarios(scenarios.toArray(new NeeoScenario[0]));