package com.ffii.core.utils

import com.ffii.fpsms.modules.master.entity.projections.DetailedProdScheduleLineInfo
import com.ffii.fpsms.modules.master.service.ProductionScheduleService
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type

open class GsonUtils {

    class BooleanTypeAdapter : JsonDeserializer<Boolean> {
        override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Boolean {
            return json.asInt == 1
        }
    }

    companion object {
        val gson = GsonBuilder()
            .registerTypeAdapter(Boolean::class.javaObjectType, BooleanTypeAdapter())
            .create()

        /**
         * Read from JSON String.
         *
         * @param content
         *            JSON String content
         *
         * @param type
         *            Type
         *
         */
         fun <T> stringToJson(content: String?, type: Type): T {
//            val type = object : TypeToken<T>() {}.type
            val gsonResult: T = gson.fromJson(content, type)

            return gsonResult;
        }
    }
}