Skip to content

Commit

Permalink
Fix WriteNulls causing values to be serialized as null, for issue #3049
Browse files Browse the repository at this point in the history
  • Loading branch information
rowstop authored and wenshao committed Oct 8, 2024
1 parent 657ca20 commit 4e2dbd8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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_);
Expand Down Expand Up @@ -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_);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4e2dbd8

Please sign in to comment.