Added gson as dependency

This commit is contained in:
Rsl1122 2019-08-06 08:11:05 +03:00
parent 46631c9ae0
commit 20df1264ad
9 changed files with 20 additions and 22 deletions

View File

@ -76,6 +76,7 @@ subprojects {
ext.hikariVersion = "3.3.1"
ext.slf4jVersion = "1.7.28"
ext.geoIpVersion = "2.12.0"
ext.gsonVersion = "2.8.5"
ext.guavaVersion = "28.0-jre"
ext.bstatsVersion = "1.4"

View File

@ -13,10 +13,11 @@ dependencies {
compile "org.slf4j:slf4j-nop:$slf4jVersion"
compile "org.slf4j:slf4j-api:$slf4jVersion"
compile "com.maxmind.geoip2:geoip2:$geoIpVersion"
compile "com.google.code.gson:gson:$gsonVersion"
compileOnly "com.google.guava:guava:$guavaVersion"
testCompile project(":api")
testCompile "com.google.code.gson:gson:2.8.5"
testCompile "com.google.code.gson:gson:$gsonVersion"
}
shadowJar {
@ -41,6 +42,7 @@ shadowJar {
relocate 'com.zaxxer', 'plan.com.zaxxer'
relocate 'com.github.benmanes', 'plan.com.github.benmanes'
relocate 'com.googlecode', 'plan.com.googlecode'
relocate 'com.google', 'plan.com.google'
relocate 'org.h2', 'plan.org.h2'
relocate 'org.bstats', 'plan.org.bstats'
relocate 'org.slf4j', 'plan.org.slf4j'

View File

@ -69,11 +69,11 @@ public class GraphsJSONHandler implements PageHandler {
case "serverCalendar":
return new JSONResponse(graphJSON.serverCalendarJSON(serverUUID));
case "worldPie":
return new JSONResponse<>(graphJSON.serverWorldPieJSONAsMap(serverUUID));
return new JSONResponse(graphJSON.serverWorldPieJSONAsMap(serverUUID));
case "activity":
return new JSONResponse<>(graphJSON.activityGraphsJSONAsMap(serverUUID));
return new JSONResponse(graphJSON.activityGraphsJSONAsMap(serverUUID));
case "geolocation":
return new JSONResponse<>(graphJSON.geolocationGraphsJSONAsMap(serverUUID));
return new JSONResponse(graphJSON.geolocationGraphsJSONAsMap(serverUUID));
case "ping":
return new JSONResponse(graphJSON.pingGraphsJSON(serverUUID));
default:

View File

@ -47,7 +47,7 @@ public class PlayerJSONHandler implements PageHandler {
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID playerUUID = identifiers.getPlayerUUID(target); // Can throw BadRequestException
return new JSONResponse<>(jsonParser.createJSONAsMap(playerUUID));
return new JSONResponse(jsonParser.createJSONAsMap(playerUUID));
}
@Override

View File

@ -55,7 +55,7 @@ public class PlayerKillsJSONHandler implements PageHandler {
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID serverUUID = identifiers.getServerUUID(target);
return new JSONResponse<>(Collections.singletonMap("player_kills", jsonFactory.serverPlayerKillsAsJSONMap(serverUUID)));
return new JSONResponse(Collections.singletonMap("player_kills", jsonFactory.serverPlayerKillsAsJSONMap(serverUUID)));
}
@Override

View File

@ -48,7 +48,7 @@ public class ServerTabJSONHandler<T> implements PageHandler {
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
return new JSONResponse<>(jsonParser.apply(serverUUID));
return new JSONResponse(jsonParser.apply(serverUUID));
}
@Override

View File

@ -55,7 +55,7 @@ public class SessionsJSONHandler implements PageHandler {
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID serverUUID = identifiers.getServerUUID(target);
return new JSONResponse<>(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID)));
return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID)));
}
@Override

View File

@ -18,6 +18,8 @@ package com.djrapitops.plan.system.webserver.response.data;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.ResponseType;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
/**
* Generic JSON response implemented using Gson.
@ -26,21 +28,14 @@ import com.djrapitops.plan.system.webserver.response.ResponseType;
*
* @author Rsl1122
*/
public class JSONResponse<T> extends Response {
public class JSONResponse extends Response {
public JSONResponse(T object) {
super(ResponseType.JSON);
super.setHeader("HTTP/1.1 200 OK");
public JSONResponse(Object object) {
this(new Gson().toJson(object));
}
try {
Class<?> gsonClass = Class.forName("com.google.gson.Gson");
Object gson = gsonClass.getConstructor().newInstance();
Object json = gsonClass.getMethod("toJson", Object.class).invoke(gson, object);
super.setContent(json.toString());
} catch (ReflectiveOperationException e) {
super.setContent("{\"error\":\"Gson for json responses not available on this server: " + e.toString() + "\"}");
}
public JSONResponse(JsonElement json) {
this(json.getAsString());
}
public JSONResponse(String jsonString) {

View File

@ -32,7 +32,7 @@ import java.util.stream.Collectors;
*
* @author Rsl1122
*/
public class RawDataResponse extends JSONResponse<Map<String, Object>> {
public class RawDataResponse extends JSONResponse {
public RawDataResponse(DataContainer dataContainer) {
super(mapToNormalMap(dataContainer));