Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant