Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to parse int in my data class #332

Open
ajay-dewari opened this issue Apr 24, 2021 · 7 comments
Open

Not able to parse int in my data class #332

ajay-dewari opened this issue Apr 24, 2021 · 7 comments

Comments

@ajay-dewari
Copy link

In my data calss, I have a field quantity defined as below:

val quantity : Int

in the json it looks like:

"quantity" : 3

When I run the code I got the exception

 Caused by: com.beust.klaxon.KlaxonException: Unable to instantiate Line:
 Parameter quantity: expected java.lang.Integer but received kotlin.Int (value: 2)

After this i undersstood the problem is with my data type, and the library by default cast the values into java data type, so I change it to java.lang.Integer.

val quantity : Integer

even this dosn't helped and still I'm facing the same error with java.lang.Integer.

 Caused by: com.beust.klaxon.KlaxonException: Unable to instantiate Line:
 Parameter quantity: expected java.lang.Integer but received java.lang.Integer (value: 2)

Since in the JSON the type of keys are not defined the library should cast based on the data class variable just like GSON and Jackson library do.

@joey-yoonsung
Copy link

I have the same issue. I got this error from every type of JSON field.

@arieagung
Copy link

You have to use null safe type, eg. Int?

@KeremAslan
Copy link

I have a similar issue:

Exception in thread "main" com.beust.klaxon.KlaxonException: Unable to instantiate GeoCoordinates:
    Parameter latitudeInDecDeg: expected kotlin.Float but received java.lang.Float (value: 70.0)

    Parameter longitudeInDecDeg: expected kotlin.Float but received java.lang.Float (value: 70.0)

    Parameter latLongResolutionInDp: expected kotlin.Int but received java.lang.Integer (value: 0)

This is my data class:

data class GeoCoordinates(val latitudeInDecDeg: Float, val longitudeInDecDeg: Float, val latLongResolutionInDp: Int = 4)

@jainsahab
Copy link

@cbeust Could you please help with this ? Why is Klaxon picking up the Java types as default. Are we all making a silly mistake here ?

@arieagung
Copy link

I have a similar issue:

Exception in thread "main" com.beust.klaxon.KlaxonException: Unable to instantiate GeoCoordinates:
    Parameter latitudeInDecDeg: expected kotlin.Float but received java.lang.Float (value: 70.0)

    Parameter longitudeInDecDeg: expected kotlin.Float but received java.lang.Float (value: 70.0)

    Parameter latLongResolutionInDp: expected kotlin.Int but received java.lang.Integer (value: 0)

This is my data class:

data class GeoCoordinates(val latitudeInDecDeg: Float, val longitudeInDecDeg: Float, val latLongResolutionInDp: Int = 4)

use null safe type:
data class GeoCoordinates(val latitudeInDecDeg: Float?, val longitudeInDecDeg: Float?, val latLongResolutionInDp: Int? = 4)

@KeremAslan
Copy link

Ah, no it is not that.

My data class has data validation in the init block which contained checks as require() or check(). The exception was being thrown by that init block, but what was happening is that Klaxon wraps that exception and throws its own exception without a stack trace. This makes it hard to debug.

@arieagung
Copy link

Yes, I also had that error, I just declare the variable with null-safe type then the error goes away.

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

No branches or pull requests

5 participants