Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

JSONObject.toJSON时未去解析注解的问题 #4372

Open
anigkus opened this issue Apr 19, 2023 · 0 comments
Open

JSONObject.toJSON时未去解析注解的问题 #4372

anigkus opened this issue Apr 19, 2023 · 0 comments

Comments

@anigkus
Copy link

anigkus commented Apr 19, 2023

#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();
            }
        }

相关补丁

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant