Hello i'm creating a rest server using ktor in my android application. It works really well but when i do a call i receive all the data but i don't get null data. What should i do to receive them.
here is my code
install(ContentNegotiation) {
gson()
}
routing {
get("StorageAndControlService/config/get/{key}") {
val key = call.parameters["key"]
lateinit var config:Any
when(key){
"TRAVELER_MODE" -> {config = settings.data.isStandardMode
if(config){
call.respond(mapOf("current" to "STD","pending" to null))
}else{
call.respond(mapOf("current" to "SUB","pending" to null))
}}
}
}
}
and here is what i get from the call
i should be getting the pending value. What should i do to have it visible ?
In Ktor, the Gson library is used for serializing and deserializing JSON data. By default, Gson does not serialize null values. To include null values in your JSON responses, you need to configure the Gson instance to serialize nulls.
Here is how you can adjust your Gson configuration to include null values:
Import the necessary Gson library functions.
Configure the Gson instance to serialize null values using serializeNulls().
import com.google.gson.GsonBuilder
import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.gson.gson
import io.ktor.response.respond
import io.ktor.routing.get
import io.ktor.routing.routing
fun Application.module() {
install(ContentNegotiation) {
gson {
serializeNulls() // Enable serialization of null values
}
}
routing {
get("StorageAndControlService/config/get/{key}") {
val key = call.parameters["key"]
lateinit var config: Any
when (key) {
"TRAVELER_MODE" -> {
config = settings.data.isStandardMode
if (config) {
call.respond(mapOf("current" to "STD", "pending" to null))
} else {
call.respond(mapOf("current" to "SUB", "pending" to null))
}
}
}
}
}}