Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mletynski opened this issue Sep 1, 2013 · 4 comments
Milestone

Comments

@mletynski
Copy link

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.

@cowtowncoder
Copy link
Member

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.

@mletynski
Copy link
Author

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

@cowtowncoder
Copy link
Member

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 cowtowncoder changed the title Serialization of transient fields with public getters Serialization of transient fields with public getters (add MapperFeature.PROPAGATE_TRANSIENT_MARKER) May 14, 2015
@cowtowncoder cowtowncoder added this to the 2.6.0 milestone May 14, 2015
@cowtowncoder
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants