]> git.basschouten.com Git - openhab-addons.git/blob
4213b18bdcbcfaa1abc4a23c385220906c2285fe
[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.netatmo.internal.deserialization;
14
15 import java.time.Instant;
16 import java.time.ZonedDateTime;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.NetatmoException;
21 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
22 import org.openhab.core.i18n.TimeZoneProvider;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
28
29 import com.google.gson.FieldNamingPolicy;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonDeserializer;
33 import com.google.gson.JsonSyntaxException;
34
35 /**
36  * The {@link NADeserializer} is responsible to instantiate suitable Gson (de)serializer
37  *
38  * @author GaĆ«l L'hopital - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = NADeserializer.class)
42 public class NADeserializer {
43     private final Gson gson;
44
45     @Activate
46     public NADeserializer(@Reference TimeZoneProvider timeZoneProvider) {
47         gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
48                 .registerTypeAdapterFactory(new StrictEnumTypeAdapterFactory())
49                 .registerTypeAdapter(NAObjectMap.class, new NAObjectMapDeserializer())
50                 .registerTypeAdapter(NAPushType.class, new NAPushTypeDeserializer())
51                 .registerTypeAdapter(ModuleType.class, new ModuleTypeDeserializer())
52                 .registerTypeAdapter(ZonedDateTime.class,
53                         (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> {
54                             long netatmoTS = json.getAsJsonPrimitive().getAsLong();
55                             Instant i = Instant.ofEpochSecond(netatmoTS);
56                             return ZonedDateTime.ofInstant(i, timeZoneProvider.getTimeZone());
57                         })
58                 .registerTypeAdapter(OnOffType.class,
59                         (JsonDeserializer<OnOffType>) (json, type, jsonDeserializationContext) -> OnOffType
60                                 .from(json.getAsJsonPrimitive().getAsString()))
61                 .registerTypeAdapter(OpenClosedType.class,
62                         (JsonDeserializer<OpenClosedType>) (json, type, jsonDeserializationContext) -> {
63                             String value = json.getAsJsonPrimitive().getAsString().toUpperCase();
64                             return "TRUE".equals(value) || "1".equals(value) ? OpenClosedType.CLOSED
65                                     : OpenClosedType.OPEN;
66                         })
67                 .create();
68     }
69
70     public <T> T deserialize(Class<T> clazz, String json) throws NetatmoException {
71         try {
72             @Nullable
73             T result = gson.fromJson(json, clazz);
74             if (result != null) {
75                 return result;
76             }
77             throw new NetatmoException("Deserialization of '%s' resulted in null value", json);
78         } catch (JsonSyntaxException e) {
79             throw new NetatmoException(e, "Unexpected error deserializing '%s'", json);
80         }
81     }
82 }