Related: 4d73187376
This commit is contained in:
yushijinhun 2018-12-02 14:54:58 +08:00
parent db59dcea65
commit 4d7af2e68b
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
2 changed files with 17 additions and 13 deletions

View File

@ -30,7 +30,7 @@ import java.util.Map;
*
* @author huangyuhui
*/
@JsonAdapter(Argument.Serializer.class)
@JsonAdapter(Argument.Deserializer.class)
@Immutable
public interface Argument extends Cloneable {
@ -43,7 +43,7 @@ public interface Argument extends Cloneable {
*/
List<String> toString(Map<String, String> keys, Map<String, Boolean> features);
class Serializer implements JsonDeserializer<Argument>, JsonSerializer<Argument> {
class Deserializer implements JsonDeserializer<Argument> {
@Override
public Argument deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonPrimitive())
@ -51,16 +51,5 @@ public interface Argument extends Cloneable {
else
return context.deserialize(json, RuledArgument.class);
}
@Override
public JsonElement serialize(Argument src, Type typeOfSrc, JsonSerializationContext context) {
if (src instanceof StringArgument)
return new JsonPrimitive(((StringArgument) src).getArgument());
else if (src instanceof RuledArgument)
return context.serialize(src, RuledArgument.class);
else
throw new AssertionError("Unrecognized argument type: " + src);
}
}
}

View File

@ -19,6 +19,13 @@ package org.jackhuang.hmcl.game;
import org.jackhuang.hmcl.util.Immutable;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.JsonAdapter;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -29,6 +36,7 @@ import java.util.regex.Pattern;
*
* @author huangyuhui
*/
@JsonAdapter(StringArgument.Serializer.class)
@Immutable
public final class StringArgument implements Argument {
@ -58,4 +66,11 @@ public final class StringArgument implements Argument {
public String toString() {
return argument;
}
public class Serializer implements JsonSerializer<StringArgument> {
@Override
public JsonElement serialize(StringArgument src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getArgument());
}
}
}