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 ab8f7b0483..fce84bc1ac 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java @@ -2385,7 +2385,7 @@ private void gwInt32( int OBJECT, int i ) { - boolean jsonb = mwc.jsonb; +// boolean jsonb = mwc.jsonb; String classNameType = mwc.classNameType; MethodWriter mw = mwc.mw; Class fieldClass = fieldWriter.fieldClass; @@ -2463,7 +2463,7 @@ private void gwInt64( int OBJECT, int i ) { - boolean jsonb = mwc.jsonb; +// boolean jsonb = mwc.jsonb; MethodWriter mw = mwc.mw; Class fieldClass = fieldWriter.fieldClass; String classNameType = mwc.classNameType; @@ -2473,20 +2473,16 @@ 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); - - 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_); @@ -2539,19 +2535,15 @@ 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_ ); - } else { - mw.visitVarInsn(Opcodes.ASTORE, FIELD_VALUE); } mw.visitLabel(writeNullValue_); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz.java b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz.java new file mode 100644 index 0000000000..7c57a00677 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz.java @@ -0,0 +1,13 @@ +package com.alibaba.fastjson2.issues_3000.issue3049; + +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.annotation.JSONField; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DoubleClazz { + @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) + private Double d; +} diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz2.java b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz2.java new file mode 100644 index 0000000000..233e088de9 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz2.java @@ -0,0 +1,10 @@ +package com.alibaba.fastjson2.issues_3000.issue3049; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DoubleClazz2 { + private Double d; +} diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/Issue3049.java b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/Issue3049.java new file mode 100644 index 0000000000..74de34d0a5 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/Issue3049.java @@ -0,0 +1,44 @@ +package com.alibaba.fastjson2.issues_3000.issue3049; + +import com.alibaba.fastjson2.JSON; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Test; +import org.skyscreamer.jsonassert.JSONAssert; + +/** + * @author 张治保 + * @since 2024/10/8 + */ +public class Issue3049 { + @Test + @SneakyThrows + void testDouble() { + DoubleClazz c = new DoubleClazz(); + c.setD(1D); + JSONAssert.assertEquals("{\"d\":1.0}", JSON.toJSONString(c), true); + c.setD(null); + JSONAssert.assertEquals("{\"d\":null}", JSON.toJSONString(c), true); + + DoubleClazz2 c2 = new DoubleClazz2(); + c2.setD(1D); + JSONAssert.assertEquals("{\"d\":1.0}", JSON.toJSONString(c2), true); + c2.setD(null); + JSONAssert.assertEquals("{}", JSON.toJSONString(c2), true); + } + + @Test + @SneakyThrows + void testLong() { + LongClazz c = new LongClazz(); + c.setD(1L); + JSONAssert.assertEquals("{\"d\":1.0}", JSON.toJSONString(c), true); + c.setD(null); + JSONAssert.assertEquals("{\"d\":null}", JSON.toJSONString(c), true); + + LongClazz2 c2 = new LongClazz2(); + c2.setD(1L); + JSONAssert.assertEquals("{\"d\":1.0}", JSON.toJSONString(c2), true); + c2.setD(null); + JSONAssert.assertEquals("{}", JSON.toJSONString(c2), true); + } +} diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz.java b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz.java new file mode 100644 index 0000000000..5cc9f5f3ee --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz.java @@ -0,0 +1,13 @@ +package com.alibaba.fastjson2.issues_3000.issue3049; + +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.annotation.JSONField; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class LongClazz { + @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) + private Long d; +} diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz2.java b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz2.java new file mode 100644 index 0000000000..eaeeebb0cd --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz2.java @@ -0,0 +1,10 @@ +package com.alibaba.fastjson2.issues_3000.issue3049; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class LongClazz2 { + private Long d; +}