Gson – RegisterTypeAdapter

Gson – RegisterTypeAdapter

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.


Json Data:


birthDate,registerDate are Date in Long type. When convert with default Date Adapter, or set date format likeval gson: Gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create() These methods cannot convert Long type Date. We must define a custom Date Adapter to treat the situation.

class DateTypeConverter : JsonSerializer, JsonDeserializer {
    override fun serialize(src: Date, srcType: Type, context: JsonSerializationContext): JsonElement {
        return JsonPrimitive(src.toString())

    override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): Date {
        try {
            return Date(json.asLong)
        } catch (e: IllegalArgumentException) {
            val date = context.deserialize(json, Date::class. java)
            return date


fun convert(){
    val json = """{"memberId":"64","memberName":"Kotlin","gender":"1","birthDate":315504000000,"mobile":"13311888009","email":"","registerDate":1470743343000}"""
    val adapter = DateTypeConverter()
    val gson = GsonBuilder().registerTypeAdapter(Date::class. java, adapter).create()

    val m = gson.fromJson(json,Member::class. java)


Member(memberId=64, memberName=Kotlin, gender=1, birthDate=Tue Jan 01 00:00:00 CST 1980, mobile=13311888009,,registerDate=Tue Aug 09 19:49:03 CST 2016}

As you see, use which method depends on the data format, if date in json like ‘1990-01-01 20:10:20’, use setDateFormat(), if date in json is Long type like 315504000000, use custom one .

好烂啊有点差凑合看看还不错很精彩 (No Ratings Yet)


电子邮件地址不会被公开。 必填项已用*标注