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

[BUG] JSON.toJSON(x) should be equivalent to JSON.parse(JSON.toJSONString(x)), but misses typeKey #2981

Open
blutorange opened this issue Sep 24, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@blutorange
Copy link

blutorange commented Sep 24, 2024

问题描述

JSON.toJSON(x) should be equivalent to JSON.parse(JSON.toJSONString(x)). But JSON.toJSON does not include the type key when using @JsonType(typeKey = "type").

环境信息

  • 版本信息: Fastjson2 2.0.53

重现步骤

如何操作可以重现该问题:

git clone https://github.com/blutorange/issue-repro
cd issue-repro
git checkout issue-fastjson-tojson-missing-type-key
mvn test

The test fails:

org.opentest4j.AssertionFailedError: 
Expected :{"type":"circle","radius":5}
Actual   :{"radius":5}

For reference, the classes are:

@JSONType(typeKey = "type", seeAlso = {Shape.Triangle.class, Shape.Circle.class})
public class Shape {
    @JSONType(typeKey = "type", typeName = "circle", serializeFeatures = JSONWriter.Feature.WriteClassName)
    public static class Circle extends Shape {
        private int radius;
        public int getRadius() { return radius; }
        public void setRadius(int radius) { this.radius = radius; }
    }
}

and the test is

    @Test
    public void issueFastJson2_ToJson_Differs_From_ToJsonString() {
        var circle = new Shape.Circle();
        circle.setRadius(5);
        assertEquals(JSON.parse(JSON.toJSONString(circle)), JSON.toJSON(circle));
    }

期待的正确结果

The JSONObject returned by JSON.toJSON should include "type":"circle".

@blutorange blutorange added the bug Something isn't working label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant