mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-28 12:07:35 +01:00
JSONResponse
This commit is contained in:
parent
98cd6bc5e0
commit
7b5739995e
@ -47,4 +47,8 @@ public class ExtensionNumberData implements ExtensionData {
|
|||||||
public String getFormattedValue(Formatter<Long> formatter) {
|
public String getFormattedValue(Formatter<Long> formatter) {
|
||||||
return formatter.apply(value);
|
return formatter.apply(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getRawValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.djrapitops.plan.system.webserver.response.data;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.system.webserver.response.Response;
|
||||||
|
import com.djrapitops.plan.system.webserver.response.ResponseType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic JSON response implemented using Gson.
|
||||||
|
* <p>
|
||||||
|
* Returns a JSON version of the given object.
|
||||||
|
*
|
||||||
|
* @author Rsl1122
|
||||||
|
*/
|
||||||
|
public class JSONResponse<T> extends Response {
|
||||||
|
|
||||||
|
public JSONResponse(T object) {
|
||||||
|
super(ResponseType.JSON);
|
||||||
|
|
||||||
|
super.setHeader("HTTP/1.1 200 OK");
|
||||||
|
|
||||||
|
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() + "\"}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,8 +17,7 @@
|
|||||||
package com.djrapitops.plan.system.webserver.response.pages;
|
package com.djrapitops.plan.system.webserver.response.pages;
|
||||||
|
|
||||||
import com.djrapitops.plan.data.store.containers.DataContainer;
|
import com.djrapitops.plan.data.store.containers.DataContainer;
|
||||||
import com.djrapitops.plan.system.webserver.response.Response;
|
import com.djrapitops.plan.system.webserver.response.data.JSONResponse;
|
||||||
import com.djrapitops.plan.system.webserver.response.ResponseType;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -33,27 +32,13 @@ import java.util.stream.Collectors;
|
|||||||
*
|
*
|
||||||
* @author Rsl1122
|
* @author Rsl1122
|
||||||
*/
|
*/
|
||||||
public class RawDataResponse extends Response {
|
public class RawDataResponse extends JSONResponse<Map<String, Object>> {
|
||||||
|
|
||||||
public RawDataResponse(DataContainer dataContainer) {
|
public RawDataResponse(DataContainer dataContainer) {
|
||||||
super(ResponseType.JSON);
|
super(mapToNormalMap(dataContainer));
|
||||||
|
|
||||||
Map<String, Object> values = mapToNormalMap(dataContainer);
|
|
||||||
|
|
||||||
super.setHeader("HTTP/1.1 200 OK");
|
|
||||||
|
|
||||||
try {
|
|
||||||
Class<?> gsonClass = Class.forName("com.google.gson.Gson");
|
|
||||||
Object gson = gsonClass.getConstructor().newInstance();
|
|
||||||
Object json = gsonClass.getMethod("toJson", Object.class).invoke(gson, values);
|
|
||||||
|
|
||||||
super.setContent(json.toString());
|
|
||||||
} catch (ReflectiveOperationException e) {
|
|
||||||
super.setContent("{\"error\":\"Gson for raw json responses not available on this server: " + e.toString() + "\"}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> mapToNormalMap(DataContainer player) {
|
private static Map<String, Object> mapToNormalMap(DataContainer player) {
|
||||||
Map<String, Object> values = new HashMap<>();
|
Map<String, Object> values = new HashMap<>();
|
||||||
player.getMap().forEach((key, value) ->
|
player.getMap().forEach((key, value) ->
|
||||||
{
|
{
|
||||||
@ -72,14 +57,14 @@ public class RawDataResponse extends Response {
|
|||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List handleList(List list) {
|
private static List handleList(List list) {
|
||||||
if (list.stream().findAny().orElse(null) instanceof DataContainer) {
|
if (list.stream().findAny().orElse(null) instanceof DataContainer) {
|
||||||
return (List) list.stream().map((obj) -> mapToNormalMap((DataContainer) obj)).collect(Collectors.toList());
|
return (List) list.stream().map((obj) -> mapToNormalMap((DataContainer) obj)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map handleMap(Map map) {
|
private static Map handleMap(Map map) {
|
||||||
if (map.values().stream().findAny().orElse(null) instanceof DataContainer) {
|
if (map.values().stream().findAny().orElse(null) instanceof DataContainer) {
|
||||||
Map<Object, Object> newMap = new HashMap<>();
|
Map<Object, Object> newMap = new HashMap<>();
|
||||||
map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value)));
|
map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value)));
|
||||||
|
Loading…
Reference in New Issue
Block a user