fastjson2
fastjson2 copied to clipboard
[BUG] PropertyNamingStrategy does not apply when deserializing JSON string into an Object
问题描述
简要描述您碰到的问题。
When serializing and deserializing I use the PropertyNamingStrategy (for example, LowerCaseWithUnderScores). It works fine when serializing Object to Json string. But when deserializing from a Json string to an Object, it does not work.
环境信息
请填写以下信息:
- OS信息:MacOS 14.4.1, Apple M1 Pro, 32Gb
- JDK信息: openjdk 21.0.2 2024-01-16 LTS
- 版本信息:FastJson 2.0.49
重现步骤
如何操作可以重现该问题:
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.PropertyNamingStrategy;
import com.alibaba.fastjson2.filter.NameFilter;
import org.junit.jupiter.api.Test;
public class FastJsonSerializationTest {
@Test
public void jsonFastSerializationTest() {
var namingStrategy = NameFilter.of(PropertyNamingStrategy.LowerCaseWithUnderScores);
var user = new User(123L, "First", "Last");
var userAsJsonString = JSON.toJSONString(user, namingStrategy);
System.out.println(userAsJsonString); // {"first_name":"First","id":123,"last_name":"Last"} -> Ok
var deserializedUser = JSON.parseObject(userAsJsonString, User.class, namingStrategy);
System.out.println(deserializedUser); // UserTest{id=123, firstName='null', lastName='null'} -> Bug!!!
}
public static class User {
private Long id;
private String firstName;
private String lastName;
public User(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "UserTest{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
}
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
The naming strategy should work both ways (serialization/deserialization)
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/ 问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证
https://github.com/alibaba/fastjson2/releases/tag/2.0.50 2.0.50已发布,请用新版本