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

[QUESTION]反序列化 JSONString 为接口时,default 方法调用失败的问题 #1396

Closed
Doghole opened this issue Apr 21, 2023 · 1 comment
Labels
fixed question Further information is requested
Milestone

Comments

@Doghole
Copy link

Doghole commented Apr 21, 2023

请描述您的问题

  • JSONString 反序列化为接口时,若接口默认方法返回值非基本类型,则该值为 null
  • JSON/JSONString 反序列化为接口时,若接口默认方法非 get/is/hashCode... 则提示 This method 'xxx' is not a getter?

期望

所有输出都为 bob

示例代码

    public static void main(String[] args) throws Exception {
        User user = new User("bob");
        JSONObject userJsonObject = JSONObject.from(user, JSONWriter.Feature.IgnoreNonFieldGetter);
        String userJsonString = JSONObject.toJSONString(user);
        
        // ...
        // transport via http/redis/file
        // ...
        
        // Normal interface method call
        LivingObject normal = new User("bob");
        log.info(normal.getSignature().toString());            // -> "bob"
        log.info(normal.getSignatureString());                 // -> "bob"
        log.info(normal.withSignatureString());                // -> "bob"

        // Normal deserialize
        LivingObject deserializeLiving = userJsonObject.to(LivingObject.class);
        log.info(deserializeLiving.getSignature().toString()); // -> "bob"
        log.info(deserializeLiving.getSignatureString());      // -> "bob"
        log.info(deserializeLiving.withSignatureString());     // -> This method 'withSignatureString' is not a getter

        // Unknown instance type
        LivingObject living = JSONObject.parseObject(userJsonString, LivingObject.class);
        log.info(living.getSignature().toString());            // -> "null"
        log.info(living.getSignatureString());                 // -> "bob"
        log.info(living.withSignatureString());                // -> This method 'withSignatureString' is not a getter
    }
    
    public static interface LivingObject {
        
        String getName();
        
        String withName();
        
        public default Signature getSignature() {
            return new Signature(getName());
        }

        public default String getSignatureString() {
            return new Signature(getName()).toString();
        }

        public default String withSignatureString() {
            return withName();
        }
        
    }
    
    public static class Signature {
        
        private String sign;
        
        public Signature(String sign) {
            this.sign = sign;
        }
        
        @Override
        public String toString() {
            return sign;
        }
    }
    
    public static class User implements LivingObject {

        private String name;
        
        public User(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
        
        public String withName() {
            return name;
        }
    }
@Doghole Doghole added the question Further information is requested label Apr 21, 2023
@wenshao wenshao added this to the 2.0.29 milestone Apr 21, 2023
wenshao added a commit that referenced this issue Apr 21, 2023
@wenshao wenshao added the fixed label Apr 22, 2023
@wenshao
Copy link
Member

wenshao commented Apr 22, 2023

@wenshao wenshao closed this as completed Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants