Add extra attribute to POJO in Kotlin
I'm getting the following model from an API call through Retrofit:
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
I need to add one extra attribute for reasons related to this SO answer.
The extra argument will be a data class PaymentsMethods:
data class PaymentsMethods(
val paymentMethods: List<PaymentMethod>
)
How can I add the attribute value to User class when it is created?
Here is what I tried so far:
// add an attribute (with a default value) that won't be filled by the parser.
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>,
@Expose var paymentsObject: PaymentMethods = PaymentMethods(payments)
)
and
// create variable and set the value on init
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
init
var paymentsObject: PaymentMethods = PaymentMethods(payments)
None of them work. Any suggestion will be much appreciated.
kotlin retrofit pojo data-class
|
show 1 more comment
I'm getting the following model from an API call through Retrofit:
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
I need to add one extra attribute for reasons related to this SO answer.
The extra argument will be a data class PaymentsMethods:
data class PaymentsMethods(
val paymentMethods: List<PaymentMethod>
)
How can I add the attribute value to User class when it is created?
Here is what I tried so far:
// add an attribute (with a default value) that won't be filled by the parser.
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>,
@Expose var paymentsObject: PaymentMethods = PaymentMethods(payments)
)
and
// create variable and set the value on init
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
init
var paymentsObject: PaymentMethods = PaymentMethods(payments)
None of them work. Any suggestion will be much appreciated.
kotlin retrofit pojo data-class
I would like to help but I have trouble reading your code. Aren't you supposed to use@SerializedName
with the json key as value?@Expose
only makes sense if you useGsonBuilder#excludeFieldsWithoutExposeAnnotation
and I don't see you using it. Also, a class with a plural name doesn't make sense. If you use Room then you can create a TypeConverter to retrieve a list from Retrofit and directly store it.
– ZUNJAE
Nov 15 '18 at 13:53
Could you give a concrete example of what your json result looks like? (In text format, so not a POJO)
– ZUNJAE
Nov 15 '18 at 13:56
@ZUNJAE@SerializedName
is only needed when the JSON attribute name differs from the POJO attribute name and for this case, I think we can ignore the@Expose
annotation.
– kike
Nov 15 '18 at 14:05
@ZUNJAE and regarding the JSON, there is not need to add it. This question is about to create an extra attribute in the data class.
– kike
Nov 15 '18 at 14:07
Depending on how you compile your application (debug, release, proguard, dexguard etc.) these variable names will be changed so SerializedName is pretty important.
– ZUNJAE
Nov 15 '18 at 14:07
|
show 1 more comment
I'm getting the following model from an API call through Retrofit:
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
I need to add one extra attribute for reasons related to this SO answer.
The extra argument will be a data class PaymentsMethods:
data class PaymentsMethods(
val paymentMethods: List<PaymentMethod>
)
How can I add the attribute value to User class when it is created?
Here is what I tried so far:
// add an attribute (with a default value) that won't be filled by the parser.
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>,
@Expose var paymentsObject: PaymentMethods = PaymentMethods(payments)
)
and
// create variable and set the value on init
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
init
var paymentsObject: PaymentMethods = PaymentMethods(payments)
None of them work. Any suggestion will be much appreciated.
kotlin retrofit pojo data-class
I'm getting the following model from an API call through Retrofit:
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
I need to add one extra attribute for reasons related to this SO answer.
The extra argument will be a data class PaymentsMethods:
data class PaymentsMethods(
val paymentMethods: List<PaymentMethod>
)
How can I add the attribute value to User class when it is created?
Here is what I tried so far:
// add an attribute (with a default value) that won't be filled by the parser.
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>,
@Expose var paymentsObject: PaymentMethods = PaymentMethods(payments)
)
and
// create variable and set the value on init
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
init
var paymentsObject: PaymentMethods = PaymentMethods(payments)
None of them work. Any suggestion will be much appreciated.
kotlin retrofit pojo data-class
kotlin retrofit pojo data-class
asked Nov 15 '18 at 13:48
kikekike
964929
964929
I would like to help but I have trouble reading your code. Aren't you supposed to use@SerializedName
with the json key as value?@Expose
only makes sense if you useGsonBuilder#excludeFieldsWithoutExposeAnnotation
and I don't see you using it. Also, a class with a plural name doesn't make sense. If you use Room then you can create a TypeConverter to retrieve a list from Retrofit and directly store it.
– ZUNJAE
Nov 15 '18 at 13:53
Could you give a concrete example of what your json result looks like? (In text format, so not a POJO)
– ZUNJAE
Nov 15 '18 at 13:56
@ZUNJAE@SerializedName
is only needed when the JSON attribute name differs from the POJO attribute name and for this case, I think we can ignore the@Expose
annotation.
– kike
Nov 15 '18 at 14:05
@ZUNJAE and regarding the JSON, there is not need to add it. This question is about to create an extra attribute in the data class.
– kike
Nov 15 '18 at 14:07
Depending on how you compile your application (debug, release, proguard, dexguard etc.) these variable names will be changed so SerializedName is pretty important.
– ZUNJAE
Nov 15 '18 at 14:07
|
show 1 more comment
I would like to help but I have trouble reading your code. Aren't you supposed to use@SerializedName
with the json key as value?@Expose
only makes sense if you useGsonBuilder#excludeFieldsWithoutExposeAnnotation
and I don't see you using it. Also, a class with a plural name doesn't make sense. If you use Room then you can create a TypeConverter to retrieve a list from Retrofit and directly store it.
– ZUNJAE
Nov 15 '18 at 13:53
Could you give a concrete example of what your json result looks like? (In text format, so not a POJO)
– ZUNJAE
Nov 15 '18 at 13:56
@ZUNJAE@SerializedName
is only needed when the JSON attribute name differs from the POJO attribute name and for this case, I think we can ignore the@Expose
annotation.
– kike
Nov 15 '18 at 14:05
@ZUNJAE and regarding the JSON, there is not need to add it. This question is about to create an extra attribute in the data class.
– kike
Nov 15 '18 at 14:07
Depending on how you compile your application (debug, release, proguard, dexguard etc.) these variable names will be changed so SerializedName is pretty important.
– ZUNJAE
Nov 15 '18 at 14:07
I would like to help but I have trouble reading your code. Aren't you supposed to use
@SerializedName
with the json key as value? @Expose
only makes sense if you use GsonBuilder#excludeFieldsWithoutExposeAnnotation
and I don't see you using it. Also, a class with a plural name doesn't make sense. If you use Room then you can create a TypeConverter to retrieve a list from Retrofit and directly store it.– ZUNJAE
Nov 15 '18 at 13:53
I would like to help but I have trouble reading your code. Aren't you supposed to use
@SerializedName
with the json key as value? @Expose
only makes sense if you use GsonBuilder#excludeFieldsWithoutExposeAnnotation
and I don't see you using it. Also, a class with a plural name doesn't make sense. If you use Room then you can create a TypeConverter to retrieve a list from Retrofit and directly store it.– ZUNJAE
Nov 15 '18 at 13:53
Could you give a concrete example of what your json result looks like? (In text format, so not a POJO)
– ZUNJAE
Nov 15 '18 at 13:56
Could you give a concrete example of what your json result looks like? (In text format, so not a POJO)
– ZUNJAE
Nov 15 '18 at 13:56
@ZUNJAE
@SerializedName
is only needed when the JSON attribute name differs from the POJO attribute name and for this case, I think we can ignore the @Expose
annotation.– kike
Nov 15 '18 at 14:05
@ZUNJAE
@SerializedName
is only needed when the JSON attribute name differs from the POJO attribute name and for this case, I think we can ignore the @Expose
annotation.– kike
Nov 15 '18 at 14:05
@ZUNJAE and regarding the JSON, there is not need to add it. This question is about to create an extra attribute in the data class.
– kike
Nov 15 '18 at 14:07
@ZUNJAE and regarding the JSON, there is not need to add it. This question is about to create an extra attribute in the data class.
– kike
Nov 15 '18 at 14:07
Depending on how you compile your application (debug, release, proguard, dexguard etc.) these variable names will be changed so SerializedName is pretty important.
– ZUNJAE
Nov 15 '18 at 14:07
Depending on how you compile your application (debug, release, proguard, dexguard etc.) these variable names will be changed so SerializedName is pretty important.
– ZUNJAE
Nov 15 '18 at 14:07
|
show 1 more comment
1 Answer
1
active
oldest
votes
I hope this is what you need:
data class User(
@Expose val id: Int,
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?
)
@Expose var payments: List<PaymentMethod> by Delegates.observable(listOf())
prop, oldValue, newValue ->
paymentsObject = PaymentMethods(newValue)
lateinit var paymentsObject: PaymentMethods
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320916%2fadd-extra-attribute-to-pojo-in-kotlin%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I hope this is what you need:
data class User(
@Expose val id: Int,
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?
)
@Expose var payments: List<PaymentMethod> by Delegates.observable(listOf())
prop, oldValue, newValue ->
paymentsObject = PaymentMethods(newValue)
lateinit var paymentsObject: PaymentMethods
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
add a comment |
I hope this is what you need:
data class User(
@Expose val id: Int,
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?
)
@Expose var payments: List<PaymentMethod> by Delegates.observable(listOf())
prop, oldValue, newValue ->
paymentsObject = PaymentMethods(newValue)
lateinit var paymentsObject: PaymentMethods
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
add a comment |
I hope this is what you need:
data class User(
@Expose val id: Int,
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?
)
@Expose var payments: List<PaymentMethod> by Delegates.observable(listOf())
prop, oldValue, newValue ->
paymentsObject = PaymentMethods(newValue)
lateinit var paymentsObject: PaymentMethods
I hope this is what you need:
data class User(
@Expose val id: Int,
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?
)
@Expose var payments: List<PaymentMethod> by Delegates.observable(listOf())
prop, oldValue, newValue ->
paymentsObject = PaymentMethods(newValue)
lateinit var paymentsObject: PaymentMethods
edited Nov 19 '18 at 15:16
answered Nov 15 '18 at 18:08
SergeySergey
4,22421835
4,22421835
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
add a comment |
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
thanks for your answer. Unfortunately, it does not work either. Apparently, Retrofit does not follow the common object initialisation (uses reflection instead), so there is no way to add an extra attribute programmatically.
– kike
Nov 19 '18 at 10:21
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
@kike I edited my answer. Try this one.
– Sergey
Nov 19 '18 at 14:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320916%2fadd-extra-attribute-to-pojo-in-kotlin%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I would like to help but I have trouble reading your code. Aren't you supposed to use
@SerializedName
with the json key as value?@Expose
only makes sense if you useGsonBuilder#excludeFieldsWithoutExposeAnnotation
and I don't see you using it. Also, a class with a plural name doesn't make sense. If you use Room then you can create a TypeConverter to retrieve a list from Retrofit and directly store it.– ZUNJAE
Nov 15 '18 at 13:53
Could you give a concrete example of what your json result looks like? (In text format, so not a POJO)
– ZUNJAE
Nov 15 '18 at 13:56
@ZUNJAE
@SerializedName
is only needed when the JSON attribute name differs from the POJO attribute name and for this case, I think we can ignore the@Expose
annotation.– kike
Nov 15 '18 at 14:05
@ZUNJAE and regarding the JSON, there is not need to add it. This question is about to create an extra attribute in the data class.
– kike
Nov 15 '18 at 14:07
Depending on how you compile your application (debug, release, proguard, dexguard etc.) these variable names will be changed so SerializedName is pretty important.
– ZUNJAE
Nov 15 '18 at 14:07