Thread
#serialization
    v79

    v79

    9 months ago
    I'm hitting an odd issue where a class can't seem to serialize itself, or at least it's not serializing its backing fields. It's a small class:
    @Serializable
    class GameState {
        var playerName = "Dougie McDougal"
        @Transient
        var saveFileName = "save1"
    
        suspend fun save() {
            println("Saving game state $saveFileName.gs.json")
            val gameStateJson = Json.encodeToString(this@GameState)
            val saveFile = applicationVfs["saves/$saveFileName.gs.json"]
            saveFile.write(gameStateJson.toByteArray())
        }
    }
    The serialized output is just
    {}
    - empty.
    playerName
    is not being serialised. Any thoughts?
    Adding the
    @Required
    annotation has solved this, though the documentation doesn't make that explicitly needed.
    Dariusz Kuc

    Dariusz Kuc

    9 months ago
    I believe default values are omitted by default - its configurable by a flag OR by explicitly marking field required so it will always be present