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

implementations.AbstractJSONCodec.kt Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
package com.github.fluidsonic.fluid.json

import java.lang.reflect.ParameterizedType
import kotlin.reflect.KClass


abstract class AbstractJSONCodec(
	private val additionalProviders: List> = emptyList(),
	decodableType: JSONCodingType? = null
) : JSONCodec {

	@Suppress("UNCHECKED_CAST")
	final override val decodableType = decodableType
		?: JSONCodingType.of((this::class.java.genericSuperclass as ParameterizedType).actualTypeArguments[0]) as JSONCodingType

	final override val encodableClass = this.decodableType.rawClass


	override fun  decoderCodecForType(decodableType: JSONCodingType): JSONDecoderCodec? {
		var codec = super.decoderCodecForType(decodableType)
		if (codec != null) {
			return codec
		}

		for (provider in additionalProviders) {
			codec = provider.decoderCodecForType(decodableType)
			if (codec != null) {
				return codec
			}
		}

		return null
	}


	override fun  encoderCodecForClass(encodableClass: KClass): JSONEncoderCodec? {
		var codec = super.encoderCodecForClass(encodableClass)
		if (codec != null) {
			return codec
		}

		for (provider in additionalProviders) {
			codec = provider.encoderCodecForClass(encodableClass)
			if (codec != null) {
				return codec
			}
		}

		return null
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy