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.netatmo.internal.deserialization;
15 import java.time.Instant;
16 import java.time.ZonedDateTime;
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;
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;
36 * The {@link NADeserializer} is responsible to instantiate suitable Gson (de)serializer
38 * @author Gaƫl L'hopital - Initial contribution
41 @Component(service = NADeserializer.class)
42 public class NADeserializer {
43 private final Gson gson;
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());
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;
70 public <T> T deserialize(Class<T> clazz, String json) throws NetatmoException {
73 T result = gson.fromJson(json, clazz);
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);