Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG] @JSONType 结合 JSONWriter.Feature.WriteClassName,JSONWriter.Feature.NotWriteRootClassName 也会输出根类名。 #2662

Closed
wjtien opened this issue Jun 4, 2024 · 1 comment
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@wjtien
Copy link

wjtien commented Jun 4, 2024

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

  • OS信息: Linux
  • JDK信息: OracleJdk 1.8.0_202
  • 版本信息: Fastjson2 2.0.50

重现步骤

如何操作可以重现该问题:
直接运行如下代码

@JSONType(serializeFeatures ={JSONWriter.Feature.WriteClassName, JSONWriter.Feature.NotWriteRootClassName})
    public static class WrapperClassA {
        private AbsPojoClassB pojo;

        public AbsPojoClassB getPojo() {
            return pojo;
        }

        public void setPojo(AbsPojoClassB pojo) {
            this.pojo = pojo;
        }
    }

    public static class AbsPojoClassB {
        private Integer int1;
        private String str2;

        public Integer getInt1() {
            return int1;
        }

        public void setInt1(Integer int1) {
            this.int1 = int1;
        }

        public String getStr2() {
            return str2;
        }

        public void setStr2(String str2) {
            this.str2 = str2;
        }
    }

    public static class PojoClassC extends AbsPojoClassB {
    }


    public static void main(String[] args) {
        final WrapperClassA wrapperClassA = new WrapperClassA();
        final PojoClassC pojoClassC = new PojoClassC();
        pojoClassC.setInt1(1);
        pojoClassC.setStr2("str");

        wrapperClassA.setPojo(pojoClassC);
        final String jsonStr = JSON.toJSONString(wrapperClassA);
        System.out.println(jsonStr);
    }

期待的正确结果

{"pojo":{"@type":"com.xx.test.TestFastJson$PojoClassC","int1":1,"str2":"str"}}

相关日志输出

{"@type":"com.xx.test.TestFastJson$WrapperClassA","pojo":{"@type":"com.xx.test.TestFastJson$PojoClassC","int1":1,"str2":"str"}}
@wjtien wjtien added the bug Something isn't working label Jun 4, 2024
@yanxutao89 yanxutao89 self-assigned this Jun 20, 2024
@wenshao wenshao added this to the 2.0.52 milestone Jun 21, 2024
@wenshao wenshao added the fixed label Jun 21, 2024
@wenshao
Copy link
Member

wenshao commented Jul 14, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.52
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jul 14, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants