Not able to parse int in my data class
See original GitHub issueIn 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.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:7
Top Results From Across the Web
Cannot convert String to Integer in Java [duplicate]
parseInt () is the preferred method as it will cache commonly used values to improve performance. Creating a new Integer will always create...
Read more >2 ways to parse String to int in Java - Examples - Java67
Java provides Integer.parseInt() method to parse a String to an int value, but that's not the only way to convert a numeric String...
Read more >How to convert a Java String to an Int - devmio
Answer: Convert a string to an integer with the parseInt method of the Java Integer class. The parseInt method is to convert the...
Read more >How to Convert a Python String to int
Two possible Python data types for representing an integer are: ... Logical Error: int() does know how to parse the string, but not...
Read more >parseInt() - JavaScript - MDN Web Docs
parseInt() does not handle BigInt values. It stops at the n character, and treats the preceding string as a normal integer, with possible...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You have to use null safe type, eg. Int?
I have the same issue. I got this error from every type of JSON field.