-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathSmartRateCollectionDeserializer.java
More file actions
43 lines (37 loc) · 1.68 KB
/
SmartRateCollectionDeserializer.java
File metadata and controls
43 lines (37 loc) · 1.68 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
43
package com.easypost.model;
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;
import java.lang.reflect.Type;
public final class SmartRateCollectionDeserializer implements JsonDeserializer<SmartRateCollection> {
/**
* Deserialize a SmartRateCollection from a JSON object.
*
* @param json JSON object to deserialize.
* @param typeOfT Type of the object to deserialize.
* @param context Deserialization context.
* @return Deserialized SmartRateCollection object.
* @throws JsonParseException if the JSON object is not a valid
* SmartRateCollection.
*/
@Override
public SmartRateCollection deserialize(final JsonElement json, final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {
SmartRateCollection smartrateCollection = new SmartRateCollection();
JsonObject jo = (JsonObject) json;
JsonElement results = jo.get("result");
if (results == null || !results.isJsonArray()) {
return smartrateCollection;
// return empty collection if "results" key does not exist or corresponding
// value is not an array
}
// the JsonDeserializationContext should have access to the other type adapters,
// so we can tap into the RateDeserializer from here
results.getAsJsonArray().forEach(rateData -> {
smartrateCollection.addRate(context.deserialize(rateData, SmartRate.class));
});
return smartrateCollection;
}
}