]> git.basschouten.com Git - openhab-addons.git/blob
f9addb18ae506577f16e2def031d2b4e6f3017fa
[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.mybmw.internal.handler.backend;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mybmw.internal.dto.charge.ChargingSessionsContainer;
21 import org.openhab.binding.mybmw.internal.dto.charge.ChargingStatisticsContainer;
22 import org.openhab.binding.mybmw.internal.dto.remote.ExecutionStatusContainer;
23 import org.openhab.binding.mybmw.internal.dto.vehicle.VehicleBase;
24 import org.openhab.binding.mybmw.internal.dto.vehicle.VehicleStateContainer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.Gson;
29 import com.google.gson.JsonSyntaxException;
30
31 /**
32  * 
33  * deserialization of a JSON string to a Java Object
34  * 
35  * @author Martin Grassl - initial contribution
36  */
37 @NonNullByDefault
38 public interface JsonStringDeserializer {
39
40     static final Logger LOGGER = LoggerFactory.getLogger(JsonStringDeserializer.class);
41
42     static final Gson GSON = new Gson();
43
44     public static List<VehicleBase> getVehicleBaseList(String vehicleBaseJson) {
45         try {
46             VehicleBase[] vehicleBaseArray = deserializeString(vehicleBaseJson, VehicleBase[].class);
47             return Arrays.asList(vehicleBaseArray);
48         } catch (JsonSyntaxException e) {
49             LOGGER.debug("JsonSyntaxException {}", e.getMessage());
50             return new ArrayList<VehicleBase>();
51         }
52     }
53
54     public static VehicleStateContainer getVehicleState(String vehicleStateJson) {
55         try {
56             VehicleStateContainer vehicleState = deserializeString(vehicleStateJson, VehicleStateContainer.class);
57             vehicleState.setRawStateJson(vehicleStateJson);
58             return vehicleState;
59         } catch (JsonSyntaxException e) {
60             LOGGER.debug("JsonSyntaxException {}", e.getMessage());
61             return new VehicleStateContainer();
62         }
63     }
64
65     public static ChargingStatisticsContainer getChargingStatistics(String chargeStatisticsJson) {
66         try {
67             ChargingStatisticsContainer chargeStatistics = deserializeString(chargeStatisticsJson,
68                     ChargingStatisticsContainer.class);
69             return chargeStatistics;
70         } catch (JsonSyntaxException e) {
71             LOGGER.debug("JsonSyntaxException {}", e.getMessage());
72             return new ChargingStatisticsContainer();
73         }
74     }
75
76     public static ChargingSessionsContainer getChargingSessions(String chargeSessionsJson) {
77         try {
78             return deserializeString(chargeSessionsJson, ChargingSessionsContainer.class);
79         } catch (JsonSyntaxException e) {
80             LOGGER.debug("JsonSyntaxException {}", e.getMessage());
81             return new ChargingSessionsContainer();
82         }
83     }
84
85     public static ExecutionStatusContainer getExecutionStatus(String executionStatusJson) {
86         try {
87             return deserializeString(executionStatusJson, ExecutionStatusContainer.class);
88         } catch (JsonSyntaxException e) {
89             LOGGER.debug("JsonSyntaxException {}", e.getMessage());
90             return new ExecutionStatusContainer();
91         }
92     }
93
94     public static <T> T deserializeString(String toBeDeserialized, Class<T> deserializedClass) {
95         return GSON.fromJson(toBeDeserialized, deserializedClass);
96     }
97 }