From 075a0e749049af1e86a908336aa7748286f2b57e Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Tue, 13 Feb 2024 22:25:25 +0100 Subject: [PATCH] Added ResponseController#jsonArray --- .../gnmyt/mcdash/http/ResponseController.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/de/gnmyt/mcdash/http/ResponseController.java b/src/main/java/de/gnmyt/mcdash/http/ResponseController.java index 0c54d83..87f3fef 100644 --- a/src/main/java/de/gnmyt/mcdash/http/ResponseController.java +++ b/src/main/java/de/gnmyt/mcdash/http/ResponseController.java @@ -7,6 +7,7 @@ import com.sun.net.httpserver.HttpExchange; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -115,6 +116,26 @@ public class ResponseController { send(); } + /** + * Sends a json response to the client (as an array) + * @param values The values you want to send + */ + public final void jsonArray(ArrayList> values) { + response.setContentType(ContentType.JSON); + + Gson gson = new Gson(); + + String json = gson.toJson(values); + + writeToOutput(json); + send(); + } + + /** + * Creates a new {@link JSONBuilder} instance + * + * @return the new {@link JSONBuilder} instance + */ public JSONBuilder json() { return new JSONBuilder(this); }