1 package kotlinx.serialization.hocon 2 3 import com.typesafe.config.ConfigValue 4 import kotlinx.serialization.ExperimentalSerializationApi 5 6 /** 7 * Encoder used by Hocon during serialization. 8 * This interface allows intercepting serialization process and insertion of arbitrary [ConfigValue] into the output. 9 * 10 * Usage example (nested config serialization): 11 * ``` 12 * @Serializable 13 * data class Example( 14 * @Serializable(NestedConfigSerializer::class) 15 * val d: Config 16 * ) 17 * object NestedConfigSerializer : KSerializer<Config> { 18 * override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("package.Config", PrimitiveKind.STRING) 19 * 20 * override fun deserialize(decoder: Decoder): Config = 21 * if (decoder is HoconDecoder) decoder.decodeConfigValue { conf, path -> conf.getConfig(path) } 22 * else throw SerializationException("This class can be decoded only by Hocon format") 23 * 24 * override fun serialize(encoder: Encoder, value: Config) { 25 * if (encoder is HoconEncoder) encoder.encodeConfigValue(value.root()) 26 * else throw SerializationException("This class can be encoded only by Hocon format") 27 * } 28 * } 29 * val nestedConfig = ConfigFactory.parseString("nested { value = \"test\" }") 30 * val globalConfig = Hocon.encodeToConfig(Example(nestedConfig)) // d: { nested: { value = "test" } } 31 * val newNestedConfig = Hocon.decodeFromConfig(Example.serializer(), globalConfig) 32 * ``` 33 */ 34 @ExperimentalSerializationApi 35 sealed interface HoconEncoder { 36 37 /** 38 * Appends the given [ConfigValue] element to the current output. 39 * 40 * @param value to insert 41 */ encodeConfigValuenull42 fun encodeConfigValue(value: ConfigValue) 43 } 44