All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.wisp.feature.MoshiExt.kt Maven / Gradle / Ivy

The newest version!
package wisp.feature

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonDataException
import mu.KotlinLogging

private val logger = KotlinLogging.logger(FeatureFlags::class.qualifiedName!!)

/**
 * Attempts to use [JsonAdapter.failOnUnknown] and logs any issues before falling back to ignoring
 * the unknown fields.
 *
 * This overload is needed for JVM compatibility.
 */
fun  JsonAdapter.toSafeJson(value: T): String = toSafeJson(value) {
    logger.warn(it) {
        "failed to parse JSON due to unknown fields. ignoring those fields and trying again"
    }
}

/**
 * Attempts to use [JsonAdapter.failOnUnknown] and logs any issues before falling back to ignoring
 * the unknown fields.
 */
fun  JsonAdapter.toSafeJson(value: T, onUnknownFields: (JsonDataException) -> Unit): String {
    return try {
        failOnUnknown().toJson(value)
    } catch (e: JsonDataException) {
        onUnknownFields(e)
        return toJson(value)
    }
}

/**
 * Attempts to use [JsonAdapter.failOnUnknown] and logs any issues before falling back to ignoring
 * the unknown fields.
 *
 * This overload is needed for JVM compatibility.
 */
fun  JsonAdapter.fromSafeJson(value: String): T? = fromSafeJson(value) {
    logger.warn(it) {
        "failed to parse JSON due to unknown fields. ignoring those fields and trying again"
    }
}

/**
 * Attempts to use [JsonAdapter.failOnUnknown] and logs any issues before falling back to ignoring
 * the unknown fields.
 */
fun  JsonAdapter.fromSafeJson(json: String, onUnknownFields: (JsonDataException) -> Unit): T? {
    return try {
        failOnUnknown().fromJson(json)
    } catch (e: JsonDataException) {
        onUnknownFields(e)
        return fromJson(json)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy