Skip to content

Commit

Permalink
fix setter priority
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 16, 2025
1 parent 23eb651 commit 714a2b6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
72 changes: 72 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/read/DWTest.java
Original file line number Diff line number Diff line change
@@ -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<Color> 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;
}
}
}

0 comments on commit 714a2b6

Please sign in to comment.