Serializing Object with nested classes return empty String Kotlin

Im new to kotlin and android development. Im currently trying to get a POST request to a django REST-API in my local server. Im using Retrofit 2 and Gson for http request and JSON.

I have a class for POST body (DataPost) and goes like this:

class DataPost(_type: String, _attributes: Item) {
    @SerializedName("type")
    var type: String? = null


    @SerializedName("attributes")
    var attributes: Item? = null

}
class Item(_userId: Int, _dbId: Int, _title: String, _body: String){
    @SerializedName("userdId")
    var userdId: Int = 0
    @SerializedName("dbId")
    var dbId: Int = 0
    @SerializedName("title")
    var title: String? = null
    @SerializedName("body")
    var body: String? = null


    companion object {
        fun className(): String {return "Item"}
    }

}

When I initialize it and try to serialize it and log it to JSON by:

var item: Item = Item(1,2,"Shiee","shie body")
var data: DataPost = DataPost("Item", item)
Log.i(TAG_LOGS, Gson.toJson(data))

I just get this output:

{}

And no error in console/log. So I assume its not seriliazing it property due the nested object, but as anything, I might be wrong. Why is returning an empty JSON my DataPost Object?

EDIT: Expected JSON:

{
            "type": "Item",
            "attributes": {
                "userdId": 1,
                "dbId": 2,
                "title": "Shiee",
                "body": "shiee body"
            }
}

Upvotes: 0

Views: 1155

Answers (2)

gpunto
gpunto

Reputation: 2822

The arguments you pass to your classes are not used in any way. For example, in DataPost you have _type as an argument in the constructor, and type as a property in the class. These two unrelated. So when you do

DataPost("Item", item)

"Item" and item are passed in the constructor as _type and _attributes, but then they're not used: in particular they are not automatically assigned to type and attributes.

I'm not sure how changing the types of the properties from nullable to non-nullble types is helping you, as it shouldn't.

What you need to do instead is either move your vars directly to the constructor or manually assign them. Using your DataPost class as an example. You can either do:

class DataPost(
    @SerializedName("type")
    var type: String?,
    @SerializedName("attributes")
    var attributes: Item?
}

or

class DataPost(_type: String, _attributes: Item) {
    @SerializedName("type")
    var type: String? = _type


    @SerializedName("attributes")
    var attributes: Item? = _attributes

}

Side note: if these are immutable values you can change var to val.

Upvotes: 0

Okay so I just needed to delete de ? operator that marks the type as nullable since I didn't declare a serialization for nullable.

From:

@SerializedName("type")
var type: String? = null


@SerializedName("attributes")
var attributes: Item? = null

To:

@SerializedName("type")
lateinit var type: String

@SerializedName("attributes")
lateinit var attributes: Item

Upvotes: 1

Related Questions