API 日期字段 returns java.time.format.DateTimeParseException: 无法解析文本

API date field returns java.time.format.DateTimeParseException: Text could not be parsed

我有一个 API 端点,它有一个日期时间字段,但我无法正确解析它。 API 是用 C# 编写的,我没有找到太多关于如何解决这个问题的帮助:

kotlinx.datetime.DateTimeFormatException: java.time.format.DateTimeParseException: Text '2022-05-02T16:12:52.393538-04:00' could not be parsed, unparsed text found at index 26 at kotlinx.datetime.LocalDateTime$Companion.parse(LocalDateTime.kt:57) at kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer.deserialize(LocalDateTimeSerializers.kt:19) at kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer.deserialize(LocalDateTimeSerializers.kt:13) at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59) at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36) at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43) at kotlinx.serialization.encoding.AbstractDecoder.decodeNullableSerializableElement(AbstractDecoder.kt:79) at com.demo.app.http.response.users.UserAccessTokenResponse$$serializer.deserialize(UserAccessTokenResponse.kt:7) at com.demo.app.http.response.users.UserAccessTokenResponse$$serializer.deserialize(UserAccessTokenResponse.kt:7) at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59) at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36) at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100) at io.ktor.serialization.kotlinx.KotlinxSerializationConverter.deserialize(KotlinxSerializationConverter.kt:55) at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install.invokeSuspend(ContentNegotiation.kt:135) at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install.invoke(Unknown Source:13) at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install.invoke(Unknown Source:6) at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123) at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81) at io.ktor.client.HttpClient.invokeSuspend(HttpClient.kt:170) at io.ktor.client.HttpClient.invoke(Unknown Source:11) at io.ktor.client.HttpClient.invoke(Unknown Source:6) at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123) at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81) at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:91) at io.ktor.client.plugins.HttpCallValidator$Companion$install.invokeSuspend(HttpCallValidator.kt:140) at io.ktor.client.plugins.HttpCallValidator$Companion$install.invoke(Unknown Source:13) at io.ktor.client.plugins.HttpCallValidator$Companion$install.invoke(Unknown Source:6) at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123) at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81) at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:101) at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77) at io.ktor.client.call.HttpClientCall.body(HttpClientCall.kt:87) at com.demo.app.http.TokenHttpClient.getToken(TokenHttpClient.kt:144) at com.demo.app.http.TokenHttpClient$getToken.invokeSuspend(Unknown Source:15) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138) at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112) at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14) at io.ktor.util.pipeline.SuspendFunctionGun$continuation.resumeWith(SuspendFunctionGun.kt:62) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46) at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138) at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)

对于 kotlinx.serialization 我没有找到很多关于这方面的内容,这对我来说是一个重大的停顿。

这是我的响应对象的样子:

@Serializable
data class UserAccessTokenResponse(
    @SerialName("http_code")
    val httpCode: Int = 200,
    @SerialName("error_response")
    val errorResponse: String? = null,
    @SerialName("access_token")
    val accessToken: String? = null,
    @SerialName("token_type")
    val tokenType: String? = null,
    @SerialName("expires_in")
    val expiresIn: Int = 0,
    @SerialName("refresh_token")
    val refreshToken: String? = null,
    @SerialName("date_created")
    val dateCreated: LocalDateTime? = null,
    @SerialName("was_refreshed")
    val wasRefresh: Boolean = false
)

看来问题是我使用了错误的类型。阅读 kotlinx-datetime doc 似乎他们建议您使用 Instant,然后在您需要在代码中实际使用它时转换为 LocalDateTime