-
Notifications
You must be signed in to change notification settings - Fork 37
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
Offer payload pattern for polymorphic adapters #164
Comments
This would solve my actual problem Im currently struggle with. I do have kinda of nested json to parse: It's kind of nested /multi level polymorphic adapter. At the moment the way I managed it, is with a custom adapter that thanks to reader.peekJson() make kind of raw parsing to figure out that is the value of the key that discriminate the type of the data class that has to be used.
I currently take advantage of the annotation @JSON(ignore = true) in the outer class to parse all the other value with the generated adapter, and into my custom one, perform kind of two way parsing exploring the json until i found the key that is ignored and then parsing using the "inner" generated adapter on the object using a when statement
What I don't really like with this approach is that I need to manually add a new check case for the when statement inside the adapter every time a new Types is added into the json response |
@ZacSweers Is this simple case achievable using only |
This issue is open because it's not supported currently |
Moshi's polymorphic adapter works just for the tag pattern, but another common one is the tag payload pattern (Reddit and GitHub both do this).
Where
payload
is based onsomeType
's type.This is easy to do in simple cases where the type and payload are the only values. It's much harder to do it for cases where they're mixed in with some other type though, like so
I think a clever solution in this case would be a two-stage adapter. Stage one would find the type, store it in the reader, then delegate to the "real" adapter and make the generated payload adapter look for this breadcrumb. In effect, this would mean generating two adapters. One that takes control of
GitHubActivityEvent
and one that handlesGitHubActivityEventPayload
. This likely requires an extra adapter to look for these "type holder" types to handle delegation safely.The text was updated successfully, but these errors were encountered: