Closed kevinthegreat1 closed 3 months ago
Originally reported SkyblockerMod/Skyblocker#890 and here SkyblockerMod/Skyblocker#894.
In general, we've encountered many problems when the config simply refuses to load and crashes when it tries to deserialize a null or nonexistent field. I believe there should be alternative options such as default values instead of crashing.
I have no affiliation with that man and yet I agree with all he says /j
I also have no affiliation with that man and yet I agree with all he says
Is a default option not specified by just giving the field an initial value?
Is a default option not specified by just giving the field an initial value?
No. I also do have a constructor for this class, and I’ve tried both adding a default value to the field and making sure the constructor doesn’t set a null value. But to my knowledge, the constructor shouldn’t get called while deserializing from json?
I figured it out. First, this is a sub class, so this is serialized and deserialized with gson. Turns out having a constructor does matter. It means that there is no default constructor, and gson skips initialization if there are no default constructors available, which means the default values do not apply.
Have default values for the fields. And either:
I don't think there's much yacl van do about this, except warn users about this behavior.
What I wanted
I have an existing config class:
which serializes as such:
I want to add a new field so my config looks like this:
What I tried
I've tried setting a default value to the
tooltip
field, addingrequired = false
, or addingnullable = true
.What I encountered
All of the options above fails to load the old config file with the following error: