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] jsonObject.to(JSON.class) #2564

Open
Cooper-Zhong opened this issue May 12, 2024 · 2 comments
Open

[BUG] jsonObject.to(JSON.class) #2564

Cooper-Zhong opened this issue May 12, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link

Cooper-Zhong commented May 12, 2024

问题描述

代码在fastjson兼容包和1.2.83均正常,在fastjson2中使用jsonObject.to(JSON.class)与fastjson不一致,JSON.toJSONString也无法按预期序列化

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson2 2.0.50]

重现步骤

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.Map;

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

public class MutatedIssue2516Test_176 {

    @Test
    public void test_mutated_issue() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", "value");

        Object jsonObjectJavaObj1 = jsonObject.to(JSONObject.class);
        Object jsonObjectJavaObj2 = jsonObject.to(Map.class);
        Object jsonObjectJavaObj3 = jsonObject.to(JSON.class);

        System.out.println(jsonObjectJavaObj1.getClass());
        System.out.println(jsonObjectJavaObj2.getClass());
        System.out.println(jsonObjectJavaObj3.getClass());
        System.out.println();
        System.out.println(JSON.toJSONString(jsonObjectJavaObj1));
        System.out.println(JSON.toJSONString(jsonObjectJavaObj2));
        System.out.println(JSON.toJSONString(jsonObjectJavaObj3));

        assertEquals(jsonObject, jsonObjectJavaObj1);
        assertEquals(jsonObject, jsonObjectJavaObj2);
        assertEquals(jsonObject, jsonObjectJavaObj3);
    }

    @Test
    public void test_mutated_issue1() {
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("key", "value");

        Object jsonObjectJavaObj1 = jsonObject.toJavaObject(com.alibaba.fastjson.JSONObject.class);
        Object jsonObjectJavaObj2 = jsonObject.toJavaObject(Map.class);
        Object jsonObjectJavaObj3 = jsonObject.toJavaObject(com.alibaba.fastjson.JSON.class);

        System.out.println(jsonObjectJavaObj1.getClass());
        System.out.println(jsonObjectJavaObj2.getClass());
        System.out.println(jsonObjectJavaObj3.getClass());
        System.out.println();
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonObjectJavaObj1));
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonObjectJavaObj2));
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonObjectJavaObj3));

        assertEquals(jsonObject, jsonObjectJavaObj1);
        assertEquals(jsonObject, jsonObjectJavaObj2);
        assertEquals(jsonObject, jsonObjectJavaObj3);
    }
}

相关日志输出

class com.alibaba.fastjson2.JSONObject
class com.alibaba.fastjson2.JSONObject
class jdk.proxy2.$Proxy8

{"key":"value"}
{"key":"value"}
{}

org.opentest4j.AssertionFailedError: expected: com.alibaba.fastjson2.JSONObject@1de5f259<{"key":"value"}> but was: jdk.proxy2.$Proxy8@729d991e<{"key":"value"}>
Expected :{"key":"value"}
Actual :{"key":"value"}

// in fastjson
class com.alibaba.fastjson.JSONObject
class com.alibaba.fastjson.JSONObject
class com.alibaba.fastjson.JSONObject

{"key":"value"}
{"key":"value"}
{"key":"value"}

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 12, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 12, 2024
@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证下

@Cooper-Zhong
Copy link
Author

验证无问题,辛苦wenshao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants