|
|
|
|
@ -9,15 +9,19 @@ import com.google.gson.JsonElement; |
|
|
|
|
import com.google.gson.JsonParseException; |
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Type; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
public class DanmakuAdapter implements JsonDeserializer<List<Danmaku>> { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<Danmaku> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
|
|
|
|
if (!json.isJsonPrimitive()) return App.gson().fromJson(json, typeOfT); |
|
|
|
|
String text = json.getAsString().trim(); |
|
|
|
|
if (Json.isArray(text)) return App.gson().fromJson(text, typeOfT); |
|
|
|
|
else return List.of(Danmaku.from(text)); |
|
|
|
|
List<Danmaku> items = json.isJsonPrimitive() ? parsePrimitive(json.getAsString().trim(), typeOfT) : App.gson().fromJson(json, typeOfT); |
|
|
|
|
return items.stream().filter(d -> !d.isEmpty()).collect(Collectors.toCollection(ArrayList::new)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<Danmaku> parsePrimitive(String text, Type type) { |
|
|
|
|
return Json.isArray(text) ? App.gson().fromJson(text, type) : List.of(Danmaku.from(text)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|