gson反序列化主要分爲兩個過程:java
ConstructorConstructor.getjson
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) {
final Type type = typeToken.getType();
final Class<? super T> rawType = typeToken.getRawType();
// first try an instance creator
@SuppressWarnings("unchecked") // types must agree
final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type);
if (typeCreator != null) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return typeCreator.createInstance(type);
}
};
}
// Next try raw type match for instance creators
@SuppressWarnings("unchecked") // types must agree
final InstanceCreator<T> rawTypeCreator =
(InstanceCreator<T>) instanceCreators.get(rawType);
if (rawTypeCreator != null) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return rawTypeCreator.createInstance(type);
}
};
}
// 獲取無參構造函數
ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType);
if (defaultConstructor != null) {
return defaultConstructor;
}
// 獲取List<T>,Map<T>等構造函數,對於List,Map的狀況
ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType);
if (defaultImplementation != null) {
return defaultImplementation;
}
// unSafe構造出對象,不調用任何的構造函數
// finally try unsafe
return newUnsafeAllocator(type, rawType);
}
複製代碼
ConstructorConstructor.newDefaultConstructorbash
private <T> ObjectConstructor<T> newDefaultConstructor(Class<? super T> rawType) {
try {
// 獲取無參構造函數
final Constructor<? super T> constructor = rawType.getDeclaredConstructor();
if (!constructor.isAccessible()) {
accessor.makeAccessible(constructor);
}
複製代碼
ConstructorConstructor.newUnsafeAllocatoride
private <T> ObjectConstructor<T> newUnsafeAllocator(
final Type type, final Class<? super T> rawType) {
return new ObjectConstructor<T>() {
private final UnsafeAllocator unsafeAllocator = UnsafeAllocator.create();
@SuppressWarnings("unchecked")
@Override public T construct() {
try {
//
Object newInstance = unsafeAllocator.newInstance(rawType);
return (T) newInstance;
} catch (Exception e) {
throw new RuntimeException(("Unable to invoke no-args constructor for " + type + ". "
+ "Registering an InstanceCreator with Gson for this type may fix this problem."), e);
}
}
};
}
複製代碼
class User(val id: Int, val name: String = "sss") {
init {
println("init")
}
}
複製代碼
public final class User {
private final int id;
@NotNull
private final String name;
public User(int id, @NotNull String name) {
Intrinsics.checkParameterIsNotNull(name, "name");
super();
this.id = id;
this.name = name;
String var3 = "init";
System.out.println(var3);
}
// $FF: synthetic method
public User(int var1, String var2, int var3, DefaultConstructorMarker var4) {
if ((var3 & 2) != 0) {
var2 = "";
}
this(var1, var2);
}
}
複製代碼
代碼:函數
@Test
fun testJson() {
val user = Gson().fromJson("{}", User::class.java)
print(user.name)
}
複製代碼
輸出:不符合預期(咱們聲明的非空的name實際結果是null)ui
null
Process finished with exit code 0
複製代碼
class User(val id: Int=1, val name: String = "sss") {
init {
println("init")
}
}
複製代碼
public final class User {
private final int id;
@NotNull
private final String name;
public User(int id, @NotNull String name) {
Intrinsics.checkParameterIsNotNull(name, "name");
super();
this.id = id;
this.name = name;
String var3 = "init";
System.out.println(var3);
}
// $FF: synthetic method
public User(int var1, String var2, int var3, DefaultConstructorMarker var4) {
if ((var3 & 1) != 0) {
var1 = 1;
}
if ((var3 & 2) != 0) {
var2 = "";
}
this(var1, var2);
}
// 無參構造函數
public User() {
this(0, (String)null, 3, (DefaultConstructorMarker)null);
}
}
複製代碼
代碼:this
@Test
fun testJson() {
val user = Gson().fromJson("{}", User::class.java)
print(user.name)
}
複製代碼
輸出:符合預期spa
init
sss
Process finished with exit code 0
複製代碼
class User(val id: Int=1 , val name: String = "sss") {
init {
println("init")
}
}
複製代碼
class User {
val id: Int = 1
val name: String = "sss"
init {
println("init")
}
}
複製代碼