Use @JsonAdapter

This commit is contained in:
yushijinhun
2018-11-23 21:49:50 +08:00
parent dcd0a42705
commit 4d73187376
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.event.RefreshingVersionsEvent;
import org.jackhuang.hmcl.setting.EnumGameDirectory; import org.jackhuang.hmcl.setting.EnumGameDirectory;
import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.setting.VersionSetting; import org.jackhuang.hmcl.setting.VersionSetting;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
@@ -227,8 +226,8 @@ public class HMCLGameRepository extends DefaultGameRepository {
beingModpackVersions.remove(id); beingModpackVersions.remove(id);
} }
private static final Gson GSON = new GsonBuilder().setPrettyPrinting() private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(VersionSetting.class, VersionSetting.Serializer.INSTANCE) .setPrettyPrinting()
.create(); .create();
private static final HashSet<String> FORBIDDEN = new HashSet<>(Arrays.asList("modpack", "minecraftinstance", "manifest")); 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; public static final int CURRENT_UI_VERSION = 0;
private static final Gson CONFIG_GSON = new GsonBuilder() 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(File.class, FileTypeAdapter.INSTANCE)
.registerTypeAdapter(ObservableList.class, new ObservableListCreator()) .registerTypeAdapter(ObservableList.class, new ObservableListCreator())
.registerTypeAdapter(ObservableSet.class, new ObservableSetCreator()) .registerTypeAdapter(ObservableSet.class, new ObservableSetCreator())
.registerTypeAdapter(ObservableMap.class, new ObservableMapCreator()) .registerTypeAdapter(ObservableMap.class, new ObservableMapCreator())
.registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true)) .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(EnumBackgroundImage.class, new EnumOrdinalDeserializer<>(EnumBackgroundImage.class)) // backward compatibility for backgroundType
.registerTypeAdapter(Proxy.Type.class, new EnumOrdinalDeserializer<>(Proxy.Type.class)) // backward compatibility for hasProxy .registerTypeAdapter(Proxy.Type.class, new EnumOrdinalDeserializer<>(Proxy.Type.class)) // backward compatibility for hasProxy
.setPrettyPrinting() .setPrettyPrinting()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -21,12 +21,6 @@ import java.io.File;
import java.util.Date; import java.util.Date;
import java.util.UUID; 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.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
@@ -39,13 +33,8 @@ public final class JsonUtils {
public static final Gson GSON = new GsonBuilder() public static final Gson GSON = new GsonBuilder()
.enableComplexMapKeySerialization() .enableComplexMapKeySerialization()
.setPrettyPrinting() .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(Date.class, DateTypeAdapter.INSTANCE)
.registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE) .registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE)
.registerTypeAdapter(Platform.class, Platform.Serializer.INSTANCE)
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE) .registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
.registerTypeAdapterFactory(ValidationTypeAdapterFactory.INSTANCE) .registerTypeAdapterFactory(ValidationTypeAdapterFactory.INSTANCE)
.registerTypeAdapterFactory(LowerCaseEnumTypeAdapterFactory.INSTANCE) .registerTypeAdapterFactory(LowerCaseEnumTypeAdapterFactory.INSTANCE)

View File

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