0

I have such class:

class OutAnswerModel(@field:SerializedName("question")
                     private val question: Int, @field:SerializedName("answer")
                     private val answer: Int)

In some situations I will send pattern which will be similar to my constructor above. But in some cases I will need to send answer like JsonArray. I heard smth about classes with two and more constructors here and Kotlin language supports such feature. Unfortunately I didn't understood how to add such functionality to my class. I tried to add constructor like that:

constructor(question: Int,answer: JsonArray) : this()

But here I have to insert to this() data which as I understood will be used at default constructor of this class. As I see I also have to remove @SerializedName() from second constructor, but I'm using this class at Retrofit for sending some data to server, and how it will work without fields? What I did incorrectly and how I can solve this task? On the other hand for me it will be better to create two different classes with certain classes but I think it will be to complex and not useful.

Andrew
  • 1,947
  • 2
  • 23
  • 61

2 Answers2

1

I can suggest my own answer. I'm not sure whether it is right but I think it good for retrofit and it sends all data with fields:

class OutAnswerModel() {
    var question:Int?=null
    var answer:Any?=null

    constructor(question: Int,answer:Int) : this() {
        this.question = question
        this.answer = answer
    }

    constructor(question: Int,answer: JsonArray) : this(){
        this.question = question
        this.answer = answer
    }
}

if someone notices an error or has a suggestion, I will be happy to correct my answer :)

Andrew
  • 1,947
  • 2
  • 23
  • 61
  • 1
    there's no need to use `@SerializedName("blah")` if your variable has the same name as `blah` :) – a_local_nobody Dec 14 '19 at 11:43
  • @a_local_nobody, hmm... I didn't know that thank you)) but for which purpose we have to use this tag? – Andrew Dec 14 '19 at 11:45
  • we use `@SerializedName("ThisIsTheValueWeSendOrReceiveSerialized") var myShortName:String? = null` so you use SerializedName to define how you get or send the value when it's serialized, but the variable name can be anything which makes sense in code – a_local_nobody Dec 14 '19 at 11:51
  • go check [here](https://stackoverflow.com/questions/28957285/what-is-the-basic-purpose-of-serializedname-annotation-in-android-using-gson) for more info, hope it makes sense :) – a_local_nobody Dec 14 '19 at 11:55
  • some people like to leave it in, so that changing a variable name doesn't break the code, but you dont HAVE to keep it – a_local_nobody Dec 14 '19 at 11:56
0

Change class OutAnswerModel(@field:SerializedName("question") private val question: Int, @field:SerializedName("answer") private val answer: Int)

to class OutAnswerModel(@field:SerializedName("question") private val question: Int? = 0, @field:SerializedName("answer") private val answer: Int? = 0)

Then you can create a constructor using an empty this(). You should consider to making Question: Int a variable inside of the class, but i would guess, that you get an error when you try to assign a value to a val inside your constructor, so you have to Change it to

But if you can't Change your main constructor of the class you have to somehow handle the initialvalue of your 2 variables inside the main constructor.

constructor(question: Int,answer: JsonArray) : this()

Your old Code will still work, as you still got your "old" main constructor

Dominik Wuttke
  • 535
  • 1
  • 4
  • 12
  • did you test your code?? it doesn't work, I can't use `private val` and `@field:SerializedName()` – Andrew Dec 14 '19 at 11:27