Fix JsonParseException

This commit is contained in:
huanghongxun
2018-09-26 13:13:21 +08:00
parent 01ff1327f7
commit 518969d2f2
7 changed files with 19 additions and 17 deletions

View File

@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.download;
import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.download.game.LibraryDownloadTask;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.LibraryDownloadInfo;
@@ -62,7 +63,7 @@ public class DefaultCacheRepository extends CacheRepository {
index = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile.toFile()), Index.class);
else
index = new Index();
} catch (IOException e) {
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Unable to read index file", e);
index = new Index();
} finally {

View File

@@ -89,9 +89,7 @@ public final class ForgeInstallTask extends TaskResult<Version> {
if (stream == null)
throw new IOException("Malformed forge installer file, install_profile.json does not exist.");
String json = IOUtils.readFullyAsString(stream);
ForgeInstallProfile installProfile = JsonUtils.GSON.fromJson(json, ForgeInstallProfile.class);
if (installProfile == null)
throw new IOException("Malformed forge installer file, install_profile.json does not exist.");
ForgeInstallProfile installProfile = JsonUtils.fromNonNullJson(json, ForgeInstallProfile.class);
// unpack the universal jar in the installer file.
Library forgeLibrary = Library.fromName(installProfile.getInstall().getPath());

View File

@@ -18,7 +18,6 @@
package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import org.jackhuang.hmcl.event.*;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.Logging;
@@ -108,11 +107,11 @@ public class DefaultGameRepository implements GameRepository {
return new File(getVersionRoot(id), id + ".json");
}
public Version readVersionJson(String id) throws IOException, JsonSyntaxException {
public Version readVersionJson(String id) throws IOException, JsonParseException {
return readVersionJson(getVersionJson(id));
}
public Version readVersionJson(File file) throws IOException, JsonSyntaxException {
public Version readVersionJson(File file) throws IOException, JsonParseException {
return JsonUtils.GSON.fromJson(FileUtils.readText(file), Version.class);
}

View File

@@ -112,7 +112,7 @@ public class Datapack {
try {
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(path, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this));
} catch (IOException e) {
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e);
}
} else {
@@ -147,7 +147,7 @@ public class Datapack {
PackMcMeta pack = enabled ? JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class)
: JsonUtils.fromNonNullJson(FileUtils.readText(mcmetaDisabled), PackMcMeta.class);
info.add(new Pack(enabled ? mcmeta : mcmetaDisabled, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this));
} catch (IOException e) {
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
}
} else if (Files.isRegularFile(subDir)) {
@@ -169,7 +169,7 @@ public class Datapack {
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(subDir, name, pack.getPackInfo().getDescription(), this));
} catch (IOException e) {
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
}
}

View File

@@ -63,8 +63,8 @@ public final class MultiMCManifest {
String json = IOUtils.readFullyAsString(Files.newInputStream(mmcPack));
MultiMCManifest manifest = JsonUtils.fromNonNullJson(json, MultiMCManifest.class);
if (manifest != null && manifest.getComponents() == null)
throw new IOException("mmc-pack.json malformed.");
if (manifest.getComponents() == null)
throw new IOException("mmc-pack.json malformed.");
return manifest;
}

View File

@@ -125,6 +125,7 @@ public final class MultiMCModpackInstallTask extends Task {
if (Files.exists(patches))
for (Path patchJson : Files.newDirectoryStream(patches)) {
if (patchJson.toString().endsWith(".json")) {
// If json is malformed, we should stop installing this modpack instead of skipping it.
MultiMCInstancePatch patch = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(patchJson)), MultiMCInstancePatch.class);
List<String> newArguments = new LinkedList<>();
for (String arg : patch.getTweakers()) {