All Downloads are FREE. Search and download functionalities are using the official Maven repository.

implementations.StandardEncoder.kt Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
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 {

	@Suppress("UNCHECKED_CAST")
	override fun writeValue(value: Any) {
		withErrorChecking {
			(codecProvider.encoderCodecForClass(value::class) as JSONEncoderCodec?)
				?.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
				)
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy