[英]How to deserialize a subclass with Gson?
I have a Message
class and a HistoryMessage
class which extends Message
. 我有一个
Message
类和一个扩展Message
的HistoryMessage
类。 How I can deserialize HistoryMessage
using Gson? 如何使用Gson反序列化
HistoryMessage
?
public class Message
{
@SerializedName("id")
private int id;
@SerializedName("date")
private long date;
@SerializedName("out")
private int out;
@SerializedName("user_id")
private int userId;
public Message(int id, long date, int userId, int out)
{
this.id = id;
this.date = date;
this.userId = userId;
this.out = out;
}
}
public class MessageHistory extends Message
{
@SerializedName("from_id")
private int fromId;
public MessageHistory(int id, long date, int userId, int out, int fromId)
{
super(id, date, userId, out);
this.fromId = fromId;
}
}
And I have class - Dialog, that is container for all messages 我有课-对话框,是所有消息的容器
public class Dialog {
private int count;
private ArrayList<Message> dialogs = new ArrayList<Message>();
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ArrayList<Message> getDialogs() {
return dialogs;
}
public void setDialogs(Message dialogs) {
this.dialogs.add(dialogs);
}
}
and deserializer for Message
, but I don't know how I can deserialize a HistoryMessage
. 和
Message
反序列化器,但是我不知道如何反序列化HistoryMessage
。
public Dialog deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Dialog dialog = new Dialog();
JsonArray jsonArray = (JsonArray)json.getAsJsonObject().get("response").getAsJsonObject().get("items");
int count = (Integer)json.getAsJsonObject().get("response").getAsJsonObject().get("count").getAsInt();
for ( int i = 0; i < jsonArray.size(); i++ ) {
JsonElement jsonElement = jsonArray.get(i);
Message message = new Message();
boolean unread = false;
if ( jsonElement.getAsJsonObject().has("unread")){
unread = true;
}
JsonObject object = (JsonObject)jsonElement.getAsJsonObject().get("message");
message = context.deserialize(object, Message.class);
message.setUnread(unread);
dialog.setDialogs(message);
}
dialog.setCount(count);
return dialog;
}
To use Gson you need to specify which type you want to deserialize into. 要使用Gson,您需要指定要反序列化为哪种类型。 If you want to construct a
HistoryMessage
instance, you need to pass HistoryMessage.class
to your fromJson()
call. 如果要构造一个
HistoryMessage
实例,则需要将HistoryMessage.class
传递给fromJson()
调用。 If you don't know what type you'll be trying to construct at compile time, you can hack around it by storing the type in the JSON like this answer describes: How to serialize a class with an interface? 如果您不知道在编译时将尝试构造什么类型,则可以通过将类型存储在JSON中来解决该问题,如此答案所述: 如何使用接口序列化类?
That's definitely not a best practice though. 但这绝对不是最佳实践。 Needing to do this suggests you may be trying to deserialize into types that are too complex.
需要执行此操作表明您可能正在尝试反序列化为过于复杂的类型。 Consider splitting your deserializing code from your business logic, eg deserialize your JSON into an intermediary class, then construct your
HistoryMessage
from that class, rather than directly from the JSON. 考虑将反序列化代码与业务逻辑分开,例如,将JSON反序列化为中介类,然后从该类而不是直接从JSON构造
HistoryMessage
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.