SalomonBrys / Kotson

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

fromJson should return not nullable #9

Closed yoavst closed 8 years ago

yoavst commented 9 years ago

Since Gson.fromJson return null only if string is null, it can return not a nullable type.

SalomonBrys commented 8 years ago

Unfortuately, that's not true as Gson().fromJson<Whatever>("null") == null.

So fromJson does returns null when given the String "null".

yoavst commented 8 years ago

I think it should be based on null ability of the generic parameter

SalomonBrys commented 8 years ago

What do you mean ?

yoavst commented 8 years ago

In my code, I use `!! immediately after every fromJson. If I would like it to be nullable, I would use Whatever?.

SalomonBrys commented 8 years ago

I see your point.

SalomonBrys commented 8 years ago

v1.7.0 corrects this

kubanychlocal commented 6 years ago

Needs simple sample of using Kotson. This does not work `import com.github.salomonbrys.kotson. import com.google.gson.

fun main (args: Array ) { val obj:JsonObject = jsonObject( "name" to "kotson", "files" to 4 ) println(" ${obj}") class Person(public val obj: JsonObject) { val name: String by obj.byString("name") val files: Int by obj.byInt("files")
} val person=Gson().fromJson(obj,Person ::class.java) }`