2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.config;
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;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
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;
30 * JsonTypeAdapter which will read a single string or a string list
32 * see: https://www.home-assistant.io/components/binary_sensor.mqtt/ -> device / identifiers
34 * @author Jochen Klein - Initial contribution
37 public class ListOrStringDeserializer extends TypeAdapter<List<String>> {
40 public void write(@Nullable JsonWriter out, @Nullable List<String> value) throws IOException {
41 Objects.requireNonNull(out);
49 for (String str : value) {
56 public @Nullable List<String> read(@Nullable JsonReader in) throws IOException {
57 Objects.requireNonNull(in);
59 JsonToken peek = in.peek();
66 return Collections.singletonList(in.nextString());
70 throw new IOException("unexpected token " + peek + ". Array of string or string expected");
74 private List<String> readList(JsonReader in) throws IOException {
77 List<String> result = new ArrayList<>();
79 JsonToken peek = in.peek();
81 while (peek != JsonToken.END_ARRAY) {
82 if (peek == JsonToken.STRING) {
83 result.add(in.nextString());
85 throw new IOException("unexpected token " + peek + ". Array of string or string expected");