add Serializable type to unserialized JSON lists

We don't know about the exact data type from the JSON mapper,
so we make it explicitly unknown, but at least Serializable here.
This commit is contained in:
Stefan Kalscheuer 2020-08-12 11:40:58 +02:00
parent 9b80a4e889
commit 8017f2671d
10 changed files with 25 additions and 22 deletions

View File

@ -11,7 +11,7 @@ steps:
---
kind: pipeline
type: docker
name: java13
name: java14
steps:
- name: test

View File

@ -248,7 +248,7 @@ public class UraClient implements Serializable {
String version = null;
String line = br.readLine();
while (line != null && (limit == null || trips.size() < limit)) {
List l = mapper.readValue(line, List.class);
List<Serializable> l = mapper.readValue(line, mapper.getTypeFactory().constructCollectionType(List.class, Serializable.class));
/* Check if result exists and has correct response type */
if (l != null && !l.isEmpty()) {
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {
@ -323,7 +323,7 @@ public class UraClient implements Serializable {
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
while ((line = br.readLine()) != null) {
List l = mapper.readValue(line, List.class);
List<Serializable> l = mapper.readValue(line, mapper.getTypeFactory().constructCollectionType(List.class, Serializable.class));
/* Check if result exists and has correct response type */
if (l != null && !l.isEmpty() && l.get(0).equals(RES_TYPE_STOP)) {
stops.add(new Stop(l));
@ -373,7 +373,7 @@ public class UraClient implements Serializable {
String version = null;
String line = br.readLine();
while (line != null && (limit == null || messages.size() < limit)) {
List l = mapper.readValue(line, List.class);
List<Serializable> l = mapper.readValue(line, mapper.getTypeFactory().constructCollectionType(List.class, Serializable.class));
/* Check if result exists and has correct response type */
if (l != null && !l.isEmpty()) {
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {

View File

@ -1,6 +1,7 @@
package de.stklcode.pubtrans.ura.model;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
/**
@ -87,7 +88,7 @@ public class Message implements Model {
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Message(final List raw) throws IOException {
public Message(final List<Serializable> raw) throws IOException {
this(raw, null);
}
@ -98,7 +99,7 @@ public class Message implements Model {
* @param version API version
* @throws IOException Thrown on invalid line format.
*/
public Message(final List raw, final String version) throws IOException {
public Message(final List<Serializable> raw, final String version) throws IOException {
if (raw == null || raw.size() < NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields");
}

View File

@ -33,7 +33,7 @@ interface Model extends Serializable {
* @param actual Actual class.
* @return The Exception.
*/
static IOException typeErrorString(int field, Class actual) {
static IOException typeErrorString(int field, Class<?> actual) {
return typeError(field, actual, "String");
}
@ -45,7 +45,7 @@ interface Model extends Serializable {
* @param expected Expected type.
* @return The Exception.
*/
static IOException typeError(int field, Class actual, String expected) {
static IOException typeError(int field, Class<?> actual, String expected) {
return new IOException(String.format("Field %d not of expected type %s, found %s",
field, expected, actual.getSimpleName()));
}

View File

@ -17,6 +17,7 @@
package de.stklcode.pubtrans.ura.model;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
/**
@ -72,7 +73,7 @@ public final class Stop implements Model {
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Stop(final List raw) throws IOException {
public Stop(final List<Serializable> raw) throws IOException {
if (raw == null || raw.size() < F_NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields");
}

View File

@ -17,6 +17,7 @@
package de.stklcode.pubtrans.ura.model;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
/**
@ -142,7 +143,7 @@ public final class Trip implements Model {
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Trip(final List raw) throws IOException {
public Trip(final List<Serializable> raw) throws IOException {
this(raw, null);
}
@ -153,7 +154,7 @@ public final class Trip implements Model {
* @param version API version
* @throws IOException Thrown on invalid line format.
*/
public Trip(final List raw, final String version) throws IOException {
public Trip(final List<Serializable> raw, final String version) throws IOException {
if (raw == null || raw.size() < NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields");
}

View File

@ -19,10 +19,7 @@ package de.stklcode.pubtrans.ura.reader;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.stklcode.pubtrans.ura.model.Trip;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.http.HttpClient;
@ -90,7 +87,7 @@ public class AsyncUraTripReader implements AutoCloseable {
String version = null;
String line = br.readLine();
while (line != null && !this.canceled) {
List l = mapper.readValue(line, List.class);
List<Serializable> l = mapper.readValue(line, mapper.getTypeFactory().constructCollectionType(List.class, Serializable.class));
// Check if result exists and has correct response type.
if (l != null && !l.isEmpty()) {
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {

View File

@ -19,6 +19,7 @@ package de.stklcode.pubtrans.ura.model;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -61,7 +62,7 @@ public class MessageTest {
@Test
public void listConstructorTest() {
/* Create valid raw data list */
List<Object> raw = new ArrayList<>();
List<Serializable> raw = new ArrayList<>();
raw.add(1);
raw.add("stopName");
raw.add("stopId");
@ -101,7 +102,7 @@ public class MessageTest {
}
/* Test exceptions on invalid data */
List<Object> invalid = new ArrayList<>(raw);
List<Serializable> invalid = new ArrayList<>(raw);
invalid.remove(7);
invalid.add(7, 123L);
try {

View File

@ -19,6 +19,7 @@ package de.stklcode.pubtrans.ura.model;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -48,7 +49,7 @@ public class StopTest {
@Test
public void listConstructorTest() {
/* Create valid raw data list */
List<Object> raw = new ArrayList<>();
List<Serializable> raw = new ArrayList<>();
raw.add(1);
raw.add("stopName");
raw.add("stopId");
@ -80,7 +81,7 @@ public class StopTest {
}
/* Test exceptions on invalid data */
List<Object> invalid = new ArrayList<>(raw);
List<Serializable> invalid = new ArrayList<>(raw);
invalid.remove(1);
invalid.add(1, 5);
try {

View File

@ -19,6 +19,7 @@ package de.stklcode.pubtrans.ura.model;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -71,7 +72,7 @@ public class TripTest {
@Test
public void listConstructorTest() {
/* Create valid raw data list */
List<Object> raw = new ArrayList<>();
List<Serializable> raw = new ArrayList<>();
raw.add(1);
raw.add("stopName");
raw.add("stopId");
@ -151,7 +152,7 @@ public class TripTest {
}
/* Test exceptions on invalid data */
List<Object> invalid = new ArrayList<>(raw);
List<Serializable> invalid = new ArrayList<>(raw);
invalid.remove(7);
invalid.add(7, "123");
try {