Skip to content
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

Nested data classes are not parsed back correctly #12

Closed
yooneskh opened this issue Mar 17, 2020 · 1 comment
Closed

Nested data classes are not parsed back correctly #12

yooneskh opened this issue Mar 17, 2020 · 1 comment

Comments

@yooneskh
Copy link

These are my models

data class IChapter<T>(
  var _id: String,
  var title: String,
  var picture: T,
  var createdAt: Long,
  var updatedAt: Long
)

data class IMedia(
  var _id: String,
  var name: String,
  var extension: String,
  var path: String,
  var size: Long,
  var createdAt: Long,
  var updatedAt: Long
)

I want to save this in prefrences.

object prefs : PreferenceHolder() {
  var chapters: List<IChapter<IMedia>> by bindToPreferenceField(listOf())
}

After i populate it with objects coming from this

    fun getRemoteChapters(): List<IChapter<IMedia>> = listOf(
      IChapter("1", "در تنهایی و ...", IMedia("1", "f1", "jpg", "https://images.all-free-download.com/images/wallpapers_large/small_island_wallpaper_beaches_nature_wallpaper_1388.jpg", 0, 0, 0), 0, 0),
      IChapter("2", "پیش از جکومت", IMedia("2", "f2", "jpg", "https://besthqwallpapers.com/Uploads/22-11-2017/29941/thumb2-sea-landscape-waves-beach-sand-clouds.jpg", 0, 0, 0), 0, 0),
      IChapter("3", "تا رهایی", IMedia("3", "f3", "jpg", "https://earthporm.com/wp-content/uploads/2014/08/small-man-grand-nature-landscape-photography-62.jpg", 0, 0, 0), 0, 0)
    )

If i close and open my app i get class cast error as below

    java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to ir.yooneskh.alisotries.database.models.IMedia

Am i doing anything wrong?

@MarcinMoskala
Copy link
Owner

Not sure. This is GSON working on here. Try to use fromJson and toJson and look at how does this works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants