TypeAdapter用于序列化/反序列化Model类中的对象

时间:2019-09-26 10:55:24

标签: android serialization gson deserialization retrofit2

我有一个如下所示的JSON响应:

[{
    "id":"61051",
    "employee_name":"koiudshau",
    "employee_salary":"111",
    "employee_age":"0",
    "profile_image":""
},
{
    "id":"61071",
    "employee_name":"Nickky Raboy",
    "employee_salary":"1234",
    "employee_age":"20",
    "profile_image":""
}]

我想创建一个自定义类型适配器,将 employee_name分为firstNamesecondName,并将它们存储为对象< / strong>名为Name,其中包含firstNamesecondName作为参数。必须在反序列化期间完成。

序列化期间,类型Name的对象必须转换为String(反序列化之前的类型)。

这些是我尝试过的代码。

模型类:

data class Employee (
    @SerializedName("id")
    val id: Int = 0,
    @SerializedName("employee_name")
    val employee_name: Name,
    @SerializedName("employee_salary")
    val employee_salary: Float = 0f,
    @SerializedName("employee_age")
    val employee_age: Int = 0,
    @SerializedName("profile_image")
    val profile_image : String = ""
)

data class Name(val firstName : String = "" , val secondName : String = "")

CustomizedTypeAdapterFactory:

abstract class CustomizedTypeAdapterFactory<C>(private val customizedClass: Class<C>) : TypeAdapterFactory {
    override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
        return if (type.rawType == customizedClass) customizeMyClassAdapter(gson, type as TypeToken<C>) as TypeAdapter<T> else null
    }
    private fun customizeMyClassAdapter(gson: Gson, type: TypeToken<C>): TypeAdapter<C> {
        val delegate = gson.getDelegateAdapter(this, type)
        val elementAdapter = gson.getAdapter(JsonElement::class.java)
        return object : TypeAdapter<C>() {
            @Throws(IOException::class)
            override fun write(out: JsonWriter, value: C) {
                val tree = delegate.toJsonTree(value)
                beforeWrite(value, tree)
                elementAdapter.write(out, tree)
            }
            @Throws(IOException::class)
            override fun read(`in`: JsonReader): C {
                val tree = elementAdapter.read(`in`)
                afterRead(tree)
                return delegate.fromJsonTree(tree)
            }
        }
    }
    protected open fun beforeWrite(source: C, toSerialize: JsonElement) {}
    protected open fun afterRead(deserialized: JsonElement) {}
}

MyClassTypeAdapterFactory:

class MyClassTypeAdapterFactory : CustomizedTypeAdapterFactory<Name>(Name::class.java) {
    override fun beforeWrite(source: Name, toSerialize: JsonElement) {
        val custom = toSerialize.asJsonObject.get("employee_name").asJsonObject
        custom.add("size", JsonPrimitive(custom.entrySet().size))
    }
    override fun afterRead(deserialized: JsonElement) {
        val custom = deserialized.asJsonObject.get("employee_name").asJsonObject
        custom.remove("size")
    }
}

RetrofitBuilder:

private var retrofitBuilder: Retrofit.Builder = Retrofit.Builder().baseUrl(API_BASE_URL).addConverterFactory(createGsonConverter()).client(okHttpClientBuilder.build())

createGsonConverter():

private fun createGsonConverter() : Converter.Factory {
    val gsonBuilder = GsonBuilder()
    gsonBuilder.registerTypeAdapterFactory(MyClassTypeAdapterFactory())
    val gson = gsonBuilder.create()
    return GsonConverterFactory.create(gson)
}

当我运行此代码时,该应用程序崩溃,并显示以下日志消息:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.shaheen.gsonparsing, PID: 15147
    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Not a JSON Object: "Karolina38606"

2 个答案:

答案 0 :(得分:0)

“ employee_name”是字符串,而不是jsonobject

答案 1 :(得分:0)

我通过更改CustomizedTypeAdapterFactory类和createGsonConverter()方法来获得所需的输出:

CustomizedTypeAdapterFactory:

class CustomizedTypeAdapterFactory : TypeAdapter<Name>() {
    override fun write(out: JsonWriter?, value: Name?) {
        out!!.value("${value?.firstName} ${value?.secondName}")
    }

    override fun read(`in`: JsonReader?): Name {
        var lastName = ""
        var firstName = ""
        val employeeName = `in`!!.nextString()
        if (employeeName!=null) {
            val name = employeeName.split(" ")
            if(name.size > 1){
                lastName += name[name.size - 1]
                for (element in 0 until (name.size - 1)){
                    firstName = if (element == (name.size - 1)) "$firstName${name[element]}" else "$firstName${name[element]} "
                }
            }
            else firstName = name[0]
        }
        return Name(firstName,lastName)
    }
}

createGsonConverter():

private fun createGsonConverter() : Converter.Factory {
    val gsonBuilder = GsonBuilder()
    gsonBuilder.registerTypeAdapter(Name::class.java,CustomizedTypeAdapterFactory()).setLenient()
    val gson = gsonBuilder.create()
    return GsonConverterFactory.create(gson)
}