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