]> git.basschouten.com Git - openhab-addons.git/blob
b99cb4db68c921fca1b43adbf3f67fe4ee7b30df
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.netatmo.internal.deserialization;
14
15 import java.lang.reflect.Type;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.netatmo.internal.api.data.EventType;
20 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
21
22 import com.google.gson.JsonDeserializationContext;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParseException;
26
27 /**
28  * Specialized deserializer for push_type field
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  */
32 @NonNullByDefault
33 class NAPushTypeDeserializer implements JsonDeserializer<NAPushType> {
34
35     @Override
36     public @Nullable NAPushType deserialize(JsonElement json, Type clazz, JsonDeserializationContext context)
37             throws JsonParseException {
38         String string = json.getAsString();
39         String[] elements = string.split("-");
40         if (elements.length > 1) {
41             try {
42                 ModuleType moduleType = ModuleType.from(elements[0]);
43                 EventType eventType = EventType.valueOf(elements[1].toUpperCase());
44
45                 return new NAPushType(moduleType, eventType);
46             } catch (IllegalArgumentException e) {
47             }
48         }
49         throw new JsonParseException("Error deserializing : " + string);
50     }
51 }