package ru.dbotthepony.kstarbound.json

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import ru.dbotthepony.kstarbound.json.builder.JsonImplementation

object JsonImplementationTypeFactory : TypeAdapterFactory {
	override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
		val delegate = type.rawType.getAnnotation(JsonImplementation::class.java)

		if (delegate != null) {
			return gson.getAdapter(delegate.implementingClass.java) as TypeAdapter<T>?
		}

		return null
	}
}
