]> git.basschouten.com Git - openhab-addons.git/blob
adcd8fe26ac0d4fe2805f469b7aa68cffdb504c8
[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.dbquery.internal.domain;
14
15 import java.lang.reflect.Type;
16 import java.time.Instant;
17 import java.time.format.DateTimeFormatter;
18 import java.util.Date;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonNull;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonPrimitive;
30 import com.google.gson.JsonSerializationContext;
31 import com.google.gson.JsonSerializer;
32
33 /**
34  * Encodes domain objects to JSON
35  *
36  * @author Joan Pujol - Initial contribution
37  */
38 @NonNullByDefault
39 public class DBQueryJSONEncoder {
40     private final Gson gson;
41
42     public DBQueryJSONEncoder() {
43         gson = new GsonBuilder().registerTypeAdapter(QueryResult.class, new QueryResultGSONSerializer())
44                 .registerTypeAdapter(ResultRow.class, new ResultRowGSONSerializer())
45                 .registerTypeAdapter(QueryParameters.class, new QueryParametersGSONSerializer()).create();
46     }
47
48     public String encode(QueryResult queryResult) {
49         return gson.toJson(queryResult);
50     }
51
52     public String encode(QueryParameters parameters) {
53         return gson.toJson(parameters);
54     }
55
56     @NonNullByDefault({})
57     private static class QueryResultGSONSerializer implements JsonSerializer<QueryResult> {
58         @Override
59         public JsonElement serialize(QueryResult src, Type typeOfSrc, JsonSerializationContext context) {
60             JsonObject jsonObject = new JsonObject();
61             jsonObject.addProperty("correct", src.isCorrect());
62             if (src.getErrorMessage() != null) {
63                 jsonObject.addProperty("errorMessage", src.getErrorMessage());
64             }
65             jsonObject.add("data", context.serialize(src.getData()));
66             return jsonObject;
67         }
68     }
69
70     private static class ResultRowGSONSerializer implements JsonSerializer<ResultRow> {
71         @Override
72         public JsonElement serialize(ResultRow src, Type typeOfSrc, JsonSerializationContext context) {
73             JsonObject jsonObject = new JsonObject();
74             for (String columnName : src.getColumnNames()) {
75                 jsonObject.add(columnName, convertValueToJsonPrimitive(src.getValue(columnName)));
76             }
77             return jsonObject;
78         }
79     }
80
81     private static class QueryParametersGSONSerializer implements JsonSerializer<QueryParameters> {
82         @Override
83         public JsonElement serialize(QueryParameters src, Type typeOfSrc, JsonSerializationContext context) {
84             JsonObject jsonObject = new JsonObject();
85             for (Map.Entry<String, @Nullable Object> param : src.getAll().entrySet()) {
86                 jsonObject.add(param.getKey(), convertValueToJsonPrimitive(param.getValue()));
87             }
88             return jsonObject;
89         }
90     }
91
92     private static JsonElement convertValueToJsonPrimitive(@Nullable Object value) {
93         if (value instanceof Number) {
94             return new JsonPrimitive((Number) value);
95         } else if (value instanceof Boolean) {
96             return new JsonPrimitive((Boolean) value);
97         } else if (value instanceof Character) {
98             return new JsonPrimitive((Character) value);
99         } else if (value instanceof Date) {
100             return new JsonPrimitive(DateTimeFormatter.ISO_INSTANT.format(((Date) value).toInstant()));
101         } else if (value instanceof Instant) {
102             return new JsonPrimitive(DateTimeFormatter.ISO_INSTANT.format((Instant) value));
103         } else if (value != null) {
104             return new JsonPrimitive(value.toString());
105         } else {
106             return JsonNull.INSTANCE;
107         }
108     }
109 }