如何将字节反序列化回我的 class 对象?
How to deserialize bytes back to my class object?
我正在将我的 class 序列化为 bytes
,然后我想在反序列化时从中提取所有内容。我的 class 看起来像这样:
public class DataWork {
private final String clientId;
private final String serverId;
public DataWork(final String clientId, final String serverId) {
this.clientId = clientId;
this.serverId = serverId;
}
public String getClientId() {
return clientId;
}
public String getServerId() {
return serverId;
}
@Override
public String toString() {
return "DataWork [clientId=" + clientId + ", serverId=" + serverId + "]";
}
}
下面是我的序列化程序 class,我在其中将 DataWork 序列化为字节,但我不知道如何在反序列化时从中提取所有内容?一般来说,我想获得完整的 DataWork 对象,同时使用相同的字节对其进行反序列化。
public class DataSerializer implements QueueSerializer<DataWork> {
// here I need to deserialize
public DataWork deserialize(byte[] buffer) {
// don't know what should I do here?
// buffer will have my actual bytes of DataWork
}
// here I am serializing
public byte[] serialize(DataWork work) {
return work.toString().getBytes();
}
}
现在这意味着我需要以这样一种方式对其进行序列化,以便在反序列化时可以正确地从中提取所有内容。
查看您的 toString()
方法实现。它会 return 你以同样的方式字节。
// here I need to deserialize
public DataWork deserialize(byte[] buffer) {
if(null == buffer || buffer.length == 0)
return null;
// reconstruct the string back from bytes.
String data = new String(buffer);
// now just parse the string and create a new object of type DataWork
// with clientID and serverID field values retrieved from the string.
String splitData = data.split(",");
String clientID = splitData[0].split("=")[1];
String serverID = splitData[1].split("=")[1];
return new DataWork(clientID, serverID.substring(0, serverID.length() -1));
}
注意:最好使用最少的分隔符序列化数据,否则解析会像您的情况一样变得麻烦。此外,它将最大限度地减少 space 所需的存储或传输。
我正在将我的 class 序列化为 bytes
,然后我想在反序列化时从中提取所有内容。我的 class 看起来像这样:
public class DataWork {
private final String clientId;
private final String serverId;
public DataWork(final String clientId, final String serverId) {
this.clientId = clientId;
this.serverId = serverId;
}
public String getClientId() {
return clientId;
}
public String getServerId() {
return serverId;
}
@Override
public String toString() {
return "DataWork [clientId=" + clientId + ", serverId=" + serverId + "]";
}
}
下面是我的序列化程序 class,我在其中将 DataWork 序列化为字节,但我不知道如何在反序列化时从中提取所有内容?一般来说,我想获得完整的 DataWork 对象,同时使用相同的字节对其进行反序列化。
public class DataSerializer implements QueueSerializer<DataWork> {
// here I need to deserialize
public DataWork deserialize(byte[] buffer) {
// don't know what should I do here?
// buffer will have my actual bytes of DataWork
}
// here I am serializing
public byte[] serialize(DataWork work) {
return work.toString().getBytes();
}
}
现在这意味着我需要以这样一种方式对其进行序列化,以便在反序列化时可以正确地从中提取所有内容。
查看您的 toString()
方法实现。它会 return 你以同样的方式字节。
// here I need to deserialize
public DataWork deserialize(byte[] buffer) {
if(null == buffer || buffer.length == 0)
return null;
// reconstruct the string back from bytes.
String data = new String(buffer);
// now just parse the string and create a new object of type DataWork
// with clientID and serverID field values retrieved from the string.
String splitData = data.split(",");
String clientID = splitData[0].split("=")[1];
String serverID = splitData[1].split("=")[1];
return new DataWork(clientID, serverID.substring(0, serverID.length() -1));
}
注意:最好使用最少的分隔符序列化数据,否则解析会像您的情况一样变得麻烦。此外,它将最大限度地减少 space 所需的存储或传输。