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

Allow overriding of transient with explicit inclusion with @JsonProperty and/or way to ignore transient #1184

Closed
lhunath opened this issue Apr 5, 2016 · 3 comments
Milestone

Comments

@lhunath
Copy link

lhunath commented Apr 5, 2016

The Java language has the transient property which indicates that certain fields should not take part in its Java object serialization framework of which Serializable is the hallmark.

Jackson appears to conflate Java's object serialization configuration with its own by forcing all transient fields to be excluded from any Jackson serialization. While it is a good idea for the transient keyword to provide a hint to Jackson that the field is uninteresting for its own serialization, the problem is really that this behaviour cannot be turned off. It is therefore impossible to include transient fields in a Jackson serialization process.

This becomes obvious when the application uses an object for two distinct types of serialization.

Case in point, I want to serialize an object in Android for storage within its SharedPreferences by serializing it using Java's object serialization (well supported by SharedPreferences). I also want to serialize this object into JSON for submission to a remote server via HTTP. When I serialize the object into JSON, I need to include a few data points that should not be included in the SharedPreferences storage, hence my setting these fields as transient (to exclude them from the latter, Java object serialization). Unfortunately, this makes Jackson ignore them wholesale, even when annotated @JsonProperty (which is counter-intuitive - at a glance, one would expect a Jackson-annotated property to override a non-specific language keyword). There is also no way to change the configuration of the Jackson serialization mechanism to stop ignoring the transient keyword.

As such, this is a feature request to provide the ability to disable filtering out all transient fields from Jackson's serialization process.

@cowtowncoder cowtowncoder changed the title Jackson conflates Java object serialization configuration and its own. Allow overriding of transient with explicit inclusion with @JsonProperty and/or way to ignore transient Apr 13, 2016
@cowtowncoder
Copy link
Member

Reworded to reflect that a fix could be something like letting @JsonProperty override transient, or perhaps adding a MapperFeature to allow disabling use of transient.
Former seems like a better option, but will need to see how implementation works: one challenge is that since transient is not annotation, its introspection is done at a deeper level and is more difficult to modify -- anything related to annotation handling is typically easier to deal with.

@cowtowncoder
Copy link
Member

Quick note: addition of simple accessor matching field will make that property accessible (unless MapperFeature.PROPAGATE_TRANSIENT_MARKER is enabled). By default only access via specified field is prevented if so.

I will still investigate to see if there's a relatively easy way to further allow @JsonProperty to override transient directly.

@cowtowncoder cowtowncoder added this to the 2.8.0 milestone May 20, 2016
@cowtowncoder
Copy link
Member

Turned out to be relatively easy to use "has explicit marker" to override transient for field, so now should work better.

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