Answer the question
In order to leave comments, you need to log in
How to add Json parsing logic without using a deserializer?
I use Retrofit to work with Rest API.
I know that Gson contains annotations that can be used to pull the data we need from Json, but what if this data needs to be preprocessed?
For example, I need to format a date received from Json. To do this, I use the following code in the deserializer ( and this is only for one field out of 10 ):
dataValue.entrySet()?.forEach {
if (it.key == "date_created") {
Log.i(TAG, "date_created exists")
val dateStr: String = it.value.asString
val parsedDateFormat =
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
val parsedDate: Date = parsedDateFormat.parse(dateStr)
val dayDateFormat = SimpleDateFormat("dd")
val day = dayDateFormat.format(parsedDate)
val monthDateFormat = SimpleDateFormat("MMMM")
val month = monthDateFormat.format(parsedDate)
val yearDateFormat = SimpleDateFormat("yyyy")
val year = yearDateFormat.format(parsedDate)
dateCreated = "$month $day, $year"
}
Answer the question
In order to leave comments, you need to log in
Yes, I have. it is not necessary to keep the formatted fields in the data. It is necessary to format immediately before use (for example, display in a view). To do this, there must be a separate layer that handles the formatting of all data for display (and this can be not only text formatting).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question