-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathWebhookDeserializer.java
More file actions
42 lines (36 loc) · 1.44 KB
/
WebhookDeserializer.java
File metadata and controls
42 lines (36 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.easypost.model;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.easypost.Constants;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
public final class WebhookDeserializer implements JsonDeserializer<Webhook[]> {
/**
* Deserialize a list of Webhook from a JSON object.
*
* @param json JSON object to deserialize.
* @param typeOfT Type of the object to deserialize.
* @param context Deserialization context.
* @return Deserialized Webhook object.
* @throws JsonParseException if the JSON object is not a valid Webhook.
*/
@Override
public Webhook[] deserialize(final JsonElement json, final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {
JsonObject jo = json.getAsJsonObject();
JsonElement results = jo.get("webhooks");
if (results == null || !results.isJsonArray()) {
return new Webhook[0];
}
List<Webhook> webhooksList = new ArrayList<>();
for (JsonElement element : results.getAsJsonArray()) {
Webhook webhook = Constants.Http.GSON.fromJson(element, Webhook.class);
webhooksList.add(webhook);
}
return webhooksList.toArray(new Webhook[0]);
}
}