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] 2.0.52 JSONArray ClassCastException。使用 fastjson1 的 JSON.toJSON() 方法,转换后的数据中存在 com.alibaba.fastjson2.JSONArray 对象。 #2856

Closed
zhangbody opened this issue Aug 7, 2024 · 1 comment
Labels
bug Something isn't working fixed
Milestone

Comments

@zhangbody
Copy link

问题描述

使用 fastjson1 的 JSON.toJSON() 方法,转换后的数据中存在 com.alibaba.fastjson2.JSONArray 对象。

环境信息

请填写以下信息:

  • OS信息:Windows 10
  • JDK信息:JDK信息:Oracle Java 1.8.0_381
  • 版本信息:Fastjson2 2.0.52

重现步骤

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

抛出异常,见日志输出。

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * JSON 处理方法
 * 
 * @author zhangrong
 * @date 2024/08/07
 */
public class Test {

  public static void main(String[] args) {
    Map map1 = new HashMap();

    List list1 = new LinkedList();
    map1.put("list1", list1);

    Map map2 = new HashMap();
    list1.add(map2);

    List list2 = new LinkedList();
    map2.put("list2", list2);

    Map map3 = new HashMap();
    list2.add(map3);

    List list3 = new LinkedList();
    map3.put("list3", list3);

    list3.add("str1");
    list3.add("str2");
    list3.add("str3");

    Object o1 = JSON.toJSON(map1);

    System.out.println(((JSONArray)((JSONObject)o1).get("list1")).get(0));
  }

}

期待的正确结果

使用 fastjson1 兼容 api 时,返回 fastjson1 的 JSONArray。

相关日志输出

Exception in thread "main" java.lang.ClassCastException: com.alibaba.fastjson2.JSONArray cannot be cast to com.alibaba.fastjson.JSONArray

@zhangbody zhangbody added the bug Something isn't working label Aug 7, 2024
Cooper-Zhong added a commit to Cooper-Zhong/fastjson2 that referenced this issue Aug 7, 2024
@wenshao wenshao added this to the 2.0.53 milestone Aug 25, 2024
@wenshao wenshao added the fixed label Aug 25, 2024
@wenshao
Copy link
Member

wenshao commented Sep 16, 2024

@wenshao wenshao closed this as completed Sep 16, 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