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.robonect.internal.model;
15 import com.google.gson.Gson;
16 import com.google.gson.GsonBuilder;
19 * This class is responsible for parsing JSNON formatted answers from the Robonect module using the Gson library.
21 * @author Marco Meyer - Initial contribution
23 public class ModelParser {
25 private final Gson gson;
28 * Creates a parser with containing a preconfigured Gson object capable of parsing the JSON answers from the
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();
40 * Parses a jsonString to a Java Object of the specified type.
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.
47 public <T> T parse(String jsonString, Class<T> type) {
48 return gson.fromJson(jsonString, type);