Skip to content

Commit

Permalink
fix write double field annotation writeNulls not work, for issue #2952
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 15, 2024
1 parent 3ed22a9 commit 89f2f2e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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_);

Expand Down Expand Up @@ -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_);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 89f2f2e

Please sign in to comment.