[BUG]JSON.parseObject将json转成对象时出现空字段的异常
这个异常在1.x版本和2.0.2版本均没有出现,在2.0.3版本出现了 下面是我的代码
Admin admin = JSON.parseObject(JSON.toJSONString(adminLoginForm), Admin.class);
adminLoginForm:
public class AdminLoginForm {
/**
* 管理员账号
*/
@NotBlank(message = "用户名不能为空")
@Length(min = 2, max = 20, message = "用户名长度为{min}-{max}位")
@Pattern(regexp = "^\\w+$", message = "用户名只能由数字、字母或下划线组成")
public String username;
/**
* 密码
* 为保证安全,password不进行序列化
*/
@NotBlank(message = "密码不能为空")
@Length(min = 4, max = 20, message = "密码长度为{min}-{max}位")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
public String password;
}
admin:
public class Admin extends BaseModel implements Serializable {
/**
* 管理员账号
*/
@NotBlank(message = "管理员账号不能为空")
@Length(min = 2, max = 20, message = "管理员账号长度为{min}-{max}位")
@Pattern(regexp = "^\\w+$", message = "管理员账号只能由数字、字母或下划线组成")
@Column(name = "username", nullable = false, unique = true)
private String username;
/**
* 密码
* 为保证安全,password不进行序列化
*/
@NotBlank(message = "密码不能为空")
@Column(name = "password", nullable = false)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
}
以上代码在2.0.2及以前运行良好,在2.0.3会报password空异常
还是会出现这个问题,代码相同的情况下只是将版本从2.0.3到2.0.2就正常使用了
2.0.3版本是自动识别Jackson的Annotation的,是不是这个导致的
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
@NotBlank(message = "管理员账号不能为空")
@Length(min = 2, max = 20, message = "管理员账号长度为{min}-{max}位")
@Pattern(regexp = "^\\w+$", message = "管理员账号只能由数字、字母或下划线组成")
这些Annotation是哪个package的?
的确,我也试过这个操作了,将admin的password上的write_only注解去掉就可以成功,但是我又需要这个注解来完成我序列化时没有password字段的操作
------------------ 原始邮件 ------------------ 发件人: "alibaba/fastjson2" @.>; 发送时间: 2022年5月21日(星期六) 中午1:21 @.>; @.>;"State @.>; 主题: Re: [alibaba/fastjson2] [BUG]JSON.parseObject将json转成对象时出现空字段的异常 (Issue #290)
2.0.3版本是自动识别Jackson的Annotation的,是不是这个导致的 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String password;
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.Message ID: @.***>
import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.validator.constraints.Length; import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.Table; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import java.io.Serializable; import java.util.Objects; 以上是我在admin实体类的导入
------------------ 原始邮件 ------------------ 发件人: "alibaba/fastjson2" @.>; 发送时间: 2022年5月21日(星期六) 中午1:22 @.>; @.>;"State @.>; 主题: Re: [alibaba/fastjson2] [BUG]JSON.parseObject将json转成对象时出现空字段的异常 (Issue #290)
@NotBlank(message = "管理员账号不能为空") @Length(min = 2, max = 20, message = "管理员账号长度为{min}-{max}位") @Pattern(regexp = "^\w+$", message = "管理员账号只能由数字、字母或下划线组成")
这些Annotation是哪个package的?
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.Message ID: @.***>
https://github.com/alibaba/fastjson2/releases/tag/2.0.24 @juquxiang 可以帮忙验证下2.0.24版本是否还存在这个问题么?
JsonProperty.Access.WRITE_ONLY || access = JsonProperty.Access.READ_ONLY 关于序列化和反序列化的对应字段为空的问题,2.0.3以后的版本才是正确的,之前算是bug,都配置了WRITE_ONLY,序列化的时候肯定要忽略该字段的;如果不忽略反倒不对了。 但是修复该bug后没有给出一个方法可以强制序列化所有字段,忽略WRITE_ONLY或者READ_ONLY;才行,建议在JSONWriter.Feature增加相关Feature,用户可以手工指定忽略WRITE_ONLY或者READ_ONLY。 望采纳,谢谢! 目前只能在2.0.2版本,等待支持手工强制序列化、反序列化所有字段的版本出现。
请问一下,这问题后面有解决方案了吗?有不识别或忽略的feature写法吗?