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

@@ -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)