https://kotlinlang.org logo
#android
Title
# android
j

jan.rabe

01/13/2017, 1:28 PM
@dragas I said deserialization but i ment serialization 😕
Copy code
@Test
    fun gsonNumberTest() {

        val json = "{\n" +
                "  \"n1\" = 1,\n" +
                "  \"n2\" = 1.0\n" +
                "}"

        data class Wrapper1(
                var n1: Int? = null,
                var n2: Float? = null)

        data class Wrapper2(
                var n1: Number? = null,
                var n2: Number? = null)

        val gson = GsonBuilder().create()

        val w1: Wrapper1 = gson.fromJson(json, Wrapper1::class.java)

        Assert.assertEquals(1, w1.n1)
        Assert.assertEquals(1.0f, w1.n2)

        val w2: Wrapper2 = gson.fromJson(json, Wrapper2::class.java)

        Assert.assertEquals(1, w2.n1) // w2.n1 is 'null'
        Assert.assertEquals(1.0f, w2.n2) // w2.n2 is 'null'
    }