Parsing error: Klaxon drops parameters when using @TypeFor
See original GitHub issueI try to deserialize objects of classes that are derieved from the Sensor
class:
@TypeFor(field = "type", adapter = SensorTypeAdapter::class)
abstract class Sensor(val type: Type) {
enum class Type { T1, T2, Test }
}
A test subclass looks like this:
class Test(val prop: String) : Sensor(Type.Test)
And the TypeAdapter like this:
import com.beust.klaxon.TypeAdapter
import kotlin.reflect.KClass
class SensorTypeAdapter : TypeAdapter<Sensor> {
override fun classFor(type: Any): KClass<out Sensor> =
when (Sensor.Type.valueOf(type as String)) {
Sensor.Type.Test -> Test::class
else -> throw IllegalArgumentException("Unknown sensor type $type")
}
}
When trying to parse this I get the exception com.beust.klaxon.KlaxonException: Unable to instantiate Test with parameters []
. So it seems to me that Klaxon found out which type to use (Test
) but ignores all other parameters that can be found in the json:
{
"type": "Test",
"prop": "String"
}
I start the parsing by using a File object:
Klaxon().parse<Sensor>(file)
When removing the prop
property from the json and the Test
class the deserialisation works.
How do I get it work with properties in my derieved class?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
What to do if your typeform isn't working - Help Center
What to do if your typeform isn't working · 1. Include your typeform URL. · 2. Make a screen recording of the issue...
Read more >Salesforce integration: Troubleshooting, limitations, and other ...
Click the Typeform Forms tab and select Forms with Active Mappings from the List Views dropdown menu. Click each form in the list...
Read more >Responses API - Typeform's APIs
The Responses API lets you access the data your typeforms collect — on demand and in JSON format — without setting up webhooks...
Read more >Logic Map - Help Center - Typeform
Logic is a feature that helps responders follow the path most relevant to them while completing a typeform. With a Logic Map you...
Read more >FAQs: Typeform and Google Analytics - Help Center
There are a few possible reasons for this: 1. Your respondents are looking at a typeform but not interacting with it - Google...
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
Great suggestion, @wolpl, I just added this badge to the
README
.Sorry for the inconvenience!
As the
build.gradle
file shows and I mentioned above I used Klaxon 5.0.1. The error disappears when using Klaxon 5.0.5. Still it should not have worked in the example project because version 5.0.1 is used there…@cbeust Thanks for your support. It would have helped to reference the latest version in the project readme - I had to find it manually at bintray.com. As of now applying the instructions in the readme (including version 5.0.1 via gradle) makes it impossible to use the polymorphism features described in the readme.
Perhaps a badge is a convenient solution to always show the latest available version: