Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

IllegalStateException when Gson returns null #44

Open
RouNNdeL opened this issue May 30, 2020 · 0 comments
Open

IllegalStateException when Gson returns null #44

RouNNdeL opened this issue May 30, 2020 · 0 comments

Comments

@RouNNdeL
Copy link

When the Gson's native fromJson method returns null (ex. when the JSON string is empty), a

java.lang.IllegalStateException: fromJson(json, typeToken<T>()) must not be null

exception is thrown due to the extension function being defined as Gson.fromJson(json: String): T. I developed a workaround for it (note the nullable type T)

inline fun <reified T: Any> Gson.fromJsonOrNull(json: Reader): T? = fromJson(json, typeToken<T>())

I'm unsure whether this would be the correct approach, thus I'm not submitting a PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant