Skip to content

Commit 2635655

Browse files
committed
Fix some tiny json issues with responses
1 parent a54b15e commit 2635655

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/main/java/pw/chew/jsonrestapi/RestServer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pw.chew.jsonrestapi;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonSyntaxException;
35
import me.clip.placeholderapi.PlaceholderAPI;
46
import org.bukkit.Bukkit;
57
import org.bukkit.OfflinePlayer;
@@ -224,9 +226,17 @@ private String buildResponse(String method, String route, Map<String, String> pa
224226
// Build the response by letting PAPI parse the placeholders
225227
String response = PlaceholderAPI.setPlaceholders(player, request);
226228

227-
// If response is empty, set it to an empty string.
228-
if (response.isEmpty()) {
229-
response = "\"\""; // Will save as ""
229+
// Simple check to see if response is valid JSON. If not, just wrap it.
230+
Gson gson = new Gson();
231+
try {
232+
// Check if string starts with what we will assume to be an object or array
233+
if (response.startsWith("[") || response.startsWith("{")) {
234+
gson.fromJson(response, Object.class);
235+
} else {
236+
response = "\"" + response + "\"";
237+
}
238+
} catch (JsonSyntaxException ex) {
239+
response = "\"" + response + "\"";
230240
}
231241

232242
// Wrap the response

0 commit comments

Comments
 (0)