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.pixometer.internal.serializer;
15 import java.lang.reflect.Type;
16 import java.time.Instant;
17 import java.time.ZonedDateTime;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.pixometer.internal.config.ReadingInstance;
22 import com.google.gson.JsonDeserializationContext;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
29 * Custom Deserializer for meter reading api responses
31 * @author Jerome Luckenbach - Initial contribution
35 public class CustomReadingInstanceDeserializer implements JsonDeserializer<ReadingInstance> {
37 private static final String COUNT = "count";
38 private static final String RESULTS = "results";
39 private static final String URL = "url";
40 private static final String APPLIED_METHOD = "applied_method";
41 private static final String READING_DATE = "reading_date";
42 private static final String VALUE = "value";
43 private static final String VALUE_SECOND_TARIFF = "value_second_tariff";
44 private static final String PROVIDED_FRACTION_DIGITS = "provided_fraction_digits";
45 private static final String PROVIDED_FRACTION_DIGITS_SECOND_TARIFF = "provided_fraction_digits_second_tariff";
49 public ReadingInstance deserialize(final JsonElement json, final Type typeOfT,
50 final JsonDeserializationContext context) throws JsonParseException {
51 final JsonObject jsonObject = json.getAsJsonObject();
53 if (!jsonObject.has(COUNT)) {
54 throw new JsonParseException("Invalid Json Response");
57 ReadingInstance result = new ReadingInstance();
58 int resultCount = Integer.parseInt(jsonObject.get(COUNT).toString());
60 // No readings provided yet
61 if (resultCount < 1) {
62 result.setReadingDate(ZonedDateTime.from(Instant.EPOCH));
66 // Fist result is the last reading instance
67 JsonObject latestReading = jsonObject.getAsJsonArray(RESULTS).get(0).getAsJsonObject();
69 result.setUrl(getStringFromJson(latestReading, URL));
70 result.setAppliedMethod(getStringFromJson(latestReading, APPLIED_METHOD));
71 result.setReadingDate(ZonedDateTime.parse(getStringFromJson(latestReading, READING_DATE)));
72 result.setValue(Double.parseDouble(getStringFromJson(latestReading, VALUE)));
74 // Not all meters provide useful data for second tariff and fraction digits , so zero should be used in case of
76 String secondTariffValue = getStringFromJson(latestReading, VALUE_SECOND_TARIFF);
77 result.setValueSecondTariff(
78 checkStringForNullValues(secondTariffValue) ? 0 : Double.parseDouble(secondTariffValue));
80 String providedFractionDigits = getStringFromJson(latestReading, PROVIDED_FRACTION_DIGITS);
81 result.setProvidedFractionDigits(
82 checkStringForNullValues(providedFractionDigits) ? 0 : Integer.parseInt(providedFractionDigits));
84 String secondprovidedFractionDigits = getStringFromJson(latestReading, PROVIDED_FRACTION_DIGITS_SECOND_TARIFF);
85 result.setProvidedFractionDigitsSecondTariff(checkStringForNullValues(secondprovidedFractionDigits) ? 0
86 : Integer.parseInt(secondprovidedFractionDigits));
92 * Returns the node value and removes possible added quotation marks, which would lead to parse errors.
94 * @param data The Json source to get the string from
95 * @param key The key for the wanted Json Node
96 * @return The wanted string without unnecessary quotation marks
98 private String getStringFromJson(JsonObject data, String key) {
99 return data.get(key).toString().replaceAll("\"", "");
103 * @param s the striong to check
104 * @return returns true if null values have been found, false otherwise
106 private boolean checkStringForNullValues(String s) {
107 return (s == null || s.isEmpty() || s.equals("null"));