java - Jackson field annotation that ignores all properties but those listed -


is there jackson annotation fields and/or getters ignores properties of value of field except listed?

it @jsonignoreproperties when applied field, except instead of including listed properties, exclude listed properties.

e.g., if annotation named @jsonincludeproperties:

class {     final int = 1;     final int b = 1;     ... // final int c = 1; through final int y = 1;     final int z = 1; }  class b {     @jsonproperty     @jsonincludeproperties({"m", "o"})     a1;     @jsonproperty     a2; } 

would serialize to:

{     "a1": {         "m": 1,         "o": 1     },     "a2": {         "a": 1,         "b": 1,         ... // "c": 1, through "y": 1,         "z": 1     } } 

you can achieve using the jackson filters , custom annotation. here example:

public class jacksonincludeproperties {     @retention(retentionpolicy.runtime)     @interface jsonincludeproperties {         string[] value();     }      @jsonfilter("filter")     @jsonincludeproperties({"a", "b1"})     static class bean {         @jsonproperty         public final string = "a";         @jsonproperty("b1")         public final string b = "b";         @jsonproperty         public final string c =  "c";     }      private static class includepropertiesfilter extends simplebeanpropertyfilter {          @override         protected boolean include(final propertywriter writer) {             final jsonincludeproperties includeproperties =                     writer.getcontextannotation(jsonincludeproperties.class);             if (includeproperties != null) {                 return arrays.aslist(includeproperties.value()).contains(writer.getname());             }             return super.include(writer);         }     }      public static void main(string[] args) throws jsonprocessingexception {         final objectmapper objectmapper = new objectmapper();         final simplefilterprovider filterprovider = new simplefilterprovider();         filterprovider.addfilter("filter", new includepropertiesfilter());         objectmapper.setfilters(filterprovider);         system.out.println(objectmapper.writevalueasstring(new bean()));     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -