Make model import more robust
This commit is contained in:
parent
a111d12969
commit
0772b8e057
|
@ -1,6 +1,7 @@
|
|||
package ctbrec.ui.tabs.recorded;
|
||||
|
||||
import com.squareup.moshi.*;
|
||||
import com.squareup.moshi.JsonReader.Token;
|
||||
import ctbrec.Config;
|
||||
import ctbrec.Model;
|
||||
import ctbrec.ModelGroup;
|
||||
|
@ -21,6 +22,8 @@ import java.nio.file.Files;
|
|||
import java.time.LocalTime;
|
||||
import java.util.*;
|
||||
|
||||
import static com.squareup.moshi.JsonReader.Token.*;
|
||||
|
||||
public class ModelImportExport {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ModelImportExport.class);
|
||||
|
@ -165,14 +168,12 @@ public class ModelImportExport {
|
|||
reader.beginArray();
|
||||
while (reader.hasNext()) {
|
||||
try {
|
||||
JsonReader.Token token = reader.peek();
|
||||
if (token == JsonReader.Token.BEGIN_OBJECT) {
|
||||
Token token = reader.peek();
|
||||
if (token == BEGIN_OBJECT) {
|
||||
Model model = modelAdapter.fromJson(reader);
|
||||
result.add(model);
|
||||
} else if (token == JsonReader.Token.NAME) {
|
||||
reader.skipName();
|
||||
} else {
|
||||
reader.skipValue();
|
||||
skipToNextModel(reader);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't parse model json", e);
|
||||
|
@ -181,4 +182,30 @@ public class ModelImportExport {
|
|||
reader.endArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void skipToNextModel(JsonReader reader) throws IOException {
|
||||
while (true) {
|
||||
Token token = reader.peek();
|
||||
if (token == BEGIN_OBJECT) {
|
||||
reader.beginObject();
|
||||
} else if (token == END_OBJECT) {
|
||||
reader.endObject();
|
||||
if (reader.getPath().matches("\\$\\.models\\[\\d+]")) {
|
||||
break;
|
||||
}
|
||||
} else if (token == NAME) {
|
||||
reader.skipName();
|
||||
Token next = reader.peek();
|
||||
if (List.of(NULL, NUMBER, STRING, BOOLEAN).contains(next)) {
|
||||
reader.skipValue();
|
||||
}
|
||||
} else if (token == BEGIN_ARRAY) {
|
||||
reader.beginArray();
|
||||
} else if (token == END_ARRAY) {
|
||||
reader.endArray();
|
||||
} else if (List.of(NULL, NUMBER, STRING, BOOLEAN).contains(token)) {
|
||||
reader.skipValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue