]> git.basschouten.com Git - openhab-addons.git/blob
69e30980932a0d8b81daa66643f5bd64d7e37518
[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.bsblan.internal.api;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiContentDTO;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
23
24 /**
25  * Utility class to create JSON content.
26  *
27  * @author Peter Schraffl - Initial contribution
28  */
29 @NonNullByDefault
30 public class BsbLanApiContentConverter {
31     private static final Logger LOGGER = LoggerFactory.getLogger(BsbLanApiContentConverter.class);
32     private static final Gson GSON = new Gson();
33
34     public static String toJson(BsbLanApiContentDTO request) {
35         return GSON.toJson(request);
36     }
37
38     public static <T> @Nullable T fromJson(String content, Class<T> resultType) {
39         try {
40             T result = GSON.fromJson(content, resultType);
41             if (result == null) {
42                 LOGGER.debug("result null after json parsing (response = {})", content);
43             }
44             return result;
45         } catch (JsonSyntaxException e) {
46             LOGGER.debug("Parsing JSON API response failed: {}", e.getMessage());
47             return null;
48         }
49     }
50 }