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 JsonDeserializer to provide default value for "missing" properties #618

Open
stevenschlansker opened this issue Nov 18, 2014 · 3 comments
Labels
most-wanted Tag to indicate that there is heavy user +1'ing action

Comments

@stevenschlansker
Copy link

This came up in https://github.com/FasterXML/jackson-datatype-jdk8/issues/2
When deserializing a bean containing Optional<T> fields, the value Optional.empty() makes more sense than filling in null for properties that are entirely missing from the JSON.

@stevenschlansker
Copy link
Author

(Once this issue is closed, it may make sense to re-open the above issue)

@tea-dragon
Copy link
Contributor

I tried really hard to support this using existing features like injectables, default "null" values, and so on, but they have pretty inconsistent behavior under common cases like creator vs field properties. Even addressing those still leaves a couple ambiguous cases though. I ended up side-stepping them and just using some fairly invasive wrapping/delegating deserializers and doing full TreeNode parsing. It is more expensive but those use cases did not involve frequent object ser/deser -- mostly configuration type stuff.

The best I could come up with for maintaining streaming (no full TreeNode parsing) with reasonable performance was passing around a bit field indexed by property id that is toggled whenever a property is found and deserialized. A single primitive long, for instance, could track up to 64 fields that are either required or have special defaults, and would certainly have negligible overhead... but then you have an awkward, hard coded limit so might be worth abstracting anyway.

It wasn't really possible to do that from the APIs made available for customization though. Or at least not without being even more incompatible with other components.

@cowtowncoder
Copy link
Member

FWTW, Jackson 2.6 actually does support injection of default values for Creator properties.
Default value is defined as value that matching JsonDeserializer method getNullValue(DeserializationContext ctxt) returns. I have also ensured that all "Optional" type serializers will return suitable "absent" instance for these cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
most-wanted Tag to indicate that there is heavy user +1'ing action
Projects
None yet
Development

No branches or pull requests

3 participants