Gson serialization error with val in kotlin
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using Gson v2.8.2 and Retrofit v2.3.0 and I have two classes, Answer.kt:
open class Answer(@SerializedName("answer")
var text: String,
val id: Int)
and AnswerSummary.kt:
class AnswerSummary(val answer: Answer) : Answer(answer.text, answer.id) {
val percent: Int = 0
}
I am using it in a list (in Java, still migrating to Kotlin):
public List<AnswerSummary> getAnswerSummaries() {
return answerSummaries;
}
I get an error when val is not removed: class AnswerSummary(val answer: Answer) ...:
java.lang.IllegalArgumentException: Unable to create converter for class com.name.app.model.response.AnswerResponse
....
Caused by java.lang.IllegalArgumentException: class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
....
After removing 'val': class AnswerSummary(answer: Answer) ..., the error goes away. Why is this, since I only annotated one field? - @SerializedName("answer") var text: String.
I found a way to circumvent the crash while still maintaining immutability ('val') - By using a different variable name: class AnswerSummary(val ans: Answer) .... What is happening behind the background - is it related to the fact that the @SerializedName field value is the same as the variable name?
add a comment |
I am using Gson v2.8.2 and Retrofit v2.3.0 and I have two classes, Answer.kt:
open class Answer(@SerializedName("answer")
var text: String,
val id: Int)
and AnswerSummary.kt:
class AnswerSummary(val answer: Answer) : Answer(answer.text, answer.id) {
val percent: Int = 0
}
I am using it in a list (in Java, still migrating to Kotlin):
public List<AnswerSummary> getAnswerSummaries() {
return answerSummaries;
}
I get an error when val is not removed: class AnswerSummary(val answer: Answer) ...:
java.lang.IllegalArgumentException: Unable to create converter for class com.name.app.model.response.AnswerResponse
....
Caused by java.lang.IllegalArgumentException: class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
....
After removing 'val': class AnswerSummary(answer: Answer) ..., the error goes away. Why is this, since I only annotated one field? - @SerializedName("answer") var text: String.
I found a way to circumvent the crash while still maintaining immutability ('val') - By using a different variable name: class AnswerSummary(val ans: Answer) .... What is happening behind the background - is it related to the fact that the @SerializedName field value is the same as the variable name?
AnswerSummary declares multiple JSON fields named answerRenameval answerinAnswerSummary(val answer: Answer)to something else.
– Onik
Nov 16 '18 at 19:03
i mentioned that i circumvent the crash when i use a different variable name:class AnswerSummary(val ans: Answer), I'm asking what's going on: why do I have to remove 'val' or change the variable name?
– chornge
Nov 16 '18 at 19:06
Because Retrofit is Unable to create converter for class com.name.app.model.response.AnswerResponse because of AnswerSummary declares multiple JSON fields named answer. What would you do on the Retrofit's place? :)
– Onik
Nov 16 '18 at 19:08
add a comment |
I am using Gson v2.8.2 and Retrofit v2.3.0 and I have two classes, Answer.kt:
open class Answer(@SerializedName("answer")
var text: String,
val id: Int)
and AnswerSummary.kt:
class AnswerSummary(val answer: Answer) : Answer(answer.text, answer.id) {
val percent: Int = 0
}
I am using it in a list (in Java, still migrating to Kotlin):
public List<AnswerSummary> getAnswerSummaries() {
return answerSummaries;
}
I get an error when val is not removed: class AnswerSummary(val answer: Answer) ...:
java.lang.IllegalArgumentException: Unable to create converter for class com.name.app.model.response.AnswerResponse
....
Caused by java.lang.IllegalArgumentException: class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
....
After removing 'val': class AnswerSummary(answer: Answer) ..., the error goes away. Why is this, since I only annotated one field? - @SerializedName("answer") var text: String.
I found a way to circumvent the crash while still maintaining immutability ('val') - By using a different variable name: class AnswerSummary(val ans: Answer) .... What is happening behind the background - is it related to the fact that the @SerializedName field value is the same as the variable name?
I am using Gson v2.8.2 and Retrofit v2.3.0 and I have two classes, Answer.kt:
open class Answer(@SerializedName("answer")
var text: String,
val id: Int)
and AnswerSummary.kt:
class AnswerSummary(val answer: Answer) : Answer(answer.text, answer.id) {
val percent: Int = 0
}
I am using it in a list (in Java, still migrating to Kotlin):
public List<AnswerSummary> getAnswerSummaries() {
return answerSummaries;
}
I get an error when val is not removed: class AnswerSummary(val answer: Answer) ...:
java.lang.IllegalArgumentException: Unable to create converter for class com.name.app.model.response.AnswerResponse
....
Caused by java.lang.IllegalArgumentException: class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
....
After removing 'val': class AnswerSummary(answer: Answer) ..., the error goes away. Why is this, since I only annotated one field? - @SerializedName("answer") var text: String.
I found a way to circumvent the crash while still maintaining immutability ('val') - By using a different variable name: class AnswerSummary(val ans: Answer) .... What is happening behind the background - is it related to the fact that the @SerializedName field value is the same as the variable name?
edited Feb 4 at 19:59
Community♦
11
11
asked Nov 16 '18 at 18:39
chorngechornge
1,18521525
1,18521525
AnswerSummary declares multiple JSON fields named answerRenameval answerinAnswerSummary(val answer: Answer)to something else.
– Onik
Nov 16 '18 at 19:03
i mentioned that i circumvent the crash when i use a different variable name:class AnswerSummary(val ans: Answer), I'm asking what's going on: why do I have to remove 'val' or change the variable name?
– chornge
Nov 16 '18 at 19:06
Because Retrofit is Unable to create converter for class com.name.app.model.response.AnswerResponse because of AnswerSummary declares multiple JSON fields named answer. What would you do on the Retrofit's place? :)
– Onik
Nov 16 '18 at 19:08
add a comment |
AnswerSummary declares multiple JSON fields named answerRenameval answerinAnswerSummary(val answer: Answer)to something else.
– Onik
Nov 16 '18 at 19:03
i mentioned that i circumvent the crash when i use a different variable name:class AnswerSummary(val ans: Answer), I'm asking what's going on: why do I have to remove 'val' or change the variable name?
– chornge
Nov 16 '18 at 19:06
Because Retrofit is Unable to create converter for class com.name.app.model.response.AnswerResponse because of AnswerSummary declares multiple JSON fields named answer. What would you do on the Retrofit's place? :)
– Onik
Nov 16 '18 at 19:08
AnswerSummary declares multiple JSON fields named answer Rename val answer in AnswerSummary(val answer: Answer) to something else.– Onik
Nov 16 '18 at 19:03
AnswerSummary declares multiple JSON fields named answer Rename val answer in AnswerSummary(val answer: Answer) to something else.– Onik
Nov 16 '18 at 19:03
i mentioned that i circumvent the crash when i use a different variable name:
class AnswerSummary(val ans: Answer), I'm asking what's going on: why do I have to remove 'val' or change the variable name?– chornge
Nov 16 '18 at 19:06
i mentioned that i circumvent the crash when i use a different variable name:
class AnswerSummary(val ans: Answer), I'm asking what's going on: why do I have to remove 'val' or change the variable name?– chornge
Nov 16 '18 at 19:06
Because Retrofit is Unable to create converter for class com.name.app.model.response.AnswerResponse because of AnswerSummary declares multiple JSON fields named answer. What would you do on the Retrofit's place? :)
– Onik
Nov 16 '18 at 19:08
Because Retrofit is Unable to create converter for class com.name.app.model.response.AnswerResponse because of AnswerSummary declares multiple JSON fields named answer. What would you do on the Retrofit's place? :)
– Onik
Nov 16 '18 at 19:08
add a comment |
1 Answer
1
active
oldest
votes
Try to rename the answer parameter:
class AnswerSummary(val ans: Answer) : Answer(ans.text, ans.id) { ... }
or use answer as parameter, not property:
class AnswerSummary(answer: Answer) : Answer(answer.text, answer.id) { ... }
It will work because when you are using val parameter 'answer' is considered as a property (without val it is considered as parameter) and serialized by Gson using property name as serialized name. At the same time you have the same serialized name @SerializedName("answer") in your base class Answer , therefore there is a conflict while properties are being serialized.
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields namedanswer
– Sergey
Nov 16 '18 at 21:17
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read thanansoranswerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.
– chornge
Nov 17 '18 at 2:22
1
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
|
show 5 more comments
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%2f53343616%2fgson-serialization-error-with-val-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
Try to rename the answer parameter:
class AnswerSummary(val ans: Answer) : Answer(ans.text, ans.id) { ... }
or use answer as parameter, not property:
class AnswerSummary(answer: Answer) : Answer(answer.text, answer.id) { ... }
It will work because when you are using val parameter 'answer' is considered as a property (without val it is considered as parameter) and serialized by Gson using property name as serialized name. At the same time you have the same serialized name @SerializedName("answer") in your base class Answer , therefore there is a conflict while properties are being serialized.
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields namedanswer
– Sergey
Nov 16 '18 at 21:17
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read thanansoranswerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.
– chornge
Nov 17 '18 at 2:22
1
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
|
show 5 more comments
Try to rename the answer parameter:
class AnswerSummary(val ans: Answer) : Answer(ans.text, ans.id) { ... }
or use answer as parameter, not property:
class AnswerSummary(answer: Answer) : Answer(answer.text, answer.id) { ... }
It will work because when you are using val parameter 'answer' is considered as a property (without val it is considered as parameter) and serialized by Gson using property name as serialized name. At the same time you have the same serialized name @SerializedName("answer") in your base class Answer , therefore there is a conflict while properties are being serialized.
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields namedanswer
– Sergey
Nov 16 '18 at 21:17
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read thanansoranswerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.
– chornge
Nov 17 '18 at 2:22
1
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
|
show 5 more comments
Try to rename the answer parameter:
class AnswerSummary(val ans: Answer) : Answer(ans.text, ans.id) { ... }
or use answer as parameter, not property:
class AnswerSummary(answer: Answer) : Answer(answer.text, answer.id) { ... }
It will work because when you are using val parameter 'answer' is considered as a property (without val it is considered as parameter) and serialized by Gson using property name as serialized name. At the same time you have the same serialized name @SerializedName("answer") in your base class Answer , therefore there is a conflict while properties are being serialized.
Try to rename the answer parameter:
class AnswerSummary(val ans: Answer) : Answer(ans.text, ans.id) { ... }
or use answer as parameter, not property:
class AnswerSummary(answer: Answer) : Answer(answer.text, answer.id) { ... }
It will work because when you are using val parameter 'answer' is considered as a property (without val it is considered as parameter) and serialized by Gson using property name as serialized name. At the same time you have the same serialized name @SerializedName("answer") in your base class Answer , therefore there is a conflict while properties are being serialized.
edited Nov 19 '18 at 15:45
answered Nov 16 '18 at 20:44
SergeySergey
4,34421835
4,34421835
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields namedanswer
– Sergey
Nov 16 '18 at 21:17
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read thanansoranswerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.
– chornge
Nov 17 '18 at 2:22
1
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
|
show 5 more comments
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields namedanswer
– Sergey
Nov 16 '18 at 21:17
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read thanansoranswerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.
– chornge
Nov 17 '18 at 2:22
1
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
why must I rename the variable? is it because @SerializedName is also "answer"?
– chornge
Nov 16 '18 at 20:57
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields named
answer– Sergey
Nov 16 '18 at 21:17
yes, the error "class com.name.app.model.pojo.AnswerSummary declares multiple JSON fields named answer" says that there are multiple fields named
answer– Sergey
Nov 16 '18 at 21:17
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read than
ans or answerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.– chornge
Nov 17 '18 at 2:22
using the @Expose annotation still results in the app crashing. what if the serialized field value was changed (in the backend api) to "ANSWER" instead of "answer", do you know if that would avoid the crash? i know i can rename the parameter but answer is much cleaner to read than
ans or answerParam. my backup option is to remove 'val' because crashes only occur when i add 'val'.– chornge
Nov 17 '18 at 2:22
1
1
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
I think renaming serialized value in the backend api to "ANSWER" could help, but I'm not sure, you can try.
– Sergey
Nov 17 '18 at 6:53
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
yes, i believe you're right. how come whenever i remove 'val' from the method signature the error disappears. why does 'val' make the issue come up?
– chornge
Nov 19 '18 at 5:00
|
show 5 more comments
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%2f53343616%2fgson-serialization-error-with-val-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
AnswerSummary declares multiple JSON fields named answerRenameval answerinAnswerSummary(val answer: Answer)to something else.– Onik
Nov 16 '18 at 19:03
i mentioned that i circumvent the crash when i use a different variable name:
class AnswerSummary(val ans: Answer), I'm asking what's going on: why do I have to remove 'val' or change the variable name?– chornge
Nov 16 '18 at 19:06
Because Retrofit is Unable to create converter for class com.name.app.model.response.AnswerResponse because of AnswerSummary declares multiple JSON fields named answer. What would you do on the Retrofit's place? :)
– Onik
Nov 16 '18 at 19:08