Skip to content

Default Values for Optionals Or Reject All NullsΒ #988

Closed
@NTCoding

Description

@NTCoding

Hello,

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 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions