]> git.basschouten.com Git - openhab-addons.git/blob
64e99adb24bb2ab1046845da1973776f1a57df59
[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.sleepiq.internal.api.impl;
14
15 import java.time.ZonedDateTime;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.sleepiq.internal.api.dto.SleepNumberRequest;
19 import org.openhab.binding.sleepiq.internal.api.dto.TimeSince;
20 import org.openhab.binding.sleepiq.internal.api.enums.Side;
21 import org.openhab.binding.sleepiq.internal.api.impl.typeadapters.SideTypeAdapter;
22 import org.openhab.binding.sleepiq.internal.api.impl.typeadapters.SleepNumberRequestAdapter;
23 import org.openhab.binding.sleepiq.internal.api.impl.typeadapters.TimeSinceTypeAdapter;
24 import org.openhab.binding.sleepiq.internal.api.impl.typeadapters.ZonedDateTimeTypeAdapter;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28
29 /**
30  * The {@link GsonGenerator} class creates the GSON generator object.
31  *
32  * @author Gregory Moyer - Initial contribution
33  */
34 @NonNullByDefault
35 public class GsonGenerator {
36     public static Gson create() {
37         return create(false);
38     }
39
40     public static Gson create(boolean prettyPrint) {
41         GsonBuilder builder = new GsonBuilder();
42         builder.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter());
43         builder.registerTypeAdapter(TimeSince.class, new TimeSinceTypeAdapter());
44         builder.registerTypeAdapter(SleepNumberRequest.class, new SleepNumberRequestAdapter());
45         builder.registerTypeAdapter(Side.class, new SideTypeAdapter());
46
47         if (prettyPrint) {
48             builder.setPrettyPrinting();
49         }
50         return builder.create();
51     }
52
53     private GsonGenerator() {
54     }
55 }