package com.fireworksdk.bridge.models import org.json.JSONObject object FWAdConfigurationDeserializer { private const val VAST_ATTRIBUTES_KEY = "vastAttributes" private const val REQUIRES_ADS_KEY = "requiresAds" private const val ADS_FETCH_TIMEOUT_KEY = "adsFetchTimeout" private const val NAME_KEY = "name" private const val VALUE_KEY = "value" fun deserialize(responseJson: JSONObject?): FWAdConfigurationModel? { responseJson ?: return null val vastAttributesJsonArray = if (responseJson.has(VAST_ATTRIBUTES_KEY)) responseJson.optJSONArray(VAST_ATTRIBUTES_KEY) else null var vastAttributes: MutableList? = null if (vastAttributesJsonArray != null) { vastAttributes = mutableListOf() (0 until vastAttributesJsonArray.length()).forEach { val item = vastAttributesJsonArray.getJSONObject(it) val vastAttribute = deserializeVastAttribute(item) if (vastAttribute != null) { vastAttributes.add(vastAttribute) } } } val requiresAds = responseJson.optBoolean(REQUIRES_ADS_KEY, false) val adsFetchTimeout = if (!responseJson.isNull(ADS_FETCH_TIMEOUT_KEY)) responseJson.optInt(ADS_FETCH_TIMEOUT_KEY) else null return FWAdConfigurationModel( vastAttributes = vastAttributes, requiresAds = requiresAds, adsFetchTimeout = adsFetchTimeout, ) } private fun deserializeVastAttribute(responseJson: JSONObject?): FWAdConfigurationModel.FWVastAttributeModel? { responseJson ?: return null val name = if (responseJson.has(NAME_KEY)) responseJson.optString(NAME_KEY) else null val value = if (responseJson.has(VALUE_KEY)) responseJson.optString(VALUE_KEY) else null return FWAdConfigurationModel.FWVastAttributeModel( name = name, value =value ) } }