Reputation: 1123
I am trying to parse an API response mainly constructured like this:
{
"ApiFunc1":{
"SomeData1":"SomeValue1",
"SomeData2":"SomeValue2",
"SomeData3":"SomeValue3"
}
}
{
"ApiFunc2":{
"SomeData4":"SomeValue4",
"SomeData5":"SomeValue5",
"SomeData6":"SomeValue6"
}
}
.
.
.
I created a base class as follows:
class Model_BaseResponse<TResponse> : Serializable {
@SerializedName("ErrorMessage")
var errorMessage: String? = null
@SerializedName("value")
var data: TResponse? = null
}
Each ApiFunc returns different data. I want to create a base class where data: TResponse
's @SerializedName("ApiFunc1") or @SerializedName("ApiFunc2") can be set in child class. Is there any way to that? I do it in regular way, defining the
@SerializedName("value")
var data: TResponse? = null
in every child class. Just been curious about that. Thanks in advance for any idea.
Upvotes: 1
Views: 904
Reputation: 82057
An annotation like SerializedData
requires its arguments to be compile-time constants. Having a look at what "compile-time constant" means in Kotlin reveals:
Properties the value of which is known at compile time can be marked as compile time constants using the const modifier. Such properties need to fulfil the following requirements:
- Top-level or member of an object
- Initialized with a value of type String or a primitive type
- No custom getter
Such properties can be used in annotations:
const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated" @Deprecated(SUBSYSTEM_DEPRECATED) fun foo() { ... }
I believe the first requirement "Top-level or member of an object" cannot be fulfilled for your use case.
Upvotes: 1