remove gson

This commit is contained in:
Timi
2026-04-08 11:25:45 +08:00
parent b6a58b7376
commit 8947269351
53 changed files with 492 additions and 844 deletions
@@ -1,6 +1,7 @@
package com.imyeyu.api.modules.music.core;
import com.google.gson.Gson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
@@ -36,7 +37,7 @@ public class Middleware implements SchedulingConfigurer {
private static final String CACHE_CLEAR_CORN = "0 0/20 * * * ?";
@Autowired
private Gson gson;
private ObjectMapper jackson;
/** 缓存频道,Map<频道 ID, 关联频道> */
private final Map<String, ChannelBinding> channels = new HashMap<>();
@@ -67,7 +68,7 @@ public class Middleware implements SchedulingConfigurer {
channelBinding.setPlayerChannel(ctx);
channelBinding.setLastActiviedAt(Time.now());
if (channelBinding.getControllerChannel() != null) {
channelBinding.getControllerChannel().writeAndFlush(new TextWebSocketFrame(gson.toJson(pkg)));
channelBinding.getControllerChannel().writeAndFlush(new TextWebSocketFrame(writeValueAsString(pkg)));
}
}
@@ -90,11 +91,19 @@ public class Middleware implements SchedulingConfigurer {
channelBinding.setControllerChannel(ctx);
channelBinding.setLastActiviedAt(Time.now());
if (pkg.getAction() != null && channelBinding.getPlayerChannel() != null) {
byte[] bytes = gson.toJson(pkg).getBytes(StandardCharsets.UTF_8);
byte[] bytes = writeValueAsString(pkg).getBytes(StandardCharsets.UTF_8);
ByteBuf buffer = Unpooled.buffer();
buffer.writeInt(bytes.length);
buffer.writeBytes(bytes);
channelBinding.getPlayerChannel().writeAndFlush(buffer);
}
}
private String writeValueAsString(Object value) {
try {
return jackson.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new IllegalStateException("write music websocket payload error", e);
}
}
}
@@ -1,6 +1,6 @@
package com.imyeyu.api.modules.music.handler;
import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Component;
public class ControllerMessageHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
@Autowired
private Gson gson;
private ObjectMapper jackson;
@Autowired
private Middleware middleware;
@@ -34,7 +34,11 @@ public class ControllerMessageHandler extends SimpleChannelInboundHandler<WebSoc
@Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame msg) {
if (msg instanceof TextWebSocketFrame textWebSocketFrame) {
middleware.pushControllerData(ctx, gson.fromJson(textWebSocketFrame.text(), ControllerPackage.class));
try {
middleware.pushControllerData(ctx, jackson.readValue(textWebSocketFrame.text(), ControllerPackage.class));
} catch (Exception e) {
throw new IllegalStateException("read controller websocket payload error", e);
}
} else {
ctx.channel().writeAndFlush(WebSocketCloseStatus.INVALID_MESSAGE_TYPE).addListener(ChannelFutureListener.CLOSE);
}
@@ -1,6 +1,6 @@
package com.imyeyu.api.modules.music.handler;
import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@@ -22,14 +22,18 @@ import org.springframework.stereotype.Component;
public class PlayerMessageHandler extends SimpleChannelInboundHandler<String> {
@Autowired
private Gson gson;
private ObjectMapper jackson;
@Autowired
private Middleware middleware;
@Override
protected synchronized void channelRead0(ChannelHandlerContext ctx, String result) {
middleware.pushPlayerData(ctx, gson.fromJson(result, PlayerPackage.class));
try {
middleware.pushPlayerData(ctx, jackson.readValue(result, PlayerPackage.class));
} catch (Exception e) {
throw new IllegalStateException("read player websocket payload error", e);
}
}
@Override