FasterXML / FasterXML/jackson-databind

Automatically detect target type (`Class`) to deserialize to

Open
#5,064 20 comments 2 reactions 0 assignees View on GitHub
3.x
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

### 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 https://github.com/mockito/mockito/pull/2779

### Describe the solution you'd like

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

```
public 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_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.