-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add JsonTypeInfo.Id.SIMPLE_NAME which defaults type id to Class.getSimpleName()
#4061
Comments
@ooraini Have you checked out Or could you provide some usage example and what it's trying to solve? Because there might be solution for existing use case and it will be clearer what the feature will be. Hopefully without using |
Hi @JooHyukKim, I wanted to frame the request more generally without it being too specific on Given:
And serializing
With minimal class:
This is not specific to sealed and the general case is similar:
Again, I want to emphasis that this is not specific to sealed, it would work really well with sealed. I mentioned |
@ooraini I see, thank you for the explanation 👍🏻 Suggested use-case seem to make sense and general. Will there also be other values (aside from WDYT, @cowtowncoder? |
I am bit confused about shown use case:
since this already declares names to use, so use of Second: when using So I guess I am not quite sure what would be provided here. But if such an option makes sense the only thing I'd suggest is that instead of |
More minimal reproduction would be below, public class TestPolymorphic extends BaseMapTest {
@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME) // <<-----"NEW"
@JsonSubTypes({
@JsonSubTypes.Type(value = Sub4061A.class),
@JsonSubTypes.Type(value = Sub4061B.class)
})
static class Super4061 { }
static class Sub4061A extends Super4061 { }
static class Sub4061B extends Super4061 { }
public void test() throws Exception {
// case (1) JsonTypeInfo.Id.NAME
assertEquals(
"{\"@type\":\"TestPolymorphic$Sub4061A\"}",
newJsonMapper().writeValueAsString(new Sub4061A()));
// case (2) JsonTypeInfo.Id.SIMPLE_CLASS_NAME
assertEquals(
"{\"name\":\"Sub4061A\"}",
newJsonMapper().writeValueAsString(new Sub4061A()));
}
}
+1 |
Hi @cowtowncoder, Regarding the example, it would not work if you used Indeed,
👍🏻 for Hope this helps. |
@JooHyukKim |
Ah. Ok, I misremembered defaulting; could have sworn it uses simple class name for No objections to adding I also started re-thinking whether it really ought to be |
Can I get assigned on this? I have encountered a couple usecases, where I would have used |
@JooHyukKim Feel free work on it, I haven't gotten around the code base yet 😅 |
First part is probably local modification of |
Made a PR #4065, could you PTAL, @cowtowncoder? If implementation seems okay, I will file a PR in |
Vote/Naming Idea WantedIn the PR FasterXML/jackson-annotations#234 (comment), we are discussing what the default identifier name of
EDIT: also created a thread in https://groups.google.com/g/jackson-dev/c/jWK4eh1cBL8, in case someone provides for more ideas 😆 |
@JooHyukKim If this purely a preference, then my vote goes to @type. |
@ooraini Thanks! there s already two votes on |
Yes, I like use of |
Just merged the annotation PR; added a comment for minor test change for #4065 -- I think this can soon be merged. |
Class.getSimpleName()
Is your feature request related to a problem? Please describe.
Add JsonTypeInfo.Id.SIMPLE_NAME that would use Class::getSimpleName for type ids.
My use case is sealed hierarchies where there is single sealed super interface, it would result in cleaner type ids. My work around:
Describe the solution you'd like
JsonTypeInfo.Id.SIMPLE_NAME
Usage example
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: