]> git.basschouten.com Git - openhab-addons.git/blob
b363ab82fdfa69081685d45966137db2c1d7b592
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.lametrictime.api.common.impl;
17
18 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.ActionTypeAdapterFactory;
19 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.ApplicationTypeAdapterFactory;
20 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.UpdateActionTypeAdapterFactory;
21 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.imported.JSR310TypeAdapters;
22 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.imported.RuntimeTypeAdapterFactory;
23 import org.openhab.binding.lametrictime.api.local.model.BooleanParameter;
24 import org.openhab.binding.lametrictime.api.local.model.IntegerParameter;
25 import org.openhab.binding.lametrictime.api.local.model.Parameter;
26 import org.openhab.binding.lametrictime.api.local.model.StringParameter;
27
28 import com.google.gson.FieldNamingPolicy;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31
32 public class GsonGenerator
33 {
34     public static Gson create()
35     {
36         return create(false);
37     }
38
39     public static Gson create(boolean prettyPrint)
40     {
41         GsonBuilder builder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
42                                                .registerTypeAdapterFactory(new ApplicationTypeAdapterFactory())
43                                                .registerTypeAdapterFactory(new ActionTypeAdapterFactory())
44                                                .registerTypeAdapterFactory(new UpdateActionTypeAdapterFactory())
45                                                .registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(Parameter.class,
46                                                                                                         "data_type")
47                                                                                                     .registerSubtype(BooleanParameter.class,
48                                                                                                                      "bool")
49                                                                                                     .registerSubtype(StringParameter.class,
50                                                                                                                      "string")
51                                                                                                     .registerSubtype(IntegerParameter.class,
52                                                                                                                      "int"));
53
54         // add Java 8 Time API support
55         JSR310TypeAdapters.registerJSR310TypeAdapters(builder);
56
57         if (prettyPrint)
58         {
59             builder.setPrettyPrinting();
60         }
61
62         return builder.create();
63     }
64
65     // @formatter:off
66     private GsonGenerator() {}
67     // @formatter:on
68 }