question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add Json configuration not serialize all null properties

See original GitHub issue

If 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
rwsbillyangcommented, Aug 20, 2020

You can set null as a default value of your nullable property and use encodeDefaults = false as shown with an example here: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#encoding-defaults

Does it work for you?

encodeDefaults = false remove all keys which have default value include non-null, as following:

@Serializable
class Project2(val name: String, val renamedTo: String? = null, val level: Int = 100)
fun test2(){
    val json = Json{ encodeDefaults = false }
    val data = Project2("kotlinx.serialization")

   //output: {"name":"kotlinx.serialization"}, remove the key: level
    println(json.stringify(Project2.serializer(), data)) 
}

The feature of Include.NON_NULL in Jackson is expected.

1reaction
sandwwraithcommented, Oct 3, 2022

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found