如何为GSON编写自定义JSON反序列化器?

如何为GSON编写自定义JSON反序列化器?

我有一个Java类,用户:

public class User{
    int id;
    String name;
    Timestamp updateDate;}

我收到一个包含来自webservice的用户对象的JSON列表:

[{"id":1,"name":"Jonas","update_date":"1300962900226"},{"id":5,"name":"Test","date_date":"1304782298024"}]

我试图编写一个自定义反序列化器:

@Overridepublic 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));}

但我的反序列化器不起作用。如何为GSON编写自定义JSON反序列化程序?


偶然的你
浏览 669回答 3
3回答

红糖糍粑

@Overridepublic User deserialize(JsonElement json, Type type,         JsonDeserializationContext context) throws JsonParseException {     JsonObject jobject = json.getAsJsonObject();     return new User(             jobject.get("id").getAsInt(),              jobject.get("name").getAsString(),              new Timestamp(jobject.get("update_date").getAsLong()));}我假设User类具有适当的构造函数。

白板的微信

我将采取以下略有不同的方法,以便将代码中的“手动”解析最小化,因为不必要地进行其他操作,这在某种程度上违背了我最初使用像gson这样的API的目的。//&nbsp;output://&nbsp;[User:&nbsp;id=1,&nbsp;name=Jonas,&nbsp;updateDate=2011-03-24&nbsp;03:35:00.226]//&nbsp;[User:&nbsp;id=5,&nbsp;name=Test,&nbsp;updateDate=2011-05-07&nbsp;08:31:38.024] //&nbsp;using&nbsp;java.sql.Timestamppublic&nbsp;class&nbsp;Foo{ &nbsp;&nbsp;static&nbsp;String&nbsp;jsonInput&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;"["&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"{\"id\":1,\"name\":\"Jonas\",\"update_date\":\"1300962900226\"},"&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"{\"id\":5,\"name\":\"Test\",\"update_date\":\"1304782298024\"}"&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;"]"; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;GsonBuilder&nbsp;gsonBuilder&nbsp;=&nbsp;new&nbsp;GsonBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); &nbsp;&nbsp;&nbsp;&nbsp;gsonBuilder.registerTypeAdapter(Timestamp.class,&nbsp;new&nbsp;TimestampDeserializer()); &nbsp;&nbsp;&nbsp;&nbsp;Gson&nbsp;gson&nbsp;=&nbsp;gsonBuilder.create(); &nbsp;&nbsp;&nbsp;&nbsp;User[]&nbsp;users&nbsp;=&nbsp;gson.fromJson(jsonInput,&nbsp;User[].class); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(User&nbsp;user&nbsp;:&nbsp;users) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(user); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}}class&nbsp;User{ &nbsp;&nbsp;int&nbsp;id; &nbsp;&nbsp;String&nbsp;name; &nbsp;&nbsp;Timestamp&nbsp;updateDate; &nbsp;&nbsp;@Override &nbsp;&nbsp;public&nbsp;String&nbsp;toString() &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;String.format( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"[User:&nbsp;id=%1$d,&nbsp;name=%2$s,&nbsp;updateDate=%3$s]", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id,&nbsp;name,&nbsp;updateDate); &nbsp;&nbsp;}}class&nbsp;TimestampDeserializer&nbsp;implements&nbsp;JsonDeserializer<Timestamp>{ &nbsp;&nbsp;@Override &nbsp;&nbsp;public&nbsp;Timestamp&nbsp;deserialize(JsonElement&nbsp;json,&nbsp;Type&nbsp;typeOfT,&nbsp;JsonDeserializationContext&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws&nbsp;JsonParseException &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;time&nbsp;=&nbsp;Long.parseLong(json.getAsString()); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Timestamp(time); &nbsp;&nbsp;}}(这假定原始问题中的“Date_Date”应该是“UPDATE_DATE”)。

aluckdog

我肯定更喜欢这个模型-如果时间戳是唯一不能解析的东西,最好是让时间戳反序列化,而不是手动解析整个用户对象,因为gson已经完全能够这样做了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java