add Text serializer
This commit is contained in:
@@ -7,6 +7,7 @@ import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.protocol.text.Text;
|
||||
import mc.protocol.utils.json.serializer.TextDeserializer;
|
||||
import mc.protocol.utils.json.serializer.TextSerializer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -45,6 +46,7 @@ public class JsonUtils {
|
||||
if (objectMapper == null) {
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addDeserializer(Text.class, new TextDeserializer());
|
||||
module.addSerializer(Text.class, new TextSerializer());
|
||||
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.registerModule(module);
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package mc.protocol.utils.json.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
import mc.protocol.text.Text;
|
||||
import mc.protocol.text.TextStyle;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TextSerializer extends StdSerializer<Text> {
|
||||
|
||||
public TextSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public TextSerializer(Class<Text> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Text text, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
gen.writeStartObject();
|
||||
|
||||
gen.writeStringField("text", text.getContent());
|
||||
|
||||
if (text.getColor() != null) {
|
||||
gen.writeStringField("colot", text.getColor().getName());
|
||||
}
|
||||
|
||||
if (text.getStyle() != null) {
|
||||
final TextStyle style = text.getStyle();
|
||||
|
||||
if (style.getBold().isPresent()) {
|
||||
gen.writeBooleanField("bold", style.getBold().get());
|
||||
}
|
||||
|
||||
if (style.getItalic().isPresent()) {
|
||||
gen.writeBooleanField("italic", style.getItalic().get());
|
||||
}
|
||||
|
||||
if (style.getObfuscated().isPresent()) {
|
||||
gen.writeBooleanField("obfuscated", style.getObfuscated().get());
|
||||
}
|
||||
|
||||
if (style.getStrikethrough().isPresent()) {
|
||||
gen.writeBooleanField("strikethrough", style.getStrikethrough().get());
|
||||
}
|
||||
|
||||
if (style.getUnderline().isPresent()) {
|
||||
gen.writeBooleanField("underlined", style.getUnderline().get());
|
||||
}
|
||||
}
|
||||
|
||||
if (text.getChildren() != null) {
|
||||
gen.writeArrayFieldStart("extra");
|
||||
for (Text textChild : text.getChildren()) {
|
||||
serialize(textChild, gen, provider);
|
||||
}
|
||||
gen.writeEndArray();
|
||||
}
|
||||
|
||||
gen.writeEndObject();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user