Move all json to Jackson instead of a mix of 2 libraries (#302)

* Swapped most GSON refrences to Jackson

* Converted FormWindow getJSONData

Co-authored-by: Redned <redned235@gmail.com>
This commit is contained in:
rtm516 2020-04-20 21:10:30 +01:00 committed by GitHub
parent 7417f57d47
commit 4ee95f585d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 107 additions and 82 deletions

View file

@ -25,8 +25,9 @@
package org.geysermc.common.window; package org.geysermc.common.window;
import com.google.gson.Gson; import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.reflect.TypeToken; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.geysermc.common.window.button.FormImage; import org.geysermc.common.window.button.FormImage;
@ -34,6 +35,7 @@ import org.geysermc.common.window.component.*;
import org.geysermc.common.window.response.CustomFormResponse; import org.geysermc.common.window.response.CustomFormResponse;
import org.geysermc.common.window.response.FormResponseData; import org.geysermc.common.window.response.FormResponseData;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -77,7 +79,11 @@ public class CustomFormWindow extends FormWindow {
} }
public String getJSONData() { public String getJSONData() {
String toModify = new Gson().toJson(this); String toModify = "";
try {
toModify = new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) { }
//We need to replace this due to Java not supporting declaring class field 'default' //We need to replace this due to Java not supporting declaring class field 'default'
return toModify.replace("defaultOptionIndex", "default") return toModify.replace("defaultOptionIndex", "default")
.replace("defaultText", "default") .replace("defaultText", "default")
@ -100,7 +106,11 @@ public class CustomFormWindow extends FormWindow {
Map<Integer, Object> responses = new HashMap<Integer, Object>(); Map<Integer, Object> responses = new HashMap<Integer, Object>();
Map<Integer, String> labelResponses = new HashMap<Integer, String>(); Map<Integer, String> labelResponses = new HashMap<Integer, String>();
List<String> componentResponses = new Gson().fromJson(data, new TypeToken<List<String>>() { }.getType()); List<String> componentResponses = new ArrayList<>();
try {
componentResponses = new ObjectMapper().readValue(data, new TypeReference<List<String>>(){});
} catch (IOException e) { }
for (String response : componentResponses) { for (String response : componentResponses) {
if (i >= content.size()) { if (i >= content.size()) {
break; break;

View file

@ -25,6 +25,7 @@
package org.geysermc.common.window; package org.geysermc.common.window;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.geysermc.common.window.response.FormResponse; import org.geysermc.common.window.response.FormResponse;
@ -50,6 +51,7 @@ public abstract class FormWindow {
this.response = response; this.response = response;
} }
@JsonIgnore
public abstract String getJSONData(); public abstract String getJSONData();
public abstract void setResponse(String response); public abstract void setResponse(String response);

View file

@ -25,7 +25,8 @@
package org.geysermc.common.window; package org.geysermc.common.window;
import com.google.gson.Gson; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.geysermc.common.window.response.ModalFormResponse; import org.geysermc.common.window.response.ModalFormResponse;
@ -59,7 +60,11 @@ public class ModalFormWindow extends FormWindow {
@Override @Override
public String getJSONData() { public String getJSONData() {
return new Gson().toJson(this); try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
return "";
}
} }
public void setResponse(String data) { public void setResponse(String data) {

View file

@ -25,7 +25,8 @@
package org.geysermc.common.window; package org.geysermc.common.window;
import com.google.gson.Gson; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.geysermc.common.window.button.FormButton; import org.geysermc.common.window.button.FormButton;
@ -63,7 +64,11 @@ public class SimpleFormWindow extends FormWindow {
@Override @Override
public String getJSONData() { public String getJSONData() {
return new Gson().toJson(this); try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
return "";
}
} }
public void setResponse(String data) { public void setResponse(String data) {

View file

@ -25,8 +25,10 @@
package org.geysermc.connector.metrics; package org.geysermc.connector.metrics;
import com.google.gson.JsonArray; import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -71,6 +73,8 @@ public class Metrics {
// A list with all custom charts // A list with all custom charts
private final List<CustomChart> charts = new ArrayList<>(); private final List<CustomChart> charts = new ArrayList<>();
private final static ObjectMapper mapper = new ObjectMapper();
private GeyserConnector connector; private GeyserConnector connector;
/** /**
@ -110,7 +114,7 @@ public class Metrics {
*/ */
private void startSubmitting() { private void startSubmitting() {
connector.getGeneralThreadPool().scheduleAtFixedRate(this::submitData, 1, 30, TimeUnit.MINUTES); connector.getGeneralThreadPool().scheduleAtFixedRate(this::submitData, 1, 30, TimeUnit.MINUTES);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start // Submit the data every 30 minutes, first time after 1 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted! // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it! // WARNING: Just don't do it!
} }
@ -120,22 +124,22 @@ public class Metrics {
* *
* @return The plugin specific data. * @return The plugin specific data.
*/ */
private JsonObject getPluginData() { private ObjectNode getPluginData() {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
data.addProperty("pluginName", name); // Append the name of the server software data.put("pluginName", name); // Append the name of the server software
data.addProperty("pluginVersion", GeyserConnector.VERSION); // Append the name of the server software data.put("pluginVersion", GeyserConnector.VERSION); // Append the name of the server software
JsonArray customCharts = new JsonArray(); ArrayNode customCharts = mapper.createArrayNode();
for (CustomChart customChart : charts) { for (CustomChart customChart : charts) {
// Add the data of the custom charts // Add the data of the custom charts
JsonObject chart = customChart.getRequestJsonObject(); JsonNode chart = customChart.getRequestJsonNode();
if (chart == null) { // If the chart is null, we skip it if (chart == null) { // If the chart is null, we skip it
continue; continue;
} }
customCharts.add(chart); customCharts.add(chart);
} }
data.add("customCharts", customCharts); data.put("customCharts", customCharts);
return data; return data;
} }
@ -145,7 +149,7 @@ public class Metrics {
* *
* @return The server specific data. * @return The server specific data.
*/ */
private JsonObject getServerData() { private ObjectNode getServerData() {
// OS specific data // OS specific data
int playerAmount = connector.getPlayers().size(); int playerAmount = connector.getPlayers().size();
@ -154,15 +158,15 @@ public class Metrics {
String osVersion = System.getProperty("os.version"); String osVersion = System.getProperty("os.version");
int coreCount = Runtime.getRuntime().availableProcessors(); int coreCount = Runtime.getRuntime().availableProcessors();
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
data.addProperty("serverUUID", serverUUID); data.put("serverUUID", serverUUID);
data.addProperty("playerAmount", playerAmount); data.put("playerAmount", playerAmount);
data.addProperty("osName", osName); data.put("osName", osName);
data.addProperty("osArch", osArch); data.put("osArch", osArch);
data.addProperty("osVersion", osVersion); data.put("osVersion", osVersion);
data.addProperty("coreCount", coreCount); data.put("coreCount", coreCount);
return data; return data;
} }
@ -171,11 +175,11 @@ public class Metrics {
* Collects the data and sends it afterwards. * Collects the data and sends it afterwards.
*/ */
private void submitData() { private void submitData() {
final JsonObject data = getServerData(); final ObjectNode data = getServerData();
JsonArray pluginData = new JsonArray(); ArrayNode pluginData = mapper.createArrayNode();
pluginData.add(getPluginData()); pluginData.add(getPluginData());
data.add("plugins", pluginData); data.putPOJO("plugins", pluginData);
new Thread(() -> { new Thread(() -> {
try { try {
@ -196,7 +200,7 @@ public class Metrics {
* @param data The data to send. * @param data The data to send.
* @throws Exception If the request failed. * @throws Exception If the request failed.
*/ */
private static void sendData(JsonObject data) throws Exception { private static void sendData(ObjectNode data) throws Exception {
if (data == null) { if (data == null) {
throw new IllegalArgumentException("Data cannot be null!"); throw new IllegalArgumentException("Data cannot be null!");
} }
@ -262,16 +266,16 @@ public class Metrics {
this.chartId = chartId; this.chartId = chartId;
} }
private JsonObject getRequestJsonObject() { private ObjectNode getRequestJsonNode() {
JsonObject chart = new JsonObject(); ObjectNode chart = new ObjectMapper().createObjectNode();
chart.addProperty("chartId", chartId); chart.put("chartId", chartId);
try { try {
JsonObject data = getChartData(); ObjectNode data = getChartData();
if (data == null) { if (data == null) {
// If the data is null we don't send the chart. // If the data is null we don't send the chart.
return null; return null;
} }
chart.add("data", data); chart.putPOJO("data", data);
} catch (Throwable t) { } catch (Throwable t) {
if (logFailedRequests) { if (logFailedRequests) {
logger.log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t); logger.log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t);
@ -281,7 +285,9 @@ public class Metrics {
return chart; return chart;
} }
protected abstract JsonObject getChartData() throws Exception;
protected abstract ObjectNode getChartData() throws Exception;
} }
@ -304,14 +310,14 @@ public class Metrics {
} }
@Override @Override
protected JsonObject getChartData() throws Exception { protected ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
String value = callable.call(); String value = callable.call();
if (value == null || value.isEmpty()) { if (value == null || value.isEmpty()) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.addProperty("value", value); data.put("value", value);
return data; return data;
} }
} }
@ -335,9 +341,9 @@ public class Metrics {
} }
@Override @Override
protected JsonObject getChartData() throws Exception { protected ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
JsonObject values = new JsonObject(); ObjectNode values = mapper.createObjectNode();
Map<String, Integer> map = callable.call(); Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -349,13 +355,13 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
values.addProperty(entry.getKey(), entry.getValue()); values.put(entry.getKey(), entry.getValue());
} }
if (allSkipped) { if (allSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.add("values", values); data.putPOJO("values", values);
return data; return data;
} }
} }
@ -379,9 +385,9 @@ public class Metrics {
} }
@Override @Override
public JsonObject getChartData() throws Exception { public ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
JsonObject values = new JsonObject(); ObjectNode values = mapper.createObjectNode();
Map<String, Map<String, Integer>> map = callable.call(); Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -389,22 +395,22 @@ public class Metrics {
} }
boolean reallyAllSkipped = true; boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) { for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JsonObject value = new JsonObject(); ObjectNode value = mapper.createObjectNode();
boolean allSkipped = true; boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) { for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
value.addProperty(valueEntry.getKey(), valueEntry.getValue()); value.put(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false; allSkipped = false;
} }
if (!allSkipped) { if (!allSkipped) {
reallyAllSkipped = false; reallyAllSkipped = false;
values.add(entryValues.getKey(), value); values.putPOJO(entryValues.getKey(), value);
} }
} }
if (reallyAllSkipped) { if (reallyAllSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.add("values", values); data.putPOJO("values", values);
return data; return data;
} }
} }
@ -428,14 +434,14 @@ public class Metrics {
} }
@Override @Override
protected JsonObject getChartData() throws Exception { protected ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
int value = callable.call(); int value = callable.call();
if (value == 0) { if (value == 0) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.addProperty("value", value); data.put("value", value);
return data; return data;
} }
@ -460,9 +466,9 @@ public class Metrics {
} }
@Override @Override
protected JsonObject getChartData() throws Exception { protected ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
JsonObject values = new JsonObject(); ObjectNode values = mapper.createObjectNode();
Map<String, Integer> map = callable.call(); Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -474,13 +480,13 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
values.addProperty(entry.getKey(), entry.getValue()); values.put(entry.getKey(), entry.getValue());
} }
if (allSkipped) { if (allSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.add("values", values); data.putPOJO("values", values);
return data; return data;
} }
@ -505,20 +511,20 @@ public class Metrics {
} }
@Override @Override
protected JsonObject getChartData() throws Exception { protected ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
JsonObject values = new JsonObject(); ObjectNode values = mapper.createObjectNode();
Map<String, Integer> map = callable.call(); Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
for (Map.Entry<String, Integer> entry : map.entrySet()) { for (Map.Entry<String, Integer> entry : map.entrySet()) {
JsonArray categoryValues = new JsonArray(); ArrayNode categoryValues = mapper.createArrayNode();
categoryValues.add(entry.getValue()); categoryValues.add(entry.getValue());
values.add(entry.getKey(), categoryValues); values.putPOJO(entry.getKey(), categoryValues);
} }
data.add("values", values); data.putPOJO("values", values);
return data; return data;
} }
@ -543,9 +549,9 @@ public class Metrics {
} }
@Override @Override
protected JsonObject getChartData() throws Exception { protected ObjectNode getChartData() throws Exception {
JsonObject data = new JsonObject(); ObjectNode data = mapper.createObjectNode();
JsonObject values = new JsonObject(); ObjectNode values = mapper.createObjectNode();
Map<String, int[]> map = callable.call(); Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -557,17 +563,17 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
JsonArray categoryValues = new JsonArray(); ArrayNode categoryValues = mapper.createArrayNode();
for (int categoryValue : entry.getValue()) { for (int categoryValue : entry.getValue()) {
categoryValues.add(categoryValue); categoryValues.add(categoryValue);
} }
values.add(entry.getKey(), categoryValues); values.putPOJO(entry.getKey(), categoryValues);
} }
if (allSkipped) { if (allSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.add("values", values); data.putPOJO("values", values);
return data; return data;
} }

View file

@ -25,9 +25,6 @@
package org.geysermc.connector.utils; package org.geysermc.connector.utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@ -43,7 +40,6 @@ import java.util.UUID;
import java.util.concurrent.*; import java.util.concurrent.*;
public class SkinProvider { public class SkinProvider {
public static final Gson GSON = new GsonBuilder().create();
public static final boolean ALLOW_THIRD_PARTY_CAPES = GeyserConnector.getInstance().getConfig().isAllowThirdPartyCapes(); public static final boolean ALLOW_THIRD_PARTY_CAPES = GeyserConnector.getInstance().getConfig().isAllowThirdPartyCapes();
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(ALLOW_THIRD_PARTY_CAPES ? 21 : 14); private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(ALLOW_THIRD_PARTY_CAPES ? 21 : 14);

View file

@ -25,8 +25,9 @@
package org.geysermc.connector.utils; package org.geysermc.connector.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.steveice10.mc.auth.data.GameProfile; import com.github.steveice10.mc.auth.data.GameProfile;
import com.google.gson.JsonObject;
import com.nukkitx.protocol.bedrock.data.ImageData; import com.nukkitx.protocol.bedrock.data.ImageData;
import com.nukkitx.protocol.bedrock.data.SerializedSkin; import com.nukkitx.protocol.bedrock.data.SerializedSkin;
import com.nukkitx.protocol.bedrock.packet.PlayerListPacket; import com.nukkitx.protocol.bedrock.packet.PlayerListPacket;
@ -108,18 +109,18 @@ public class SkinUtils {
try { try {
GameProfile.Property skinProperty = profile.getProperty("textures"); GameProfile.Property skinProperty = profile.getProperty("textures");
JsonObject skinObject = SkinProvider.GSON.fromJson(new String(Base64.getDecoder().decode(skinProperty.getValue()), StandardCharsets.UTF_8), JsonObject.class); JsonNode skinObject = new ObjectMapper().readTree(new String(Base64.getDecoder().decode(skinProperty.getValue()), StandardCharsets.UTF_8));
JsonObject textures = skinObject.getAsJsonObject("textures"); JsonNode textures = skinObject.get("textures");
JsonObject skinTexture = textures.getAsJsonObject("SKIN"); JsonNode skinTexture = textures.get("SKIN");
String skinUrl = skinTexture.get("url").getAsString(); String skinUrl = skinTexture.get("url").asText();
boolean isAlex = skinTexture.has("metadata"); boolean isAlex = skinTexture.has("metadata");
String capeUrl = null; String capeUrl = null;
if (textures.has("CAPE")) { if (textures.has("CAPE")) {
JsonObject capeTexture = textures.getAsJsonObject("CAPE"); JsonNode capeTexture = textures.get("CAPE");
capeUrl = capeTexture.get("url").getAsString(); capeUrl = capeTexture.get("url").asText();
} }
return new GameProfileData(skinUrl, capeUrl, isAlex); return new GameProfileData(skinUrl, capeUrl, isAlex);