]> git.basschouten.com Git - openhab-addons.git/blob
160eea35e76ac71d2cff37284aed0334c4e3818e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 /*
14  * Pulled from Stack Overflow answer located here: http://stackoverflow.com/a/11272452
15  * and placed in an appropriate package within this library.
16  */
17 package org.openhab.binding.lametrictime.internal.api.common.impl.typeadapters.imported;
18
19 import java.io.IOException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonElement;
26 import com.google.gson.TypeAdapter;
27 import com.google.gson.TypeAdapterFactory;
28 import com.google.gson.reflect.TypeToken;
29 import com.google.gson.stream.JsonReader;
30 import com.google.gson.stream.JsonWriter;
31
32 /**
33  * Abstract type adapter factory.
34  *
35  * @author Gregory Moyer - Initial contribution
36  */
37 @NonNullByDefault
38 public abstract class CustomizedTypeAdapterFactory<C> implements TypeAdapterFactory {
39     private final Class<C> customizedClass;
40
41     public CustomizedTypeAdapterFactory(Class<C> customizedClass) {
42         this.customizedClass = customizedClass;
43     }
44
45     @Override
46     @SuppressWarnings("unchecked") // we use a runtime check to guarantee that 'C' and 'T' are equal
47     @Nullable
48     public final <T> TypeAdapter<T> create(@Nullable Gson gson, @Nullable TypeToken<T> type) {
49         return type.getRawType() == customizedClass
50                 ? (TypeAdapter<T>) customizeMyClassAdapter(gson, (TypeToken<C>) type)
51                 : null;
52     }
53
54     private TypeAdapter<C> customizeMyClassAdapter(@Nullable Gson gson, TypeToken<C> type) {
55         final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type);
56         final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
57         return new TypeAdapter<C>() {
58             @Override
59             public void write(JsonWriter out, @Nullable C value) throws IOException {
60                 JsonElement tree = delegate.toJsonTree(value);
61                 beforeWrite(value, tree);
62                 elementAdapter.write(out, tree);
63             }
64
65             @Override
66             public @Nullable C read(JsonReader in) throws IOException {
67                 JsonElement tree = elementAdapter.read(in);
68                 afterRead(tree);
69                 if (tree == null) {
70                     throw new IOException("null reader");
71                 }
72                 return delegate.fromJsonTree(tree);
73             }
74         };
75     }
76
77     /**
78      * Override this to muck with {@code toSerialize} before it is written to
79      * the outgoing JSON stream.
80      */
81     protected void beforeWrite(C source, JsonElement toSerialize) {
82     }
83
84     /**
85      * Override this to muck with {@code deserialized} before it parsed into the
86      * application type.
87      */
88     protected void afterRead(@Nullable JsonElement deserialized) {
89     }
90 }