Default Values for Optionals Or Reject All Nulls
See original GitHub issueHello,
I think your library is great but it doesn’t quite work for one Scala use case. When there is a property of type Option<T> I want it to set a default value of none even when FAIL_ON_MISSING_CREATOR properties is true.
I would like to do this conventionally without having to apply any annotations to the code. Could you give me advice on the best way to implement this and I will do the work and send a pull request.
Something in here seems sensible:
protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingException
{
// First: do we have injectable value?
Object injectableValueId = prop.getInjectableValueId();
if (injectableValueId != null) {
return _context.findInjectableValue(prop.getInjectableValueId(),
prop, null);
}
// Second: required?
if (prop.isRequired()) {
throw _context.mappingException("Missing required creator property '%s' (index %d)",
prop.getName(), prop.getCreatorIndex());
}
if (_context.isEnabled(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES)) {
throw _context.mappingException("Missing creator property '%s' (index %d); DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES enabled",
prop.getName(), prop.getCreatorIndex());
}
// Third: default value
JsonDeserializer<Object> deser = prop.getValueDeserializer();
return deser.getNullValue(_context);
}
Perhaps a new service “defaultCreatorValueProvider” or similar?
If there is a better way, or even an existing way, please let me know.
Thanks
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top Results From Across the Web
SQL: Using NULL values vs. default values - Stack Overflow
So e-mail column can have a null value (because e-mail is optional), while postCount column is NOT NULL but has default value 0...
Read more >Swift Optionals: How to Use them (With Examples) - Programiz
However there is another data type in Swift called Optional, whose default value is a null value ( nil ). You can use...
Read more >KIP-581: Value of optional null field which has default value
Generally, when an optional field which has default value is null , we can treat it as null or default value , it...
Read more >Terraform Optional Variables and Attributes — Using Null and ...
This feature actually forces the attribute default values to null, which means they will not be brought to the plan if no corresponding...
Read more >Guide To Java 8 Optional | Baeldung
Note that we used the isPresent() method to check if there is a value inside the Optional object. A value is present only...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi,
I understand that you are a Java developer and perhaps are not familiar with monads and optionals (apologies if that is condescending). However, an Option[T] is explicitly stating that the type is optional - part of the type’s DNA is that there may be no value for it - and hence a missing constructor value means it should default to a value of None.
It is explained in more detail here: https://github.com/FasterXML/jackson-module-scala/issues/203
If an object has two properties - one is optional and one is not - the type system is expressing what is mandatory - the optional values. Is there any way to do this currently - if the value is missing throw an exception unless it is an Option?
In the “other case” you mention, then it would not fail if a value was missing from a non-optional type and the value would default to null. Would you consider a pull request with a new deserialization option: “REJECT_ALL_NULL_VALUES” or are you completely against adding new functionality that helps to support Scala optionals?
Replaced with #1402, which I think would provide all the pieces needed. Scala module might need something additional (and definitely needs to support this); but from
jackson-databind
perspective I think this should be sufficient. Planning to get implemented for 2.9.