SalomonBrys / Kotson

Kotlin bindings for JSON manipulation via Gson
MIT License
709 stars 37 forks source link

IllegalStateException when Gson returns null #44

Open RouNNdeL opened 4 years ago

RouNNdeL commented 4 years ago

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.