Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)

一、解决将Long型转化为科学计数法的问题

1.1 场景

将一个对象转为Map类型时,调用Gson.fromJson发现,原来对象中的long类型的personId字段,被解析成了科学计数法,导致请求接口失败,报参数错误。

解决结果图
Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)_第1张图片

1.2、ExampleUnitTest.kt示例代码
data class ReuestParmObj(val personId: Long = 1668394335647, val personType: Int = 1)
class ExampleUnitTest {
    private val mGson: Gson = Gson()

    @Test
    fun gsonReuestParmJson() {
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        val reuestParmJson: String = mGson.toJson(reuestParmObj)
        println("reuestParmJson信息:$reuestParmJson")
        val mapParm: Map<String, *> =
            mGson.fromJson(reuestParmJson, Map::class.java) as Map<String, *>
        println("mapParms信息:$mapParm")
    }
}

测试结果
Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)_第2张图片

1.3 解决将Long型转化为科学计数法问题的方案

修改gson配置
private val mGson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create()

data class ReuestParmObj(val personId: Long = 1668394335647, val personType: Int = 1)
class ExampleUnitTest {
//    private val mGson: Gson = Gson()
  private val mGson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create()

    @Test
    fun gsonReuestParmJson() {
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        val reuestParmJson: String = mGson.toJson(reuestParmObj)
        println("reuestParmJson信息:$reuestParmJson")
        val mapParm: Map<String, *> =
            mGson.fromJson(reuestParmJson, Map::class.java) as Map<String, *>
        println("mapParms信息:$mapParm")
    }
}

测试结果
Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)_第3张图片

二、 把null值也转换,默认是不转换null值的

设置:serializeNulls()

 private val mGson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls().create()

三、Html的转换

Gson默认是把Html 转义的,但也可以设置不转义。
设置:disableHtmlEscaping()

class ExampleUnitTest {
	@Test
    fun testDisableHtmlEscaping(){
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().disableHtmlEscaping().create()
        val htmlStr = "

测试disableHtmlEscaping

"
println("未设置disableHtmlEscaping转换htmlStr结果:${gson1.toJson(htmlStr)}") println("设置disableHtmlEscaping转换htmlStr结果:${gson2.toJson(htmlStr)}") } }

测试结果
Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)_第4张图片

四、设置输入json格式

默认的输出是compact格式,示例
{"personId":1668394335647,"personType":1}
设置:setPrettyPrinting()

class ExampleUnitTest {
	 @Test
    fun testSetPrettyPrinting(){
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).setPrettyPrinting().create()
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        println("未设置 setPrettyPrinting 转换reuestParmObj结果:${gson1.toJson(reuestParmObj)}")
        println("设置 setPrettyPrinting 转换reuestParmObj结果:${gson2.toJson(reuestParmObj)}")
    }
}

测试结果
Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)_第5张图片

五、ExampleUnitTest.kt文件完整代码

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.LongSerializationPolicy
import com.google.gson.reflect.TypeToken
import org.junit.Test

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */

data class ReuestParmObj(val personId: Long = 1668394335647, val personType: Int = 1)
class ExampleUnitTest {
    private val mGson1: Gson = Gson()
    private val mGson2: Gson =
        GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls()
            .disableHtmlEscaping().create()

    @Test
    fun gsonReuestParmJson() {
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        val reuestParmJson1: String = mGson1.toJson(reuestParmObj)
        val reuestParmJson2: String = mGson2.toJson(reuestParmObj)
        println("reuestParmJson1 信息:$reuestParmJson1")
        println("reuestParmJson2 信息:$reuestParmJson2")

        //        val type = object : TypeToken<Map<String, *>>() {}.type
//        val mapParm: Map<String, *> = mGson1.fromJson(reuestParmJson, type)

        val mapParm1: Map<String, *> =
            mGson1.fromJson(reuestParmJson1, Map::class.java) as Map<String, *>
        println("mapParm1 信息:$mapParm1")
        val mapParm2: Map<String, *> =
            mGson2.fromJson(reuestParmJson2, Map::class.java) as Map<String, *>
        println("mapParms2 信息:$mapParm2")
        /*
            reuestParmJson1 信息:{"personId":1668394335647,"personType":1}
            reuestParmJson2 信息:{"personId":"1668394335647","personType":1}
            mapParm1 信息:{personId=1.668394335647E12, personType=1.0}
            mapParms2 信息:{personId=1668394335647, personType=1.0}
         */

    }

    @Test
    fun testDisableHtmlEscaping() {
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().disableHtmlEscaping().create()
        val htmlStr = "

测试disableHtmlEscaping

"
println("未设置disableHtmlEscaping转换htmlStr结果:${gson1.toJson(htmlStr)}") println("设置disableHtmlEscaping转换htmlStr结果:${gson2.toJson(htmlStr)}") /* 未设置disableHtmlEscaping转换htmlStr结果:"\u003ch3\u003e测试disableHtmlEscaping\u003c/h3\u003e" 设置disableHtmlEscaping转换htmlStr结果:"

测试disableHtmlEscaping

"
*/ } @Test fun testSetPrettyPrinting() { val gson1: Gson = Gson() val gson2: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING) .setPrettyPrinting().create() val reuestParmObj = ReuestParmObj(1668394335647, 1) println("未设置 setPrettyPrinting 转换reuestParmObj结果:${gson1.toJson(reuestParmObj)}") println("设置 setPrettyPrinting 转换reuestParmObj结果:${gson2.toJson(reuestParmObj)}") /* 未设置 setPrettyPrinting 转换reuestParmObj结果:{"personId":1668394335647,"personType":1} 设置 setPrettyPrinting 转换reuestParmObj结果:{ "personId": "1668394335647", "personType": 1 } */ } }

你可能感兴趣的:(#,Android知识点,android)