2 * Copyright (c) 2010-2022 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.dbquery.internal.domain;
15 import java.lang.reflect.Type;
16 import java.time.Instant;
17 import java.time.format.DateTimeFormatter;
18 import java.util.Date;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
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;
34 * Encodes domain objects to JSON
36 * @author Joan Pujol - Initial contribution
39 public class DBQueryJSONEncoder {
40 private final Gson gson;
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();
48 public String encode(QueryResult queryResult) {
49 return gson.toJson(queryResult);
52 public String encode(QueryParameters parameters) {
53 return gson.toJson(parameters);
57 private static class QueryResultGSONSerializer implements JsonSerializer<QueryResult> {
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());
65 jsonObject.add("data", context.serialize(src.getData()));
70 private static class ResultRowGSONSerializer implements JsonSerializer<ResultRow> {
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)));
81 private static class QueryParametersGSONSerializer implements JsonSerializer<QueryParameters> {
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()));
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());
106 return JsonNull.INSTANCE;