-
Notifications
You must be signed in to change notification settings - Fork 761
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
PolymorphicJsonAdapterFactory withDefaultSubtype and withFallbackSubtype support #784
Comments
|
I worry this would be kind of a footguns since it assumes they all have compatible data fields for the default subtype. I've always just seen unknown fallbacks used with basically like a singleton "unknown" instance to signal it was unrecognized, otherwise you could have problems trying to deserialized unknown data and hoping it works |
abstract class PromoBanner {
String title;
int background_color;
static final class ItemPromoBanner {
String message;
}
static final class CollectionPromoBanner {
int count;
}
static final class DefaultUnknownPromoBanner {
}
} I imagine a case like this, where I can still use the some of the dynamic data that I know about ( |
Hey guys, do you have any plan on having this? It would be really useful for a scenario kinda similar to the one @NightlyNexus pointed out. |
For the example given, you can just make a singleton instance of .withUnknownHandler { label: String, reader: JsonReader, moshi: Moshi ->
// Do whatever you want here, return a subtype of the base type though
} |
Thanks for the fast reply, let me clarify the scenario a bit more.
In addition we want to handle the fallback scenario by logging the data we don't "understand". So, ideally we want to have an
Having it as an Having said all that, I agree with you that the api could get gnarly with different fallback strategies, although something like Edit: disconsider what I said on
|
I'm wondering if it would be "just" simpler to provide an entry point that allow to pass a This should provide enough flexibility to developers as well as letting developers working with moshi building-blocks already familiar to them. The interface that I have in mind is something like object CardAdapterFactory {
fun get() =
PolymorphicJsonAdapterFactory.of(Card::class.java, "cardType")
.withSubtype(BannerCard::class.java, "banner")
.withSubtype(SmallBannerCard::class.java, "smallbanner")
.withSubtype(SimpleHeaderCard::class.java, "simpleheader")
.withFallbackJsonAdapter(object: JsonAdapter<Card> { ... })
} I ended up here as I was looking for a workaround to the |
Looks like this can be closed now 👍 |
Issue #756(How to handle new polymorphic label in backward compatible way?) is folded into #739(PolymorphicJsonAdapterFactory could have a fallback class like with enum) which is closed by the PR #778.
The newly added method
withDefaultValue
falls back to default value(like an error message) but can't fall back to a default type(or label).Code example:
now we can do this
we can't do this without breaking backward compatility
The text was updated successfully, but these errors were encountered: