Skip to content

fix return JSONObject instead of null, for issue #3419 #3493

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

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
@@ -210,7 +210,7 @@ public JSONObject getJSONObject(int index) {
return writerAdapter.toJSONObject(value);
}

return null;
return (JSONObject) JSON.toJSON(value);
}

/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.issues_3400;

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

import java.util.HashMap;
import java.util.Map;

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

public class Issue3419 {
@Test
public void test() {
Map<String, Object> map = new HashMap<>();
map.put("str", new float[] {1.0f, 1.0f, 1.0f, 1.0f, 1.0f});

com.alibaba.fastjson.JSONObject jo1 = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONArray ja1 = new com.alibaba.fastjson.JSONArray();
ja1.addAll(map.entrySet());
jo1.put("content", ja1);

JSONObject jo2 = new JSONObject();
JSONArray ja2 = new JSONArray();
ja2.addAll(map.entrySet());
jo2.put("content", ja2);

assertEquals(jo1.getJSONArray("content").getJSONObject(0), jo2.getJSONArray("content").getJSONObject(0));
}
}