Kalpesh Chandora
08/11/2023, 5:55 AMdata class
into a json array
while sending the response. I am converting the model’s content to JsonObject
and then adding the object to JsonArray
. Can someone please suggest a better approach?
@Serializable
data class MyModel(
val name: String,
val number: String
)
val list = arrayListOf<MyModel>()
repeat(5) {
list.add(
MyModel(
name = UUID.randomUUID().toString(),
number = UUID.randomUUID().toString()
)
)
}
val jsonArray = buildJsonArray {
list.forEach {
val obj = buildJsonObject {
put("name", it.name)
put("number", it.number)
}
add(obj)
}
}
call.respond(buildJsonObject {
put("pageSize", 10)
put("list", jsonArray)
})
Aleksei Tirman [JB]
08/11/2023, 8:20 AMval list = mutableListOf<MyModel>()
repeat(5) {
list.add(
MyModel(
name = UUID.randomUUID().toString(),
number = UUID.randomUUID().toString()
)
)
}
call.respond(list)
Kalpesh Chandora
08/11/2023, 11:17 AMJsonArray
we can directly use call.respond(list)
. But can you please help with when we need to send a JsonObject
with JsonArray
as mentioned in the above snippetAleksei Tirman [JB]
08/11/2023, 12:00 PMKalpesh Chandora
08/11/2023, 12:17 PMJsonObject
{
"key" : "some_key",
"pageSize" : 10,
"notifications" : [
// notifications list
]
}
I have a data class
NotificationDto. I need to send List<NotificationDto>
as notifications
array in my response but I couldn’t find any method to directly convert my model list into JsonArray
.
To achieve the similar results for another API, I have done this:
val jsonArray = buildJsonArray {
users.forEach {
val obj = buildJsonObject {
put("userId", it.userId)
put("name", it.name)
put("photoUrl", it.photoUrl)
put("username", it.username)
put("isFollowedBySelf", followings[it.userId].isTrue())
}
add(obj)
}
}
respond(buildJsonObject {
put("key", nextKey)
put("pageSize", jsonArray.size)
put("users", jsonArray)
})
In the above code, I have manually created the fields and adding them to the JsonArray
.
How can we send a List of data class
without converting each field manually?
This is the code I am trying with:
val notificationsDtoList = mutableListOf<NotificationDto>()
notifications.forEach {
notificationsDtoList.add(
NotificationDto(
userId = it.actionTakenBy,
type = it.type,
postId = it.postId,
postImageUrl = it.postImageUrl,
timeStamp = it.timeStamp,
username = usersMap[it.actionTakenBy]?.username,
photoUrl = usersMap[it.actionTakenBy]?.photoUrl
)
)
}
val nextKey = if (notifications.size == pageSize) {
notifications[pageSize - 1].timeStamp
} else -1
call.respond(buildJsonObject {
put("key", nextKey)
put("pageSize", notifications.size)
putJsonArray("notifications", notificationsDtoList) // Compilation error here because notificationsDtoList is not a JsonArray
})
This is the result I need to achieve:
{
"key": "some_key",
"pageSize": 10,
"notifications": [
{
"userId": "abcde",
"type": "type",
"postId": "postId",
"postImageUrl": "postImageUrl",
"timeStamp": "timeStamp",
"username": "username",
"photoUrl": "photoUrl"
}
]
}
Kalpesh Chandora
08/11/2023, 12:25 PMdata class
to send the response:
@Serializable
data class NotificationDtoResponse(
val key: Long,
val pageSize: Int,
val notifications: List<NotificationDto>
)
call.respond(
NotificationDtoResponse(
pageSize = 10,
key = System.currentTimeMillis(),
notifications = notificationsDtoList
)
)
Not sure, it’s the correct way to do itAleksei Tirman [JB]
08/11/2023, 1:11 PMKalpesh Chandora
08/11/2023, 3:59 PM