Coverage Summary for Class: IdsAdapter (cloud.mindbox.mobile_sdk.models.operation.adapters)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| IdsAdapter |
100%
(3/3)
|
50%
(1/2)
|
100%
(5/5)
|
95%
(38/40)
|
| IdsAdapter$gson$2 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(3/3)
|
| IdsAdapter$read$1$1 |
100%
(1/1)
|
66.7%
(4/6)
|
80%
(8/10)
|
90.9%
(40/44)
|
| IdsAdapter$read$1$1$1 |
100%
(1/1)
|
|
100%
(3/3)
|
100%
(16/16)
|
| IdsAdapter$write$1 |
100%
(1/1)
|
33.3%
(2/6)
|
66.7%
(2/3)
|
59.3%
(16/27)
|
| Total |
100%
(7/7)
|
50%
(7/14)
|
86.4%
(19/22)
|
86.9%
(113/130)
|
package cloud.mindbox.mobile_sdk.models.operation.adapters
import cloud.mindbox.mobile_sdk.models.operation.Ids
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
internal class IdsAdapter : TypeAdapter<Ids?>() {
private val gson by lazy { Gson() }
override fun write(out: JsonWriter?, value: Ids?) {
LoggingExceptionHandler.runCatching {
if (value == null) {
out?.nullValue()
} else {
out?.jsonValue(gson.toJson(value.ids))
}
}
}
override fun read(`in`: JsonReader?): Ids? = `in`?.let { reader ->
LoggingExceptionHandler.runCatching(defaultValue = null) {
if (reader.peek() === JsonToken.NULL) {
reader.nextNull()
return@runCatching null
}
// Workaround for case when id value is handled by gson as Double, not as String or Int
// We manually parse value as String
val ids = mutableMapOf<String, String?>()
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
reader.beginObject()
while (reader.peek() != JsonToken.END_OBJECT) {
LoggingExceptionHandler.runCatching {
val key = reader.nextName()
val valueString = reader.nextString()
ids[key] = valueString
}
}
reader.endObject()
}
Ids(ids)
}
}
}