dany giguere
06/17/2024, 10:21 PMdata class ProgramDto(
val id: Long?,
val title: String,
val description: String,
var images: List<ImageDto>? = emptyList()
)
that may (or not) have Sessions. Sometime the Front end needs it, sometimes it doesn’t. So I’ve created another Dto called ProgramWithSessionsDto. And then I added this function in ProgramDto that converts the ProgramDto to a ProgramWithSessionsDto:
fun ProgramDto.toProgramWithSessionsDto(): ProgramWithSessionsDto = ProgramWithSessionsDto(
id = id,...
ProgramWithSessionsDto, is this:
data class ProgramWithSessionsDto(
val id: Long?,
val title: String,
val description: String,
var images: List<ImageDto>? = emptyList(),
var sessions: List<SessionDto>? = emptyList()
)
But that’s a lot of Dtos I’m going to add to my project. Is there anyway I could simply add this to my ProgramDto:
var sessions: List<SessionDto>? = emptyList(),
but not return it (the sessions) if I don’t need to ? and then I could get rid of ProgramWithSessionsDto.Raimund Klein
06/17/2024, 10:27 PM@JsonUnwrapped
.dany giguere
06/17/2024, 10:31 PM@JsonUnwrapped
does it mean the sessions key won’t even be included in the json ?Raimund Klein
06/17/2024, 10:35 PMProgramWithSessionsDto
would contain a ProgramDto
rather than the individual attributes. If you then add @JsonUnwrapped
, the JSON structure would look the same as now.dany giguere
06/17/2024, 10:36 PMRiccardo Lippolis
06/18/2024, 6:47 AM@JsonInclude(JsonInclude.Include.NON_EMPTY)
, which means that only non-null and non-empty fields will end up in the json. So if the sessions
field contains an empty list, it won't be serializeddany giguere
06/18/2024, 8:54 PM