1

What should my data class be so that when I convert it to json using moshi it would come out like this?

{"id":"abcdef""formValues":{}}

At the moment my class looks like this.

@JsonClass(generateAdapter = true)
class MyDataClass(
    @Json(name = "id")
    val id: String
) {

    @Json(name = "formValues")
    val formValues = FormValues()

    @JsonClass(generateAdapter = true)
    class FormValues
}

But you see the statement

Moshi.Builder().build().adapter(MyDataClass::class.java).toJson(MyDataClass("abcdef"))

produces this

{"id": "abcdef"}

and I want this

{"id":"abcdef""formValues":{}}

1 Answer 1

2

It turns out you have to use var instead of val for your class members. I change my class to this

@JsonClass(generateAdapter = true)
class MyDataClass(
    @Json(name = "id")
    var id: String
) {

    @Json(name = "formValues")
    var formValues = FormValues()

    @JsonClass(generateAdapter = true)
    class FormValues
}

and moshi generates json like this

{"id":"abcdef""formValues":{}}
Sign up to request clarification or add additional context in comments.

Comments

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.