Search in sources :

Example 1 with MatchAlliancesContainer

use of com.thebluealliance.androidclient.models.MatchAlliancesContainer in project the-blue-alliance-android by the-blue-alliance.

the class MatchAllianceDeserializer method deserialize.

@Override
public MatchAlliancesContainer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    MatchAlliancesContainer alliances = new MatchAlliancesContainer();
    JsonObject data = json.getAsJsonObject();
    if (!isNull(data.get("red"))) {
        JsonObject redAlliance = data.getAsJsonObject("red");
        MatchAlliancesContainer.MatchAlliance red = new MatchAlliancesContainer.MatchAlliance();
        red.setScore(redAlliance.get("score").getAsInt());
        red.setTeamKeys(context.deserialize(redAlliance.get("team_keys"), new TypeToken<List<String>>() {
        }.getType()));
        if (red.getTeamKeys() == null || red.getTeamKeys().isEmpty()) {
            // Fall back to apiv2 format so push notifications don't break
            red.setTeamKeys(context.deserialize(redAlliance.get("teams"), new TypeToken<List<String>>() {
            }.getType()));
        }
        if (!isNull(data.get("surrogate_team_keys"))) {
            red.setSurrogateTeamKeys(context.deserialize(redAlliance.get("surrogate_team_keys"), new TypeToken<List<String>>() {
            }.getType()));
        }
        alliances.setRed(red);
    }
    if (!isNull(data.get("blue"))) {
        JsonObject blueAlliance = data.getAsJsonObject("blue");
        MatchAlliancesContainer.MatchAlliance blue = new MatchAlliancesContainer.MatchAlliance();
        blue.setScore(blueAlliance.get("score").getAsInt());
        blue.setTeamKeys(context.deserialize(blueAlliance.get("team_keys"), new TypeToken<List<String>>() {
        }.getType()));
        if (blue.getTeamKeys() == null || blue.getTeamKeys().isEmpty()) {
            // Fall back to apiv2 format so push notifications don't break
            blue.setTeamKeys(context.deserialize(blueAlliance.get("teams"), new TypeToken<List<String>>() {
            }.getType()));
        }
        if (!isNull(data.get("surrogate_team_keys"))) {
            blue.setSurrogateTeamKeys(context.deserialize(blueAlliance.get("surrogate_team_keys"), new TypeToken<List<String>>() {
            }.getType()));
        }
        alliances.setBlue(blue);
    }
    return alliances;
}
Also used : JsonObject(com.google.gson.JsonObject) List(java.util.List) MatchAlliancesContainer(com.thebluealliance.androidclient.models.MatchAlliancesContainer)

Aggregations

JsonObject (com.google.gson.JsonObject)1 MatchAlliancesContainer (com.thebluealliance.androidclient.models.MatchAlliancesContainer)1 List (java.util.List)1