Skip to content

NullPointerException in ObjectMapper.valueToTree(UUID) #5225

@kiryong-lee

Description

@kiryong-lee

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When calling mapper.valueToTree(UUID.randomUUID()), a NullPointerException is thrown inside the private _writeAsBinary(...) method.
The root cause is that _writeCapabilities is null when ctxt.isEnabled(...) is checked.

Version Information

3.0.0-rc5

Reproduction

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.valueToTree(UUID.randomUUID());

Expected behavior

No NullPointerException is thrown

Additional context

The root cause is that _assignGenerator isn’t called when valueToTree is invoked; since valueToTree isn’t actually performing serialization, it’s appropriate to handle it by not calling _assignGenerator.

here seem to be two possible approaches:

  1. Null-check in isEnabled
    Add a null check for _writeCapabilities inside isEnabled, and return false if it’s null. This is safe because valueToTree doesn’t actually serialize—instead, the JsonNode simply holds the UUID as a string.
public final boolean isEnabled(StreamWriteCapability cap) {
    return _wirteCapabilities != null && _writeCapabilities.isEnabled(cap);
}
  1. Default initialization in the SerializationContext
    Initialize _writeCapabilities with a default value when the SerializationContext is created. This ensures it’s never null, but may be wasteful since a fresh capabilities object will be created anyway once actual serialization occurs.
JacksonFeatureSet<StreamWriteCapability> _writeCapabilities
        = JacksonFeatureSet.fromDefaults(StreamWriteCapability.values());

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions