Remove Lang.requireJsonNonNull(), use JsonUtils.fromNonNullJson() instead

This commit is contained in:
yushijinhun 2018-02-18 15:28:48 +08:00
parent 95f404bb64
commit d081a6ee92
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
5 changed files with 22 additions and 23 deletions

View File

@ -20,8 +20,7 @@ package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.mod.Modpack;
import org.jackhuang.hmcl.util.CompressingUtils;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.JsonUtils;
import org.jackhuang.hmcl.util.StringUtils;
import java.io.File;
@ -82,9 +81,9 @@ public final class HMCLModpackManager {
*/
public static Modpack readHMCLModpackManifest(File file) throws IOException, JsonParseException {
String manifestJson = CompressingUtils.readTextZipEntry(file, "modpack.json");
Modpack manifest = Lang.requireJsonNonNull(Constants.GSON.fromJson(manifestJson, Modpack.class));
Modpack manifest = JsonUtils.fromNonNullJson(manifestJson, Modpack.class);
String gameJson = CompressingUtils.readTextZipEntry(file, "minecraft/pack.json");
Version game = Lang.requireJsonNonNull(Constants.GSON.fromJson(gameJson, Version.class));
Version game = JsonUtils.fromNonNullJson(gameJson, Version.class);
if (game.getJar() == null)
if (StringUtils.isBlank(manifest.getVersion()))
throw new JsonParseException("Cannot recognize the game version of modpack " + file + ".");

View File

@ -1,9 +1,8 @@
package org.jackhuang.hmcl.auth.yggdrasil;
import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.JsonUtils;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.IOException;
@ -36,7 +35,7 @@ public final class AuthlibInjectorBuildInfo {
}
public static AuthlibInjectorBuildInfo requestBuildInfo(String updateUrl) throws IOException, JsonParseException {
return Lang.requireJsonNonNull(Constants.GSON.fromJson(NetworkUtils.doGet(NetworkUtils.toURL(updateUrl)), AuthlibInjectorBuildInfo.class));
return JsonUtils.fromNonNullJson(NetworkUtils.doGet(NetworkUtils.toURL(updateUrl)), AuthlibInjectorBuildInfo.class);
}
public static final String UPDATE_URL = "https://authlib-injector.to2mbn.org/api/buildInfo";

View File

@ -20,15 +20,13 @@ package org.jackhuang.hmcl.mod;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.CompressingUtils;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.JsonUtils;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
*
@ -120,7 +118,7 @@ public final class CurseManifest {
*/
public static Modpack readCurseForgeModpackManifest(File f) throws IOException, JsonParseException {
String json = CompressingUtils.readTextZipEntry(f, "manifest.json");
CurseManifest manifest = Lang.requireJsonNonNull(Constants.GSON.fromJson(json, CurseManifest.class));
CurseManifest manifest = JsonUtils.fromNonNullJson(json, CurseManifest.class);
return new Modpack(manifest.getName(), manifest.getAuthor(), manifest.getVersion(), manifest.getMinecraft().getGameVersion(),
CompressingUtils.readTextZipEntryQuietly(f, "modlist.html").orElse( "No description"), manifest);
}

View File

@ -0,0 +1,15 @@
package org.jackhuang.hmcl.util;
import com.google.gson.JsonParseException;
public final class JsonUtils {
private JsonUtils() {}
public static <T> T fromNonNullJson(String json, Class<T> classOfT) throws JsonParseException {
T parsed = Constants.GSON.fromJson(json, classOfT);
if (parsed == null)
throw new JsonParseException("Json object cannot be null.");
return parsed;
}
}

View File

@ -5,8 +5,6 @@
*/
package org.jackhuang.hmcl.util;
import com.google.gson.JsonParseException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@ -22,16 +20,6 @@ public final class Lang {
private Lang() {
}
public static <T> T requireJsonNonNull(T obj) throws JsonParseException {
return requireJsonNonNull(obj, "Json object cannot be null.");
}
public static <T> T requireJsonNonNull(T obj, String message) throws JsonParseException {
if (obj == null)
throw new JsonParseException(message);
return obj;
}
@SafeVarargs
public static <K, V> Map<K, V> mapOf(Pair<K, V>... pairs) {
HashMap<K, V> map = new HashMap<>();