Is it possible to take a look at the Serializer cl...
# serialization
v
Is it possible to take a look at the Serializer classes the plugin generates? Some of my class's properties are not being serialized and I'm rather confused.
My class had a companion object. I removed it, and it serialized as expected. Is that... correct?
d
It’s hard to know without seeing your code.
e
the generated serializers do not exist in source form. you can disassemble the compiled classes
the companion object should not make any difference unless it also had a
fun serializer()
which the plugin won't overwrite
perhaps something stranger is going on in your case; more details?
v
Curiouser and curiouser... it works if I have but don't use the companion. It fails if I use the companion.
Copy code
@Serializable
class Company(var name: String, var sciences: MutableMap<Science, Float>) {

   companion object {
       val VOID: Company = Company("VOID", mutableMapOf())
   }
}

@Serializable
class GameState : Node() {

    var year: Int = 1980
    var company: Company = Company("My company", mutableMapOf())
    var country: Country? = null
}
This serializes correctly. But:
Copy code
@Serializable
class Company(var name: String, var sciences: MutableMap<Science, Float>) {

   companion object {
       val VOID: Company = Company("VOID", mutableMapOf())
   }
}

@Serializable
class GameState : Node() {

    var year: Int = 1980
    var company: Company = Company.VOID
    var country: Country? = null
}
When this serializes, there is no Company in the json output. In both cases, the value of company will have changed before save() is called, so I'd never expect to see "VOID" in the output but I would expect to see something. Typical correct output is:
Copy code
{
    "year": 1965,
    "company": {
        "name": "EuroStellar Space Corp,
        "sciences": {
            "PHYSICS": 9.359396,
            "ASTRONOMY": 3.01254,
            "BIOCHEMISTRY": 4.3982563,
            "GEOLOGY": 9.570941,
            "MATHEMATICS": 2.9410443,
            "PSYCHOLOGY": 2.9936967,
            "UNKNOWN": 7.0150113
        }
    },
    "country": {
        "id": 2,
        "name": "North America",
        "colour": {
            "r": 1.0,
            "a": 1.0,
            "r8": 255,
            "a8": 255,
            "s": 1.0,
            "v": 1.0
        }
    }
}
e
ah if you have encodeDefaults=false (default) then that's easy to explain. it's not a
data class
and you don't override
equals
, so
Copy code
Company.VOID == Company.VOID
Company("My company", mutableMapOf()) != Company("My company", mutableMapOf())
so one of them is
==
to the default and the other is not
having a mutable default is a pretty bad idea though
v
It's a game - it's full of bad ideas and the model is going to change a lot 😉 I will rationalize it once I know what I really need. Thanks for the explanation!