@Test(expected = JsonParseException.class) public void testNoAndroidSettings() { mJsonData.remove(APIStatusDeserializer.ANDROID_SETTINGS_TAG); mStatus = mDeserializer.deserialize(mJsonData, ApiStatus.class, mContext); }
@Override public ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Data is not JsonObject"); } JsonObject data = json.getAsJsonObject(); ApiStatus status = new ApiStatus(); JsonElement maxSeason = data.get(MAX_SEASON_TAG); if (maxSeason == null || !maxSeason.isJsonPrimitive()) { throw new JsonParseException("Max Season is not a primitive"); } status.setMaxSeason(maxSeason.getAsInt()); JsonElement currentSeason = data.get(CURRENT_SEASON_TAG); if (currentSeason == null || !currentSeason.isJsonPrimitive()) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); status.setCurrentSeason(currentYear); } else { status.setCurrentSeason(currentSeason.getAsInt()); } int maxSeasonCheck = Math.max(status.getMaxSeason(), status.getCurrentSeason()); status.setMaxSeason(maxSeasonCheck); JsonElement fmsApiDown = data.get(FMS_API_DOWN_TAG); if (fmsApiDown == null || !fmsApiDown.isJsonPrimitive()) { throw new JsonParseException("Is Datafeed Down is not a primitive"); } status.setFmsApiDown(fmsApiDown.getAsBoolean()); JsonElement downEvents = data.get(DOWN_EVENTS_TAG); if (downEvents == null || !downEvents.isJsonArray()) { throw new JsonParseException("Down Events is not an array"); } List<String> downKeys = new ArrayList<>(); for (JsonElement eventKey : downEvents.getAsJsonArray()) { if (!eventKey.isJsonPrimitive()) { continue; } downKeys.add(eventKey.getAsString()); } status.setDownEvents(downKeys); JsonElement androidSpecific = data.get(ANDROID_SETTINGS_TAG); if (androidSpecific == null || !androidSpecific.isJsonObject()) { throw new JsonParseException("No Android specific settings"); } JsonObject androidSettings = androidSpecific.getAsJsonObject(); JsonElement minAppVersion = androidSettings.get(MIN_APP_VERSION_TAG); if (minAppVersion == null || !minAppVersion.isJsonPrimitive()) { throw new JsonParseException("Min App Version not found"); } status.setMinAppVersion(minAppVersion.getAsInt()); JsonElement latestAppVersion = androidSettings.get(LATEST_APP_VERSION_TAG); if (latestAppVersion == null || !latestAppVersion.isJsonPrimitive()) { throw new JsonParseException("Latest app version not found"); } status.setLatestAppVersion(latestAppVersion.getAsInt()); JsonElement message = data.get(MESSAGE_DICT); if (message != null && !message.isJsonNull() && message.isJsonObject()) { JsonObject adminMessage = data.get(MESSAGE_DICT).getAsJsonObject(); JsonElement messageText = adminMessage.get(MESSAGE_TEXT); JsonElement messageExpiration = adminMessage.get(MESSAGE_EXPIRATION); if (messageText == null || messageText.isJsonNull() || messageExpiration == null || messageExpiration.isJsonNull()) { throw new JsonParseException("Message requires text and expiration"); } status.setHasMessage(true); status.setMessageText(messageText.getAsString()); status.setMessageExpiration(new Date(messageExpiration.getAsLong() * MS_PER_SECOND) .getTime()); } else { status.setHasMessage(false); } JsonElement lastCacheClear = data.get(LAST_OKHTTP_CACHE_CLEAR); if (lastCacheClear != null && !lastCacheClear.isJsonNull() && lastCacheClear.isJsonPrimitive()) { JsonPrimitive lastCacheTime = lastCacheClear.getAsJsonPrimitive(); long lastTimestamp = lastCacheTime.getAsLong(); status.setLastOkHttpCacheClear(lastTimestamp); } else { status.setLastOkHttpCacheClear((long) -1); } status.setJsonBlob(json.toString()); return status; }
APIStatusDeserializer implements JsonDeserializer<ApiStatus> { @Override public ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Data is not JsonObject"); } JsonObject data = json.getAsJsonObject(); ApiStatus status = new ApiStatus(); JsonElement maxSeason = data.get(MAX_SEASON_TAG); if (maxSeason == null || !maxSeason.isJsonPrimitive()) { throw new JsonParseException("Max Season is not a primitive"); } status.setMaxSeason(maxSeason.getAsInt()); JsonElement currentSeason = data.get(CURRENT_SEASON_TAG); if (currentSeason == null || !currentSeason.isJsonPrimitive()) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); status.setCurrentSeason(currentYear); } else { status.setCurrentSeason(currentSeason.getAsInt()); } int maxSeasonCheck = Math.max(status.getMaxSeason(), status.getCurrentSeason()); status.setMaxSeason(maxSeasonCheck); JsonElement fmsApiDown = data.get(FMS_API_DOWN_TAG); if (fmsApiDown == null || !fmsApiDown.isJsonPrimitive()) { throw new JsonParseException("Is Datafeed Down is not a primitive"); } status.setFmsApiDown(fmsApiDown.getAsBoolean()); JsonElement downEvents = data.get(DOWN_EVENTS_TAG); if (downEvents == null || !downEvents.isJsonArray()) { throw new JsonParseException("Down Events is not an array"); } List<String> downKeys = new ArrayList<>(); for (JsonElement eventKey : downEvents.getAsJsonArray()) { if (!eventKey.isJsonPrimitive()) { continue; } downKeys.add(eventKey.getAsString()); } status.setDownEvents(downKeys); JsonElement androidSpecific = data.get(ANDROID_SETTINGS_TAG); if (androidSpecific == null || !androidSpecific.isJsonObject()) { throw new JsonParseException("No Android specific settings"); } JsonObject androidSettings = androidSpecific.getAsJsonObject(); JsonElement minAppVersion = androidSettings.get(MIN_APP_VERSION_TAG); if (minAppVersion == null || !minAppVersion.isJsonPrimitive()) { throw new JsonParseException("Min App Version not found"); } status.setMinAppVersion(minAppVersion.getAsInt()); JsonElement latestAppVersion = androidSettings.get(LATEST_APP_VERSION_TAG); if (latestAppVersion == null || !latestAppVersion.isJsonPrimitive()) { throw new JsonParseException("Latest app version not found"); } status.setLatestAppVersion(latestAppVersion.getAsInt()); JsonElement message = data.get(MESSAGE_DICT); if (message != null && !message.isJsonNull() && message.isJsonObject()) { JsonObject adminMessage = data.get(MESSAGE_DICT).getAsJsonObject(); JsonElement messageText = adminMessage.get(MESSAGE_TEXT); JsonElement messageExpiration = adminMessage.get(MESSAGE_EXPIRATION); if (messageText == null || messageText.isJsonNull() || messageExpiration == null || messageExpiration.isJsonNull()) { throw new JsonParseException("Message requires text and expiration"); } status.setHasMessage(true); status.setMessageText(messageText.getAsString()); status.setMessageExpiration(new Date(messageExpiration.getAsLong() * MS_PER_SECOND) .getTime()); } else { status.setHasMessage(false); } JsonElement lastCacheClear = data.get(LAST_OKHTTP_CACHE_CLEAR); if (lastCacheClear != null && !lastCacheClear.isJsonNull() && lastCacheClear.isJsonPrimitive()) { JsonPrimitive lastCacheTime = lastCacheClear.getAsJsonPrimitive(); long lastTimestamp = lastCacheTime.getAsLong(); status.setLastOkHttpCacheClear(lastTimestamp); } else { status.setLastOkHttpCacheClear((long) -1); } status.setJsonBlob(json.toString()); return status; } }
APIStatusDeserializer implements JsonDeserializer<ApiStatus> { @Override public ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Data is not JsonObject"); } JsonObject data = json.getAsJsonObject(); ApiStatus status = new ApiStatus(); JsonElement maxSeason = data.get(MAX_SEASON_TAG); if (maxSeason == null || !maxSeason.isJsonPrimitive()) { throw new JsonParseException("Max Season is not a primitive"); } status.setMaxSeason(maxSeason.getAsInt()); JsonElement currentSeason = data.get(CURRENT_SEASON_TAG); if (currentSeason == null || !currentSeason.isJsonPrimitive()) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); status.setCurrentSeason(currentYear); } else { status.setCurrentSeason(currentSeason.getAsInt()); } int maxSeasonCheck = Math.max(status.getMaxSeason(), status.getCurrentSeason()); status.setMaxSeason(maxSeasonCheck); JsonElement fmsApiDown = data.get(FMS_API_DOWN_TAG); if (fmsApiDown == null || !fmsApiDown.isJsonPrimitive()) { throw new JsonParseException("Is Datafeed Down is not a primitive"); } status.setFmsApiDown(fmsApiDown.getAsBoolean()); JsonElement downEvents = data.get(DOWN_EVENTS_TAG); if (downEvents == null || !downEvents.isJsonArray()) { throw new JsonParseException("Down Events is not an array"); } List<String> downKeys = new ArrayList<>(); for (JsonElement eventKey : downEvents.getAsJsonArray()) { if (!eventKey.isJsonPrimitive()) { continue; } downKeys.add(eventKey.getAsString()); } status.setDownEvents(downKeys); JsonElement androidSpecific = data.get(ANDROID_SETTINGS_TAG); if (androidSpecific == null || !androidSpecific.isJsonObject()) { throw new JsonParseException("No Android specific settings"); } JsonObject androidSettings = androidSpecific.getAsJsonObject(); JsonElement minAppVersion = androidSettings.get(MIN_APP_VERSION_TAG); if (minAppVersion == null || !minAppVersion.isJsonPrimitive()) { throw new JsonParseException("Min App Version not found"); } status.setMinAppVersion(minAppVersion.getAsInt()); JsonElement latestAppVersion = androidSettings.get(LATEST_APP_VERSION_TAG); if (latestAppVersion == null || !latestAppVersion.isJsonPrimitive()) { throw new JsonParseException("Latest app version not found"); } status.setLatestAppVersion(latestAppVersion.getAsInt()); JsonElement message = data.get(MESSAGE_DICT); if (message != null && !message.isJsonNull() && message.isJsonObject()) { JsonObject adminMessage = data.get(MESSAGE_DICT).getAsJsonObject(); JsonElement messageText = adminMessage.get(MESSAGE_TEXT); JsonElement messageExpiration = adminMessage.get(MESSAGE_EXPIRATION); if (messageText == null || messageText.isJsonNull() || messageExpiration == null || messageExpiration.isJsonNull()) { throw new JsonParseException("Message requires text and expiration"); } status.setHasMessage(true); status.setMessageText(messageText.getAsString()); status.setMessageExpiration(new Date(messageExpiration.getAsLong() * MS_PER_SECOND) .getTime()); } else { status.setHasMessage(false); } JsonElement lastCacheClear = data.get(LAST_OKHTTP_CACHE_CLEAR); if (lastCacheClear != null && !lastCacheClear.isJsonNull() && lastCacheClear.isJsonPrimitive()) { JsonPrimitive lastCacheTime = lastCacheClear.getAsJsonPrimitive(); long lastTimestamp = lastCacheTime.getAsLong(); status.setLastOkHttpCacheClear(lastTimestamp); } else { status.setLastOkHttpCacheClear((long) -1); } status.setJsonBlob(json.toString()); return status; }  }
APIStatusDeserializer implements JsonDeserializer<ApiStatus> { @Override public ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Data is not JsonObject"); } JsonObject data = json.getAsJsonObject(); ApiStatus status = new ApiStatus(); JsonElement maxSeason = data.get(MAX_SEASON_TAG); if (maxSeason == null || !maxSeason.isJsonPrimitive()) { throw new JsonParseException("Max Season is not a primitive"); } status.setMaxSeason(maxSeason.getAsInt()); JsonElement currentSeason = data.get(CURRENT_SEASON_TAG); if (currentSeason == null || !currentSeason.isJsonPrimitive()) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); status.setCurrentSeason(currentYear); } else { status.setCurrentSeason(currentSeason.getAsInt()); } int maxSeasonCheck = Math.max(status.getMaxSeason(), status.getCurrentSeason()); status.setMaxSeason(maxSeasonCheck); JsonElement fmsApiDown = data.get(FMS_API_DOWN_TAG); if (fmsApiDown == null || !fmsApiDown.isJsonPrimitive()) { throw new JsonParseException("Is Datafeed Down is not a primitive"); } status.setFmsApiDown(fmsApiDown.getAsBoolean()); JsonElement downEvents = data.get(DOWN_EVENTS_TAG); if (downEvents == null || !downEvents.isJsonArray()) { throw new JsonParseException("Down Events is not an array"); } List<String> downKeys = new ArrayList<>(); for (JsonElement eventKey : downEvents.getAsJsonArray()) { if (!eventKey.isJsonPrimitive()) { continue; } downKeys.add(eventKey.getAsString()); } status.setDownEvents(downKeys); JsonElement androidSpecific = data.get(ANDROID_SETTINGS_TAG); if (androidSpecific == null || !androidSpecific.isJsonObject()) { throw new JsonParseException("No Android specific settings"); } JsonObject androidSettings = androidSpecific.getAsJsonObject(); JsonElement minAppVersion = androidSettings.get(MIN_APP_VERSION_TAG); if (minAppVersion == null || !minAppVersion.isJsonPrimitive()) { throw new JsonParseException("Min App Version not found"); } status.setMinAppVersion(minAppVersion.getAsInt()); JsonElement latestAppVersion = androidSettings.get(LATEST_APP_VERSION_TAG); if (latestAppVersion == null || !latestAppVersion.isJsonPrimitive()) { throw new JsonParseException("Latest app version not found"); } status.setLatestAppVersion(latestAppVersion.getAsInt()); JsonElement message = data.get(MESSAGE_DICT); if (message != null && !message.isJsonNull() && message.isJsonObject()) { JsonObject adminMessage = data.get(MESSAGE_DICT).getAsJsonObject(); JsonElement messageText = adminMessage.get(MESSAGE_TEXT); JsonElement messageExpiration = adminMessage.get(MESSAGE_EXPIRATION); if (messageText == null || messageText.isJsonNull() || messageExpiration == null || messageExpiration.isJsonNull()) { throw new JsonParseException("Message requires text and expiration"); } status.setHasMessage(true); status.setMessageText(messageText.getAsString()); status.setMessageExpiration(new Date(messageExpiration.getAsLong() * MS_PER_SECOND) .getTime()); } else { status.setHasMessage(false); } JsonElement lastCacheClear = data.get(LAST_OKHTTP_CACHE_CLEAR); if (lastCacheClear != null && !lastCacheClear.isJsonNull() && lastCacheClear.isJsonPrimitive()) { JsonPrimitive lastCacheTime = lastCacheClear.getAsJsonPrimitive(); long lastTimestamp = lastCacheTime.getAsLong(); status.setLastOkHttpCacheClear(lastTimestamp); } else { status.setLastOkHttpCacheClear((long) -1); } status.setJsonBlob(json.toString()); return status; }  @Override ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); }
APIStatusDeserializer implements JsonDeserializer<ApiStatus> { @Override public ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Data is not JsonObject"); } JsonObject data = json.getAsJsonObject(); ApiStatus status = new ApiStatus(); JsonElement maxSeason = data.get(MAX_SEASON_TAG); if (maxSeason == null || !maxSeason.isJsonPrimitive()) { throw new JsonParseException("Max Season is not a primitive"); } status.setMaxSeason(maxSeason.getAsInt()); JsonElement currentSeason = data.get(CURRENT_SEASON_TAG); if (currentSeason == null || !currentSeason.isJsonPrimitive()) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); status.setCurrentSeason(currentYear); } else { status.setCurrentSeason(currentSeason.getAsInt()); } int maxSeasonCheck = Math.max(status.getMaxSeason(), status.getCurrentSeason()); status.setMaxSeason(maxSeasonCheck); JsonElement fmsApiDown = data.get(FMS_API_DOWN_TAG); if (fmsApiDown == null || !fmsApiDown.isJsonPrimitive()) { throw new JsonParseException("Is Datafeed Down is not a primitive"); } status.setFmsApiDown(fmsApiDown.getAsBoolean()); JsonElement downEvents = data.get(DOWN_EVENTS_TAG); if (downEvents == null || !downEvents.isJsonArray()) { throw new JsonParseException("Down Events is not an array"); } List<String> downKeys = new ArrayList<>(); for (JsonElement eventKey : downEvents.getAsJsonArray()) { if (!eventKey.isJsonPrimitive()) { continue; } downKeys.add(eventKey.getAsString()); } status.setDownEvents(downKeys); JsonElement androidSpecific = data.get(ANDROID_SETTINGS_TAG); if (androidSpecific == null || !androidSpecific.isJsonObject()) { throw new JsonParseException("No Android specific settings"); } JsonObject androidSettings = androidSpecific.getAsJsonObject(); JsonElement minAppVersion = androidSettings.get(MIN_APP_VERSION_TAG); if (minAppVersion == null || !minAppVersion.isJsonPrimitive()) { throw new JsonParseException("Min App Version not found"); } status.setMinAppVersion(minAppVersion.getAsInt()); JsonElement latestAppVersion = androidSettings.get(LATEST_APP_VERSION_TAG); if (latestAppVersion == null || !latestAppVersion.isJsonPrimitive()) { throw new JsonParseException("Latest app version not found"); } status.setLatestAppVersion(latestAppVersion.getAsInt()); JsonElement message = data.get(MESSAGE_DICT); if (message != null && !message.isJsonNull() && message.isJsonObject()) { JsonObject adminMessage = data.get(MESSAGE_DICT).getAsJsonObject(); JsonElement messageText = adminMessage.get(MESSAGE_TEXT); JsonElement messageExpiration = adminMessage.get(MESSAGE_EXPIRATION); if (messageText == null || messageText.isJsonNull() || messageExpiration == null || messageExpiration.isJsonNull()) { throw new JsonParseException("Message requires text and expiration"); } status.setHasMessage(true); status.setMessageText(messageText.getAsString()); status.setMessageExpiration(new Date(messageExpiration.getAsLong() * MS_PER_SECOND) .getTime()); } else { status.setHasMessage(false); } JsonElement lastCacheClear = data.get(LAST_OKHTTP_CACHE_CLEAR); if (lastCacheClear != null && !lastCacheClear.isJsonNull() && lastCacheClear.isJsonPrimitive()) { JsonPrimitive lastCacheTime = lastCacheClear.getAsJsonPrimitive(); long lastTimestamp = lastCacheTime.getAsLong(); status.setLastOkHttpCacheClear(lastTimestamp); } else { status.setLastOkHttpCacheClear((long) -1); } status.setJsonBlob(json.toString()); return status; }  @Override ApiStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context);  }