0

I often will create a data class in Kotlin that is used internally for data models. Example:

data class MyDataModel(
    var id: String? = null,
    var ownerId: String,
    var name: String,
    var isPrivate: Boolean = false,
)

I often need to serialize these classes to JSON. The problem is that some of the class properties are not nullable and there are cases where I need to exclude those fields in the serialization. I haven't found a clean and simple way to do that. The solution I currently use is not to use non-nullable properties and then set those that I don't want serialized to null.

Is there another approach?

10
  • what are you using for serialization/deserialization? There are multiple approaches to this that depend on that. Commented Oct 14, 2020 at 8:10
  • Gson. Kotlin also has its own serialization but it really isn't ripe at this stage. I don't need deserialization. Just serialization. Commented Oct 14, 2020 at 8:28
  • 1
    In that case I would recommend using one of the ways that are presented here baeldung.com/gson-exclude-fields-serialization Commented Oct 14, 2020 at 8:33
  • I'm aware of that solution. Overly complex just to get a null serialization. Commented Oct 14, 2020 at 8:37
  • 1
    Yes, it works with kotlin, and yes, you only need add @JsonIgnore. We use jackson because it comes with spring (almost all of our services are in kotlin). You would need to use the jackson-module-kotlin (it is in the main maven repo). The thing is, I don't know the rest of your code to know if jackson would be better than gson in every aspect, but at least in this particular case, it is much more easier. You can see here on number 7 baeldung.com/jackson-field-serializable-deserializable-or-not (btw, I am not affiliated with baeldung) Commented Oct 14, 2020 at 10:20

1 Answer 1

0

Solution using kotlinx.serialization:

  1. Define class, including all fields you want to be serialized, mark it as @Serializable
@Serializable
open class MyDataModelSerializable(
    open var id: String? = null
)
  1. Make your data class to be its subtype:
data class MyDataModel(
    var ownerId: String,
    var name: String,
    var isPrivate: Boolean = false,
    override var id: String? = null
) : MyDataModelSerializable(id)
  1. Serialize instances of MyDataModel class with serializer for MyDataModelSerializable:
val s = serializer<MyDataModelSerializable>()
println(Json.encodeToString(s, MyDataModel(ownerId = "1", name = "2", id = "3", isPrivate = true))) //{"id":"3"}
println(Json.encodeToString(s, MyDataModel(ownerId = "1", name = "2", isPrivate = true))) //{}
println(Json{encodeDefaults = true}.encodeToString(s, MyDataModel(ownerId = "1", name = "2"))) //{"id":null}
Sign up to request clarification or add additional context in comments.

4 Comments

But that serializes the entire class. Nothing that Gson doesn't already do. Also, the output you showed for the first item shows only the id, yet the name and ownerId are not null and should be serialized as well.
But that was your original requirment - to not serialize a non-nullable fields. Maybe you confuse them? Nullable references are the one that can hold null (like String?), non-nullable - are those that can not (like String).
If you need the opposite - to exclude nullable types, it is even easier, you don't need any auxilary classes, just mark you original data class as @Serializable, and nullable fields as @Transient
Also the serialization library doesn't encode default values, so if you just don't want properties with a null value appearing, give them that as a default (like in your example, var id: String? = null) and it will only be included if it's set to a non-null value. I agree that it's not exactly clear what you want though, might be worth reading through the guide intro and seeing if it fits what you're trying to do: github.com/Kotlin/kotlinx.serialization/blob/master/docs/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.