google / google/gson

Is it possible to ignore/null a field during gson.fromJson if field type is incorrect?

Open
#1,346 2 comments 4 reactions 0 assignees View on GitHub
needs-info question
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

I've encountered an issue where our server returns an incorrect response and during our call to `gson.fromJson()`, gson throws an IllegalStateException. This is a specific edge case that we normally don't encounter (our calls are generally wrapped in a try/catch for this exception). Ideally, we'd like `gson.fromJson()` to fail gracefully when encountering a type mismatch, setting that field to null instead of throwing an exception and causing a crash/the whole response to be null. Is there existing functionality/a way to achieve this? If not, is this something that could be added to gson in the future?

[jrp9988](https://github.com/jrp9988) can provide more info on the issue if needed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.