Kotlin 数据类用 gson 转换...默认值被转换为零或空

Ray*_*man 7 java android gson kotlin

我使用 GSON 来转换服务器返回的数据类。我像这样定义了数据类......还有一些我不想用 GSON 转换的字段......所以我添加了带有注释的 ExclusionStrategy 。通过测试,发现AnnotationExclusionStrategy运行正常,并跳过不想转换的字段。我添加了带有 uiType 和 testData 等字段的注释,但实际上当我打印对象时。我发现 uiType 和 testData 字段已转换为零和空值..

然后我创建一个新的数据类。uiType 和 testData 没有擦除为零或空值。GSON 转换时会出错吗??

如果有什么办法可以解决这个问题吗?

这是代码。

数据类实体:

data class Product(@SerializedName("num_iid") var productId: Long,
               @SerializedName("title") var productName: String,
               @SerializedName("item_url") var productUrl: String,
               @SerializedName("pict_url") var productPic: String,
               @SerializedName("small_images") var productPics: ProductImageList,
               @SerializedName("zk_final_price") var productPrice: String,
               @SerializedName("reserve_price") var originPrice: String,
               @SerializedName("provcity") var place: String,
               @SerializedName("volume") var sellCount: String,
               @SerializedName("user_type") var sellerType: String,
               @SerializedName("nick") var sellerName: String,
               @SerializedName("seller_id") var sellerId: Long
) : MultiItemEntity {

@Exclude
@ShoppingCenterConstant.ProductListUiType
var uiType = ShoppingCenterConstant.LIST_UI_TYPE_COMMON

var testData = "TestData"

override fun getItemType(): Int {
    return ShoppingCenterConstant.LIST_UI_TYPE_COMMON
}

override fun toString(): String {
    return "Product(productId=$productId, productName='$productName', productUrl='$productUrl', productPic='$productPic', productPics=$productPics, productPrice='$productPrice', originPrice='$originPrice', place='$place', sellCount='$sellCount', sellerType='$sellerType', sellerName='$sellerName', sellerId=$sellerId, uiType=$uiType, testData='$testData')"
}
}
Run Code Online (Sandbox Code Playgroud)

GSON 注释排除:

public class AnnotationExclusion implements ExclusionStrategy {

public static final String TAG = GsonUtil.TAG;

@Override
public boolean shouldSkipField(FieldAttributes f) {
    boolean isShouldSkip = f.getAnnotation(Exclude.class) != null;
    return isShouldSkip;
}

@Override
public boolean shouldSkipClass(Class<?> clazz) {
    boolean isShouldSkip = clazz.getAnnotation(Exclude.class) != null;
    return isShouldSkip;
}
Run Code Online (Sandbox Code Playgroud)

}

我打印的地方:

override fun showData(data: BaseListBean<Product>) {
    var product = Product(0, "", "", ""
            , ProductImageList(List(0, { String() })), "", "", "", ""
            , "", "", 0)
    LogUtils.info("print out new product?" + product)
    mResultListener.onResponse(data)
}
Run Code Online (Sandbox Code Playgroud)

结果与 GSON Converter您可以看到字段 uiType 和 testData 已转换为零或空值:

Product(productId=552634381786, productName='eoodoo???????????????????????????', productUrl='http://item.taobao.com/item.htm?id=552634381786', productPic='https://img.alicdn.com/tfscom/i4/2832303009/O1CN013tXM6L1Y6AFHSy7xN_!!0-item_pic.jpg', productPics=com.laka.ergou.mvp.shopping.center.model.bean.ProductImageList@1033f68, productPrice='259.72', originPrice='755.00', place='?? ??', sellCount='1159', sellerType='1', sellerName='eoodoo???', sellerId=2832303009, uiType=0, testData='null')
Run Code Online (Sandbox Code Playgroud)

结果与 create new Object这似乎很好:

Product(productId=0, productName='', productUrl='', productPic='', productPics=com.laka.ergou.mvp.shopping.center.model.bean.ProductImageList@1c228b, productPrice='', originPrice='', place='', sellCount='', sellerType='', sellerName='', sellerId=0, uiType=1, testData='testData')
Run Code Online (Sandbox Code Playgroud)

这是我使用的 JSON 。但是像 uiType 和 testData 这样的字段。只是我在代码中定义并在本地使用的字段,这些不是服务器恢复json字段

{
"tbk_item_get_response":{
    "results":{
        "n_tbk_item":[
            {
                "item_url":"http://item.taobao.com/item.htm?id=582889134126",
                "nick":"????????",
                "num_iid":582889134126,
                "pict_url":"https://img.alicdn.com/tfscom/i1/1720028843/O1CN01ERqlHz2FC8tL1m2tz_!!0-item_pic.jpg",
                "provcity":"?? ??",
                "reserve_price":"1318.00",
                "seller_id":1720028843,
                "small_images":{
                    "string":[
                        "https://img.alicdn.com/tfscom/i4/1720028843/O1CN01xD9JyD2FC8tJMRuk2_!!1720028843.jpg",
                        "https://img.alicdn.com/tfscom/i4/1720028843/O1CN01twbf3O2FC8tLEHVJc_!!1720028843.jpg",
                        "https://img.alicdn.com/tfscom/i4/1720028843/O1CN01zM2clN2FC8tE0LO5q_!!1720028843.jpg",
                        "https://img.alicdn.com/tfscom/i3/1720028843/O1CN01WW6xzz2FC8tKjoitz_!!1720028843.jpg"
                    ]
                },
                "title":"????2018????????????????????????",
                "user_type":1,
                "volume":2138,
                "zk_final_price":"498.00"
            }
        ]
    },
    "total_results":19643,
    "request_id":"kvbqd1jrgqcg"
}
Run Code Online (Sandbox Code Playgroud)

}

And*_*dov 10

Gson 目前正在破坏 Kotlin 类型安全。它可以将 null 写入非 null 属性。当您阅读的 json 将该字段分配为 null 时,可能会发生这种情况。例如,如果您有数据类:

data class A (
    var nonNullable: String = "",
    var other: Int = 42
)
Run Code Online (Sandbox Code Playgroud)

并遵循JSON:

{
    "nonNullable": null,
    "other": 91
}
Run Code Online (Sandbox Code Playgroud)

你将不得不nullnonNullable作为结果。

为避免这种情况,您可以为您的类创建自定义 GSON 类型转换器或使其nonNullable实际可为空,但提供 getter 和 setter,这将忽略null值:

class A {
    var nonNullable: String? = ""
        get() = field ?: ""
        set(value) {
            if (value != null) {
                field = value
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

与 0 相同 - 这意味着您解析的 JSON 将该字段分配为零。或者,如果不是这样 - 向我们提供您的 JSON 以使问题更清楚。


小智 6

您应该为所有字段设置默认值。如果所有字段都具有默认值,则当 JSON 中缺少相应字段时,Gson 将尊重它们。