QuiltMC / quilt-kotlin-libraries

Quilt's official Kotlin libraries, which provide wrappers for QSL and Minecraft to ease development in Kotlin
Apache License 2.0
52 stars 19 forks source link

Quilt Config via Kotlinx Serialization #13

Open Oliver-makes-code opened 2 years ago

Oliver-makes-code commented 2 years ago

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

Kroppeb commented 2 years ago

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

Oliver-makes-code commented 2 years ago

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

Kroppeb commented 2 years ago

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?

Oliver-makes-code commented 2 years ago
@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 commented 2 years ago

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

Kroppeb commented 2 years ago

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

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