I have JSON data which is retrieved from the local realm database. I'm trying to convert it into the corresponding data class. I have an ISO date field
{
....
"createdAt" : "2022-05-04T10:16:56.489Z"
....
}
What I'm trying to do is to convert this string date field into kotlinx-datetime
's Instant
object which is a serializable class. Thus I made my data class as
import kotlinx.datetime.Instant
data class PollPinComment(
...
val createdAt: Instant? = null,
...
)
Which doesn't work and gives an error as follows
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 288 path $[0].createdAt
I'm sure that I might need to write some serialization/deserialization logic with gson to convert this string into Instant
object. So my question is how can I do that? Thanks in advance
You can create a custom
Deserializer
for this and register it astype adapter
to yourGson
object.Create
Gson()
object pasing thatdeserializer
astype adapter
If you are using it with
Retrofit
In
Retrofit
builder pass this toGsonConverterFactory