Roman
Roman

Reputation: 862

How do I exclude fields with Jackson not using annotations?

I need to exclude some fields by names before rendering. The list of fields is dynamic, so I can't use annotations.

I've tried to create custom serializer but I can't get field name there.

In GSON I've used ExclusionStrategy, but Jackson has no such functionality. Is there an equivalent?

Upvotes: 43

Views: 98436

Answers (5)

Ryan Bohn
Ryan Bohn

Reputation: 93

I wrote a library called Squiggly Filter, which selects fields based on a subset of the Facebook Graph API syntax. For example, to select the zipCode of the address field of the user object, you would use the query string ?fields=address{zipCode}. One of the advantages of Squiggly Filter is that as long as you have access to the ObjectMapper that renders the json, you do not to have to modify the code of any of your controller methods.

Assuming, you are using the servlet API, you can do the following:

1) Register a filter

<filter> 
    <filter-name>squigglyFilter</filter-name>
    <filter-class>com.github.bohnman.squiggly.web.SquigglyRequestFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>squigglyFilter</filter-name>
    <url-pattern>/**</url-pattern> 
</filter-mapping>

2) Initialize the ObjectMapper

Squiggly.init(objectMapper, new RequestSquigglyContextProvider());

3) You can now filter your json

curl https://yourhost/path/to/endpoint?fields=field1,field2{nested1,nested2}

More information on Squiggly Filter is available on github.

Upvotes: 4

Prakash Angappan
Prakash Angappan

Reputation: 129

If you have filters defined on two or more pojo, you can try this:

@JsonFilter("filterAClass") 
class AClass  
{       
  public String id = "42";  
  public String name = "Fred";  
  public String color = "blue";
  public int sal = 56;
  public BClass bclass = new BClass();  
}  

//@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonFilter("filterBClass") 
class BClass  
{  

  public String id = "99";  
  public String size = "90";  
  public String height = "tall";  
  public String nulcheck =null;  
}  
public class MultipleFilterConcept {
    public static void main(String[] args) throws Exception  
      {   
        ObjectMapper mapper = new ObjectMapper();
     // Exclude Null Fields
        mapper.setSerializationInclusion(Inclusion.NON_NULL);
        String[] ignorableFieldNames = { "id", "color" };  
        String[] ignorableFieldNames1 = { "height","size" };  
        FilterProvider filters = new SimpleFilterProvider()  
          .addFilter("filterAClass",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames))
          .addFilter("filterBClass", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames1));  
        ObjectWriter writer = mapper.writer(filters);
       System.out.println(writer.writeValueAsString(new AClass())); 

      }
}

Upvotes: 11

monitorjbl
monitorjbl

Reputation: 4350

I wrote a library to deal with a similar use case. I needed to programmatically ignore fields based on the user requesting data. The normal Jackson options were just too heavy-handed, and I hated the way it made my code look.

The library makes this a whole lot easier to understand. It allows you to simply do this:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.monitorjbl.json.JsonView;
import com.monitorjbl.json.JsonViewSerializer;
import static com.monitorjbl.json.Match.match;

//initialize jackson
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(JsonView.class, new JsonViewSerializer());
mapper.registerModule(module);

 //get a list of the objects
List<MyObject> list = myObjectService.list();

String json;
if(user.getRole().equals('ADMIN')){
    json = mapper.writeValueAsString(list);
} else {
    json = mapper.writeValueAsString(JsonView.with(list)
        .onClass(MyObject.class, match()
           .exclude("*")
           .include("name")));
}

System.out.println(json);

The code is available on GitHub, hope it helps!

Upvotes: 14

StaxMan
StaxMan

Reputation: 116472

Jackson relies on annotations for most things like this; but you do not have to directly annotate value classes. You can also use "mix-in annotations" (see http://www.cowtowncoder.com/blog/archives/2009/08/entry_305.html).

And then there are a few options you can use beyond basic @JsonIgnore (per-property) or @JsonIgnoreProperties (per-class), see http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html

Upvotes: 6

Programmer Bruce
Programmer Bruce

Reputation: 66935

The below example of excluding fields by name is from my blog post, Gson v Jackson - Part 4. (Search for the PropertyFilterMixIn.) This example demonstrates using a FilterProvider with a SimpleBeanPropertyFilter to serializeAllExcept a user-specified list of field names.

@JsonFilter("filter properties by name")  
class PropertyFilterMixIn {}  

class Bar  
{  
  public String id = "42";  
  public String name = "Fred";  
  public String color = "blue";  
  public Foo foo = new Foo();  
}  

class Foo  
{  
  public String id = "99";  
  public String size = "big";  
  public String height = "tall";  
}  

public class JacksonFoo  
{  
  public static void main(String[] args) throws Exception  
  {  
    ObjectMapper mapper = new ObjectMapper();  
    mapper.getSerializationConfig().addMixInAnnotations(  
        Object.class, PropertyFilterMixIn.class);  

    String[] ignorableFieldNames = { "id", "color" };  
    FilterProvider filters = new SimpleFilterProvider()  
      .addFilter("filter properties by name",   
          SimpleBeanPropertyFilter.serializeAllExcept(  
              ignorableFieldNames));  
    ObjectWriter writer = mapper.writer(filters);  

    System.out.println(writer.writeValueAsString(new Bar()));  
    // output:  
    // {"name":"James","foo":{"size":"big","height":"tall"}}  
  }  
} 

(Note: The relevant API may have changed slightly with a recent Jackson release.)

While the example does use a seemingly unnecessary annotation, the annotation is not applied to the fields to be excluded. (To help get the API changed to simplify the necessary configuration a bit, please don't hesitate to vote for implementation of issue JACKSON-274.

Upvotes: 44

Related Questions