Coverage Summary for Class: DateOnlyAdapter (cloud.mindbox.mobile_sdk.models.operation.adapters)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| DateOnlyAdapter |
100%
(3/3)
|
50%
(1/2)
|
100%
(5/5)
|
94.6%
(35/37)
|
| DateOnlyAdapter$read$1$1 |
100%
(1/1)
|
66.7%
(4/6)
|
100%
(4/4)
|
89.5%
(34/38)
|
| DateOnlyAdapter$write$1 |
100%
(1/1)
|
66.7%
(4/6)
|
100%
(3/3)
|
81.5%
(22/27)
|
| Total |
100%
(5/5)
|
64.3%
(9/14)
|
100%
(12/12)
|
89.2%
(91/102)
|
package cloud.mindbox.mobile_sdk.models.operation.adapters
import cloud.mindbox.mobile_sdk.models.operation.DateOnly
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import java.text.SimpleDateFormat
import java.util.Locale
internal class DateOnlyAdapter : TypeAdapter<DateOnly>() {
private val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
override fun write(out: JsonWriter?, value: DateOnly?) {
LoggingExceptionHandler.runCatching {
if (value == null) {
out?.nullValue()
} else {
out?.value(formatter.format(value))
}
}
}
override fun read(`in`: JsonReader?): DateOnly? = `in`?.let { reader ->
LoggingExceptionHandler.runCatching(defaultValue = null) {
if (reader.peek() === JsonToken.NULL) {
reader.nextNull()
return@runCatching null
}
reader.nextString()?.let { formatter.parse(it)?.time?.let(::DateOnly) }
}
}
}