Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WriteNulls causing values to be serialized as null, for issue #3049 #3063

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading