0

первые намётки сериализации Text

This commit is contained in:
2020-05-12 12:20:22 +03:00
parent 95930c3aa8
commit 6785f59de9
5 changed files with 58 additions and 6 deletions

View File

@@ -0,0 +1,6 @@
package mc.protocol.converter;
public interface Converter<S, T> {
T convert(S source);
}

View File

@@ -0,0 +1,48 @@
package mc.protocol.converter;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mc.protocol.text.Text;
import mc.protocol.text.TextStyle;
import mc.protocol.utils.JsonUtils;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class TextToJsonConverter implements Converter<Text, String> {
private static final TextToJsonConverter instance = new TextToJsonConverter();
@Override
public String convert(Text text) {
return JsonUtils.objectToJson(createObjectNode(text));
}
private ObjectNode createObjectNode(Text text) {
final ObjectNode objectNode = JsonUtils.getObjectMapper().createObjectNode();
objectNode.put("text", text.getContent());
if (text.getColor() != null) {
objectNode.put("color", text.getColor().getName());
}
if (text.getStyle() != null) {
final TextStyle style = text.getStyle();
style.getBold().ifPresent(value -> objectNode.put("bold", value));
style.getItalic().ifPresent(value -> objectNode.put("italic", value));
style.getObfuscated().ifPresent(value -> objectNode.put("obfuscated", value));
style.getStrikethrough().ifPresent(value -> objectNode.put("strikethrough", value));
style.getUnderline().ifPresent(value -> objectNode.put("underlined", value));
}
if (text.getChildren() != null) {
final ArrayNode arrayNode = JsonUtils.getObjectMapper().createArrayNode();
text.getChildren().forEach(childText -> arrayNode.add(createObjectNode(childText)));
objectNode.set("extra", arrayNode);
}
return objectNode;
}
}

View File

@@ -2,9 +2,8 @@ package mc.protocol.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import mc.protocol.text.Text;
import java.util.List;
import java.util.UUID;
@@ -18,8 +17,8 @@ public class ServerInfo {
@JsonProperty("players")
private PlayersInfo playersInfo;
//TODO необходимо реализовать объект типа Chat (см. https://wiki.vg/index.php?title=Chat&oldid=8329)
private JsonNode description;
//FIXME так не получится. Нужен кастомный Jackson сериализатор
private Text description;
@JsonProperty("favicon")
private String faviconBase64;

View File

@@ -1,7 +1,6 @@
package mc.protocol.text;
import com.google.common.collect.ImmutableList;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.*;

View File

@@ -38,7 +38,7 @@ public class JsonUtils {
return result;
}
private ObjectMapper getObjectMapper() {
public ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}