我使用GSON转换服务器恢复的数据类。 我这样定义数据类... 还有一些我不想用GSON转换的字段。 所以我用注释添加了ExclusionStrategy。 通过测试,我发现AnnotationExclusionStrategy运行正常,并跳过了我不想转换的字段。 我在uiType和testData之类的字段中添加了注释,但是实际上当我打印出对象时。我发现字段uiType和testData已转换为零和null。
然后我创建一个新的数据类。 uiType和testData尚未擦除为零或null。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')"
}
}
GSON AnnotationExclusion:
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;
}
}
我要打印的位置:
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)
}
使用GSON Converter的结果,,您会看到uiType和testData字段已转换为零或null:
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')
创建新对象的结果,这似乎很好:
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')
这是我使用的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"
}
}
答案 0 :(得分:2)
Gson目前正在破坏Kotlin的类型安全性。它可以将null写入非null属性。当您读取的json将该字段分配为null时,可能会发生这种情况。 例如,如果您有数据类:
data class A (
var nonNullable: String = "",
var other: Int = 42
)
并遵循JSON:
{
"nonNullable": null,
"other": 91
}
您将在null
中有nonNullable
。
为避免这种情况,您可以为您的类创建自定义GSON类型转换器,或使nonNullable
实际上为空,但提供getter和setter,它将忽略null
的值:
class A {
var nonNullable: String? = ""
get() = field ?: ""
set(value) {
if (value != null) {
field = value
}
}
}
与0相同的事情-这意味着您解析的JSON将该字段分配为零。或者,如果不是这样,请向我们提供您的JSON,以使问题更清楚。
答案 1 :(得分:0)
通常不需要序列化的任何字段,都应使用“瞬态”修饰符。这也适用于json序列化程序,包括gson
@Target([AnnotationTarget.FIELD])注释类Transient标记 带注释的属性的JVM支持字段为瞬态,这意味着 它不是该对象的默认序列化形式的一部分。
有关更多参考,请查看文档:{{3}}
示例:
data class User(@SerializedName("user_id") var id: String = "-",
@SerializedName("full_name") var name: String = "",
@Transient var sessionToken: String = "")
答案 2 :(得分:0)
您应该为所有字段设置默认值。 如果所有字段都具有默认值,那么当JSON中缺少相应的字段时,Gson将尊重它们。
答案 3 :(得分:0)
还有另一个选项可以让您的参数不为空。假设您有一堂课:
data class User(val username: String = "User")
您需要做的就是定义自定义 InstanceCreator
:
class UserInstanceCreator : InstanceCreator<User> {
override fun createInstance(type: Type?): User {
return User()
}
}
并在创建 GsonBuilder
时注册:
GsonBuilder()
.registerTypeAdapter(User::class.java, UserInstanceCreator())
.create()
多亏了您的对象将使用默认值创建,因此您可以将属性保持为非空。