]> git.basschouten.com Git - openhab-addons.git/blob
0ca320d3a3855191d47389209f338c1cb224459a
[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.robonect.internal.model;
14
15 import com.google.gson.Gson;
16 import com.google.gson.GsonBuilder;
17
18 /**
19  * This class is responsible for parsing JSNON formatted answers from the Robonect module using the Gson library.
20  * 
21  * @author Marco Meyer - Initial contribution
22  */
23 public class ModelParser {
24
25     private final Gson gson;
26
27     /**
28      * Creates a parser with containing a preconfigured Gson object capable of parsing the JSON answers from the
29      * Robonect module.
30      */
31     public ModelParser() {
32         GsonBuilder gsonBuilder = new GsonBuilder();
33         gsonBuilder.registerTypeAdapter(MowerStatus.class, new MowerStatusDeserializer());
34         gsonBuilder.registerTypeAdapter(MowerMode.class, new MowerModeDeserializer());
35         gsonBuilder.registerTypeAdapter(Timer.TimerMode.class, new TimerModeDeserializer());
36         this.gson = gsonBuilder.create();
37     }
38
39     /**
40      * Parses a jsonString to a Java Object of the specified type.
41      * 
42      * @param jsonString - the json string to parse
43      * @param type - the class of the type of the expected object to be returned.
44      * @param <T> - the type of expected return value.
45      * @return
46      */
47     public <T> T parse(String jsonString, Class<T> type) {
48         return gson.fromJson(jsonString, type);
49     }
50 }