This repository was archived by the owner on Oct 23, 2024. It is now read-only.
This repository was archived by the owner on Oct 23, 2024. It is now read-only.
JSONObject.toJSON时未去解析注解的问题 #4372
Open
Description
#Example
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
public class Test {
@lombok.Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class Req implements Serializable {
@JSONField(name = "method", defaultValue = "payStatus")
private String method;
@JSONField(name = "merchantCode")
private String merchantCode;
@JSONField(name = "data")
private String data;
}
public static void main(String[] args) {
Test.Req req = Test.Req.builder().merchantCode("b").data("c").build();
String json = JSONObject.toJSONString(req);
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(req);
System.out.println(json); //{"data":"c","merchantCode":"b","method":"payStatus"}
System.out.println(jsonObject); //{"merchantCode":"b","data":"c"}
}
}
已经在com.alibaba.fastjson.serializer.FieldSerializer#getPropertyValue
方法中增加了逻辑,看你们觉得是否要合并下这块代码
if (propertyValue == null) {
JSONField jsonField = fieldInfo.getAnnotation();
if (jsonField != null && !"".equals(jsonField.defaultValue())) {
propertyValue = jsonField.defaultValue();
}
}
Metadata
Metadata
Assignees
Labels
No labels