Fix date deserializer parsing issue due to no thread safety
This commit is contained in:
parent
1f30f93e77
commit
165eb3da5a
5 changed files with 7 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
|||
package io.github.wulkanowy.api
|
||||
|
||||
import org.jsoup.Jsoup.parse
|
||||
import org.threeten.bp.DateTimeUtils
|
||||
import org.threeten.bp.DayOfWeek.MONDAY
|
||||
import org.threeten.bp.Instant.ofEpochMilli
|
||||
import org.threeten.bp.LocalDate
|
||||
|
@ -8,7 +9,6 @@ import org.threeten.bp.LocalDateTime
|
|||
import org.threeten.bp.ZoneId.systemDefault
|
||||
import org.threeten.bp.format.DateTimeFormatter.ofPattern
|
||||
import org.threeten.bp.temporal.TemporalAdjusters.previousOrSame
|
||||
import java.sql.Date.valueOf
|
||||
import java.text.Normalizer
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
@ -17,7 +17,7 @@ fun String.toDate(format: String): Date = SimpleDateFormat(format).parse(this)
|
|||
|
||||
fun Date.toLocalDate(): LocalDate = ofEpochMilli(time).atZone(systemDefault()).toLocalDate()
|
||||
|
||||
fun LocalDate.toDate(): Date = valueOf(format(ofPattern("yyyy-MM-dd")))
|
||||
fun LocalDate.toDate(): Date = DateTimeUtils.toDate(atStartOfDay(systemDefault()).toInstant())
|
||||
|
||||
fun LocalDate.toFormat(format: String): String = format(ofPattern(format))
|
||||
|
||||
|
|
|
@ -4,18 +4,18 @@ import com.google.gson.JsonDeserializationContext
|
|||
import com.google.gson.JsonDeserializer
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonParseException
|
||||
import io.github.wulkanowy.api.toDate
|
||||
import java.lang.reflect.Type
|
||||
import java.text.ParseException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
||||
class DateDeserializer<T : Date>(private val mSimpleDateFormat: SimpleDateFormat, private val mClazz: Class<T>) : JsonDeserializer<T> {
|
||||
class DateDeserializer<T : Date>(private val mClazz: Class<T>) : JsonDeserializer<T> {
|
||||
|
||||
override fun deserialize(element: JsonElement, arg1: Type, context: JsonDeserializationContext): T {
|
||||
val dateString = element.asString
|
||||
try {
|
||||
return mClazz.newInstance().apply {
|
||||
time = mSimpleDateFormat.parse(dateString).time
|
||||
time = dateString.toDate(GradeDate.FORMAT).time
|
||||
}
|
||||
} catch (e: InstantiationException) {
|
||||
throw JsonParseException(e.message, e)
|
||||
|
|
|
@ -3,7 +3,6 @@ package io.github.wulkanowy.api.grades
|
|||
import com.google.gson.annotations.SerializedName
|
||||
import pl.droidsonroids.jspoon.annotation.Format
|
||||
import pl.droidsonroids.jspoon.annotation.Selector
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
||||
class Grade {
|
||||
|
@ -58,6 +57,5 @@ class Grade {
|
|||
class GradeDate : Date() {
|
||||
companion object {
|
||||
const val FORMAT = "dd.MM.yyyy"
|
||||
val DATE_FORMAT = SimpleDateFormat(FORMAT)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,7 +126,7 @@ class ServiceManager(
|
|||
.addConverterFactory(if (gson) GsonConverterFactory.create(GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd HH:mm:ss")
|
||||
.serializeNulls()
|
||||
.registerTypeAdapter(GradeDate::class.java, DateDeserializer(GradeDate.DATE_FORMAT, GradeDate::class.java))
|
||||
.registerTypeAdapter(GradeDate::class.java, DateDeserializer(GradeDate::class.java))
|
||||
.create()) else JspoonConverterFactory.create())
|
||||
.addCallAdapterFactory(if (!login) RxJava2CallAdapterFactory.create() else
|
||||
RxJava2ReauthCallAdapterFactory.create(
|
||||
|
|
|
@ -62,7 +62,7 @@ abstract class BaseLocalTest : BaseTest() {
|
|||
.addConverterFactory(if (!html) GsonConverterFactory.create(GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd HH:mm:ss")
|
||||
.serializeNulls()
|
||||
.registerTypeAdapter(GradeDate::class.java, DateDeserializer(GradeDate.DATE_FORMAT, GradeDate::class.java))
|
||||
.registerTypeAdapter(GradeDate::class.java, DateDeserializer(GradeDate::class.java))
|
||||
.create()) else JspoonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.baseUrl(url)
|
||||
|
|
Loading…
Reference in a new issue