Skip to content

Commit

Permalink
fix parse private class error, for issue #3134
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 2, 2024
1 parent 2b0efee commit e07d013
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3032,7 +3032,9 @@ private <T> int genReadFieldValue(

if (list) {
Class itemClass = TypeUtils.getMapping(itemType);
if (itemClass != null && Collection.class.isAssignableFrom(itemClass)) {
if (itemClass != null
&& (Collection.class.isAssignableFrom(itemClass) || !Modifier.isPublic(itemClass.getModifiers()))
) {
list = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.fastjson2.issues_3100;

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

import java.util.List;

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

public class Issue3134 {
@Test
public void test() {
String json = "{\"name\":\"希望小学\",\"students\":{\"age\":\"12\",\"id\":\"1\",\"name\":\"张宇\"}}";
School schoolOne = JSON.parseObject(json, School.class);
JSONObject schoolTwo = JSON.parseObject(json);
School schoolThree = schoolTwo.toJavaObject(School.class);
JSONObject studentOne = schoolTwo.getJSONObject("students");
JSONArray studentTwo = schoolTwo.getJSONArray("students");
assertEquals("[{\"age\":\"12\",\"id\":\"1\",\"name\":\"张宇\"}]", studentTwo.toString());
}

@Data
private static class School{
private String name;
private List<Student> students;
}

@Data
private static class Student{
private String name;
private String id;
private String age;
}
}

0 comments on commit e07d013

Please sign in to comment.