add JsonUtils
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package mc.protocol.status.server;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.Data;
|
||||
import mc.protocol.Packet;
|
||||
import mc.protocol.dto.ServerInfo;
|
||||
import mc.protocol.io.NetInputStream;
|
||||
import mc.protocol.io.NetOutputStream;
|
||||
import mc.protocol.utils.JsonUtils;
|
||||
|
||||
/**
|
||||
* Status server packet, response.
|
||||
@@ -58,13 +57,8 @@ public class StatusServerResponse implements Packet {
|
||||
|
||||
public ServerInfo getServerInfoDto() {
|
||||
if (serverInfoDto == null) {
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
serverInfoDto = mapper.readValue(serverInfo, ServerInfo.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return new ServerInfo();
|
||||
}
|
||||
JsonUtils.jsonToObject(serverInfo, ServerInfo.class)
|
||||
.ifPresent(serverInfoDto -> this.serverInfoDto = serverInfoDto);
|
||||
}
|
||||
|
||||
return serverInfoDto;
|
||||
@@ -78,13 +72,7 @@ public class StatusServerResponse implements Packet {
|
||||
@Override
|
||||
public void writeSelf(NetOutputStream netOutputStream) {
|
||||
if (serverInfo == null) {
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
serverInfo = mapper.writeValueAsString(serverInfoDto);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
serverInfo = "{}";
|
||||
}
|
||||
serverInfo = JsonUtils.objectToJson(serverInfo);
|
||||
}
|
||||
|
||||
netOutputStream.writeString(serverInfo);
|
||||
|
||||
48
src/main/java/mc/protocol/utils/JsonUtils.java
Normal file
48
src/main/java/mc/protocol/utils/JsonUtils.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package mc.protocol.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@UtilityClass
|
||||
public class JsonUtils {
|
||||
|
||||
private final String EMPTY_OBJECT = "{}";
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public String objectToJson(Object object) {
|
||||
try {
|
||||
return getObjectMapper().writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Error serialize object {}", object, e);
|
||||
}
|
||||
return EMPTY_OBJECT;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> Optional<T> jsonToObject(String json, Class<T> returnType) {
|
||||
Optional<T> result;
|
||||
|
||||
try {
|
||||
result = Optional.of(getObjectMapper().readValue(json, returnType));
|
||||
} catch (JsonProcessingException e1) {
|
||||
e1.printStackTrace();
|
||||
result = Optional.empty();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private ObjectMapper getObjectMapper() {
|
||||
if (objectMapper == null) {
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
return objectMapper;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol;
|
||||
package mc.protocol.utils;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
Reference in New Issue
Block a user