]> git.basschouten.com Git - openhab-addons.git/blob
503dd12ffd8bf644ef143328c2f321fd93c9980a
[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.mqtt.homeassistant.internal.config;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Objects;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 import com.google.gson.TypeAdapter;
25 import com.google.gson.stream.JsonReader;
26 import com.google.gson.stream.JsonToken;
27 import com.google.gson.stream.JsonWriter;
28
29 /**
30  * JsonTypeAdapter which will read a single string or a string list
31  *
32  * see: https://www.home-assistant.io/components/binary_sensor.mqtt/ -> device / identifiers
33  *
34  * @author Jochen Klein - Initial contribution
35  */
36 @NonNullByDefault
37 public class ListOrStringDeserializer extends TypeAdapter<List<String>> {
38
39     @Override
40     public void write(@Nullable JsonWriter out, @Nullable List<String> value) throws IOException {
41         Objects.requireNonNull(out);
42
43         if (value == null) {
44             out.nullValue();
45             return;
46         }
47
48         out.beginArray();
49         for (String str : value) {
50             out.jsonValue(str);
51         }
52         out.endArray();
53     }
54
55     @Override
56     public @Nullable List<String> read(@Nullable JsonReader in) throws IOException {
57         Objects.requireNonNull(in);
58
59         JsonToken peek = in.peek();
60
61         switch (peek) {
62             case NULL:
63                 in.nextNull();
64                 return null;
65             case STRING:
66                 return Collections.singletonList(in.nextString());
67             case BEGIN_ARRAY:
68                 return readList(in);
69             default:
70                 throw new IOException("unexpected token " + peek + ". Array of string or string expected");
71         }
72     }
73
74     private List<String> readList(JsonReader in) throws IOException {
75         in.beginArray();
76
77         List<String> result = new ArrayList<>();
78
79         JsonToken peek = in.peek();
80
81         while (peek != JsonToken.END_ARRAY) {
82             if (peek == JsonToken.STRING) {
83                 result.add(in.nextString());
84             } else {
85                 throw new IOException("unexpected token " + peek + ". Array of string or string expected");
86             }
87             peek = in.peek();
88         }
89         in.endArray();
90
91         return result;
92     }
93 }