]> git.basschouten.com Git - openhab-addons.git/blob
4b600715256ea497d153961efd43712bec7891a9
[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.surepetcare.internal.utils;
14
15 import java.io.IOException;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.surepetcare.internal.dto.SurePetcareDeviceCurfew;
21 import org.openhab.binding.surepetcare.internal.dto.SurePetcareDeviceCurfewList;
22
23 import com.google.gson.Gson;
24 import com.google.gson.TypeAdapter;
25 import com.google.gson.TypeAdapterFactory;
26 import com.google.gson.reflect.TypeToken;
27 import com.google.gson.stream.JsonReader;
28 import com.google.gson.stream.JsonToken;
29 import com.google.gson.stream.JsonWriter;
30 import com.google.gson.stream.MalformedJsonException;
31
32 /**
33  * The {@link GsonColonDateTypeAdapter} class is a custom TypeAdapter factory to ensure deserialization always returns a
34  * list even if the Json document contains only a single curfew object and not an array.
35  *
36  * See https://stackoverflow.com/questions/43412261/make-gson-accept-single-objects-where-it-expects-arrays
37  *
38  * @author Rene Scherer - Initial contribution
39  */
40 @NonNullByDefault
41 public final class SurePetcareDeviceCurfewListTypeAdapterFactory<E> implements TypeAdapterFactory {
42
43     // Gson can instantiate it itself
44     private SurePetcareDeviceCurfewListTypeAdapterFactory() {
45     }
46
47     @Override
48     public @Nullable <T> TypeAdapter<T> create(final @Nullable Gson gson, final @Nullable TypeToken<T> typeToken) {
49         if (gson == null || typeToken == null) {
50             return null;
51         }
52         final TypeAdapter<SurePetcareDeviceCurfew> elementTypeAdapter = gson
53                 .getAdapter(TypeToken.get(SurePetcareDeviceCurfew.class));
54         @SuppressWarnings("unchecked")
55         final TypeAdapter<T> alwaysListTypeAdapter = (TypeAdapter<T>) new SurePetcareDeviceCurfewListTypeAdapter(
56                 elementTypeAdapter).nullSafe();
57         return alwaysListTypeAdapter;
58     }
59
60     private static final class SurePetcareDeviceCurfewListTypeAdapter
61             extends TypeAdapter<List<SurePetcareDeviceCurfew>> {
62
63         private final TypeAdapter<SurePetcareDeviceCurfew> elementTypeAdapter;
64
65         private SurePetcareDeviceCurfewListTypeAdapter(final TypeAdapter<SurePetcareDeviceCurfew> elementTypeAdapter) {
66             this.elementTypeAdapter = elementTypeAdapter;
67         }
68
69         @Override
70         public void write(final @Nullable JsonWriter out, @Nullable final List<SurePetcareDeviceCurfew> list)
71                 throws IOException {
72             if (out != null && list != null) {
73                 out.beginArray();
74                 for (SurePetcareDeviceCurfew curfew : list) {
75                     elementTypeAdapter.write(out, curfew);
76                 }
77                 out.endArray();
78             }
79         }
80
81         @Override
82         public @Nullable List<SurePetcareDeviceCurfew> read(final @Nullable JsonReader in) throws IOException {
83             // This is where we detect the list "type"
84             final SurePetcareDeviceCurfewList list = new SurePetcareDeviceCurfewList();
85             if (in != null) {
86                 final JsonToken token = in.peek();
87                 switch (token) {
88                     case BEGIN_ARRAY:
89                         // If it's a regular list, just consume [, <all elements>, and ]
90                         in.beginArray();
91                         while (in.hasNext()) {
92                             SurePetcareDeviceCurfew cf = elementTypeAdapter.read(in);
93                             if (cf != null) {
94                                 list.add(cf);
95                             }
96                         }
97                         in.endArray();
98                         break;
99                     case BEGIN_OBJECT:
100                         SurePetcareDeviceCurfew cf = elementTypeAdapter.read(in);
101                         if (cf != null) {
102                             list.add(cf);
103                         }
104                         break;
105                     case NULL:
106                         throw new AssertionError(
107                                 "Must never happen: check if the type adapter configured with .nullSafe()");
108                     case STRING:
109                     case NUMBER:
110                     case BOOLEAN:
111                     case NAME:
112                     case END_ARRAY:
113                     case END_OBJECT:
114                     case END_DOCUMENT:
115                         throw new MalformedJsonException("Unexpected token: " + token);
116                     default:
117                         throw new AssertionError("Must never happen: " + token);
118                 }
119             }
120             return list;
121         }
122     }
123 }