add Json configuration not serialize all null properties
See original GitHub issueIf a property is null, usually it is not necessary for client(at least javascript web app), only consumes network traffic of remote server such as ktor.
Hope add a Json configuration not serialize all null properties:
val format = Json { ignoreNull = true } //default can be false
then
@Serializable
class Project(val name: String, val renamedTo: String?)
fun main() {
val data = Project("kotlinx.serialization", null)
println(Json.encodeToString(data))
}
output:
{“name”:“kotlinx.serialization”}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:13 (7 by maintainers)
Top Results From Across the Web
How to ignore a property in class if null, using json.net
You can do this to ignore all nulls in an object you're serializing, and any null properties won't then appear in the JSON...
Read more >How to ignore properties with System.Text.Json | Microsoft Learn
Never - The property is always serialized and deserialized, regardless of the DefaultIgnoreCondition , IgnoreReadOnlyProperties , and ...
Read more >NullValueHandling setting - Json.NET
This sample serializes an object to JSON with NullValueHandling set to Ignore so that properties with a default value aren't included in the...
Read more >Ignore Null Fields with Jackson | Baeldung
This quick tutorial is going to cover how to set up Jackson to ignore null fields when serializing a java class.
Read more >JSON serialization: Ignore selective properties or null properties
For eg., Database Id column is a property on the class, but not needed to be serialized JSON object. We can use [JsonIgnore]...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
encodeDefaults = false
remove all keys which have default value include non-null, as following:The feature of Include.NON_NULL in Jackson is expected.
@ArtemMikhaylov This issue is about serialization, not deserialization of some rest API result call. For your use-case, check out
coerceInputValues
property in json builder: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#coercing-input-values