Skip to content

Commit 524a25c

Browse files
committed
优化 kotlin data class 默认值逻辑处理
优化 Json 解析容错监听器的方法命名
1 parent 20f012d commit 524a25c

File tree

9 files changed

+62
-22
lines changed

9 files changed

+62
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ android {
3939
4040
dependencies {
4141
// Gson 解析容错:https://github.com/getActivity/GsonFactory
42-
implementation 'com.github.getActivity:GsonFactory:9.2'
42+
implementation 'com.github.getActivity:GsonFactory:9.3'
4343
// Json 解析框架:https://github.com/google/gson
4444
implementation 'com.google.code.gson:gson:2.10.1'
4545
// Kotlin 反射库:用于反射 Kotlin data class 类对象

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.hjq.gson.factory.demo"
99
minSdkVersion 16
1010
targetSdkVersion 31
11-
versionCode 920
12-
versionName "9.2"
11+
versionCode 930
12+
versionName "9.3"
1313
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1414
}
1515

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.hjq.gson.factory.test
22

33
data class DataClassBean(
4-
val name: String?,
5-
val age: Int = 18,
6-
val address: String?,
7-
val birthday: Long = System.currentTimeMillis()
4+
val name: String = "轮子哥",
5+
val alias: String,
6+
val address: String? = "",
7+
val age: Int = 20,
8+
val weight: Int,
9+
val stature: Int? = 180,
810
)

app/src/androidTest/java/com/hjq/gson/factory/test/JsonUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public void onParseObjectException(TypeToken<?> typeToken, String fieldName, Jso
4444
}
4545

4646
@Override
47-
public void onParseListException(TypeToken<?> typeToken, String fieldName, JsonToken listItemJsonToken) {
47+
public void onParseListItemException(TypeToken<?> typeToken, String fieldName, JsonToken listItemJsonToken) {
4848
handlerGsonParseException("解析 List 异常:" + typeToken + "#" + fieldName + ",后台返回的条目类型为:" + listItemJsonToken);
4949
}
5050

5151
@Override
52-
public void onParseMapException(TypeToken<?> typeToken, String fieldName, String mapItemKey, JsonToken mapItemJsonToken) {
52+
public void onParseMapItemException(TypeToken<?> typeToken, String fieldName, String mapItemKey, JsonToken mapItemJsonToken) {
5353
handlerGsonParseException("解析 Map 异常:" + typeToken + "#" + fieldName + ",mapItemKey = " + mapItemKey + ",后台返回的条目类型为:" + mapItemJsonToken);
5454
}
5555

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66

77
defaultConfig {
88
minSdkVersion 12
9-
versionCode 920
10-
versionName "9.2"
9+
versionCode 930
10+
versionName "9.3"
1111
}
1212

1313
// 使用 JDK 1.8

library/src/main/java/com/hjq/gson/factory/ParseExceptionCallback.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ public interface ParseExceptionCallback {
2121
void onParseObjectException(TypeToken<?> typeToken, String fieldName, JsonToken jsonToken);
2222

2323
/**
24-
* List 类型解析异常
24+
* List item 类型解析异常
2525
*
2626
* @param typeToken 类型 Token
2727
* @param fieldName 字段名称(可能为空)
2828
* @param listItemJsonToken List 条目类型(可能为空)
2929
*/
30-
void onParseListException(TypeToken<?> typeToken, String fieldName, JsonToken listItemJsonToken);
30+
void onParseListItemException(TypeToken<?> typeToken, String fieldName, JsonToken listItemJsonToken);
3131

3232
/**
33-
* Map 类型解析异常
33+
* Map item 类型解析异常
3434
*
3535
* @param typeToken 类型 Token
3636
* @param fieldName 字段名称(可能为空)
3737
* @param mapItemKey Map 集合中的 key 值,如果等于为 "null" 字符串,则证明后端返回了错误类型的 key 过来
3838
* @param mapItemJsonToken Map 条目类型(可能为空)
3939
*/
40-
void onParseMapException(TypeToken<?> typeToken, String fieldName, String mapItemKey, JsonToken mapItemJsonToken);
40+
void onParseMapItemException(TypeToken<?> typeToken, String fieldName, String mapItemKey, JsonToken mapItemJsonToken);
4141
}

library/src/main/java/com/hjq/gson/factory/constructor/KotlinDataClassDefaultValueConstructor.kt

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.hjq.gson.factory.constructor
22

33
import com.google.gson.internal.ObjectConstructor
44
import kotlin.reflect.KParameter
5+
import kotlin.reflect.KType
56
import kotlin.reflect.full.primaryConstructor
67
import kotlin.reflect.jvm.isAccessible
78

@@ -33,11 +34,24 @@ class KotlinDataClassDefaultValueConstructor<T : Any>(private val rawType: Class
3334
continue
3435
}
3536

36-
when {
37-
// 判断这个参数是不是可选的
38-
constructor.parameters[i].isOptional -> fullInitialized = false
39-
// 判断这个参数是否标记为空的
40-
constructor.parameters[i].type.isMarkedNullable -> values[i] = null
37+
val parameter = constructor.parameters[i]
38+
39+
// 判断这个参数是不是可选的
40+
if (parameter.isOptional) {
41+
fullInitialized = false
42+
}
43+
44+
// 判断这个参数是否标记为空的
45+
if (parameter.type.isMarkedNullable) {
46+
values[i] = null
47+
} else if (!parameter.isOptional) {
48+
// 如果这个参数没有标记为可空的,并且没有携带默认值
49+
// 就需要赋一个默认值给它,否则会实例化构造函数会出现崩溃
50+
// java.lang.IllegalArgumentException: method XxxBean.<init> argument 3 has type int, got java.lang.Object
51+
// 如果是基本数据类型就一定会出现崩溃,如果是对象的话,需要同时满足以下条件才会出现崩溃
52+
// 1. 后台给这个参数返回 null 的情况下
53+
// 2. 获取对象的时候,如果没有做判空,也会出现异常
54+
values[i] = getTypeDefaultValue(parameter.type)
4155
}
4256
}
4357

@@ -50,6 +64,30 @@ class KotlinDataClassDefaultValueConstructor<T : Any>(private val rawType: Class
5064
return result as T
5165
}
5266

67+
private fun getTypeDefaultValue(type: KType): Any? {
68+
// "class kotlin.Int".endsWith("kotlin.Int")
69+
if (String::class.toString().endsWith(type.toString())) {
70+
return ""
71+
} else if (Byte::class.toString().endsWith(type.toString())) {
72+
return 0.toByte()
73+
} else if (Short::class.toString().endsWith(type.toString())) {
74+
return 0.toShort()
75+
} else if (Int::class.toString().endsWith(type.toString())) {
76+
return 0
77+
} else if (Long::class.toString().endsWith(type.toString())) {
78+
return 0L
79+
} else if (Float::class.toString().endsWith(type.toString())) {
80+
return 0.0f
81+
} else if (Double::class.toString().endsWith(type.toString())) {
82+
return 0.0
83+
} else if (Char::class.toString().endsWith(type.toString())) {
84+
return '\u0000'
85+
} else if (Boolean::class.toString().endsWith(type.toString())) {
86+
return false
87+
}
88+
return null
89+
}
90+
5391
/** 一个简单的 Map,它使用参数索引而不是排序或哈希。 */
5492
class IndexedParameterMap(
5593
private val parameterKeys: List<KParameter>,

library/src/main/java/com/hjq/gson/factory/element/CollectionTypeAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Collection<E> read(JsonReader in) throws IOException {
6868
e.printStackTrace();
6969
ParseExceptionCallback callback = GsonFactory.getParseExceptionCallback();
7070
if (callback != null) {
71-
callback.onParseListException(mTypeToken, mFieldName, itemJsonToken);
71+
callback.onParseListItemException(mTypeToken, mFieldName, itemJsonToken);
7272
}
7373
}
7474
}

library/src/main/java/com/hjq/gson/factory/element/MapTypeAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public Map<K, V> read(JsonReader in) throws IOException {
9494
e.printStackTrace();
9595
ParseExceptionCallback callback = GsonFactory.getParseExceptionCallback();
9696
if (callback != null) {
97-
callback.onParseMapException(mTypeToken, mFieldName, String.valueOf(key), itemJsonToken);
97+
callback.onParseMapItemException(mTypeToken, mFieldName, String.valueOf(key), itemJsonToken);
9898
}
9999
}
100100
}

0 commit comments

Comments
 (0)