fix(web): avoid gson-2.1 members-wrapped JSON responses; update AGENTS.md

- sendJsonResponse now serializes JsonElement via toString(), since gson-2.1's
  JSON_ELEMENT factory misses JsonObject/JsonArray subclasses and falls back to
  reflection (leaking the internal "members" field into every REST response)
- Document javac full-path requirement, pre-existing compile warnings,
  frontend page data flow, and gson quirks in AGENTS.md
This commit is contained in:
2026-08-24 23:59:14 +08:00
parent 35c430114e
commit 9cea74943c
3 changed files with 28 additions and 6 deletions
@@ -119,7 +119,15 @@ public class KLALBWebServer {
private void sendJsonResponse(HttpExchange exchange, int statusCode, Object data) throws IOException {
setCorsHeaders(exchange);
byte[] bytes = gson.toJson(data).getBytes(StandardCharsets.UTF_8);
String json;
if (data instanceof JsonElement) {
// gson-2.1 的 toJson(Object) 会按运行时类型 JsonObject 反射序列化出内部的 members 字段,
// 必须走 JsonElement 重载(或 toString)直接输出 JSON 树
json = ((JsonElement) data).toString();
} else {
json = gson.toJson(data);
}
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
exchange.sendResponseHeaders(statusCode, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {