From 80c60bb737ac1e359e0c300d08072cd959491306 Mon Sep 17 00:00:00 2001 From: rowstop Date: Tue, 8 Oct 2024 18:07:36 +0800 Subject: [PATCH] Fix WriteNulls causing values to be serialized as null, for issue #3049 --- .../writer/ObjectWriterCreatorASM.java | 24 ++++------ .../issues_3000/issue3049/DoubleClazz.java | 13 ++++++ .../issues_3000/issue3049/DoubleClazz2.java | 10 +++++ .../issues_3000/issue3049/Issue3049.java | 44 +++++++++++++++++++ .../issues_3000/issue3049/LongClazz.java | 13 ++++++ .../issues_3000/issue3049/LongClazz2.java | 10 +++++ 6 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz.java create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/DoubleClazz2.java create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/Issue3049.java create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz.java create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3000/issue3049/LongClazz2.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 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; +}