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

Closed
Cooper-Zhong opened this issue May 12, 2024 · 3 comments
Closed

[BUG] jsonArray.to(JSON.class) #2565

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

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

#2564 相似,jsonArray.to(JSON.class),代码在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]
  • 版本信息:[fastjson/fastjson2 2.0.50]

重现步骤

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

import java.util.Collection;
import java.util.List;

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

public class MutatedIssue2516Test_176_1 {
    @Test
    public void test_mutated_issue() {
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(1);

        Object jsonArrayJavaObj1 = jsonArray.to(JSONArray.class);
        Object jsonArrayJavaObj2 = jsonArray.to(Collection.class);
        Object jsonArrayJavaObj3 = jsonArray.to(List.class);
        Object jsonArrayJavaObj4 = jsonArray.to(JSON.class);

        System.out.println(jsonArrayJavaObj1.getClass());
        System.out.println(jsonArrayJavaObj2.getClass());
        System.out.println(jsonArrayJavaObj3.getClass());
        System.out.println(jsonArrayJavaObj4.getClass());
        System.out.println();
        System.out.println(JSON.toJSONString(jsonArrayJavaObj1));
        System.out.println(JSON.toJSONString(jsonArrayJavaObj2));
        System.out.println(JSON.toJSONString(jsonArrayJavaObj3));
        System.out.println(JSON.toJSONString(jsonArrayJavaObj4));

        assertEquals(jsonArray, jsonArrayJavaObj1);
        assertEquals(jsonArray, jsonArrayJavaObj2);
        assertEquals(jsonArray, jsonArrayJavaObj3);
        assertEquals(jsonArray, jsonArrayJavaObj4);
    }

    @Test
    public void test_mutated_issue1() {
        com.alibaba.fastjson.JSONArray jsonArray = new com.alibaba.fastjson.JSONArray();
        jsonArray.add(1);

        Object jsonArrayJavaObj1 = jsonArray.toJavaObject(com.alibaba.fastjson.JSONArray.class);
        Object jsonArrayJavaObj2 = jsonArray.toJavaObject(Collection.class);
        Object jsonArrayJavaObj3 = jsonArray.toJavaObject(List.class);
        Object jsonArrayJavaObj4 = jsonArray.toJavaObject(com.alibaba.fastjson.JSON.class);

        System.out.println(jsonArrayJavaObj1.getClass());
        System.out.println(jsonArrayJavaObj2.getClass());
        System.out.println(jsonArrayJavaObj3.getClass());
        System.out.println(jsonArrayJavaObj4.getClass());
        System.out.println();
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonArrayJavaObj1));
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonArrayJavaObj2));
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonArrayJavaObj3));
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(jsonArrayJavaObj4));

        assertEquals(jsonArray, jsonArrayJavaObj1);
        assertEquals(jsonArray, jsonArrayJavaObj2);
        assertEquals(jsonArray, jsonArrayJavaObj3);
        assertEquals(jsonArray, jsonArrayJavaObj4);
    }
}

相关日志输出

  • fastjson2 2.0.50
    class com.alibaba.fastjson2.JSONArray
    class java.util.ArrayList
    class java.util.ArrayList
    class jdk.proxy2.$Proxy8

    [1]
    [1]
    [1]
    {}

    org.opentest4j.AssertionFailedError:
    Expected :[1]
    Actual :{}

  • fastjson 2.0.50
    class com.alibaba.fastjson.JSONArray
    class java.util.ArrayList
    class java.util.ArrayList
    class java.util.Collections$EmptyList

    [1]
    [1]
    [1]
    []

    org.opentest4j.AssertionFailedError:
    Expected :[1]
    Actual :[]

  • fastjson 1.2.83
    class com.alibaba.fastjson.JSONArray
    class com.alibaba.fastjson.JSONArray
    class com.alibaba.fastjson.JSONArray
    class com.alibaba.fastjson.JSONArray

    [1]
    [1]
    [1]
    [1]

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label 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版本验证下

@wenshao wenshao added this to the 2.0.51 milestone May 12, 2024
@wenshao wenshao added the fixed label May 12, 2024
@Cooper-Zhong
Copy link
Contributor Author

验证无问题,辛苦wenshao

@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

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

@wenshao wenshao closed this as completed Jun 1, 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 fixed
Projects
None yet
Development

No branches or pull requests

2 participants