@JsonAnyGetter with a map which can contain keys for which a specific getter exists

12 views
Skip to first unread message

Raffaele Gambelli

unread,
Jul 10, 2019, 7:55:08 PM7/10/19
to jackson-user
Hi all,

I
'm experiencing some jon fields duplication problems using @JsonAnyGetter annotation.

If I have a class with a name attribute and its getter and a map annotated with @JsonAnyGetter containing an entry having "name" as key, resulting json has two fields "name".

Consider this simple and incomplete snippet:

public class JsonObject {

   
private String name;    
   
private final Map<String, Object> map = new HashMap<>();

   
public String getName() {
       
return (String) get("name");
   
}
 
   
@JsonAnyGetter
   
public Map<String, Object> get() {
       
return map;
   
}

   
@JsonAnySetter
   
public Object put(String key, Object value) {
       
return map.put(key, value);
   
}
}

Is there a way to ignore my getName() method or is there a way to remove the duplication?
Please consider that my example is very simple, my real case is more complex, I have some classes which extend a class containing the map, children classes contain specific getter and setter (e.g. getName() setName()) which work directly with the map attribute present in parent class.

Thank you very much, best regards
Inserisci qui il codice...


Tatu Saloranta

unread,
Jul 14, 2019, 3:30:19 AM7/14/19
to jackson-user
No, unfortunately there is currently no way to instruct Jackson to
ignore entries from `Map` returned by `@JsonAnyGetter` annotated
method.

But maybe it is worth filing a feature request issue at Github? This
sounds like a reasonable feature request.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages