]> git.basschouten.com Git - openhab-addons.git/blob
74c8305cd652d96fb95982f0ea15a19453e833fc
[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.freeboxos.internal.api.deserialization;
14
15 import java.lang.reflect.Type;
16 import java.util.Objects;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.ForegroundApp;
21 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.PlayerContext;
22 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.TvContext;
23
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParseException;
28
29 /**
30  * Custom deserializer to handle {@link ForegroundApp} object
31  *
32  * @author GaĆ«l L'hopital - Initial contribution
33  */
34 @NonNullByDefault
35 public class ForegroundAppDeserializer implements JsonDeserializer<ForegroundApp> {
36
37     @Override
38     public @NonNull ForegroundApp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
39             throws JsonParseException {
40         Object obj;
41
42         String thePackage = json.getAsJsonObject().get("package").getAsString();
43         JsonElement jsonElement2 = json.getAsJsonObject().get("context");
44         if (jsonElement2 == null) {
45             obj = null;
46         } else if ("fr.freebox.tv".equals(thePackage)) {
47             obj = context.deserialize(jsonElement2, TvContext.class);
48         } else {
49             obj = context.deserialize(jsonElement2, PlayerContext.class);
50         }
51
52         int packageId = json.getAsJsonObject().get("package_id").getAsInt();
53         String curlUrl = json.getAsJsonObject().get("cur_url").getAsString();
54         Objects.requireNonNull(thePackage);
55         return new ForegroundApp(packageId, curlUrl, obj, thePackage);
56     }
57 }