fastjson 对kotlin的data class支持还是有问题

oaxa6hgo  于 2021-11-27  发布在  Java
关注(0)|答案(4)|浏览(405)

data class 只有一个field时,会报告数组越界问题

sulc1iza

sulc1iza1#

能提供错误信息么?

t3irkdon

t3irkdon2#

//This is a Java class
public class BaseBean {

@Override
public String toString() {
    try {
        return JSONToBeanHandler.toJsonString(this);
    } catch (JSONFormatException e) {
        e.printStackTrace();
    }
    return super.toString();
}

}

//This is the Kotlin data class which may cause crash
data class LogoutResult constructor(@JSONField(name = "msg") var msg: String): BaseBean()

//The following is the data received form server
{
"data": {
"msg": "bye"
},
"meta": {
"code": 200
}
}

//This is the crash information
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

ugmeyewa

ugmeyewa3#

@wenshao 不好意思,错误日志已经上传

lh80um4z

lh80um4z4#

data class WiFiLoginResult constructor(
@JSONField(name = "wifi_user") var wifiUser: WiFiUser,
@JSONField(name = "user") var user: User,
@JSONField(name = "token") var token: String): BaseBean()

data class WiFiUser constructor(
@JSONField(name = "loginPath") var loginPath: String,
@JSONField(name = "minHeadImgUrl") var minAvartarUrl: String,
@JSONField(name = "nickname") var nickname: String,
@JSONField(name = "sex") var gender: String,
@JSONField(name = "unionId") var uid: String
): BaseBean()

服务端返回的wifi_user数据项,如果仅有上面类中的一部分的话,也会发生异常
{
"data": {
"token": "2|1:0|10:1541403557|5:token|20:OUJKcjk6OmVhMjg3Zg==|fb6289021515404c7a743bd52ff867de90006c0970d081076f8235ca16666e62",
"user": {
"gender": 2,
"nickname": "mattj",
"uid": "9BJr9"
},
"wifi_user": {
"loginPath": "wifi_logged",
"unionId": "470B52CEFD554EE3A6D4C1162D5D2C25"
}
},
"meta": {
"code": 200
}
}

create instance error, public com.wifi.mask.user.entity.WiFiUser(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)

相关问题