HoconDecoder
Decoder used by Hocon during deserialization. This interface allows to call methods from the Lightbend/config library on the Config object to intercept default deserialization process.
Usage example (nested config serialization):
@Serializable
data class Example(
@Serializable(NestedConfigSerializer::class)
val d: Config
)
object NestedConfigSerializer : KSerializer<Config> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("package.Config", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Config =
if (decoder is HoconDecoder) decoder.decodeConfigValue { conf, path -> conf.getConfig(path) }
else throw SerializationException("This class can be decoded only by Hocon format")
override fun serialize(encoder: Encoder, value: Config) {
if (encoder is AbstractHoconEncoder) encoder.encodeConfigValue(value.root())
else throw SerializationException("This class can be encoded only by Hocon format")
}
}
val nestedConfig = ConfigFactory.parseString("nested { value = \"test\" }")
val globalConfig = Hocon.encodeToConfig(Example(nestedConfig)) // d: { nested: { value = "test" } }
val newNestedConfig = Hocon.decodeFromConfig(Example.serializer(), globalConfig)
Content copied to clipboard