If you want you can add an annotation to the class...
# getting-started
k
If you want you can add an annotation to the classes with the corresponding json name, make the hierarchy out of sealed classes and iterate over them with reflection.
h
so would the annotation be based on the type field? then it would look through all the members of the sealed class and instantiate the class based on the annotation? does that have any extra merit for parsing data? Why couldn't I iterate over them with reflection without the annotation
unless you refer to using annotation to direct the klaxon parser to a class that doesn't exactly share the name as the one in the json file?
k
The latter.
Just realized that https://youtrack.jetbrains.net/issue/KT-14657 hasn't been implemented yet, which is annoying.
h
ah
i guess i'll just stick to this method:
Copy code
val type: String? = null,
val entry: EntryType = Class.forName(type!!.capitalize()).getConstructor().newInstance() as EntryType
k
Hmm how many types are there? I'd do a when expression if there are less than 20.
h
I presume that if a field isn't specified in the json file it treats it like instantiating an object without declaring the constructor parameters?, e.g. sticks it in the first declared parameter field?
Also, is this advisible? seems risky
Copy code
open class ItemData (
    var rarity: Any
) {
    init {
        rarity = when (rarity) {
            "Common" -> Rarity.COMMON
            else -> Rarity.NONE
        }
}

enum class Rarity {
    NONE, COMMON

}
I'm referring to the Any declaration, really
k
Why make it
Any
and not
Rarity
?
And for the other question you'll have to ask it for your specific json library 😛
h
I figured it wouldn't choose the correct enum class member since I can't make a typealias or something
but i think i can dictate with the klaxon's tools how to parse each property
so i can just do it that way, i think
fuck me this is starting to hurt my brain. i'll get back to it later