[BUG] 小写字母名称字段 对于大写字符串 解析处理方式不一致
Cooper-Zhong opened this issue · comments
Cooper commented
问题描述
fastjson 和 fastjson2 对于小写字母名称字段,解析大写字符串的处理方式不一致
环境信息
- OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
- JDK信息: [Openjdk 17.0.6]
- 版本信息:[Fastjson/Fastjson2 2.0.49]
重现步骤
import com.alibaba.fastjson2.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Issue924Mutated_47 {
@Test
public void test() {
Entity entity = new Entity(101L);
String json = JSON.toJSONString(entity);
assertEquals("{\"c\":101}", json);
Entity entity1 = JSON.parseObject(json, Entity.class);
assertEquals(entity.getC(), entity1.getC());
Entity entity2 = JSON.parseObject("{\"C\":101}", Entity.class);
assertEquals(entity.getC(), entity2.getC());
}
@Test
public void testfj() {
Entity entity = new Entity(101L);
String json = com.alibaba.fastjson.JSON.toJSONString(entity);
assertEquals("{\"c\":101}", json);
Entity entity1 = com.alibaba.fastjson.JSON.parseObject(json, Entity.class);
assertEquals(entity.getC(), entity1.getC());
Entity entity2 = com.alibaba.fastjson.JSON.parseObject("{\"C\":101}", Entity.class);
assertEquals(entity.getC(), entity2.getC());
}
@Test
public void test1() {
Entity1 entity = new Entity1();
entity.setId(101L);
String json = JSON.toJSONString(entity);
assertEquals("{\"id\":101}", json);
Entity1 entity1 = JSON.parseObject(json, Entity1.class);
assertEquals(entity.getId(), entity1.getId());
Entity1 entity2 = JSON.parseObject("{\"ID\":101}", Entity1.class);
assertEquals(entity.getId(), entity2.getId());
}
@Test
public void test1fj() {
Entity1 entity = new Entity1();
entity.setId(101L);
String json = com.alibaba.fastjson.JSON.toJSONString(entity);
assertEquals("{\"id\":101}", json);
Entity1 entity1 = com.alibaba.fastjson.JSON.parseObject(json, Entity1.class);
assertEquals(entity.getId(), entity1.getId());
Entity1 entity2 = com.alibaba.fastjson.JSON.parseObject("{\"ID\":101}", Entity1.class);
assertEquals(entity.getId(), entity2.getId());
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public static class Entity {
private Long c;
}
public static class Entity1 {
private Long id;
public Entity1() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
}
期待的正确结果
对于小写字段属性,fastjson能够处理含大写的字符串但fastjson2不能
期望两者的处理方式应保持一致
相关日志输出
org.opentest4j.AssertionFailedError:
Expected :101
Actual :null
Shaojin Wen commented
fastjson 1.x缺省启用SmartMatch,这个历史上遇到了很多问题,顾此失彼。fastjson2缺省不打开了,可以手动打开,比如:
JSON.parseObject("...", Bean.class, JSONReader.Feature.SupportSmartMatch);
Cooper commented
启用SmartMatch没有问题,谢谢温少