How can I tell jackson to ignore a property for which I have no control over the source code?

In short, one of my entities has a GeometryCollection that throws an exception when you call "getBoundary" (why this is another book, now let's say how it works).

Is there a way I can say that Jackson should not include this particular getter? I know that I can use @JacksonIgnore when I own / manage code. But this is not the case; Jackson finishes reaching this point by continuously serializing the parent objects. I saw the filter option in Jackson's documentation. Is this a plausible solution?

Thank!

+98
java json jackson
Sep 14 '11 at 18:52
source share
9 answers

You can use Jackson Mixins . For example:

class YourClass { public int ignoreThis() { return 0; } } 

With this mixin

 abstract class MixIn { @JsonIgnore abstract int ignoreThis(); // we don't need it! } 

With this:

 objectMapper.getSerializationConfig().addMixInAnnotations(YourClass.class, MixIn.class); 

Edit:

Thanks to the comments, with Jackson 2.5+, the API has changed and should be called with objectMapper.addMixIn(Class<?> target, Class<?> mixinSource)

+142
Sep 14 2018-11-11T00:
source share

Another possibility is that if you want to ignore all unknown properties, you can configure the display as follows:

 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
+60
Sep 17 '13 at 12:55 on
source share

Using Java Class

 new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 

Using annotations

 @JsonIgnoreProperties(ignoreUnknown=true) 
+22
Jul 23 '15 at 15:57
source share
 public @interface JsonIgnoreProperties 

Annotations that can be used to suppress serialization of properties (during serialization) or to ignore the processing of JSON properties read (during deserialization).

to exclude serialization or deserialization of given fields, you can use:

 @JsonIgnoreProperties(ignoreUnknown=true) 
+9
Nov 21 '14 at 10:42
source share

Mix annotations work pretty well here, as already mentioned. Another possibility that goes beyond the @JsonIgnore property is to use @JsonIgnoreType if you have a type that should never be included (i.e. if all instances of GeometryCollection properties should be ignored). Then you can either add it directly (if you control the type) or use mixing, for example:

 @JsonIgnoreType abstract class MixIn { } // and then register mix-in, either via SerializationConfig, or by using SimpleModule 

This may be more convenient if you have many classes that have one accessory available “IgnoredType getContext ()” or so (which is the case for many frameworks)

+8
Sep 15 '11 at 17:23
source share

Annotation-based approach is better. But sometimes manual control is required. For this you can use without the ObjectWriter method.

 ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) ObjectWriter writer = mapper.writer().withoutAttribute("property1").withoutAttribute("property2"); String jsonText = writer.writeValueAsString(sourceObject); 
+8
Apr 02 '17 at 12:00
source share

I had a similar problem, but it was related to the Hibernate bidirectional relationship. I wanted to show one side of the relationship and programmatically ignore the other, depending on which point of view I was dealing with. If you cannot do this, you will get nasty StackOverflowException s. For example, if I had these objects

 public class A{ Long id; String name; List<B> children; } public class B{ Long id; A parent; } 

I would like to programmatically ignore the parent field in B if I look at A, and ignore the children field in A if I look at B.

I started using the mixin to do this, but it quickly becomes terrible; there are so many useless classes around you that exist solely for formatting data. I ended up writing my own serializer for cleaner processing: https://github.com/monitorjbl/json-view .

This allows you to programmatically indicate which fields to ignore:

 ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(JsonView.class, new JsonViewSerializer()); mapper.registerModule(module); List<A> list = getListOfA(); String json = mapper.writeValueAsString(JsonView.with(list) .onClass(B.class, match() .exclude("parent"))); 

It also allows you to easily specify very simplified representations using wildcards:

 String json = mapper.writeValueAsString(JsonView.with(list) .onClass(A.class, match() .exclude("*") .include("id", "name"))); 

In my original case, the need for simple ideas like this was to show a minimum minimum for the parent / child, but it also became useful for our role-based security. Less privileged representations of objects are needed to get less information about the object.

It all comes from a serializer, but I used Spring MVC in my application. So that he handles these cases correctly, I wrote an integration that you can incorporate into existing Spring controller classes:

 @Controller public class JsonController { private JsonResult json = JsonResult.instance(); @Autowired private TestObjectService service; @RequestMapping(method = RequestMethod.GET, value = "/bean") @ResponseBody public List<TestObject> getTestObject() { List<TestObject> list = service.list(); return json.use(JsonView.with(list) .onClass(TestObject.class, Match.match() .exclude("int1") .include("ignoredDirect"))) .returnValue(); } } 

Both are available at Maven Central. I hope this helps someone else, this is a particularly ugly problem with Jackson, which did not have a good solution for my case.

+5
Jun 14 '15 at 16:06
source share

Another good point is to use @JsonFilter . Some details are here http://wiki.fasterxml.com/JacksonFeatureJsonFilter

0
Sep 01 '15 at 13:04 on
source share

If you want to ALWAYS exclude specific properties for any class, you can use the setMixInResolver method:

  @JsonIgnoreProperties({"id", "index", "version"}) abstract class MixIn { } mapper.setMixInResolver(new ClassIntrospector.MixInResolver(){ @Override public Class<?> findMixInClassFor(Class<?> cls) { return MixIn.class; } @Override public ClassIntrospector.MixInResolver copy() { return this; } }); 
0
Dec 11 '18 at 3:06
source share



All Articles