question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to parse int in my data class

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

1reaction
arieagungcommented, Jun 14, 2021

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

1reaction
joey-yoonsungcommented, May 25, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found