jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Serialization of transient fields with public getters (add MapperFeature.PROPAGATE_TRANSIENT_MARKER)

Open mletynski opened this issue 13 years ago • 4 comments

Currently when my field is transient (with a public getter) it is serialized. I can put @JsonIgnore annotation to tell jackson to not serialize it. But with this approach my object is coupled directly with jackson. Is it possible to add such functionality to "tell" Jackson globally to not serialize transient fields ? Via ObjectMapper#SerializationFeature/Visibility or something different.

mletynski avatar Sep 01 '13 08:09 mletynski

The problem is that there is no assumption that 'transient' in field means that the logical property, as accessed using getter/setter is to be ignored. It would be possible to change this logic, but that is a backwards incompatible functional change; so most likely it should be a new MapperFeature. I can take this issue to mean requesting of such a change.

In short term, what you can do is to sub-class JacksonAnnotationIntrospector and override hasIgnoreMarker() to return true for fields that are transient. This would then work as if field had explicit @JsonIgnore. So this should make it work the way you want I think.

cowtowncoder avatar Sep 04 '13 04:09 cowtowncoder

Thanks, it would be good to have such functionality. I will try now with your solution.

mletynski avatar Sep 05 '13 04:09 mletynski

Looked at this briefly, and was about to add a MapperFeature, but then remembered the underlying complication: removal of transient fields occurs too early in the processing (by AnnotatedClass). It would need to occur at a later point. Will see how easy that would be.

cowtowncoder avatar May 14 '15 05:05 cowtowncoder

Turned out easy enough to implement: the default behavior did not change, but if enabling: MapperFeature.PROPAGATE_TRANSIENT_MARKER, transient is handled same way as a @JsonIgnore on field would, leading to removal of the whole logical property unless there is an explicit @JsonProperty to counteract.

Default handling was not changed due to backwards-compatibility concerns, although for Jackson 3.x we could definitely consider changing of defaults.

cowtowncoder avatar May 14 '15 06:05 cowtowncoder