Ahmed Riyadh
06/23/2024, 6:48 PMdata class
that doesn't have all the properties of that file without losing the properties that are not part of the data class
yet exist in the JSON file?
It's possible using Json.parseToJsonElement
and manually update using jsonObject?.toMutableMap()
The only problem that it doesn't use the same Serial name in the data class and is not type-saftey.ephemient
06/23/2024, 8:19 PMval json = Json {
ignoreUnknownKeys = true
}
val input = json.parseToJsonElement(...)
val data1: Data = json.decodeFromJsonElement(input)
val data2 = data1.copy(...)
val output = JsonObject(input.jsonObject + json.encodeToJsonElement(data2).jsonObject)
Ahmed Riyadh
06/23/2024, 8:22 PMparseToJsonElement
and encodeToJsonElement
You could also use Json
directly instead of using the Json with ignore unknown keys.Ahmed Riyadh
06/24/2024, 2:12 AMdata class
inside your data class
. You will have to choose to either use the one from parseToJsonElement
or
You will have to deep merge the two JSON objects.
The only issue, if you remove something from the updated data class, you will still have it in the JSON element if it's there.