Skip to content

Commit 34648ed

Browse files
authored
[ISSUE #10161] reduce bytes copy in json encode(#10162)
* reduce bytes copy * reduce bytes copy
1 parent 7c10c77 commit 34648ed

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public static byte[] encode(final Object obj) {
3030
if (obj == null) {
3131
return null;
3232
}
33-
final String json = toJson(obj, false);
34-
return json.getBytes(CHARSET_UTF8);
33+
return JSON.toJSONBytes(obj, CHARSET_UTF8);
3534
}
3635

3736
public static String toJson(final Object obj, boolean prettyFormat) {
@@ -45,31 +44,22 @@ public static <T> T decode(final byte[] data, Class<T> classOfT) {
4544
if (data == null) {
4645
return null;
4746
}
48-
return fromJson(data, classOfT);
47+
return JSON.parseObject(data, classOfT);
4948
}
5049

5150
public static <T> List<T> decodeList(final byte[] data, Class<T> classOfT) {
5251
if (data == null) {
5352
return null;
5453
}
55-
String json = new String(data, CHARSET_UTF8);
56-
return JSON.parseArray(json, classOfT);
54+
return JSON.parseArray(data, 0, data.length, CHARSET_UTF8, classOfT);
5755
}
5856

5957
public static <T> T fromJson(String json, Class<T> classOfT) {
6058
return JSON.parseObject(json, classOfT);
6159
}
6260

63-
private static <T> T fromJson(byte[] data, Class<T> classOfT) {
64-
return JSON.parseObject(data, classOfT);
65-
}
66-
6761
public byte[] encode() {
68-
final String json = this.toJson();
69-
if (json != null) {
70-
return json.getBytes(CHARSET_UTF8);
71-
}
72-
return null;
62+
return JSON.toJSONBytes(this, CHARSET_UTF8);
7363
}
7464

7565
/**
@@ -79,8 +69,7 @@ public byte[] encode() {
7969
* @return serialized data.
8070
*/
8171
public byte[] encode(JSONWriter.Feature... features) {
82-
final String json = JSON.toJSONString(this, features);
83-
return json.getBytes(CHARSET_UTF8);
72+
return JSON.toJSONBytes(this, CHARSET_UTF8, features);
8473
}
8574

8675
public String toJson() {
@@ -90,4 +79,4 @@ public String toJson() {
9079
public String toJson(final boolean prettyFormat) {
9180
return toJson(this, prettyFormat);
9281
}
93-
}
82+
}

0 commit comments

Comments
 (0)