Skip to content

Commit

Permalink
fix writeClassName not work on JSONField.serializeFeatures, for issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 29, 2023
1 parent 94c4700 commit 18ee5ab
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ private void genMethodWrite(
mw.visitVarInsn(Opcodes.ISTORE, COMMA); // comma = false

Label writeFields_ = new Label();
isWriteTypeInfo(objectFeatures, mw, OBJECT, FIELD_TYPE, writeFields_);
isWriteTypeInfo(objectFeatures, mw, OBJECT, FIELD_TYPE, FIELD_FEATURES, writeFields_);

mw.visitVarInsn(Opcodes.ALOAD, THIS);
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
Expand Down Expand Up @@ -709,6 +709,7 @@ private static void isWriteTypeInfo(
MethodWriter mw,
int OBJECT,
int FIELD_TYPE,
int FEILD_FEATURE,
Label notWriteType
) {
if ((objectFeatures & JSONWriter.Feature.WriteClassName.mask) == 0) {
Expand All @@ -723,7 +724,8 @@ private static void isWriteTypeInfo(
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitVarInsn(Opcodes.ALOAD, OBJECT);
mw.visitVarInsn(Opcodes.ALOAD, FIELD_TYPE);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "isWriteTypeInfo", "(Ljava/lang/Object;Ljava/lang/reflect/Type;)Z", false);
mw.visitVarInsn(Opcodes.LLOAD, FEILD_FEATURE);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "isWriteTypeInfo", "(Ljava/lang/Object;Ljava/lang/reflect/Type;J)Z", false);
mw.visitJumpInsn(Opcodes.IFEQ, notWriteType);
}
}
Expand Down Expand Up @@ -773,7 +775,7 @@ private void genMethodWriteJSONB(
}

Label notWriteType = new Label();
isWriteTypeInfo(objectFeatures, mw, OBJECT, FIELD_TYPE, notWriteType);
isWriteTypeInfo(objectFeatures, mw, OBJECT, FIELD_TYPE, FIELD_FEATURES, notWriteType);

mw.visitVarInsn(Opcodes.ALOAD, THIS);
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
Expand Down Expand Up @@ -820,7 +822,7 @@ private void genMethodWriteArrayMappingJSONB(

{
Label notWriteType = new Label();
isWriteTypeInfo(objectFeatures, mw, OBJECT, FIELD_TYPE, notWriteType);
isWriteTypeInfo(objectFeatures, mw, OBJECT, FIELD_TYPE, FIELD_FEATURES, notWriteType);

mw.visitVarInsn(Opcodes.ALOAD, THIS);
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.alibaba.fastjson2.issues_1900;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1947 {
@Test
public void test() {
A a = new A();
a.id = 1;
a.name = "a";

B b = new B();
b.id = 2;

C c = new C();
c.list = new ID[]{a, b};

String str = JSON.toJSONString(c);
assertEquals(
"{\"list\":[{\"@type\":\"" + Issue1947.class.getName() + "$A\",\"id\":1,\"name\":\"a\"},{\"@type\":\"com.alibaba.fastjson2.issues_1900.Issue1947$B\",\"id\":2}]}",
str);
}

public static class C {
@JSONField(serializeFeatures = JSONWriter.Feature.WriteClassName)
public ID[] list;
}

public static class A
implements ID {
public int id;
public String name;

@Override
public int getId() {
return id;
}
}

public static class B
implements ID {
public int id;

@Override
public int getId() {
return id;
}
}

public interface ID {
int getId();
}
}

0 comments on commit 18ee5ab

Please sign in to comment.