Ho una classe Java, l'utente:Come si scrive un deserializzatore JSON personalizzato per Gson?
public class User
{
int id;
String name;
Timestamp updateDate;
}
e ricevo una lista JSON contenente oggetti utente da un webservice:
[{"id":1,"name":"Jonas","update_date":"1300962900226"},
{"id":5,"name":"Test","date_date":"1304782298024"}]
Ho cercato di scrivere un deserializzatore personalizzato:
@Override
public User deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
return new User(
json.getAsJsonPrimitive().getAsInt(),
json.getAsString(),
json.getAsInt(),
(Timestamp)context.deserialize(json.getAsJsonPrimitive(),
Timestamp.class));
}
Ma il mio deserializzatore non funziona. Come posso scrivere un deserializzatore JSON personalizzato per Gson?
Questo esempio aiuto a tutti? http://stackoverflow.com/questions/5845822/gson-deserializing-key-value-to-custom-object –
Cosa intendi per "non funziona"? Qual è il problema? – ColinD
"date_date" deve essere "update_date"? –