1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2024-11-23 12:05:44 +01:00

Add custom serializer for game arguments for more concise version JSON

This commit is contained in:
Henry Le Grys 2020-12-22 13:50:25 +00:00
parent c25e672869
commit 5f0b44acd3
2 changed files with 28 additions and 0 deletions

View File

@ -4,9 +4,11 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.skcraft.launcher.model.minecraft.mapper.ArgumentValueDeserializer;
import com.skcraft.launcher.model.minecraft.mapper.ArgumentValueSerializer;
import com.skcraft.launcher.util.Environment;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -19,6 +21,7 @@ import java.util.List;
public class GameArgument {
@JsonProperty("value")
@JsonDeserialize(using = ArgumentValueDeserializer.class)
@JsonSerialize(using = ArgumentValueSerializer.class)
private List<String> values;
private List<Rule> rules;

View File

@ -0,0 +1,25 @@
package com.skcraft.launcher.model.minecraft.mapper;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.io.IOException;
import java.util.List;
public class ArgumentValueSerializer extends StdSerializer<List<String>> {
protected ArgumentValueSerializer() {
super(TypeFactory.defaultInstance().constructCollectionType(List.class, String.class));
}
@Override
public void serialize(List<String> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
if (value.size() == 1) {
jgen.writeString(value.get(0));
} else {
provider.defaultSerializeValue(value, jgen);
}
}
}