quilt-kotlin-libraries icon indicating copy to clipboard operation
quilt-kotlin-libraries copied to clipboard

Quilt Config via Kotlinx Serialization

Open Oliver-makes-code opened this issue 3 years ago • 6 comments
trafficstars

Intro:

Jetbrains has a Kotlinx library that handles serialization of classes for config and other stuff. A bridge between this and Quilt Config would be beneficial to modders who are already familiar with Kotlinx Serialization

Existing Work:

Not any that I can find, mostly because Quilt Config is relatively new

Oliver-makes-code avatar Jun 11 '22 21:06 Oliver-makes-code

This issue is a bit to vague ihmo. What exactly is the goal?

Kroppeb avatar Jun 16 '22 21:06 Kroppeb

The goal is to be able to use a preexisting serializable class with the quilt config system

Oliver-makes-code avatar Jun 16 '22 21:06 Oliver-makes-code

The goal is to be able to use a preexisting serializable class with the quilt config system

In what sense? Do you think you could give an example?

Kroppeb avatar Jun 16 '22 21:06 Kroppeb

@Serializable
class SerializableClass {
  var name: String = "Name" // Will be serialized into a string
  var custom: Custom = Custom.apply {
    name = "Test"
  }
}

@Serializable            
@SerialName("Custom")
class Custom {
  var name: String
}

class CustomSerializaer : KSerializer<Custom> {
  override fun serialize(encoder: Encoder, value: Custom) {
    val string = value.name
    encoder.encodeString(string)
  }

  override fun deserialize(decoder: Decoder): Custom {
    val string = decoder.decodeString()
    return Custom().apply {
      name = string
    }
  }
}

This would get serialized into this (Assuming JSON5)

{
  name: "Name",
  custom: "Test!"
}

It can get more complex that that, though, with multiple properties in custom serializers

Oliver-makes-code avatar Jun 16 '22 21:06 Oliver-makes-code

@NoComment1105 has more experience with kotlinx serialization, so they might be able to help more with this

Oliver-makes-code avatar Jun 16 '22 21:06 Oliver-makes-code

ok that is the Kotlinx serialization part, but how does it "connect" to configs?

  • How does a config update when I change the value of name in custom
  • How does this data update when the config changes
  • What about default values? Each entry needs a default value

(Also fyi: custom serializers don't automatically get used like that)

Kroppeb avatar Jun 16 '22 21:06 Kroppeb