diff --git a/README.md b/README.md index 33bb74cf..9fdb215c 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ org.noear snack3 - 3.2.66 + 3.2.67 ``` diff --git a/UPDATE_LOG.md b/UPDATE_LOG.md index 1a6bd622..847b13a8 100644 --- a/UPDATE_LOG.md +++ b/UPDATE_LOG.md @@ -1,3 +1,6 @@ +#### 3.2.67 +* 增加 java record 的参数与字段去重处理 + #### 3.2.66 * 增加 Charset 反序化支持 diff --git a/snack3/pom.xml b/snack3/pom.xml index 5a41166d..1594eeb5 100644 --- a/snack3/pom.xml +++ b/snack3/pom.xml @@ -6,7 +6,7 @@ org.noear snack3 - 3.2.66 + 3.2.67 jar ${project.artifactId} diff --git a/snack3/src/main/java/org/noear/snack/to/ObjectToer.java b/snack3/src/main/java/org/noear/snack/to/ObjectToer.java index ffc49fa4..46b2c223 100644 --- a/snack3/src/main/java/org/noear/snack/to/ObjectToer.java +++ b/snack3/src/main/java/org/noear/snack/to/ObjectToer.java @@ -500,17 +500,25 @@ public Object analyseBean(Context ctx, ONode o, Object rst, Class clz, Type t throw new IllegalArgumentException("The constructor missing parameters: " +clz.getName(), e); } } else { + //排除字段 + Set excNames = null; + if (rst == null) { if(clzWrap.recordConstructor() == null) { rst = BeanUtil.newInstance(clz); }else{ - //只有带参数的构造函(像 kotlin data) + //只有带参数的构造函(像 java record, kotlin data) + excNames = new LinkedHashSet<>(); Parameter[] argsP = clzWrap.recordParams(); Object[] argsV = new Object[argsP.length]; for (int j = 0; j < argsP.length; j++) { Parameter f = argsP[j]; String fieldK = f.getName(); + + //构造参数有的,进入排除 + excNames.add(fieldK); + if (o.contains(fieldK)) { Class fieldT = f.getType(); Type fieldGt = f.getParameterizedType(); @@ -538,8 +546,13 @@ public Object analyseBean(Context ctx, ONode o, Object rst, Class clz, Type t if (f.isDeserialize() == false) { continue; } - String fieldK = f.getName(); + + if(excNames != null && excNames.contains(fieldK)){ + continue; + } + + if (o.contains(fieldK)) { Class fieldT = f.type; Type fieldGt = f.genericType; diff --git a/snack3_demo/pom.xml b/snack3_demo/pom.xml index bdd692e6..ce76240d 100644 --- a/snack3_demo/pom.xml +++ b/snack3_demo/pom.xml @@ -20,7 +20,7 @@ org.noear snack3 - 3.2.66 + 3.2.67 diff --git a/snack3_demo/src/test/java/features/_test5.java b/snack3_demo/src/test/java/features/_test5.java new file mode 100644 index 00000000..f91664e0 --- /dev/null +++ b/snack3_demo/src/test/java/features/_test5.java @@ -0,0 +1,20 @@ +package features; + +import features.test5.A; +import org.junit.Test; +import org.noear.snack.ONode; + +/** + * @author noear 2023/5/10 created + */ +public class _test5 { + @Test + public void test() { + String poc = "{\"@type\":\"features.test5.A\"," + + "\"b\":{\"@type\":\"features.test5.B\",\"bList\":\"str1\"}}"; + System.out.println(poc); + A o = ONode.deserialize(poc); + System.out.println(o.getB().getbList().size()); + assert o.getB().getbList().size() == 1; + } +} diff --git a/snack3_demo/src/test/java/features/test5/A.java b/snack3_demo/src/test/java/features/test5/A.java new file mode 100644 index 00000000..f0c16135 --- /dev/null +++ b/snack3_demo/src/test/java/features/test5/A.java @@ -0,0 +1,20 @@ +package features.test5; + +/** + * @author noear 2023/5/10 created + */ +public class A { + private B b; + + public A(B b) { + this.b = b; + } + + public B getB() { + return b; + } + + public void setB(B b) { + this.b = b; + } +} diff --git a/snack3_demo/src/test/java/features/test5/B.java b/snack3_demo/src/test/java/features/test5/B.java new file mode 100644 index 00000000..e682d192 --- /dev/null +++ b/snack3_demo/src/test/java/features/test5/B.java @@ -0,0 +1,22 @@ +package features.test5; + +import java.util.List; + +/** + * @author noear 2023/5/10 created + */ +public class B { + private List bList; + + public B(List bList) { + this.bList = bList; + } + + public List getbList() { + return bList; + } + + public void setbList(List bList) { + this.bList = bList; + } +} diff --git "a/\346\226\207\347\253\240_Java_\344\270\255_Snack3\347\232\204\344\275\277\347\224\250.md" "b/\346\226\207\347\253\240_Java_\344\270\255_Snack3\347\232\204\344\275\277\347\224\250.md" index 8192de8b..cfaf0492 100644 --- "a/\346\226\207\347\253\240_Java_\344\270\255_Snack3\347\232\204\344\275\277\347\224\250.md" +++ "b/\346\226\207\347\253\240_Java_\344\270\255_Snack3\347\232\204\344\275\277\347\224\250.md" @@ -8,7 +8,7 @@ Snack3 基于jdk8,80kb大小,非常小巧。 org.noear snack3 - 3.2.66 + 3.2.67 ``` diff --git "a/\346\226\207\347\253\240_Snack3_\344\270\200\344\270\252\346\226\260\347\232\204\345\276\256\345\236\213JSON\346\241\206\346\236\266.md" "b/\346\226\207\347\253\240_Snack3_\344\270\200\344\270\252\346\226\260\347\232\204\345\276\256\345\236\213JSON\346\241\206\346\236\266.md" index 684db3e9..f6a5dff7 100644 --- "a/\346\226\207\347\253\240_Snack3_\344\270\200\344\270\252\346\226\260\347\232\204\345\276\256\345\236\213JSON\346\241\206\346\236\266.md" +++ "b/\346\226\207\347\253\240_Snack3_\344\270\200\344\270\252\346\226\260\347\232\204\345\276\256\345\236\213JSON\346\241\206\346\236\266.md" @@ -34,7 +34,7 @@ ONode 即 `One node` 之意;借签了 `Javascript` 所有变量由 `var` 申 org.noear snack3 - 3.2.66 + 3.2.67 ``` diff --git "a/\346\226\207\347\253\240_Snack3\344\271\213Jsonpath\344\275\277\347\224\250.md" "b/\346\226\207\347\253\240_Snack3\344\271\213Jsonpath\344\275\277\347\224\250.md" index 1faad222..01293e63 100644 --- "a/\346\226\207\347\253\240_Snack3\344\271\213Jsonpath\344\275\277\347\224\250.md" +++ "b/\346\226\207\347\253\240_Snack3\344\271\213Jsonpath\344\275\277\347\224\250.md" @@ -9,7 +9,7 @@ Snack3 是一个支持JSONPath的JSON框架。JSONPath是一个很强大的功 org.noear snack3 - 3.2.66 + 3.2.67 ```