implementations.StandardEncoder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-json-coding Show documentation
Show all versions of fluid-json-coding Show documentation
A JSON library written in pure Kotlin (coding extension)
package com.github.fluidsonic.fluid.json
internal class StandardEncoder(
override val context: Context,
private val codecProvider: JSONCodecProvider,
private val destination: JSONWriter
) : JSONEncoder, JSONWriter by destination {
override fun writeValue(value: Any) {
withErrorChecking {
codecProvider.encoderCodecForClass(value::class)
?.run {
try {
isolateValueWrite {
encode(value = value)
}
}
catch (e: JSONException) {
// TODO remove .java once KT-28418 is fixed
e.addSuppressed(JSONException.Serialization("… when encoding value of ${value::class} using ${this::class.java.name}: $value"))
throw e
}
}
?: throw JSONException.Serialization(
message = "No encoder codec registered for ${value::class}: $value",
path = path
)
}
}
}