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.binding.netatmo.internal.api.dto.HomeData;
23 import org.openhab.core.i18n.TimeZoneProvider;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
30 import com.google.gson.FieldNamingPolicy;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonDeserializer;
34 import com.google.gson.JsonSyntaxException;
37 * The {@link NADeserializer} is responsible to instantiate suitable Gson (de)serializer
39 * @author Gaƫl L'hopital - Initial contribution
42 @Component(service = NADeserializer.class)
43 public class NADeserializer {
44 private final Gson gson;
47 public NADeserializer(@Reference TimeZoneProvider timeZoneProvider) {
48 gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
49 .registerTypeAdapterFactory(new StrictEnumTypeAdapterFactory())
50 .registerTypeAdapter(NAObjectMap.class, new NAObjectMapDeserializer())
51 .registerTypeAdapter(NAPushType.class, new NAPushTypeDeserializer())
52 .registerTypeAdapter(ModuleType.class, new ModuleTypeDeserializer())
53 .registerTypeAdapter(HomeData.class,
54 (JsonDeserializer<HomeData>) (json, type, context) -> context.deserialize(json,
55 json.getAsJsonObject().has("therm_mode") ? HomeData.Energy.class
56 : HomeData.Security.class))
57 .registerTypeAdapter(ZonedDateTime.class, (JsonDeserializer<ZonedDateTime>) (json, type, context) -> {
58 long netatmoTS = json.getAsJsonPrimitive().getAsLong();
59 Instant i = Instant.ofEpochSecond(netatmoTS);
60 return ZonedDateTime.ofInstant(i, timeZoneProvider.getTimeZone());
62 .registerTypeAdapter(OnOffType.class,
63 (JsonDeserializer<OnOffType>) (json, type, context) -> OnOffType
64 .from(json.getAsJsonPrimitive().getAsString()))
65 .registerTypeAdapter(OpenClosedType.class, (JsonDeserializer<OpenClosedType>) (json, type, context) -> {
66 String value = json.getAsJsonPrimitive().getAsString().toUpperCase();
67 return "TRUE".equals(value) || "1".equals(value) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
71 public <T> T deserialize(Class<T> clazz, String json) throws NetatmoException {
74 T result = gson.fromJson(json, clazz);
78 throw new NetatmoException("Deserialization of '%s' resulted in null value", json);
79 } catch (JsonSyntaxException e) {
80 throw new NetatmoException(e, "Unexpected error deserializing '%s'", json);