Skip to content

Commit 2e3264d

Browse files
committed
optimize for deserialize none-constructor class
1 parent 52a75c7 commit 2e3264d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderNoneDefaultConstructor.java

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.InvocationTargetException;
99
import java.lang.reflect.Type;
1010
import java.util.*;
11+
import java.util.function.BiFunction;
1112
import java.util.function.Function;
1213

1314
import static com.alibaba.fastjson2.JSONB.Constants.BC_NULL;
@@ -20,6 +21,8 @@ public class ObjectReaderNoneDefaultConstructor<T>
2021
final Function<Map<Long, Object>, T> creatorFunction;
2122
final Map<Long, FieldReader> paramFieldReaderMap;
2223
final Constructor noneDefaultConstructor;
24+
final BiFunction bifunction;
25+
final Function function;
2326

2427
public ObjectReaderNoneDefaultConstructor(
2528
Class objectClass,
@@ -60,6 +63,13 @@ public ObjectReaderNoneDefaultConstructor(
6063
} else {
6164
noneDefaultConstructor = null;
6265
}
66+
if (creator instanceof ConstructorFunction) {
67+
bifunction = ((ConstructorFunction<T>) creator).biFunction;
68+
function = ((ConstructorFunction<T>) creator).function;
69+
} else {
70+
bifunction = null;
71+
function = null;
72+
}
6373
}
6474

6575
static FieldReader[] concat(FieldReader[] a, FieldReader[] b) {
@@ -536,6 +546,12 @@ typeName, getObjectClass(), features | getFeatures()
536546

537547
public T createInstance(Object[] args) {
538548
try {
549+
if (function != null) {
550+
return (T) function.apply(args[0]);
551+
}
552+
if (bifunction != null) {
553+
return (T) bifunction.apply(args[0], args[1]);
554+
}
539555
return (T) noneDefaultConstructor.newInstance(args);
540556
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException |
541557
InvocationTargetException e) {

core/src/main/java/com/alibaba/fastjson2/support/LambdaMiscCodec.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public static Function createFunction(Constructor constructor) {
442442
METHOD_TYPE_FUNCTION,
443443
METHOD_TYPE_OBJECT_OBJECT,
444444
methodHandle,
445-
MethodType.methodType(declaringClass, param0)
445+
MethodType.methodType(declaringClass, box(param0))
446446
);
447447
return (Function) callSite.getTarget().invokeExact();
448448
} catch (Throwable ignored) {

test-jdk17/src/test/java/com/alibaba/fastjson2/RecordTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,21 @@ public void test4() {
8080

8181
private record Item4(int value) {
8282
}
83+
84+
85+
@Test
86+
public void test5() {
87+
Item5 item = new Item5(new User5(true, "abc"));
88+
String str = JSON.toJSONString(item);
89+
Item5 item2 = JSON.parseObject(str, Item5.class);
90+
assertEquals(item.user.default_profile, item2.user.default_profile);
91+
assertEquals(item.user.screen_name, item2.user.screen_name);
92+
}
93+
94+
private record User5(boolean default_profile, String screen_name) {
95+
}
96+
97+
private record Item5(User5 user) implements Serializable {
98+
}
99+
83100
}

0 commit comments

Comments
 (0)