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

Question: select subtype when serializing in case multiple names are present for the same type #4064

Closed
amseager opened this issue Aug 2, 2023 · 4 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@amseager
Copy link

amseager commented Aug 2, 2023

Describe your Issue

Suppose I have smth like this (took it from this issue):

@JsonTypeInfo(  
    use = JsonTypeInfo.Id.NAME,  
    include = JsonTypeInfo.As.PROPERTY,  
    property = "animalType",
    visible = true)  
@JsonSubTypes({  
    @Type(value = Mammal.class, name = "ZEBRA"),  
    @Type(value = Mammal.class, name = "SHEEP"),  
    @Type(value = Bird.class, name = "PARROT"),
        @Type(value = Bird.class, name = "OWL")})
public class Animal{

Suppose that it also wrapped in some other object:

public class AnimalWrapper {
    public Animal animal;
}

Now you want to convert it to e.g. HashMap<String, Object>:

Map<String, Object> result = objectMapper.convertValue(animalWrapperObject, new TypeReference<>() {
            });

As I understood, in case of "Mammal" object, the very first name ("ZEBRA") will be selected as a key (string).

The question is about configuring jackson somehow to be able to select other values ("SHEEP" etc.). Maybe some settings are already exist for it?

@amseager amseager added the to-evaluate Issue that has been received but not yet evaluated label Aug 2, 2023
@cowtowncoder
Copy link
Member

Currently this should fail on attempt to serialize, due to ambiguity, I think.
If not that should be the first name.
And no, there is no way to change the behavior, whichever it is.

One thing that might work is to use explicit @JsonTypeName on Mammal: since it only takes one value, that should be the primary name to use.

@amseager
Copy link
Author

amseager commented Aug 3, 2023

@cowtowncoder thank you for the response.
In my case, this is a generated class, I can't change it, that's why I was thinking about some workarounds with jackson. Will try to do smth else with it then.
I'm closing this issue.

@amseager amseager closed this as completed Aug 3, 2023
@JooHyukKim
Copy link
Member

JooHyukKim commented Aug 3, 2023

@amseager Probably you know this, but sharing it just in case.

If you manage to modify the code/class generation behavior, plz take note that there is property @JsonSubTypes.Type.names of type String[] used like below. That way you don't need to duplicate class.

@JsonSubTypes({
            @JsonSubTypes.Type(value = Mammal.class, name = "ZEBRA", names = {"SHEEP", "HORSE"}),
            @JsonSubTypes.Type(value = Fish.class)
    })

@cowtowncoder
Copy link
Member

@amseager Another thing that might help is use of "mix-in annotations" which do allow you to associate any annotations with classes you cannot modify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

3 participants