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

Serializing and deserializing into json #234

Closed
Matt-84 opened this issue Mar 3, 2018 · 5 comments
Closed

Serializing and deserializing into json #234

Matt-84 opened this issue Mar 3, 2018 · 5 comments

Comments

@Matt-84
Copy link

Matt-84 commented Mar 3, 2018

Hello, I would like to use this API to save the downloaded content into a file for later use. I was wondering how I could serialize the @RedditModel annotated classes using existing methods into string/json and read it back.

I am sure there must be an easy way for this for the library already does that, I am just unable to find the implementation. Could anyone please help me with this?

@mattbdean
Copy link
Owner

See the FAQ

@Matt-84
Copy link
Author

Matt-84 commented Mar 4, 2018

The Submission class works fine. I thought there would be some convenience functions to serialize the PublicContribution interface.

I tried to serialize this way, but I am skeptical if this is the right way to do:

val user = BotUser
        .redditClient
        .user("* USERNAME *")
        .history("overview")
        .limit(Paginator.RECOMMENDED_MAX_LIMIT)
        .sorting(UserHistorySort.NEW)
        .build()

val elems: Iterator<Listing<PublicContribution<*>>> = user.iterator()
val first = elems.next()

val submissionAdapter = JrawUtils.moshi.adapter(Submission::class.java).serializeNulls()
val commentAdapter = JrawUtils.moshi.adapter(Comment::class.java).serializeNulls()

for (elem in first) {
    when(elem) {
        is Submission -> {
            println("SUBMISSION")
            println(submissionAdapter.toJson(elem))
        }
        is Comment -> {
            println("COMMENT")
            println(commentAdapter.toJson(elem))  // Line 55 is this one
        }
        else -> {
            throw RuntimeException("Not handled!")
        }
    }
}

Though when serializing the Comment class, it finds an unimplemented function:

kotlin.NotImplementedError: An operation is not implemented: not implemented
    at net.dean.jraw.databind.RedditModelAdapterFactory$ListingAdapter.toJson(RedditModelAdapterFactory.kt:212)
    at net.dean.jraw.databind.RedditModelAdapterFactory$ListingAdapter.toJson(RedditModelAdapterFactory.kt:154)
    at net.dean.jraw.databind.RepliesAdapterFactory$RepliesAdapter.toJson(RepliesAdapterFactory.kt:48)
    at net.dean.jraw.databind.RepliesAdapterFactory$RepliesAdapter.toJson(RepliesAdapterFactory.kt:46)
    at net.dean.jraw.models.AutoValue_Comment$MoshiJsonAdapter.toJson(AutoValue_Comment.java:267)
    at net.dean.jraw.models.AutoValue_Comment$MoshiJsonAdapter.toJson(AutoValue_Comment.java:30)
    at com.squareup.moshi.JsonAdapter$1.toJson(JsonAdapter.java:106)
    at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:46)
    at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:52)
    at EntryKt.main(start.kt:55)

I added JRAW to gradle this way:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    compile 'com.github.mattbdean:JRAW:dbbe319'
}

@mattbdean
Copy link
Owner

That NotImplementedError your seeing is from here. I'll take a look into it. All models implement Serializable, so you should be able to do something with that for now.

@Matt-84
Copy link
Author

Matt-84 commented Mar 6, 2018

Okay, thank you for the answer.

@mattbdean
Copy link
Owner

Okay so the commit I've just pushed lets you serialize basic Listings, so something like Listing<Comment> or Listing<Submission> (classes that have the @RedditModel annotation) will probably work fine. The issue arises when you try to deserialize something like Listing<PublicContribution>. JRAW does some black magic to deserialize models like that but it's even harder to serialize them. I've closed the issue for now but I may take a swing at it at a later date.

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

No branches or pull requests

2 participants