@JsonIgnoreProperties and @JsonProperty

1,792 views
Skip to first unread message

Nick Williams

unread,
Aug 24, 2013, 5:39:47 PM8/24/13
to us...@jackson.codehaus.org, jackso...@googlegroups.com
I have a set of entities whose byte code is manipulated by Hibernate prior to use. Hibernate adds an unspecified number of properties to these entities, and I want both JAXB and Jackson to ignore these properties. For JAXB, the solution was to annotate the entity class with @XmlAccessorType(XmlAccessType.NONE) and then specifically annotate each property that I DO want serialized/deserialized with @XmlElement (or @XmlAttribute, as the case may be). This way, JAXB ONLY processes the properties that I have specifically told it to.

I'm anticipating a similar situation with Jackson. I believe I need to annotate the entity with @JsonIgnoreProperties and then each property I want serialized/deserialized with @JsonProperty. Is this correct, or am I approaching this the wrong way?

Nick

Tatu Saloranta

unread,
Aug 25, 2013, 11:15:34 PM8/25/13
to Nick Williams, us...@jackson.codehaus.org, jackso...@googlegroups.com
The idea is valid, but annotation to use is different: with @JsonIgnoreProperties, one has to either specify list of things to ignore, or specify that "anything not mapping is ok to ignore".
But there is `@JsonAutoDetect` that can do what you want, by changing requirements for auto-detection -- basically you will indicate that no auto-detection is to be performed.

-+ Tatu +-




Nick

--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nick Williams

unread,
Aug 25, 2013, 11:35:57 PM8/25/13
to Tatu Saloranta, us...@jackson.codehaus.org, jackso...@googlegroups.com
Thanks.

FYI, this was made way harder between versions 1.x and 2.x. In 1.x, you could disable all auto detection simply with @JsonAutoDetect(JsonMethod.NONE). As of 2.x, it appears you have to do the following:

@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE,
fieldVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE)

This is HUGELY unfriendly. Unless I'm misunderstanding something, Jackson is in desperate need of something that actually replaces @JsonAutoDetect(JsonMethod.NONE) (i.e., disables all auto-detection) with one line of code. I can disable all auto-detection in JAXB with @XmlAccessorType(XmlAccessType.NONE). I should be able to disable all auto-detection similarly with Jackson.

Nick

Tatu Saloranta

unread,
Aug 25, 2013, 11:44:38 PM8/25/13
to Nick Williams, us...@jackson.codehaus.org, jackso...@googlegroups.com
There are couple of ways around this: with annotations, you may want to consider "annotation bundles", with `@JacksonAnnotationsInside`, which lets you define aliases for commonly used sets of annotations.
Or, if this is setting you want to be the default, use `ObjectMapper.setVisibility()` to define baseline to be used for all introspection, unless overridden locally by `@JsonAutoDetect`.

-+ Tatu +-

Nick Williams

unread,
Aug 25, 2013, 11:50:56 PM8/25/13
to Tatu Saloranta, us...@jackson.codehaus.org, jackso...@googlegroups.com
These may be alternatives, yes, but I would suggest not ideal alternatives. I would like to propose an easy-to-use @JsonIgnorePropertiesByDefault or similar annotation that applies only to the class it annotates. It would, of course, be overridden by any @JsonProperty or analog annotation on individual properties.

Just my $0.02.

Nick

Tatu Saloranta

unread,
Aug 26, 2013, 12:24:17 AM8/26/13
to Nick Williams, us...@jackson.codehaus.org, jackso...@googlegroups.com
While I can see why this looks like something you'd want, there are reasons for this change in 2.0, to support almost any combination of visibility levels -- 1.x had problems modifying just one category (like disabling detection of just setters).
Combining multiple levels of defaulting, coupled with limitations of Java annotations model make it difficult to have system that supports all use cases.

But feel free to file an RFE for the idea. If others feel there is merit to this, perhaps an improvement could be found, whether that'd be a new annotation, or a new property for `@JsonAutoDetect`.

One last suggestion: @JacksonAnnotationInside is something I would recommend for use in this case -- it will make it very easy to use a single annotation over multiple classes; and since it is a general purpose annotation, it is rather useful for other similar cases where seemingly simple configuration requires multiple jackson annotations.

-+ Tatu +-

Reply all
Reply to author
Forward
0 new messages