diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java b/core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java index ef93360027..ad2bbd3c09 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java @@ -268,6 +268,16 @@ public int compareTo(FieldReader o) { return -1; } + // Collection first + if (Collection.class.isAssignableFrom(otherParamType) && !Collection.class.isAssignableFrom(thisParamType)) { + return 1; + } + + if (Collection.class.isAssignableFrom(thisParamType) && !Collection.class.isAssignableFrom(otherParamType)) { + return -1; + } + + // field class compare if (needCompareToActualFieldClass(thisParamType) || needCompareToActualFieldClass(otherParamType)) { Class actualFieldClass = null; try { diff --git a/core/src/test/java/com/alibaba/fastjson2/read/DWTest.java b/core/src/test/java/com/alibaba/fastjson2/read/DWTest.java new file mode 100644 index 0000000000..6feda2d72c --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/read/DWTest.java @@ -0,0 +1,72 @@ +package com.alibaba.fastjson2.read; + +import com.alibaba.fastjson2.*; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class DWTest { + @Test + public void test_parse_object_with_array_field() { + String json = "{\n" + + " \"stores\": [\n" + + " {\n" + + " \"car\": {\n" + + " \"color\": {\n" + + " \"name\": \"red\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"car\": {\n" + + " \"color\": [\n" + + " {\n" + + " \"name\": \"red\"\n" + + " },\n" + + " {\n" + + " \"name\": \"green\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + " ]\n" + + " }"; + JSONObject chartObject = JSON.parseObject(json); + JSONArray stores = chartObject.getJSONArray("stores"); + + for (int i = 0; i < stores.size(); i++) { + JSONObject store = stores.getJSONObject(i); + + Car car = JSON.parseObject(store.getString("car"), Car.class); + System.out.println(JSON.toJSONString(car, JSONWriter.Feature.PrettyFormat)); + } + } + + public static class Car { + private Color color; + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + public void setColor(List colors) { + this.color = colors.get(0); + } + } + + public static class Color { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +}