Skip to content

Automatically detect target type (Class) to deserialize to #5064

@rohanlopes20

Description

@rohanlopes20

Is your feature request related to a problem? Please describe.

Currently to deserialize object in ObjectMapper we use below syntax -

ObjectMapper m = new ObjectMapper();
final String JSON = "{ \"x\" : 3 }";

Bean bean = m.readValue(JSON, Bean.class);

Can ObjectMapper automatically detect the class to be deserialized based on the return type?

This is already implemented in Mockito mockito/mockito#2779

Describe the solution you'd like

Below method can be added in ObjectMapper class to detect class automatically.

    public <T> T readValue(String content, T... reified) throws JsonProcessingException {
        if (reified.length > 0) {
            throw new IllegalArgumentException("Please don't pass any values here. Java will detect class automatically.");
        }
        return readValue(content, _typeFactory.constructType(getClassOf(reified)));
    }

Usage example

ObjectMapper m = new ObjectMapper();
final String JSON = "{ \"x\" : 3 }";

Bean bean = m.readValue(JSON);

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    to-evaluateIssue that has been received but not yet evaluated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions