That's what I "tried" writing here: Adapter that f...
# squarelibraries
c
That's what I "tried" writing here: Adapter that filters nulls for everything. It just modified Zacs answer in the github link above: https://stackoverflow.com/questions/60377758/moshi-factory-to-ignore-null-values-and-use-kotlin-default-value-when-deserializ
Copy code
class DefaultIfNullFactory : JsonAdapter.Factory {
    override fun create(type: Type, annotations: MutableSet<out Annotation>,
                        moshi: Moshi): JsonAdapter<*>? {
        val delegate = moshi.nextAdapter<Any>(this, type, annotations)
        return object : JsonAdapter<Any>() {
            override fun fromJson(reader: JsonReader): Any? {
                    val blob1 = reader.readJsonValue()
                try {
                    val blob = blob1 as Map<String, Any?>
                    val noNulls = blob.filterValues { it != null }
                    return delegate.fromJsonValue(noNulls)
                } catch (e: Exception) {
                    return delegate.fromJsonValue(blob1)
                }
            }
            override fun toJson(writer: JsonWriter, value: Any?) {
                return delegate.toJson(writer, value)
            }
        }
    }
}
Which actually seems to work great (not a fan of the try statement), but it still has that interesting issue where while it filters out nulls, it turns Json numbers in Java doubles, and so when
1583674200000
comes in over the wire, my data class has
timestamp: String
and so the end result is "1.5836742E12" instead of "1583674200000". So it "appears" that it doesn't work. Unless there's some other way to filter nulls besides Zacs advice of reading jsonValue as map, filter nulls, pass it along the chain.