-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gson Conversion Issue: Unable to Convert java.util.ArrayList to RealmList Implicitly During Deserialization in Android Studio #2730
Comments
If possible, could you please create a small self-contained example or demo project? Otherwise it is difficult to reproduce this or to find the cause. For example maybe there is some non-type-safe code involved which causes the incorrect deserialized class. Could be related to / same issue as #419 or #1708 though, for example here a custom interface MyList<T> extends List<T> {}
public static void main(String[] args) {
var listType = new TypeToken<MyList<String>>() {};
MyList<String> list = new Gson().fromJson("[]", listType);
} |
Same happening for RealmDictionary When changing it to RealmList error converts to has type io.realm.kotlin.types.RealmList, got java.util.ArrayList |
Hi sorry for the late reply. I have created a demo android project to showcase the error: MainActivity
with the following model: ModelA and ModelB:
Gson breaks with the following stack trace when the
and as @sonuindori commented, the same error occurs when using RealmDictionary too. For context here are the related dependencies I am using:
Let me know if you need any more info. |
Thanks a lot for this detailed information! I think even if this specific To solve this, you need to register a custom Note however that as mentioned in Gson's README, support for Kotlin and other JVM languages is limited. It seems realm-kotlin provides a In case my suggestion to use an |
I tried using My InstanceCreator
Changed the
From what I could understand, this error is occurring because Gson is deserialising the However we can do this:
So if somehow we can tell Gson to use the |
What I meant was registering val gson = GsonBuilder()
.registerTypeAdapter(RealmList::class.java, InstanceCreator {
realmListOf<Any>()
})
.registerTypeAdapter(RealmDictionary::class.java, InstanceCreator {
realmDictionaryOf<Any>()
})
.create() (Same probably also for At least in my local tests that seems to solve the issue. |
Thank you for your help. This worked perfectly. |
Gson version
2.10.1 AND 2.11.0
Java / Android version
Android version 13
Used tools
Stack Trace
Description
When deserialising a JSON into an object containing RealmList Gson is unable to populate it correctly and throws the following error. In fact the same error occurs in case of RealmDictionary too.
Expected behavior
The deserialised JSON should correctly populate the any list type to RealmList
Actual behaviour
IllegalArgumentException is thrown
The text was updated successfully, but these errors were encountered: