Skip to content

Commit 33beaee

Browse files
committed
refactor(json)!: remove Gson compatibility methods and consolidate JSON processing
- The codebase has fully migrated to using Jackson for all JSON parsing and serialization. - All methods and utilities providing backward compatibility for Gson `JsonObject` - and `JsonArray` have been removed. - This includes Gson-specific update methods in entities, builder methods, storage - methods, and utility conversion functions. - This change simplifies the codebase, reduces dependency surface, and improves - performance by removing unnecessary conversions. BREAKING CHANGE: All Gson-specific compatibility methods and utilities have been removed. Code relying on these methods will need to be updated to use Jackson directly.
1 parent 57ba115 commit 33beaee

22 files changed

+8
-406
lines changed

src/main/java/snw/kookbc/impl/entity/CustomEmojiImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ public void setName0(String name) {
8585
this.name = name;
8686
}
8787

88-
// GSON compatibility method
89-
public synchronized void update(com.google.gson.JsonObject data) {
90-
update(snw.kookbc.util.JacksonUtil.convertFromGsonJsonObject(data));
91-
}
92-
93-
// ===== Jackson API - 高性能版本 =====
94-
9588
@Override
9689
public synchronized void update(JsonNode data) {
9790
isTrue(Objects.equals(getId(), getRequiredString(data, "id")), "You can't update the emoji by using different data");

src/main/java/snw/kookbc/impl/entity/GameImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ public void setNameAndIcon(@NotNull String name, @NotNull String icon) {
9898
this.icon = icon;
9999
}
100100

101-
// GSON compatibility method
102-
public synchronized void update(com.google.gson.JsonObject data) {
103-
update(snw.kookbc.util.JacksonUtil.convertFromGsonJsonObject(data));
104-
}
105-
106-
// ===== Jackson API - 高性能版本 =====
107-
108101
@Override
109102
public synchronized void update(JsonNode data) {
110103
this.name = getStringOrDefault(data, "name", "Unknown Game");

src/main/java/snw/kookbc/impl/entity/GuildImpl.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public Role createRole(String s) {
251251
.put("name", s)
252252
.build();
253253
JsonNode res = client.getNetworkClient().post(HttpAPIRoute.ROLE_CREATE.toFullURL(), body);
254-
Role result = client.getEntityBuilder().buildRole(this, snw.kookbc.util.JacksonUtil.convertToGsonJsonObject(res));
254+
Role result = client.getEntityBuilder().buildRole(this, res);
255255
client.getStorage().addRole(this, result);
256256
return result;
257257
}
@@ -365,11 +365,6 @@ public void setAvatar(String avatarUrl) {
365365
this.avatarUrl = avatarUrl;
366366
}
367367

368-
// GSON compatibility method - marked for deprecation
369-
public synchronized void updateFromGson(com.google.gson.JsonObject data) {
370-
update(snw.kookbc.util.JacksonUtil.convertFromGsonJsonObject(data));
371-
}
372-
373368
@Override
374369
public synchronized void update(JsonNode data) {
375370
final String id = data.get("id").asText();

src/main/java/snw/kookbc/impl/entity/RoleImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,6 @@ public void setMentionable0(boolean mentionable) {
166166
this.mentionable = mentionable;
167167
}
168168

169-
// GSON compatibility method
170-
public synchronized void update(com.google.gson.JsonObject data) {
171-
update(snw.kookbc.util.JacksonUtil.convertFromGsonJsonObject(data));
172-
}
173-
174-
// ===== Jackson API - 高性能版本 =====
175-
176169
@Override
177170
public synchronized void update(JsonNode data) {
178171
isTrue(getId() == getRequiredInt(data, "role_id"), "You can't update the role by using different data");

src/main/java/snw/kookbc/impl/entity/UserImpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,6 @@ public void setVipAvatarUrl(String vipAvatarUrl) {
344344
this.vipAvatarUrl = vipAvatarUrl;
345345
}
346346

347-
// GSON compatibility method
348-
public void update(com.google.gson.JsonObject data) {
349-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
350-
update(snw.kookbc.util.JacksonUtil.convertFromGsonJsonObject(data));
351-
}
352-
353347
@Override
354348
public synchronized void update(JsonNode data) {
355349
Validate.isTrue(Objects.equals(getId(), data.get("id").asText()),

src/main/java/snw/kookbc/impl/entity/builder/CardBuilder.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,6 @@ public static Object buildCard(String jsonString) {
8484
}
8585
}
8686

87-
// ===== Gson兼容方法(向后兼容)=====
88-
89-
/**
90-
* 从Gson JsonArray构建多卡片组件(向后兼容)
91-
* @param array Gson JsonArray
92-
* @return MultipleCardComponent
93-
*/
94-
public static MultipleCardComponent buildCard(com.google.gson.JsonArray array) {
95-
// 转换Gson JsonArray到Jackson JsonNode
96-
JsonNode arrayNode = JacksonCardUtil.parse(array.toString());
97-
return buildCardArray(arrayNode);
98-
}
99-
100-
/**
101-
* 从Gson JsonObject构建单个卡片组件(向后兼容)
102-
* @param object Gson JsonObject
103-
* @return CardComponent
104-
*/
105-
public static CardComponent buildCard(com.google.gson.JsonObject object) {
106-
// 转换Gson JsonObject到Jackson JsonNode
107-
JsonNode objectNode = JacksonCardUtil.parse(object.toString());
108-
return buildCardObject(objectNode);
109-
}
110-
11187
// ===== 序列化方法 =====
11288

11389
/**
@@ -128,37 +104,4 @@ public static JsonNode serializeToNode(MultipleCardComponent component) {
128104
return JacksonCardUtil.toJsonNode(component);
129105
}
130106

131-
/**
132-
* 序列化单个卡片组件为Gson JsonArray(向后兼容)
133-
* @param component CardComponent
134-
* @return JsonArray
135-
*/
136-
public static com.google.gson.JsonArray serialize(CardComponent component) {
137-
com.google.gson.JsonArray result = new com.google.gson.JsonArray();
138-
result.add(serialize0(component));
139-
return result;
140-
}
141-
142-
/**
143-
* 序列化多卡片组件为Gson JsonArray(向后兼容)
144-
* @param component MultipleCardComponent
145-
* @return JsonArray
146-
*/
147-
public static com.google.gson.JsonArray serialize(MultipleCardComponent component) {
148-
com.google.gson.JsonArray array = new com.google.gson.JsonArray();
149-
for (CardComponent card : component.getComponents()) {
150-
array.add(serialize0(card));
151-
}
152-
return array;
153-
}
154-
155-
/**
156-
* 序列化单个卡片组件为Gson JsonObject(向后兼容)
157-
* @param component CardComponent
158-
* @return JsonObject
159-
*/
160-
public static com.google.gson.JsonObject serialize0(CardComponent component) {
161-
String json = JacksonCardUtil.toJson(component);
162-
return new com.google.gson.JsonParser().parse(json).getAsJsonObject();
163-
}
164107
}

src/main/java/snw/kookbc/impl/entity/builder/EntityBuildUtil.java

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,58 +39,6 @@
3939

4040
public class EntityBuildUtil {
4141

42-
// ===== Gson兼容方法(向后兼容)=====
43-
44-
public static Collection<Channel.RolePermissionOverwrite> parseRPO(com.google.gson.JsonObject object) {
45-
com.google.gson.JsonArray array = object.getAsJsonArray("permission_overwrites");
46-
Collection<Channel.RolePermissionOverwrite> rpo = new ConcurrentLinkedQueue<>();
47-
for (com.google.gson.JsonElement element : array) {
48-
com.google.gson.JsonObject orpo = element.getAsJsonObject();
49-
rpo.add(
50-
new Channel.RolePermissionOverwrite(
51-
orpo.get("role_id").getAsInt(),
52-
orpo.get("allow").getAsInt(),
53-
orpo.get("deny").getAsInt()));
54-
}
55-
return rpo;
56-
}
57-
58-
public static Collection<Channel.UserPermissionOverwrite> parseUPO(KBCClient client, com.google.gson.JsonObject object) {
59-
com.google.gson.JsonArray array = object.getAsJsonArray("permission_users");
60-
Collection<Channel.UserPermissionOverwrite> upo = new ConcurrentLinkedQueue<>();
61-
for (com.google.gson.JsonElement element : array) {
62-
com.google.gson.JsonObject oupo = element.getAsJsonObject();
63-
com.google.gson.JsonObject rawUser = oupo.getAsJsonObject("user");
64-
upo.add(
65-
new Channel.UserPermissionOverwrite(
66-
client.getStorage().getUser(rawUser.get("id").getAsString(), rawUser),
67-
oupo.get("allow").getAsInt(),
68-
oupo.get("deny").getAsInt()));
69-
}
70-
return upo;
71-
}
72-
73-
public static NotifyType parseNotifyType(com.google.gson.JsonObject object) {
74-
Guild.NotifyType type = null;
75-
int rawNotifyType = object.get("notify_type").getAsInt();
76-
for (Guild.NotifyType value : Guild.NotifyType.values()) {
77-
if (value.getValue() == rawNotifyType) {
78-
type = value;
79-
break;
80-
}
81-
}
82-
notNull(type, String.format("Internal Error: Unexpected NotifyType from remote: %s", rawNotifyType));
83-
return type;
84-
}
85-
86-
public static Guild parseEmojiGuild(String id, KBCClient client, com.google.gson.JsonObject object) {
87-
Guild guild = null;
88-
if (id.contains("/")) {
89-
guild = client.getStorage().getGuild(id.substring(0, id.indexOf("/")));
90-
}
91-
return guild;
92-
}
93-
9442
@Nullable
9543
public static Channel parseChannel(KBCClient client, String id, int type) {
9644
switch (type) {
@@ -147,10 +95,9 @@ public static Collection<Channel.UserPermissionOverwrite> parseUPO(KBCClient cli
14795
int allow = snw.kookbc.util.JacksonUtil.getIntOrDefault(element, "allow", 0);
14896
int deny = snw.kookbc.util.JacksonUtil.getIntOrDefault(element, "deny", 0);
14997

150-
// 临时桥接到Gson JsonObject
151-
com.google.gson.JsonObject gsonUser = convertToGsonJsonObject(rawUser);
98+
// 直接使用Jackson JsonNode
15299
upo.add(new Channel.UserPermissionOverwrite(
153-
client.getStorage().getUser(userId, gsonUser),
100+
client.getStorage().getUser(userId, rawUser),
154101
allow, deny));
155102
}
156103
}
@@ -189,9 +136,4 @@ public static Guild parseEmojiGuild(String id, KBCClient client, JsonNode node)
189136
return guild;
190137
}
191138

192-
// 临时桥接方法 - 将JsonNode转换为JsonObject供现有代码使用
193-
private static com.google.gson.JsonObject convertToGsonJsonObject(JsonNode jacksonNode) {
194-
return snw.kookbc.util.JacksonUtil.convertToGsonJsonObject(jacksonNode);
195-
}
196-
197139
}

src/main/java/snw/kookbc/impl/entity/builder/EntityBuilder.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -56,60 +56,6 @@ public EntityBuilder(KBCClient client) {
5656
this.client = client;
5757
}
5858

59-
/**
60-
* 构建User对象 (Gson兼容版本)
61-
* 该方法保留用于向后兼容,内部委托给Jackson版本
62-
*/
63-
public User buildUser(com.google.gson.JsonObject s) {
64-
JsonNode node = convertFromGsonJsonObject(s);
65-
return buildUser(node);
66-
}
67-
68-
/**
69-
* 构建Guild对象 (Gson兼容版本)
70-
* 该方法保留用于向后兼容,内部委托给Jackson版本
71-
*/
72-
public Guild buildGuild(com.google.gson.JsonObject object) {
73-
JsonNode node = convertFromGsonJsonObject(object);
74-
return buildGuild(node);
75-
}
76-
77-
/**
78-
* 构建Channel对象 (Gson兼容版本)
79-
* 该方法保留用于向后兼容,内部委托给Jackson版本
80-
*/
81-
public Channel buildChannel(com.google.gson.JsonObject object) {
82-
JsonNode node = convertFromGsonJsonObject(object);
83-
return buildChannel(node);
84-
}
85-
86-
/**
87-
* 构建Role对象 (Gson兼容版本)
88-
* 该方法保留用于向后兼容,内部委托给Jackson版本
89-
*/
90-
public Role buildRole(Guild guild, com.google.gson.JsonObject object) {
91-
JsonNode node = convertFromGsonJsonObject(object);
92-
return buildRole(guild, node);
93-
}
94-
95-
/**
96-
* 构建CustomEmoji对象 (Gson兼容版本)
97-
* 该方法保留用于向后兼容,内部委托给Jackson版本
98-
*/
99-
public CustomEmoji buildEmoji(com.google.gson.JsonObject object) {
100-
JsonNode node = convertFromGsonJsonObject(object);
101-
return buildEmoji(node);
102-
}
103-
104-
/**
105-
* 构建Game对象 (Gson兼容版本)
106-
* 该方法保留用于向后兼容,内部委托给Jackson版本
107-
*/
108-
public Game buildGame(com.google.gson.JsonObject object) {
109-
JsonNode node = convertFromGsonJsonObject(object);
110-
return buildGame(node);
111-
}
112-
11359
// ===== Jackson API - 高性能版本(处理Kook不完整JSON数据)=====
11460

11561
/**

src/main/java/snw/kookbc/impl/entity/builder/MessageBuilder.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,6 @@ public static Object[] serialize(BaseComponent component) {
8686
throw new RuntimeException("Unsupported component");
8787
}
8888

89-
public PrivateMessage buildPrivateMessage(com.google.gson.JsonObject object) {
90-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
91-
return buildPrivateMessage(JacksonUtil.convertFromGsonJsonObject(object));
92-
}
93-
94-
public ChannelMessage buildChannelMessage(com.google.gson.JsonObject object) {
95-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
96-
return buildChannelMessage(JacksonUtil.convertFromGsonJsonObject(object));
97-
}
98-
99-
private User getAuthor(com.google.gson.JsonObject extra) {
100-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
101-
return getAuthor(JacksonUtil.convertFromGsonJsonObject(extra));
102-
}
103-
104-
private Message getQuote(com.google.gson.JsonObject extra) {
105-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
106-
return getQuote(JacksonUtil.convertFromGsonJsonObject(extra));
107-
}
108-
10989
private ChannelMessageImpl buildMessage(String id, User author, BaseComponent component, long timeStamp,
11090
Message message, String targetId, int channelType) {
11191
if (channelType == CHANNEL_TYPE_TEXT) {
@@ -118,19 +98,6 @@ private ChannelMessageImpl buildMessage(String id, User author, BaseComponent co
11898
throw new RuntimeException("We can not found channel type: " + channelType);
11999
}
120100

121-
public Message buildQuote(com.google.gson.JsonObject object) {
122-
if (object == null) {
123-
return null;
124-
}
125-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
126-
return buildQuote(JacksonUtil.convertFromGsonJsonObject(object));
127-
}
128-
129-
public BaseComponent buildComponent(com.google.gson.JsonObject object) {
130-
// 性能优化:使用 convertFromGsonJsonObject 避免 toString() 序列化开销
131-
return buildComponent(JacksonUtil.convertFromGsonJsonObject(object));
132-
}
133-
134101
// ===== Jackson API - 高性能版本 =====
135102

136103
public PrivateMessage buildPrivateMessage(JsonNode node) {

src/main/java/snw/kookbc/impl/entity/channel/ChannelImpl.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import static snw.jkook.util.Validate.isTrue;
4040
import static snw.kookbc.impl.entity.builder.EntityBuildUtil.parseRPO;
4141
import static snw.kookbc.impl.entity.builder.EntityBuildUtil.parseUPO;
42-
import static snw.kookbc.util.JacksonUtil.convertFromGsonJsonObject;
4342
import static snw.kookbc.util.JacksonUtil.getAsInt;
4443
import static snw.kookbc.util.JacksonUtil.getAsString;
4544

@@ -320,18 +319,13 @@ public User getMaster() {
320319
return master;
321320
}
322321

323-
// GSON compatibility method
324-
public synchronized void update(com.google.gson.JsonObject data) {
325-
update(convertFromGsonJsonObject(data));
326-
}
327-
328322
public synchronized void update(JsonNode data) {
329323
isTrue(Objects.equals(getId(), snw.kookbc.util.JacksonUtil.get(data, "id").asText()), "You can't update channel by using different data");
330324
this.name = snw.kookbc.util.JacksonUtil.get(data, "name").asText();
331325
this.permSync = snw.kookbc.util.JacksonUtil.get(data, "permission_sync").asInt() != 0;
332326
this.guild = client.getStorage().getGuild(snw.kookbc.util.JacksonUtil.get(data, "guild_id").asText());
333-
this.rpo = parseRPO(snw.kookbc.util.JacksonUtil.convertToGsonJsonObject(data));
334-
this.upo = parseUPO(client, snw.kookbc.util.JacksonUtil.convertToGsonJsonObject(data));
327+
this.rpo = parseRPO(data);
328+
this.upo = parseUPO(client, data);
335329

336330
// Why we delay the add operation?
337331
// We may construct the channel object at any time,

0 commit comments

Comments
 (0)