From 89f2f2e3399001d015ebf7cab3d0831b51428b1e Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 15 Sep 2024 18:43:32 +0800 Subject: [PATCH] fix write double field annotation writeNulls not work, for issue #2952 --- .../writer/ObjectWriterCreatorASM.java | 42 +++++++++++-------- .../fastjson2/issues_2900/Issue2952.java | 31 ++++++++++++++ 2 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2952.java diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java index 6136118a92..d61a97cca9 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java @@ -1897,7 +1897,6 @@ private void gwFieldValueObject( genGetObject(mwc, fieldWriter, i, OBJECT); mw.visitInsn(Opcodes.DUP); mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); - mw.visitJumpInsn(Opcodes.IFNULL, null_); if (Map.class.isAssignableFrom(fieldClass)) { @@ -2473,16 +2472,21 @@ private void gwInt64( Label endIfNull_ = new Label(), notNull_ = new Label(), writeNullValue_ = new Label(); genGetObject(mwc, fieldWriter, i, OBJECT); - mw.visitInsn(Opcodes.DUP); - mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); - mw.visitJumpInsn(Opcodes.IFNONNULL, notNull_); + if ((fieldWriter.features & WriteNulls.mask) == 0) { + mw.visitInsn(Opcodes.DUP); + mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); - mwc.genIsEnabled( - WriteNulls.mask | NullAsDefaultValue.mask | WriteNullNumberAsZero.mask, - writeNullValue_, - endIfNull_ - ); + mw.visitJumpInsn(Opcodes.IFNONNULL, notNull_); + + mwc.genIsEnabled( + WriteNulls.mask | NullAsDefaultValue.mask | WriteNullNumberAsZero.mask, + writeNullValue_, + endIfNull_ + ); + } else { + mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); + } mw.visitLabel(writeNullValue_); @@ -2534,16 +2538,20 @@ private void gwDouble( Label endIfNull_ = new Label(), notNull_ = new Label(), writeNullValue_ = new Label(); genGetObject(mwc, fieldWriter, i, OBJECT); - mw.visitInsn(Opcodes.DUP); - mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); - mw.visitJumpInsn(Opcodes.IFNONNULL, notNull_); + if ((fieldWriter.features & WriteNulls.mask) == 0) { + mw.visitInsn(Opcodes.DUP); + mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); + mw.visitJumpInsn(Opcodes.IFNONNULL, notNull_); - mwc.genIsEnabled( - WriteNulls.mask | NullAsDefaultValue.mask | WriteNullNumberAsZero.mask, - writeNullValue_, - endIfNull_ - ); + mwc.genIsEnabled( + WriteNulls.mask | NullAsDefaultValue.mask | WriteNullNumberAsZero.mask, + writeNullValue_, + endIfNull_ + ); + } else { + mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); + } mw.visitLabel(writeNullValue_); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2952.java b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2952.java new file mode 100644 index 0000000000..f86b7deec1 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2952.java @@ -0,0 +1,31 @@ +package com.alibaba.fastjson2.issues_2900; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.annotation.JSONField; +import lombok.Data; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2952 { + @Test + public void toJsonList() { + assertEquals("{\"d\":null,\"i\":null,\"l\":null,\"s\":null}", JSON.toJSONString(new C())); + } + + @Data + public class C { + @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) + private Long l; + + @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) + private Double d; + + @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) + private Integer i; + + @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) + private String s; + } +}