Use @JsonAdapter

This commit is contained in:
yushijinhun 2018-11-23 21:49:50 +08:00
parent dcd0a42705
commit 4d73187376
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
11 changed files with 20 additions and 52 deletions

View File

@ -25,7 +25,6 @@ import org.jackhuang.hmcl.event.RefreshedVersionsEvent;
import org.jackhuang.hmcl.event.RefreshingVersionsEvent;
import org.jackhuang.hmcl.setting.EnumGameDirectory;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.setting.VersionSetting;
import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.io.FileUtils;
@ -227,8 +226,8 @@ public class HMCLGameRepository extends DefaultGameRepository {
beingModpackVersions.remove(id);
}
private static final Gson GSON = new GsonBuilder().setPrettyPrinting()
.registerTypeAdapter(VersionSetting.class, VersionSetting.Serializer.INSTANCE)
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.create();
private static final HashSet<String> FORBIDDEN = new HashSet<>(Arrays.asList("modpack", "minecraftinstance", "manifest"));

View File

@ -52,15 +52,11 @@ public final class Config implements Cloneable, Observable {
public static final int CURRENT_UI_VERSION = 0;
private static final Gson CONFIG_GSON = new GsonBuilder()
.registerTypeAdapter(VersionSetting.class, VersionSetting.Serializer.INSTANCE)
.registerTypeAdapter(Profile.class, Profile.Serializer.INSTANCE)
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
.registerTypeAdapter(ObservableList.class, new ObservableListCreator())
.registerTypeAdapter(ObservableSet.class, new ObservableSetCreator())
.registerTypeAdapter(ObservableMap.class, new ObservableMapCreator())
.registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true))
.registerTypeAdapter(Theme.class, new Theme.TypeAdapter())
.registerTypeAdapter(SupportedLocale.class, new SupportedLocale.TypeAdapter())
.registerTypeAdapter(EnumBackgroundImage.class, new EnumOrdinalDeserializer<>(EnumBackgroundImage.class)) // backward compatibility for backgroundType
.registerTypeAdapter(Proxy.Type.class, new EnumOrdinalDeserializer<>(Proxy.Type.class)) // backward compatibility for hasProxy
.setPrettyPrinting()

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.setting;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import com.jfoenix.concurrency.JFXUtilities;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
@ -47,6 +48,7 @@ import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating;
*
* @author huangyuhui
*/
@JsonAdapter(Profile.Serializer.class)
public final class Profile implements Observable {
private final WeakListenerHolder listenerHolder = new WeakListenerHolder();
private final HMCLGameRepository repository;
@ -217,11 +219,6 @@ public final class Profile implements Observable {
}
public static final class Serializer implements JsonSerializer<Profile>, JsonDeserializer<Profile> {
public static final Serializer INSTANCE = new Serializer();
private Serializer() {
}
@Override
public JsonElement serialize(Profile src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null)

View File

@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.setting;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import javafx.beans.binding.Bindings;
@ -34,6 +35,7 @@ import java.util.logging.Level;
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
@JsonAdapter(Theme.TypeAdapter.class)
public class Theme {
public static final Theme BLUE = new Theme("blue", "#5C6BC0");

View File

@ -18,6 +18,8 @@
package org.jackhuang.hmcl.setting;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import javafx.beans.InvalidationListener;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.game.LaunchOptions;
@ -43,6 +45,7 @@ import static org.jackhuang.hmcl.setting.ConfigHolder.config;
*
* @author huangyuhui
*/
@JsonAdapter(VersionSetting.Serializer.class)
public final class VersionSetting {
public transient String id;
@ -550,11 +553,6 @@ public final class VersionSetting {
}
public static class Serializer implements JsonSerializer<VersionSetting>, JsonDeserializer<VersionSetting> {
public static final Serializer INSTANCE = new Serializer();
private Serializer() {
}
@Override
public JsonElement serialize(VersionSetting src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) return JsonNull.INSTANCE;

View File

@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.util.i18n;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.jackhuang.hmcl.util.Lang;
@ -83,6 +84,7 @@ public final class Locales {
else throw new IllegalArgumentException("Unknown locale: " + locale);
}
@JsonAdapter(SupportedLocale.TypeAdapter.class)
public static class SupportedLocale {
private final Locale locale;
private final String name;

View File

@ -18,6 +18,8 @@
package org.jackhuang.hmcl.game;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import org.jackhuang.hmcl.util.Immutable;
import java.lang.reflect.Type;
@ -28,6 +30,7 @@ import java.util.Map;
*
* @author huangyuhui
*/
@JsonAdapter(Argument.Serializer.class)
@Immutable
public interface Argument extends Cloneable {
@ -41,12 +44,6 @@ public interface Argument extends Cloneable {
List<String> toString(Map<String, String> keys, Map<String, Boolean> features);
class Serializer implements JsonDeserializer<Argument>, JsonSerializer<Argument> {
public static final Serializer INSTANCE = new Serializer();
private Serializer() {
}
@Override
public Argument deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonPrimitive())

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.game;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.reflect.TypeToken;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.ToStringBuilder;
@ -35,6 +36,7 @@ import java.util.Optional;
*
* @author huangyuhui
*/
@JsonAdapter(Library.Serializer.class)
public class Library implements Comparable<Library> {
private final String groupId;
@ -189,12 +191,6 @@ public class Library implements Comparable<Library> {
}
public static class Serializer implements JsonDeserializer<Library>, JsonSerializer<Library> {
public static final Serializer INSTANCE = new Serializer();
private Serializer() {
}
@Override
public Library deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
if (json == null || json == JsonNull.INSTANCE)

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.game;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.reflect.TypeToken;
import org.jackhuang.hmcl.util.Immutable;
@ -32,6 +33,7 @@ import java.util.stream.Collectors;
*
* @author huangyuhui
*/
@JsonAdapter(RuledArgument.Serializer.class)
@Immutable
public class RuledArgument implements Argument {
@ -74,12 +76,6 @@ public class RuledArgument implements Argument {
}
public static class Serializer implements JsonSerializer<RuledArgument>, JsonDeserializer<RuledArgument> {
public static final Serializer INSTANCE = new Serializer();
private Serializer() {
}
@Override
public JsonElement serialize(RuledArgument src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();

View File

@ -21,12 +21,6 @@ import java.io.File;
import java.util.Date;
import java.util.UUID;
import org.jackhuang.hmcl.game.Argument;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.RuledArgument;
import org.jackhuang.hmcl.game.StringArgument;
import org.jackhuang.hmcl.util.platform.Platform;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
@ -39,13 +33,8 @@ public final class JsonUtils {
public static final Gson GSON = new GsonBuilder()
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.registerTypeAdapter(Library.class, Library.Serializer.INSTANCE)
.registerTypeAdapter(Argument.class, Argument.Serializer.INSTANCE)
.registerTypeAdapter(StringArgument.class, Argument.Serializer.INSTANCE)
.registerTypeAdapter(RuledArgument.class, RuledArgument.Serializer.INSTANCE)
.registerTypeAdapter(Date.class, DateTypeAdapter.INSTANCE)
.registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE)
.registerTypeAdapter(Platform.class, Platform.Serializer.INSTANCE)
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
.registerTypeAdapterFactory(ValidationTypeAdapterFactory.INSTANCE)
.registerTypeAdapterFactory(LowerCaseEnumTypeAdapterFactory.INSTANCE)

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.util.platform;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import java.lang.reflect.Type;
@ -27,6 +28,7 @@ import java.lang.reflect.Type;
*
* @author huangyuhui
*/
@JsonAdapter(Platform.Serializer.class)
public enum Platform {
BIT_32("32"),
BIT_64("64"),
@ -56,12 +58,6 @@ public enum Platform {
* The json serializer to {@link Platform}.
*/
public static class Serializer implements JsonSerializer<Platform>, JsonDeserializer<Platform> {
public static final Serializer INSTANCE = new Serializer();
private Serializer() {
}
@Override
public JsonElement serialize(Platform t, Type type, JsonSerializationContext jsc) {
if (t == null)