Skip to content

Commit

Permalink
bug fix IgnoreNoneSerializable not work on rootObject, for issue #477
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 15, 2022
1 parent c2057ce commit 9228858
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,13 @@ static byte[] toBytes(Object object, JSONWriter.Feature... features) {
new JSONWriter.Context(JSONFactory.defaultObjectWriterProvider, features),
null
)) {
JSONWriter.Context context = writer.context;

if (object == null) {
writer.writeNull();
} else {
writer.rootObject = object;

JSONWriter.Context context = writer.context;
if ((context.features & JSONWriter.Feature.ReferenceDetection.mask) != 0) {
writer.refs = new IdentityHashMap(8);
writer.refs.put(object, writer.path = JSONWriter.Path.ROOT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,24 @@ private void genMethodWrite(

mw.visitLabel(object_);

MethodWriterContext mwc = new MethodWriterContext(objectType, objectFeatures, classNameType, mw, 8, false);
mwc.genVariantsMethodBefore();

Label return_ = new Label();
{
Label endIgnoreNoneSerializable_ = new Label();
mwc.genIsEnabled(JSONWriter.Feature.IgnoreNoneSerializable.mask, endIgnoreNoneSerializable_);
mw.visitVarInsn(Opcodes.ALOAD, OBJECT);
mw.visitTypeInsn(Opcodes.INSTANCEOF, "java/io/Serializable");
mw.visitJumpInsn(Opcodes.IFNE, endIgnoreNoneSerializable_);

mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "writeNull", "()V", false);
mw.visitJumpInsn(Opcodes.GOTO, return_);

mw.visitLabel(endIgnoreNoneSerializable_);
}

mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "startObject", "()V", false);

Expand All @@ -511,9 +529,6 @@ private void genMethodWrite(

mw.visitLabel(writeFields_);

MethodWriterContext mwc = new MethodWriterContext(objectType, objectFeatures, classNameType, mw, 8, false);
mwc.genVariantsMethodBefore();

for (int i = 0; i < fieldWriters.size(); i++) {
FieldWriter fieldWriter = fieldWriters.get(i);
gwFieldValue(mwc, fieldWriter, OBJECT, i);
Expand All @@ -522,6 +537,7 @@ private void genMethodWrite(
mw.visitVarInsn(Opcodes.ALOAD, 1);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "endObject", "()V", false);

mw.visitLabel(return_);
mw.visitInsn(Opcodes.RETURN);
mw.visitMaxs(mwc.maxVariant + 1, mwc.maxVariant + 1);
mw.visitEnd();
Expand All @@ -542,6 +558,23 @@ private void genMethodWriteJSONB(
final int FIELD_FEATURES = 5;

MethodWriterContext mwc = new MethodWriterContext(objectType, objectFeatures, classNameType, mw, 7, true);
mwc.genVariantsMethodBefore();

Label LReturn = new Label();
{
Label endIgnoreNoneSerializable_ = new Label();
mwc.genIsEnabled(JSONWriter.Feature.IgnoreNoneSerializable.mask, endIgnoreNoneSerializable_);
mw.visitVarInsn(Opcodes.ALOAD, OBJECT);
mw.visitTypeInsn(Opcodes.INSTANCEOF, "java/io/Serializable");
mw.visitJumpInsn(Opcodes.IFNE, endIgnoreNoneSerializable_);

mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "writeNull", "()V", false);
mw.visitJumpInsn(Opcodes.GOTO, LReturn);

mw.visitLabel(endIgnoreNoneSerializable_);
}

int entryCnt = fieldWriters.size();

Label notWriteType = new Label();
Expand All @@ -562,8 +595,6 @@ private void genMethodWriteJSONB(
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "startObject", "()V", false);

mwc.genVariantsMethodBefore();

for (int i = 0; i < fieldWriters.size(); i++) {
FieldWriter fieldWriter = fieldWriters.get(i);
gwFieldValueJSONB(mwc, fieldWriter, OBJECT, i);
Expand All @@ -572,6 +603,7 @@ private void genMethodWriteJSONB(
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "endObject", "()V", false);

mw.visitLabel(LReturn);
mw.visitInsn(Opcodes.RETURN);
mw.visitMaxs(mwc.maxVariant + 1, mwc.maxVariant + 1);
mw.visitEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void test_feature_jsonb() {
);
}

public static class Bean {
public static class Bean
implements Serializable {
public A a;
public B b;
}
Expand All @@ -64,4 +65,69 @@ public static class A

public static class B {
}

@Test
public void test1() {
Bean1 bean = new Bean1();
bean.a = new A();
bean.b = new B();

assertEquals("{\n" +
"\t\"a\":{},\n" +
"\t\"b\":{}\n" +
"}", JSONB.toJSONString(JSONB.toBytes(bean, JSONWriter.Feature.FieldBased)));

assertEquals("{\n" +
"\t\"a\":{}\n" +
"}", JSONB.toJSONString(JSONB.toBytes(bean, JSONWriter.Feature.IgnoreNoneSerializable, JSONWriter.Feature.FieldBased)));

assertEquals("{\n" +
"\t\"a\":{}\n" +
"}", JSONB.toJSONString(
JSONB.toBytes(bean, JSONWriter.Feature.IgnoreNoneSerializable, JSONWriter.Feature.FieldBased), JSONB.symbolTable("id")
)
);
}

public static class Bean1
implements Serializable {
private A a;
private B b;
}

@Test
public void test2_serialize() {
Bean2 bean = new Bean2();
bean.a = new A();
bean.b = new B();

assertEquals("{\n" +
"\t\"a\":{},\n" +
"\t\"b\":{}\n" +
"}", JSONB.toJSONString(JSONB.toBytes(bean, JSONWriter.Feature.FieldBased)));

assertEquals("null", JSONB.toJSONString(
JSONB.toBytes(bean, JSONWriter.Feature.IgnoreNoneSerializable, JSONWriter.Feature.FieldBased))
);
}

@Test
public void test2_serialize_map() {
JSONObject object = JSONObject.of("value", new Bean2());
assertEquals("{\"value\":{}}", JSON.toJSONString(object));
assertEquals("{\"value\":null}", JSON.toJSONString(object, JSONWriter.Feature.IgnoreNoneSerializable));

assertEquals("{\n" +
"\t\"value\":{}\n" +
"}", JSONB.toJSONString(object.toJSONBBytes()));

assertEquals("{\n" +
"\t\"value\":null\n" +
"}", JSONB.toJSONString(object.toJSONBBytes(JSONWriter.Feature.IgnoreNoneSerializable)));
}

private static class Bean2 {
private A a;
private B b;
}
}

0 comments on commit 9228858

Please sign in to comment.