Skip to content

Commit d48df21

Browse files
committed
improve getter/setter support
1 parent 547a546 commit d48df21

File tree

8 files changed

+189
-4
lines changed

8 files changed

+189
-4
lines changed

src/main/java/com/alibaba/fastjson/parser/ParserConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public ParserConfig(){
235235
derializers.put(AtomicInteger.class, IntegerDeserializer.instance);
236236
derializers.put(AtomicLong.class, LongDeserializer.instance);
237237
derializers.put(AtomicReference.class, ReferenceDeserializer.instance);
238-
238+
239239
derializers.put(WeakReference.class, ReferenceDeserializer.instance);
240240
derializers.put(SoftReference.class, ReferenceDeserializer.instance);
241241

@@ -522,6 +522,17 @@ public boolean isPrimitive(Class<?> clazz) {
522522
}
523523

524524
public static Field getField(Class<?> clazz, String fieldName) {
525+
Field field = getField0(clazz, fieldName);
526+
if (field == null) {
527+
field = getField0(clazz, "_" + fieldName);
528+
}
529+
if (field == null) {
530+
field = getField0(clazz, "m_" + fieldName);
531+
}
532+
return field;
533+
}
534+
535+
private static Field getField0(Class<?> clazz, String fieldName) {
525536
for (Field item : clazz.getDeclaredFields()) {
526537
if (fieldName.equals(item.getName())) {
527538
return item;
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.alibaba.fastjson.serializer;
22

33
public interface NameFilter extends SerializeFilter {
4-
54
String process(Object source, String name, Object value);
65
}

src/main/java/com/alibaba/fastjson/util/DeserializeBeanInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
214214

215215
String propertyName;
216216
if (Character.isUpperCase(c3)) {
217+
if (Character.isUpperCase(methodName.charAt(4))) {
218+
propertyName = methodName.substring(3);
219+
} else {
220+
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
221+
}
217222
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
218223
} else if (c3 == '_') {
219224
propertyName = methodName.substring(4);

src/main/java/com/alibaba/fastjson/util/TypeUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,11 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String, String>
941941

942942
String propertyName;
943943
if (Character.isUpperCase(c3)) {
944-
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
944+
if (Character.isUpperCase(methodName.charAt(4))) {
945+
propertyName = methodName.substring(3);
946+
} else {
947+
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
948+
}
945949
} else if (c3 == '_') {
946950
propertyName = methodName.substring(4);
947951
} else if (c3 == 'f') {
@@ -958,7 +962,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String, String>
958962

959963
Field field = ParserConfig.getField(clazz, propertyName);
960964
if (field == null) {
961-
field = ParserConfig.getField(clazz, methodName.substring(3));
965+
field = ParserConfig.getField(clazz, propertyName);
962966
}
963967

964968
if (field != null) {
@@ -1011,6 +1015,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String, String>
10111015
}
10121016

10131017
Field field = ParserConfig.getField(clazz, propertyName);
1018+
10141019
if (field != null) {
10151020
JSONField fieldAnnotation = field.getAnnotation(JSONField.class);
10161021

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.alibaba.json.bvt.serializer;
2+
3+
import org.junit.Assert;
4+
import junit.framework.TestCase;
5+
6+
import com.alibaba.fastjson.JSON;
7+
import com.alibaba.fastjson.annotation.JSONField;
8+
9+
public class JSONFieldTest3 extends TestCase {
10+
public void test_jsonField() throws Exception {
11+
VO vo = new VO();
12+
13+
vo.setId(123);
14+
vo.setFlag(true);
15+
16+
String text = JSON.toJSONString(vo);
17+
Assert.assertEquals("{\"id\":123}", text);
18+
}
19+
20+
public static class VO {
21+
private int id;
22+
23+
@JSONField(serialize = false)
24+
private boolean _flag;
25+
26+
@JSONField(serialize = false)
27+
private int _id2;
28+
29+
public int getId() {
30+
return id;
31+
}
32+
33+
public void setId(int id) {
34+
this.id = id;
35+
}
36+
37+
public boolean isFlag() {
38+
return _flag;
39+
}
40+
41+
public void setFlag(boolean flag) {
42+
this._flag = flag;
43+
}
44+
45+
46+
public int getId2() {
47+
return _id2;
48+
}
49+
50+
51+
public void setId2(int id2) {
52+
this._id2 = id2;
53+
}
54+
55+
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.alibaba.json.bvt.serializer;
2+
3+
import org.junit.Assert;
4+
import junit.framework.TestCase;
5+
6+
import com.alibaba.fastjson.JSON;
7+
import com.alibaba.fastjson.annotation.JSONField;
8+
9+
public class JSONFieldTest4 extends TestCase {
10+
public void test_jsonField() throws Exception {
11+
VO vo = new VO();
12+
13+
vo.setId(123);
14+
vo.setFlag(true);
15+
16+
String text = JSON.toJSONString(vo);
17+
Assert.assertEquals("{\"id\":123}", text);
18+
}
19+
20+
public static class VO {
21+
private int id;
22+
23+
@JSONField(serialize = false)
24+
private boolean m_flag;
25+
26+
@JSONField(serialize = false)
27+
private int m_id2;
28+
29+
public int getId() {
30+
return id;
31+
}
32+
33+
public void setId(int id) {
34+
this.id = id;
35+
}
36+
37+
public boolean isFlag() {
38+
return m_flag;
39+
}
40+
41+
public void setFlag(boolean flag) {
42+
this.m_flag = flag;
43+
}
44+
45+
46+
public int getId2() {
47+
return m_id2;
48+
}
49+
50+
51+
public void setId2(int id2) {
52+
this.m_id2 = id2;
53+
}
54+
55+
56+
}
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.alibaba.json.bvt.serializer;
2+
3+
import junit.framework.TestCase;
4+
5+
import org.junit.Assert;
6+
7+
import com.alibaba.fastjson.JSON;
8+
9+
public class JSONFieldTest5 extends TestCase {
10+
11+
public void test_jsonField() throws Exception {
12+
VO vo = new VO();
13+
14+
vo.setID(123);
15+
16+
String text = JSON.toJSONString(vo);
17+
Assert.assertEquals("{\"ID\":123}", text);
18+
19+
Assert.assertEquals(123, JSON.parseObject(text, VO.class).getID());
20+
}
21+
22+
public static class VO {
23+
24+
private int id;
25+
26+
public int getID() {
27+
return id;
28+
}
29+
30+
public void setID(int id) {
31+
this.id = id;
32+
}
33+
34+
}
35+
}

src/test/java/com/alibaba/json/demo/User.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.alibaba.json.demo;
22

3+
4+
import org.junit.Assert;
5+
6+
import com.alibaba.fastjson.JSON;
7+
38
public class User {
49

510
private Long id;
@@ -20,4 +25,15 @@ public String getName() {
2025
public void setName(String name) {
2126
this.name = name;
2227
}
28+
29+
public static void main(String[] args) {
30+
User user = new User();
31+
user.setId(123L);
32+
user.setName("wenshao");
33+
String text = JSON.toJSONString(user);
34+
System.out.println(text);
35+
36+
User user1 = JSON.parseObject(text, User.class);
37+
Assert.assertEquals("{\"id\":123,\"name\":\"wenshao\"}", text);
38+
}
2339
}

0 commit comments

Comments
 (0)